Skip to content

Commit 6c90f5e

Browse files
authored
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 <[email protected]>
1 parent 3361bbf commit 6c90f5e

File tree

1 file changed

+41
-6
lines changed

1 file changed

+41
-6
lines changed

contrib/backport/backport.go

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@ func main() {
7979
Name: "no-xdg-open",
8080
Usage: "Set this flag to not use xdg-open to open the PR URL",
8181
},
82+
cli.BoolFlag{
83+
Name: "continue",
84+
Usage: "Set this flag to continue from a git cherry-pick that has broken",
85+
},
8286
}
8387
cli.AppHelpTemplate = `NAME:
8488
{{.Name}} - {{.Usage}}
@@ -104,7 +108,19 @@ func runBackport(c *cli.Context) error {
104108
ctx, cancel := installSignals()
105109
defer cancel()
106110

111+
continuing := c.Bool("continue")
112+
113+
var pr string
114+
107115
version := c.String("version")
116+
if version == "" && continuing {
117+
// determine version from current branch name
118+
var err error
119+
pr, version, err = readCurrentBranch(ctx)
120+
if err != nil {
121+
return err
122+
}
123+
}
108124
if version == "" {
109125
version = readVersion()
110126
}
@@ -135,13 +151,14 @@ func runBackport(c *cli.Context) error {
135151
localReleaseBranch := path.Join(upstream, upstreamReleaseBranch)
136152

137153
args := c.Args()
138-
if len(args) == 0 {
154+
if len(args) == 0 && pr == "" {
139155
return fmt.Errorf("no PR number provided\nProvide a PR number to backport")
140-
} else if len(args) != 1 {
156+
} else if len(args) != 1 && pr == "" {
141157
return fmt.Errorf("multiple PRs provided %v\nOnly a single PR can be backported at a time", args)
142158
}
143-
144-
pr := args[0]
159+
if pr == "" {
160+
pr = args[0]
161+
}
145162

146163
backportBranch := c.String("backport-branch")
147164
if backportBranch == "" {
@@ -168,8 +185,10 @@ func runBackport(c *cli.Context) error {
168185
}
169186
}
170187

171-
if err := checkoutBackportBranch(ctx, backportBranch, localReleaseBranch); err != nil {
172-
return err
188+
if !continuing {
189+
if err := checkoutBackportBranch(ctx, backportBranch, localReleaseBranch); err != nil {
190+
return err
191+
}
173192
}
174193

175194
if err := cherrypick(ctx, sha); err != nil {
@@ -353,6 +372,22 @@ func determineRemote(ctx context.Context, forkUser string) (string, string, erro
353372
return "", "", fmt.Errorf("unable to find appropriate remote in:\n%s", string(out))
354373
}
355374

375+
func readCurrentBranch(ctx context.Context) (pr, version string, err error) {
376+
out, err := exec.CommandContext(ctx, "git", "branch", "--show-current").Output()
377+
if err != nil {
378+
fmt.Fprintf(os.Stderr, "Unable to read current git branch:\n%s\n", string(out))
379+
return "", "", fmt.Errorf("unable to read current git branch: %w", err)
380+
}
381+
parts := strings.Split(strings.TrimSpace(string(out)), "-")
382+
383+
if len(parts) != 3 || parts[0] != "backport" {
384+
fmt.Fprintf(os.Stderr, "Unable to continue from git branch:\n%s\n", string(out))
385+
return "", "", fmt.Errorf("unable to continue from git branch:\n%s", string(out))
386+
}
387+
388+
return parts[1], parts[2], nil
389+
}
390+
356391
func readVersion() string {
357392
bs, err := os.ReadFile("docs/config.yaml")
358393
if err != nil {

0 commit comments

Comments
 (0)