Skip to content

Commit ae29662

Browse files
javoireclaude
andauthored
fix(sync): switch to base branch when current branch was merged (#69)
When `stack sync` detects the current branch's PR has been merged, automatically switch to the base branch instead of leaving the user stranded on a merged branch. Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 9d398c6 commit ae29662

1 file changed

Lines changed: 12 additions & 1 deletion

File tree

cmd/sync.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ func runSync(gitClient git.GitClient, githubClient github.GitHubClient) error {
9898
var originalBranch string
9999
stashed := false
100100
rebaseConflict := false
101+
originalBranchMerged := false
101102

102103
// Check for existing sync state (from previous interrupted sync)
103104
savedStashed := gitClient.GetConfig(configSyncStashed)
@@ -455,6 +456,9 @@ func runSync(gitClient git.GitClient, githubClient github.GitHubClient) error {
455456
} else {
456457
fmt.Printf(" %s Removed. You can delete this branch with: %s\n", ui.SuccessIcon(), ui.Command(fmt.Sprintf("git branch -d %s", branch.Name)))
457458
}
459+
if branch.Name == originalBranch {
460+
originalBranchMerged = true
461+
}
458462
fmt.Println()
459463
continue
460464
}
@@ -842,7 +846,14 @@ func runSync(gitClient git.GitClient, githubClient github.GitHubClient) error {
842846

843847
// Return to original branch if needed
844848
currentBranch, err := gitClient.GetCurrentBranch()
845-
if err == nil && currentBranch != originalBranch {
849+
if originalBranchMerged {
850+
if currentBranch != baseBranch {
851+
if err := gitClient.CheckoutBranch(baseBranch); err != nil {
852+
fmt.Fprintf(os.Stderr, "Warning: failed to switch to %s: %v\n", baseBranch, err)
853+
}
854+
}
855+
fmt.Printf("Switched to %s (%s was merged)\n", ui.Branch(baseBranch), ui.Branch(originalBranch))
856+
} else if err == nil && currentBranch != originalBranch {
846857
fmt.Printf("Returning to %s...\n", ui.Branch(originalBranch))
847858
if err := gitClient.CheckoutBranch(originalBranch); err != nil {
848859
fmt.Fprintf(os.Stderr, "Warning: failed to return to original branch: %v\n", err)

0 commit comments

Comments
 (0)