Skip to content

fix(squash): commit on a detached HEAD so a failed squash keeps the branch - #4206

Merged
max-sixty merged 4 commits into
mainfrom
4193
Sep 21, 2026
Merged

max-sixty merged 4 commits into
mainfrom
4193

Conversation

@max-sixty

@max-sixty max-sixty commented Sep 21, 2026 •

Copy link
Copy Markdown
Owner

wt step squash and wt merge's squash step ran git reset --soft <merge-base> and then git commit. When the commit failed — a pre-commit hook rejecting it, a broken signing setup — the branch was left rewound to the merge base with its commits collapsed into anonymous staged changes, on a path that reported failure. #4193 found it.

The squash now rewrites history the way git rebase does. It detaches HEAD onto the merge base with update-ref --no-deref, which leaves the branch, the index and the working tree untouched; commits there; then moves the branch to the new commit with one compare-and-swap update-ref and puts HEAD back on it. The branch keeps its commits until the commit exists, so a rejected or failed commit leaves it exactly as it was, and the reattach restores HEAD.

Committing through git commit rather than plumbing is what keeps git's own hooks, commit.cleanup, signing and identity resolution behaving as they do for any other commit. Those hooks see a detached HEAD, as they do under git rebase — verified on git 2.55, where a real rebase -i squash runs prepare-commit-msg and post-commit with no current branch and skips pre-commit and commit-msg entirely. The wt step squash help says so.

Two smaller changes ride along:

  • The no-net-changes check compares the index tree against the merge base's tree instead of asking git diff --cached, which is porcelain and reads submodule.<name>.ignore. A squash whose only change is a submodule pointer hidden by that setting is now committed rather than reported as empty. That path still leaves the branch at the merge base, so wt merge goes on treating a branch whose commits cancel out as already integrated.
  • signs_commits asks git from the current worktree instead of reading the cached config map, which is read from the common dir and so missed worktree-scoped config, and which read a valueless gpgsign as false where git reads it as true. wt merge --no-ff builds its merge commit with commit-tree and asks this helper whether to sign it.

test_step_squash_failed_commit_leaves_branch_intact covers the path end to end: a .git/hooks/pre-commit that exits 1 fails the squash and leaves the branch, HEAD and the history untouched; without the hook the same squash lands. It fails against the old code on the branch-tip assertion, and against a plumbing-based squash on the hook assertion. Two more hooks drive the failure paths the new sequence adds: one moves the branch mid-squash, so the compare-and-swap refuses rather than clobbering the other write, and one seals the worktree's git directory before rejecting the commit, so the reattach fails and has to say how to put HEAD back. The ORIG_HEAD write is best-effort, as git reset makes it — the branch has already moved by then, so failing there would report a squash that landed as an error.

This was written by Claude Code on behalf of max-sixty

max-sixty and others added 3 commits September 20, 2026 17:56
…ranch

