Skip to content

fix(promote): refuse to run on a dirty tree and never strand HEAD on <to> - #29

Merged
sujal-maheshwari2004 merged 4 commits into
mainfrom
fix/promote-dirty-tree
Nov 15, 2026
Merged

sujal-maheshwari2004 merged 4 commits into
mainfrom
fix/promote-dirty-tree

Conversation

@sujal-maheshwari2004

Copy link
Copy Markdown
Member

Fixes #14. Fixes #15.

promote checks out <to>, commits there, and checks the original branch back out. go-git's Checkout only refuses unstaged edits, which caused two bugs:

Change

  • Before any checkout, promote lists staged or modified tracked files and stops with promote needs a clean working tree; commit or stash first: <files>.
  • Untracked files are allowed, because the checkout round trip leaves them alone; a test covers this.
  • If the checkout of <to> still fails for another reason, HEAD is restored to the starting branch or detached commit before exiting.
  • Under core.autocrlf=true, go-git's status does not report CRLF-checked-out files as modified, so the check doesn't block Windows users. I verified that separately.

Tested

  • New tests in promote_dirty_test.go. The two refusal cases run promote in a child process because it exits through log.Fatal. They check:
    • staged edit plus staged new file: refused, both kept and still staged, prod unchanged
    • unstaged edit: refused, HEAD stays on the original branch, edit kept
    • untracked file present: promote succeeds and the file survives
  • On main, the first two fail: promote reports promoted acme/a.prompt onto prod with staged changes, and checkout prod: worktree contains unstaged changes for the unstaged case.
  • go vet ./... and go test ./... pass.

@sujal-maheshwari2004

sujal-maheshwari2004 commented Nov 15, 2026 •

Copy link
Copy Markdown
Member Author

Pushed two follow-up commits for Windows line endings.

go-git ignores core.autocrlf whenever it has to hash a file (a racy index entry right after a checkout, or a touched file), so on Windows a clean CRLF checkout of an LF blob shows as modified. I reproduced this outside the test suite: a fresh git checkout under the system-level autocrlf=true was reported as M until git status refreshed the index. That tripped both the new clean-tree check and go-git's own Checkout.

  • A worktree-modified file counts only if it differs from HEAD by more than line endings (reusing changedLines from fix(diff): compare normalized lines so CRLF-only differences are skipped (#11) #12).
  • Files that differ only in line endings are rewritten with their committed content before the round trip, so go-git's checkout doesn't refuse. Only their line endings change, as already happens to every file go-git checks out. Checkout{Force: true} was not an option because it deletes untracked files.

Correction: the first follow-up commit (go test ./... was failing on it) went up before I saw the failure. It's fixed in the second commit: TestPromoteAllowsLineEndingOnlyDifferences and TestPromoteAllowsUntrackedFiles both pass, and go test ./... exits 0.

…<to>

promote round-trips through a checkout of <to>. go-git's Checkout only
refuses unstaged edits, so staged edits and staged new files were reset away
with exit 0, and when it did refuse it had already moved HEAD, leaving the
user on <to> with their work in the tree.

promote now checks for staged or modified tracked files up front and stops
with "commit or stash first" (untracked files are fine; the checkout leaves
them alone). If the checkout of <to> still fails, HEAD is restored to the
branch or commit the user started on.

Fixes #14
Fixes #15
go-git ignores core.autocrlf when it has to hash a file (a racy index entry
right after a checkout, or a touched file), so a clean CRLF checkout of an LF
blob shows as modified and the new clean-tree check would block promote on
Windows. A worktree-modified file now only counts when it differs from HEAD
by more than line endings (changedLines).
…ckout refuse

The previous commit let a CRLF-only file through promote's own check, but
go-git's Checkout hits the same false positive and refuses with "worktree
contains unstaged changes" (TestPromoteAllowsLineEndingOnlyDifferences
failed). Such files are now rewritten with their committed content before
the round trip; only their line endings change, as for every file go-git
checks out. Checkout Force was not an option: it deletes untracked files.
The changedLines helper this branch used came from #12, which was reverted
in #33. Use a small sameIgnoringLineEndings helper instead so promote does
not depend on that change.
@sujal-maheshwari2004
sujal-maheshwari2004 merged commit 40ea2e4 into main Nov 15, 2026
1 check passed
@sujal-maheshwari2004
sujal-maheshwari2004 deleted the fix/promote-dirty-tree branch November 15, 2026 05:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

promote leaves you on the target branch when its checkout fails promote silently discards staged changes in the working tree

1 participant