forked from gitea/gitea
1
0
Fork 0

Add continue option to backport.go (#22930)

Sometimes it can be helpful to continue from a broken cherry-pick. This
PR adds another option which can be used to determine the version and pr
number from the current branch name instead of reading the config.yaml
file.

---------

Signed-off-by: Andrew Thornton <art27@cantab.net>
This commit is contained in:
zeripath 2023-02-16 09:48:08 +00:00 committed by GitHub
parent 3361bbf058
commit 6c90f5e4c0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 41 additions and 6 deletions

View File

@ -79,6 +79,10 @@ func main() {
Name: "no-xdg-open", Name: "no-xdg-open",
Usage: "Set this flag to not use xdg-open to open the PR URL", Usage: "Set this flag to not use xdg-open to open the PR URL",
}, },
cli.BoolFlag{
Name: "continue",
Usage: "Set this flag to continue from a git cherry-pick that has broken",
},
} }
cli.AppHelpTemplate = `NAME: cli.AppHelpTemplate = `NAME:
{{.Name}} - {{.Usage}} {{.Name}} - {{.Usage}}
@ -104,7 +108,19 @@ func runBackport(c *cli.Context) error {
ctx, cancel := installSignals() ctx, cancel := installSignals()
defer cancel() defer cancel()
continuing := c.Bool("continue")
var pr string
version := c.String("version") version := c.String("version")
if version == "" && continuing {
// determine version from current branch name
var err error
pr, version, err = readCurrentBranch(ctx)
if err != nil {
return err
}
}
if version == "" { if version == "" {
version = readVersion() version = readVersion()
} }
@ -135,13 +151,14 @@ func runBackport(c *cli.Context) error {
localReleaseBranch := path.Join(upstream, upstreamReleaseBranch) localReleaseBranch := path.Join(upstream, upstreamReleaseBranch)
args := c.Args() args := c.Args()
if len(args) == 0 { if len(args) == 0 && pr == "" {
return fmt.Errorf("no PR number provided\nProvide a PR number to backport") return fmt.Errorf("no PR number provided\nProvide a PR number to backport")
} else if len(args) != 1 { } else if len(args) != 1 && pr == "" {
return fmt.Errorf("multiple PRs provided %v\nOnly a single PR can be backported at a time", args) return fmt.Errorf("multiple PRs provided %v\nOnly a single PR can be backported at a time", args)
} }
if pr == "" {
pr := args[0] pr = args[0]
}
backportBranch := c.String("backport-branch") backportBranch := c.String("backport-branch")
if backportBranch == "" { if backportBranch == "" {
@ -168,8 +185,10 @@ func runBackport(c *cli.Context) error {
} }
} }
if err := checkoutBackportBranch(ctx, backportBranch, localReleaseBranch); err != nil { if !continuing {
return err if err := checkoutBackportBranch(ctx, backportBranch, localReleaseBranch); err != nil {
return err
}
} }
if err := cherrypick(ctx, sha); err != nil { if err := cherrypick(ctx, sha); err != nil {
@ -353,6 +372,22 @@ func determineRemote(ctx context.Context, forkUser string) (string, string, erro
return "", "", fmt.Errorf("unable to find appropriate remote in:\n%s", string(out)) return "", "", fmt.Errorf("unable to find appropriate remote in:\n%s", string(out))
} }
func readCurrentBranch(ctx context.Context) (pr, version string, err error) {
out, err := exec.CommandContext(ctx, "git", "branch", "--show-current").Output()
if err != nil {
fmt.Fprintf(os.Stderr, "Unable to read current git branch:\n%s\n", string(out))
return "", "", fmt.Errorf("unable to read current git branch: %w", err)
}
parts := strings.Split(strings.TrimSpace(string(out)), "-")
if len(parts) != 3 || parts[0] != "backport" {
fmt.Fprintf(os.Stderr, "Unable to continue from git branch:\n%s\n", string(out))
return "", "", fmt.Errorf("unable to continue from git branch:\n%s", string(out))
}
return parts[1], parts[2], nil
}
func readVersion() string { func readVersion() string {
bs, err := os.ReadFile("docs/config.yaml") bs, err := os.ReadFile("docs/config.yaml")
if err != nil { if err != nil {