`wt step squash` and `wt merge`'s squash step ran `git reset --soft
<merge-base>` and then `git commit`. When the commit failed (a `pre-commit`
hook rejecting it, a broken signing setup), the branch was left rewound to the
merge base with its commits collapsed into staged changes.

The squash now rewrites history the way `git rebase` does: it detaches HEAD
onto the merge base with `update-ref --no-deref`, which leaves the branch, the
index and the working tree untouched, commits there, then moves the branch to
the new commit with one compare-and-swap `update-ref` and puts HEAD back on it.
The branch keeps its commits until the commit exists, so a rejected or failed
commit leaves it exactly as it was; the reattach restores HEAD. Committing
through `git commit` rather than plumbing keeps git's own hooks,
`commit.cleanup`, signing and identity resolution behaving as they do for any
other commit — those hooks see a detached HEAD, as they do under `git rebase`.
ORIG_HEAD still records the pre-squash tip, as `git reset` left it.

The no-net-changes check compares the index tree against the merge base's tree
instead of asking `git diff --cached`, so a squash whose only change is a
submodule pointer hidden by `submodule.<name>.ignore` is committed rather than
reported as empty. That path still leaves the branch at the merge base.

`signs_commits` now asks git from the current worktree instead of reading the
cached config map, which is read from the common dir and so missed
worktree-scoped config, and which read a valueless `gpgsign` as false where git
reads it as true. `wt merge --no-ff` builds its merge commit with `commit-tree`
and asks this helper whether to sign it.

The failure mode was found in #4193.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
The squash detaches HEAD to build its commit, and every path that puts HEAD
back now reports the same recovery line when that fails — including the one
after a successful squash, which said only that it had failed.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>

@worktrunk-bot worktrunk-bot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two things worth a decision before this merges.

The ORIG_HEAD write can report a squash that landed as a failure. move_branch writes ORIG_HEAD after the compare-and-swap that moves the branch, and both errors return through the same path. If the branch update succeeds and the ORIG_HEAD write then fails — a ref lock held by something else in the worktree, ENOSPC, a read-only object store — handle_squash returns Err with the branch already at the squash commit and HEAD reattached to it. wt step squash exits non-zero on a squash that landed, and wt merge aborts before the rebase with the branch already rewritten. The same line reaches the no-net-changes path, where the branch has already been rewound to the merge base when the error goes out. That also makes reattach_after_failure's docstring wrong for this call site: "The branch never moved, so a successful reattach restores the worktree exactly as it was" holds for the commit failure above it, not for a move_branch error raised after the swap. Inline suggestion below makes the ORIG_HEAD write best-effort, which keeps the PR's own invariant in both directions — nothing written when the squash doesn't land, and a cosmetic write failure doesn't mask one that did.

codecov/patch is red at 92.85% against a base project coverage of 98.32%, so it needs the maintainer call the root CLAUDE.md → Coverage requires. Five lines are missed:

Line Path
src/git/repository/mod.rs:1749 signs_commits's non-0/non-1 exit arm
src/commands/step/squash.rs:376 the move_branch failure arm after the commit
src/commands/step/squash.rs:406, :409, :425 reattach_head's context closure and reattach_after_failure's Err arm

Two of them have deterministic triggers and are worth covering on their own merits, since both pin safety properties this PR introduces rather than just moving the number: commit.gpgSign = maybe makes git config --type=bool --get exit 128, which drives mod.rs:1749 straight from the new unit test; and a .git/hooks/pre-commit that runs git update-ref refs/heads/feature <other-sha> moves the branch inside the detached window, which is the only way to show the compare-and-swap actually refuses. That still leaves reattach_head failing with no deterministic trigger, so the patch would come to 95.71% (67/70) — under target either way.

One smaller thing: the replaced code carried a note that an external process could modify the index between the state mutation and the commit, and why that was accepted. The new sequence has the same window — write-tree snapshots the index for the emptiness check, then git commit re-reads it — but the comment block above it only speaks to the branch race the compare-and-swap covers. Worth carrying the index-race note across so the next reader doesn't re-derive it.

Comment thread src/commands/step/squash.rs Resolved
Comment thread src/git/repository/tests.rs Resolved
…ure paths

The branch has already moved by the time ORIG_HEAD is written, so failing
there reported a squash that landed as an error. `git reset` makes the same
write best-effort — it exits 0 and silently skips ORIG_HEAD when it cannot be
written — so this does too.

Tests cover the paths the change introduced: a `commit.gpgSign` value git
cannot read as a boolean, a branch moved by a concurrent writer while the
squash commit is being made, and a reattach that cannot write HEAD. The
TOCTOU note on the index window is back, rewritten for the new sequence.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@worktrunk-bot

Copy link
Copy Markdown
Collaborator

codecov/patch is green on 8ab8730 — 100.00% of diff hit against the 98.32% target — so the maintainer call I flagged on coverage is moot. The hook that seals the worktree's git directory covers the reattach_head arm I'd written off as having no deterministic trigger, which is why the patch landed above the 95.71% I projected.

Both inline fixes read as claimed: move_branch can now only fail at the compare-and-swap, before the branch moves, so reattach_after_failure's docstring holds at both call sites and an ORIG_HEAD failure can no longer turn a squash that landed into a non-zero exit. The index-window TOCTOU note is back, rewritten for the write-tree/git commit pair.

Required checks passed on that head (test (linux|macos|windows), lint, check-docs, code-coverage, fast-checks); affected tests (macos, advisory) and affected tests (windows, advisory) were still running when I looked.

@max-sixty
max-sixty merged commit d65b1a0 into main Sep 21, 2026
40 checks passed
@max-sixty
max-sixty deleted the 4193 branch September 21, 2026 01:43
@max-sixty max-sixty mentioned this pull request Sep 27, 2026
max-sixty added a commit that referenced this pull request Sep 27, 2026
Bumps 0.79.0 → 0.80.0 and adds the `## 0.80.0` changelog section, cut
from `12efb5159`. The bump is minor because the library API breaks (see
Verification).

## Verification

- `cargo run -- hook pre-merge --yes` on the cut tip: 4985 passed.
- `nightly.yaml` on `12efb5159`, all jobs green including
feature-powerset:
https://github.com/max-sixty/worktrunk/actions/runs/36338233859
- `cargo semver-checks check-release -p worktrunk` reports five
major-level lints, the removals and signature changes listed in the
Internal entry.
- Changelog: repeated verification passes against source until one came
back clean. Fixed headlines name the condition a bug needed rather than
its worst case, and near-zero-reach fixes are folded into two roll-up
bullets, per #4295.
- Data-loss surface review over `v0.79.0..HEAD` with independent
finders. The one real finding, a nested stale detached worktree read as
the enclosing repo's rebase so `wt step prune` could unregister it, was
fixed in #4302. The review also found a squash regression in the backup
path, fixed in #4303. The remaining candidates were adjudicated
acceptable and signed off: #4228 unregistering stale worktrees whose
directory remains (files stay on disk; the branch is deleted only if
integrated), #4206 leaving a detached `HEAD` on Ctrl-C during a slow
commit hook, #4251 allowing `--no-squash` merges with untracked files,
#4219 dropping `--no-hooks` from the skill example, and #4279's `docker
stop` recipe. #4252 closes a submodule data-loss path.

> _This was written by Claude Code on behalf of max-sixty_

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
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.

2 participants