Skip to content

fix(list, prune): degrade gracefully on unborn worktrees - #2937

Merged
max-sixty merged 1 commit into
mainfrom
fix/issue-2936
May 29, 2026
Merged

max-sixty merged 1 commit into
mainfrom
fix/issue-2936

Conversation

@worktrunk-bot

Copy link
Copy Markdown
Collaborator

Problem

git worktree add --orphan creates a linked worktree on an unborn branch — worktree list --porcelain reports its HEAD as the null OID. Two paths then fail:

  • wt list shows working-tree-diff (fatal: ambiguous argument 'HEAD') and working-tree-conflicts (fatal: 0000… is not a valid object) because the diff and merge-tree probes try to resolve HEAD / treat the null OID as a commit.
  • wt step prune aborts the entire scan with fatal: Needed a single revision because integration_reason runs git rev-parse --verify <unborn-branch>, blocking pruning of any other worktrees in the repo.

Solution

  • src/commands/list/collect/execution.rs: add WorkingTreeDiff and WorkingTreeConflicts to COMMIT_TASKS. Both seed defaults (None and Some(None)) already exist and render as ·, matching how the other commit-dependent columns degrade for unborn worktrees.
  • src/commands/step/prune.rs: skip !wt.has_commits() worktrees in gather_check_items — they can't be integrated and aren't auto-prune candidates. Mirrors the existing skip for locked worktrees.

Two existing tests (test_list_shows_warning_on_git_error and the diagnostic corrupt_worktree_head helper) used to write 0000… into a ref/HEAD file and rely on the resulting error warnings. After the fix those inputs read as unborn and skip silently, so both are updated to use 0000…001 — a non-null invalid SHA that still exercises the warning surfacing they were written to test.

Testing

  • test_list_unborn_worktree_no_task_failures — fails on main, passes after the list fix.
  • test_prune_skips_unborn_worktree — fails on main with Needed a single revision, passes after the prune fix and leaves the unborn worktree in place while still pruning the merged sibling.
  • Full cargo test --test integration clean (skipping pre-existing nushell flakes where nu isn't installed in this runner).
  • cargo clippy --all-targets -- -D warnings clean; cargo fmt --check clean.

Closes #2936 — automated triage

A linked worktree on an unborn branch (`git worktree add --orphan`)
reports `HEAD 0000…` in `worktree list --porcelain`. Two paths then fail:

- `wt list`: `WorkingTreeDiff` runs `git diff HEAD` and
  `WorkingTreeConflicts` runs `git merge-tree` against the null OID,
  surfacing `fatal: ambiguous argument 'HEAD'` and
  `fatal: 0000… is not a valid object`. Added both tasks to
  `COMMIT_TASKS` so they're skipped for `!has_commits()` worktrees,
  matching how the other commit-dependent columns degrade.

- `wt step prune`: `integration_reason` calls
  `git rev-parse --verify <branch>` against the unborn branch and aborts
  the whole scan with `fatal: Needed a single revision`. Skip unborn
  worktrees in `gather_check_items` — they have no commits to integrate.

Updates `test_list_shows_warning_on_git_error` and the diagnostic
`corrupt_worktree_head` helper to use a non-null invalid SHA so the
warning paths they exercise are not silenced by the new skip.

Closes #2936

Co-Authored-By: Claude <[email protected]>
@max-sixty

Copy link
Copy Markdown
Owner

what is an unborn branch?!

do we need to handle them elsewhere?

are there any other git intricacies we should be aware of, like this?

@worktrunk-bot

Copy link
Copy Markdown
Collaborator Author

What is an unborn branch?

A branch that has been named but has no commits yet: HEAD is a symbolic ref pointing at refs/heads/<name>, but the ref file doesn't exist (and git rev-parse HEAD errors with "ambiguous argument 'HEAD'"). You get one from git init, git checkout --orphan, or — for a linked worktree — git worktree add --orphan. In git worktree list --porcelain it shows up as HEAD 0000000000000000000000000000000000000000 (the null OID), which is the signal has_commits() keys off in src/git/mod.rs.

Do we need to handle them elsewhere?

The codebase already has a fair bit of unborn-aware plumbing — NULL_OID, has_commits(), seed_unborn_main_state in the list collector, head_sha() returning None on rev-parse failure, and comments throughout working_tree.rs and list/collect/ calling out the case. The shape of bug #2936 was specifically commands that iterate over every worktree and abort the whole scan on one bad row: wt list (the diff/merge-tree probes ran on every worktree, including unborn) and wt step prune (one git rev-parse --verify failure on an unborn branch killed the scan for every other worktree). This PR closes those two.

I don't think the targeted commands (wt merge, wt step rebase, wt step squash, wt worktree push, wt worktree finish) need new handling — they all run rev-parse HEAD and would surface a git error, but only if the user explicitly invoked them against an unborn worktree, which is meaningfully an operator error (there's nothing to merge/rebase/push). wt step commit is the interesting one: it does the first commit and then rev-parse HEAD afterwards (src/commands/commit.rs:199), so by then HEAD is no longer unborn — I haven't actually tested this end-to-end though, so worth flagging as a "should work, should verify" if you want to be sure.

