Skip to content

Commit 7767bb4

Browse files
committed
handle rebase restore failures
1 parent b9bfd86 commit 7767bb4

1 file changed

Lines changed: 33 additions & 10 deletions

File tree

cmd/sync.go

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package cmd
22

33
import (
4+
"fmt"
5+
46
"github.com/github/gh-stack/internal/config"
57
"github.com/github/gh-stack/internal/git"
68
"github.com/github/gh-stack/internal/stack"
@@ -175,14 +177,11 @@ func runSync(cfg *config.Config, _ *syncOptions) error {
175177
if git.IsRebaseInProgress() {
176178
_ = git.RebaseAbort()
177179
}
178-
for branch, sha := range originalRefs {
179-
_ = git.CheckoutBranch(branch)
180-
_ = git.ResetHard(sha)
181-
}
180+
restoreErrors := restoreBranches(originalRefs)
182181
_ = git.CheckoutBranch(currentBranch)
183182

184183
cfg.Errorf("Conflict detected rebasing %s onto %s", br.Branch, newBase)
185-
cfg.Printf(" All branches restored to their original state.")
184+
reportRestoreStatus(cfg, restoreErrors)
186185
cfg.Printf(" Run %s to resolve conflicts interactively.",
187186
cfg.ColorCyan("gh stack rebase"))
188187
conflicted = true
@@ -210,14 +209,11 @@ func runSync(cfg *config.Config, _ *syncOptions) error {
210209
if git.IsRebaseInProgress() {
211210
_ = git.RebaseAbort()
212211
}
213-
for branch, sha := range originalRefs {
214-
_ = git.CheckoutBranch(branch)
215-
_ = git.ResetHard(sha)
216-
}
212+
restoreErrors := restoreBranches(originalRefs)
217213
_ = git.CheckoutBranch(currentBranch)
218214

219215
cfg.Errorf("Conflict detected rebasing %s onto %s", br.Branch, base)
220-
cfg.Printf(" All branches restored to their original state.")
216+
reportRestoreStatus(cfg, restoreErrors)
221217
cfg.Printf(" Run %s to resolve conflicts interactively.",
222218
cfg.ColorCyan("gh stack rebase"))
223219
conflicted = true
@@ -318,6 +314,33 @@ func updateBranchRef(branch, sha string) error {
318314
return git.UpdateBranchRef(branch, sha)
319315
}
320316

317+
// restoreBranches resets each branch to its original SHA, collecting any errors.
318+
func restoreBranches(originalRefs map[string]string) []string {
319+
var errors []string
320+
for branch, sha := range originalRefs {
321+
if err := git.CheckoutBranch(branch); err != nil {
322+
errors = append(errors, fmt.Sprintf("checkout %s: %s", branch, err))
323+
continue
324+
}
325+
if err := git.ResetHard(sha); err != nil {
326+
errors = append(errors, fmt.Sprintf("reset %s: %s", branch, err))
327+
}
328+
}
329+
return errors
330+
}
331+
332+
// reportRestoreStatus prints whether branch restoration succeeded or partially failed.
333+
func reportRestoreStatus(cfg *config.Config, restoreErrors []string) {
334+
if len(restoreErrors) > 0 {
335+
cfg.Warningf("Some branches could not be fully restored:")
336+
for _, e := range restoreErrors {
337+
cfg.Printf(" %s", e)
338+
}
339+
} else {
340+
cfg.Printf(" All branches restored to their original state.")
341+
}
342+
}
343+
321344
// short returns the first 7 characters of a SHA.
322345
func short(sha string) string {
323346
if len(sha) > 7 {

0 commit comments

Comments
 (0)