Skip to content

Commit fedfb15

Browse files
rios0rios0claude
andcommitted
fix(sync): changed restore to stay on default branch after sync
- changed `RestoreAfterSync` to checkout the default branch instead of the original branch after rebasing the WIP branch - removed the `else if` block that checked out the original branch for clean repos on non-default branches (already on default from the sync step) - this prevents `prune` from failing with "cannot delete branch used by worktree" when the merged branch is currently checked out Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 1c7b199 commit fedfb15

2 files changed

Lines changed: 12 additions & 6 deletions

File tree

internal/repo/sync.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -172,10 +172,8 @@ func RestoreAfterSync(
172172
if err := runner.Run(repoPath, "rebase", defaultBranch); err != nil {
173173
_ = runner.Run(repoPath, "rebase", "--abort")
174174
}
175-
_ = runner.Run(repoPath, "checkout", currentBranch)
175+
_ = runner.Run(repoPath, "checkout", defaultBranch)
176176
status = fmt.Sprintf("synced (wip: %s)", wipBranch)
177-
} else if currentBranch != defaultBranch {
178-
_ = runner.Run(repoPath, "checkout", currentBranch)
179177
}
180178
return SyncResult{Name: name, Status: status}
181179
}

internal/repo/sync_test.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ func TestRestoreAfterSync(t *testing.T) {
257257
assert.Equal(t, "synced", result.Status)
258258
})
259259

260-
t.Run("should checkout original branch when not on default", func(t *testing.T) {
260+
t.Run("should stay on default branch when clean repo was on non-default branch", func(t *testing.T) {
261261
t.Parallel()
262262
// given
263263
var checkedOut string
@@ -274,19 +274,27 @@ func TestRestoreAfterSync(t *testing.T) {
274274

275275
// then
276276
assert.Equal(t, "synced", result.Status)
277-
assert.Equal(t, "feat/x", checkedOut)
277+
assert.Empty(t, checkedOut)
278278
})
279279

280-
t.Run("should rebase WIP branch and return wip status for dirty repo", func(t *testing.T) {
280+
t.Run("should rebase WIP branch and checkout default branch for dirty repo", func(t *testing.T) {
281281
t.Parallel()
282282
// given
283+
var lastCheckedOut string
283284
runner := doubles.NewGitRunnerStub()
285+
runner.RunFunc = func(_ string, args ...string) error {
286+
if len(args) > 0 && args[0] == "checkout" {
287+
lastCheckedOut = args[1]
288+
}
289+
return nil
290+
}
284291

285292
// when
286293
result := repo.RestoreAfterSync("/repo", "my-repo", "main", "feat/x", "wip/feat/x", true, runner)
287294

288295
// then
289296
assert.Contains(t, result.Status, "synced (wip: wip/feat/x)")
297+
assert.Equal(t, "main", lastCheckedOut)
290298
})
291299
}
292300

0 commit comments

Comments
 (0)