|
1 | 1 | package cmd |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "fmt" |
| 5 | + |
4 | 6 | "github.com/github/gh-stack/internal/config" |
5 | 7 | "github.com/github/gh-stack/internal/git" |
6 | 8 | "github.com/github/gh-stack/internal/stack" |
@@ -175,14 +177,11 @@ func runSync(cfg *config.Config, _ *syncOptions) error { |
175 | 177 | if git.IsRebaseInProgress() { |
176 | 178 | _ = git.RebaseAbort() |
177 | 179 | } |
178 | | - for branch, sha := range originalRefs { |
179 | | - _ = git.CheckoutBranch(branch) |
180 | | - _ = git.ResetHard(sha) |
181 | | - } |
| 180 | + restoreErrors := restoreBranches(originalRefs) |
182 | 181 | _ = git.CheckoutBranch(currentBranch) |
183 | 182 |
|
184 | 183 | 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) |
186 | 185 | cfg.Printf(" Run %s to resolve conflicts interactively.", |
187 | 186 | cfg.ColorCyan("gh stack rebase")) |
188 | 187 | conflicted = true |
@@ -210,14 +209,11 @@ func runSync(cfg *config.Config, _ *syncOptions) error { |
210 | 209 | if git.IsRebaseInProgress() { |
211 | 210 | _ = git.RebaseAbort() |
212 | 211 | } |
213 | | - for branch, sha := range originalRefs { |
214 | | - _ = git.CheckoutBranch(branch) |
215 | | - _ = git.ResetHard(sha) |
216 | | - } |
| 212 | + restoreErrors := restoreBranches(originalRefs) |
217 | 213 | _ = git.CheckoutBranch(currentBranch) |
218 | 214 |
|
219 | 215 | 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) |
221 | 217 | cfg.Printf(" Run %s to resolve conflicts interactively.", |
222 | 218 | cfg.ColorCyan("gh stack rebase")) |
223 | 219 | conflicted = true |
@@ -318,6 +314,33 @@ func updateBranchRef(branch, sha string) error { |
318 | 314 | return git.UpdateBranchRef(branch, sha) |
319 | 315 | } |
320 | 316 |
|
| 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 | + |
321 | 344 | // short returns the first 7 characters of a SHA. |
322 | 345 | func short(sha string) string { |
323 | 346 | if len(sha) > 7 { |
|
0 commit comments