Are there other git intricacies like this?

Yes — there's a family of "weird worktree states" worth being aware of. Most are already handled, but they share the same lesson: iteration commands need per-row degradation, not whole-scan failure.

  • Detached HEAD — branch is None. Handled via wt.branch.as_deref().unwrap_or("HEAD") in several places and by require_branch() returning an error when callers explicitly need a branch name.
  • Locked worktrees (git worktree lock) — wt.locked.is_some(). Already filtered in step/prune.rs:411.
  • Prunable worktrees — directory deleted, git metadata remains. is_prunable() filter; for_each already skips them.
  • Bare repositories — no main worktree; the "primary worktree" concept (default-branch worktree) covers this.
  • Worktrees mid-operation — rebase/bisect/merge/cherry-pick in progress. ActiveGitOperation surfaces this in wt list; git itself refuses many follow-up operations.
  • Shallow clones — git merge-base can fail to find a common ancestor because history is truncated. Worth keeping in mind for any new code that diffs against origin/<default>.

The throughline for the iteration commands (wt list, wt step prune, wt step for-each, wt relocate, wt step promote, wt step copy-ignored, wt list statusline): a single broken worktree should never cause the whole command to fail. Worth a grep when adding new iteration code.

@max-sixty
max-sixty merged commit f416143 into main May 29, 2026
33 checks passed
@max-sixty
max-sixty deleted the fix/issue-2936 branch May 29, 2026 06:29
@max-sixty max-sixty mentioned this pull request May 30, 2026
max-sixty added a commit that referenced this pull request May 30, 2026
v0.55.0.

