fix(worktree): force checkout so retry recovers from a dirty worktree
refresh checked out the task branch without Force before hard-resetting to base. A prior attempt's leftover uncommitted changes -- a killed container, or a stage that edits files before ever reaching a commit -- made go-git's default MergeReset checkout refuse to reconcile them, even with HEAD already on the correct branch and the target commit unmoved. The fallback then made it worse: any checkout failure was assumed to mean the branch didn't exist yet, so it retried with Create: true, which failed with a much more confusing error since the branch did exist -- "a branch named ... already exists" -- masking the real cause and parking the retry with the same unhelpful message every time. Reproduced directly against a live deployment's actual broken worktree (modified tracked file, untracked file, branch already checked out) and confirmed retry then succeeds and correctly restores to base. Force is safe unconditionally here: refresh hard-resets to base a few lines later regardless of what checkout leaves behind. Closes #523
This commit is contained in:
1 parent
d83f5a9be3
commit
05944b6fd3
2 files changed
+67
-2
No files matched your search
@@ -164,10 +164,19 @@ func (m *Manager) refresh(ctx context.Context, dir, base, branch string) error {
|
||||
if err != nil {
|
||||
return fmt.Errorf("open worktree: %w", err)
|
||||
}
|
||||
// Force discards whatever the working tree looks like right now: an
|
||||
// interrupted prior attempt (killed container, a stage that edits
|
||||
// files before ever reaching commit) can leave local modifications
|
||||
// that make go-git's default MergeReset refuse to reconcile them, even
|
||||
// onto a commit HEAD is already on. That refusal used to surface as
|
||||
// "branch already exists" from the fallback below, which mis-assumed
|
||||
// any checkout failure meant the branch didn't exist yet. Force is
|
||||
// safe here regardless of cause: the hard reset to base a few lines
|
||||
// down is about to discard the working tree anyway.
|
||||
ref := plumbing.NewBranchReferenceName(branch)
|
||||
if err := wt.Checkout(&git.CheckoutOptions{Branch: ref}); err != nil {
|
||||
if err := wt.Checkout(&git.CheckoutOptions{Branch: ref, Force: true}); err != nil {
|
||||
// The branch does not exist yet in this worktree.
|
||||
if err := wt.Checkout(&git.CheckoutOptions{Branch: ref, Create: true}); err != nil {
|
||||
if err := wt.Checkout(&git.CheckoutOptions{Branch: ref, Create: true, Force: true}); err != nil {
|
||||
return fmt.Errorf("checkout branch %s: %w", branch, err)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user