2022-08-23 19:38:41 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/AlecAivazis/survey/v2"
|
|
|
|
"github.com/go-git/go-git/v5"
|
|
|
|
"github.com/go-git/go-git/v5/plumbing"
|
|
|
|
"github.com/go-git/go-git/v5/plumbing/object"
|
|
|
|
"github.com/peterbourgon/ff/v3/ffcli"
|
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2022-08-30 18:58:49 +00:00
|
|
|
frontportFS = flag.NewFlagSet("frontport", flag.ContinueOnError)
|
|
|
|
frontportFromFlag = frontportFS.String("from", "", "Release to frontport from (ex: `1.17`, default: <latest>)")
|
|
|
|
frontportToFlag = frontportFS.String("to", "", "Release to frontport to (ex: `main`, default: `main`)")
|
|
|
|
Frontport = &ffcli.Command{
|
2022-08-23 19:38:41 +00:00
|
|
|
Name: "frontport",
|
|
|
|
FlagSet: frontportFS,
|
2022-08-30 18:58:49 +00:00
|
|
|
ShortUsage: "frontport --from [release=latest] --to [release=main]",
|
2022-08-23 19:38:41 +00:00
|
|
|
ShortHelp: "frontport cherry-picks a <commit> and applies it to a clean branch based on <release>",
|
2022-08-30 18:58:49 +00:00
|
|
|
Exec: func(ctx context.Context, _ []string) error {
|
|
|
|
from := *frontportFromFlag
|
|
|
|
if from == "" {
|
|
|
|
from = latestRelease()
|
2022-08-23 19:38:41 +00:00
|
|
|
}
|
|
|
|
if !strings.HasPrefix(from, "release") {
|
2022-08-30 18:58:49 +00:00
|
|
|
from = fmt.Sprintf("release/v%s", from)
|
2022-08-23 19:38:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fetch(ctx)
|
|
|
|
|
|
|
|
if !isClean() {
|
|
|
|
log.Fatal().Msg("working tree is dirty")
|
|
|
|
}
|
|
|
|
|
|
|
|
commits, err := repo().Log(&git.LogOptions{
|
|
|
|
From: head(from),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
optMap := make(map[string]plumbing.Hash)
|
|
|
|
var opts []string
|
|
|
|
if err := commits.ForEach(func(c *object.Commit) error {
|
|
|
|
title := strings.Split(c.Message, "\n")[0]
|
|
|
|
opts = append(opts, title)
|
|
|
|
optMap[title] = c.Hash
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var resp string
|
|
|
|
if err := survey.AskOne(&survey.Select{
|
|
|
|
Message: "Commit to cherry-pick",
|
|
|
|
Options: opts,
|
|
|
|
}, &resp, survey.WithValidator(survey.Required)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
hash := optMap[resp]
|
|
|
|
branch := fmt.Sprintf("frontport-%s", hash)
|
2022-08-30 18:58:49 +00:00
|
|
|
|
|
|
|
base := *frontportToFlag
|
|
|
|
if base == "" {
|
|
|
|
base = "upstream/main"
|
|
|
|
}
|
|
|
|
if !strings.HasPrefix(base, "upstream") {
|
|
|
|
base = fmt.Sprintf("upstream/release/v1.%s", base)
|
2022-08-23 19:38:41 +00:00
|
|
|
}
|
|
|
|
if err := Branch.ParseAndRun(ctx, []string{branch, base}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return run(ctx, "git", "cherry-pick", hash.String())
|
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|