`cargo semver-checks` reports breaking library API changes (9 inherent methods removed from `Repository` / `ShellConfig` by the dead-code cut-overs in #2931 — `is_ancestor`, `same_commit`, `has_added_changes`, `trees_match`, `head_tree_matches_branch`, `has_merge_conflicts`, `has_merge_conflicts_by_tree`, `merge_integration_probe`, `is_posix`). None are intentional public API, but they force a minor bump pre-1.0.

Headline user-visible changes:
- `wt switch` accepts forge PR/MR URLs anywhere `pr:N` / `mr:N` works (#2898, thanks @thiagowfx for the request).
- Claude Code plugin keeps unmerged branches during worktree cleanup — the `WorktreeRemove` hook drops `-D` (#2940, thanks @jbeda for reporting).
- `wt list` and `wt step prune` degrade gracefully on unborn worktrees (#2937, thanks @nedtwigg for reporting).
- `-vv` startup pointer now names `diagnostic.md` (#2919).
- Doc-site and `--help` prose cleanup (#2922, #2925); internal cut-over of duplicated/dead code paths (#2931, #2932, #2934).

Full CHANGELOG entry in this PR.

> _This was written by Claude Code on behalf of max_
social4hyq pushed a commit to social4hyq/homebrew-core that referenced this pull request Sep 20, 2026
worktrunk 0.55.0

Created-by: HarmonybrewBot
Commit-by: HarmonybrewBot
Merged-by: HarmonybrewBot
Description: Created by `brew bump`

---

Created with `brew bump-formula-pr`.<details>
  <summary>release notes</summary>
  <pre>## Release Notes

### Improved

- **`wt switch` accepts forge PR/MR URLs**: `wt switch https://github.com/owner/repo/pull/123` now resolves the same way as `wt switch pr:123`, and the URL form works anywhere a `pr:N` / `mr:N` shortcut does (positional argument and `--base`). Detection is shape-based, not host-based — any `http(s)://` URL whose path contains `/pull/N`, `/pulls/N`, `/-/merge_requests/N`, or `/pullrequest/N` matches, covering GitHub (including Enterprise), GitLab, Gitea, and Azure DevOps, including self-hosted instances. ([#2898](max-sixty/worktrunk#2898), thanks @thiagowfx for the request)

- **`-vv` startup pointer names `diagnostic.md`**: The `-vv` pointer now lists the shared log directory once with all three files it will contain — `trace.log`, `subprocess.log`, and `diagnostic.md` — so the diagnostic bundle is discoverable at startup rather than only when the gist hint fires at exit. The pointer verb reads `Writing to …` instead of `Tracing to …`. ([#2919](max-sixty/worktrunk#2919))

### Fixed

- **Claude Code plugin keeps unmerged branches during worktree cleanup**: The plugin's `WorktreeRemove` hook passed `-D` (`--force-delete`) to `wt remove`, which removes a branch even when it carries commits that aren't merged or pushed. The hook now uses the default removal: a merged or integrated branch is removed cleanly, while one with unmerged commits is kept, with a `wt remove -D <branch>` hint for deleting it deliberately. ([#2940](max-sixty/worktrunk#2940), thanks @jbeda for reporting)

- **`wt list` and `wt step prune` degrade gracefully on unborn worktrees**: A linked worktree created with `git worktree add --orphan` sits on an unborn branch whose `HEAD` is the null OID. `wt list` used to show `working-tree-diff (fatal: ambiguous argument 'HEAD')` and a merge-tree error in its columns, and `wt step prune` aborted its entire scan with `fatal: Needed a single revision`, blocking pruning of every other worktree. Both now treat an unborn worktree as having no commits: `wt list` renders `·` for the commit-dependent columns, and `wt step prune` skips it (as it does locked worktrees) and continues. ([#2937](max-sixty/worktrunk#2937), thanks @nedtwigg for reporting)

### Documentation

- **Doc-site and `--help` prose cleanup**: A writing-prose pass across the FAQ, config, list, remove, Claude Code, hook, LLM-commits, and Tips & Patterns pages (some via `--help` text in `after_long_help`). ([#2922](max-sixty/worktrunk#2922), [#2925](max-sixty/worktrunk#2925))

### Internal

- **Collapsed duplicated code paths and removed dead code left by completed cut-overs**: A net reduction across the shell, git, and command layers. ([#2931](max-sixty/worktrunk#2931), [#2932](max-sixty/worktrunk#2932), [#2934](max-sixty/worktrunk#2934))

## Install worktrunk 0.55.0

### Install prebuilt binaries via shell script

```sh
curl --proto '=https' --tlsv1.2 -LsSf https://github.com/max-sixty/worktrunk/releases/download/v0.55.0/worktrunk-installer.sh | sh && wt config shell install
```

### Install prebuilt binaries via powershell script

```sh
powershell -ExecutionPolicy Bypass -c "irm https://github.com/max-sixty/worktrunk/releases/download/v0.55.0/worktrunk-installer.ps1 | iex"; git-wt config shell install
```

### Install prebuilt binaries via Homebrew

```sh
brew install worktrunk && wt config shell install
```

## Download worktrunk 0.55.0

|  File  | Platform | Checksum |
|--------|----------|----------|
| [worktrunk-aarch64-apple-darwin.tar.xz](https://github.com/max-sixty/worktrunk/releases/download/v0.55.0/worktrunk-aarch64-apple-darwin.tar.xz) | Apple Silicon macOS | [checksum](https://github.com/max-sixty/worktrunk/releases/download/v0.55.0/worktrunk-aarch64-apple-darwin.tar.xz.sha256) |
| [worktrunk-x86_64-apple-darwin.tar.xz](https://github.com/max-sixty/worktrunk/releases/download/v0.55.0/worktrunk-x86_64-apple-darwin.tar.xz) | Intel macOS | [checksum](https://github.com/max-sixty/worktrunk/releases/download/v0.55.0/worktrunk-x86_64-apple-darwin.tar.xz.sha256) |
| [worktrunk-x86_64-pc-windows-msvc.zip](https://github.com/max-sixty/worktrunk/releases/download/v0.55.0/worktrunk-x86_64-pc-windows-msvc.zip) | x64 Windows | [checksum](https://github.com/max-sixty/worktrunk/releases/download/v0.55.0/worktrunk-x86_64-pc-windows-msvc.zip.sha256) |
| [worktrunk-aarch64-unknown-linux-musl.tar.xz](https://github.com/max-sixty/worktrunk/releases/download/v0.55.0/worktrunk-aarch64-unknown-linux-musl.tar.xz) | ARM64 MUSL Linux | [checksum](https://github.com/max-sixty/worktrunk/releases/download/v0.55.0/worktrunk-aarch64-unknown-linux-musl.tar.xz.sha256) |
| [worktrunk-x86_64-unknown-linux-musl.tar.xz](https://github.com/max-sixty/worktrunk/releases/download/v0.55.0/worktrunk-x86_64-unknown-linux-musl.tar.xz) | x64 MUSL Linux | [checksum](https://github.com/max-sixty/worktrunk/releases/download/v0.55.0/worktrunk-x86_64-unknown-linux-musl.tar.xz.sha256) |



### Install via Cargo

```sh
cargo install worktrunk && wt config shell install
```

### Install via Winget (Windows)

```sh
winget install max-sixty.worktrunk && git-wt config shell install
```

### Install via AUR (Arch Linux)

```sh
paru worktrunk-bin && wt config shell install
```
</pre>
  <p>View the full release notes at <a href="https://github.com/max-sixty/worktrunk/releases/tag/v0.55.0">https://github.com/max-sixty/worktrunk/releases/tag/v0.55.0</a>.</p>
</details>
<hr>

See merge request: Harmonybrew/homebrew-core!9819
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.

working-tree-diff (fatal: ambiguous argument 'HEAD')

2 participants