Skip to content

fix(list): tolerate missing index in temp-index probe - #2884

Merged
max-sixty merged 6 commits into
mainfrom
missing-index
Jun 5, 2026
Merged

max-sixty merged 6 commits into
mainfrom
missing-index

Conversation

@max-sixty

Copy link
Copy Markdown
Owner

Summary

  • A worktree whose <gitdir>/index file is absent used to surface as working-tree-conflicts (Failed to copy index file) in wt list --full because WorkingTree::temp_index() (and the parallel stage_to_temp_index() used by wt step commit --dry-run) called std::fs::copy on the real index and bailed on ENOENT.
  • Git itself treats a missing index as empty — git add against GIT_INDEX_FILE=<nonexistent path> creates a fresh valid index. Mirror that: on ENOENT from the copy, remove the freshly-created 0-byte tempfile (git rejects 0-byte files as "smaller than expected") and let the first git call against GIT_INDEX_FILE create the index. Other copy errors (corrupt index, permission denied, etc.) still bubble up unchanged.
  • Verified experimentally first: git init repo with .git/index removed, then GIT_INDEX_FILE=<absent path> git add -A succeeds and git write-tree returns a tree containing the working-tree files.

Test plan

  • Unit test: temp_index() succeeds without <gitdir>/index; git add -A + write-tree produces a tree containing file.txt, tracked.txt, untracked.txt; real index remains absent.
  • Integration test: wt list --full against a dirty linked worktree with deleted <gitdir>/index emits no Failed to copy index file or working-tree-conflicts ( footer; real index not resurrected.
  • Integration test: wt step commit --dry-run against a missing root index succeeds rather than erroring.
  • Existing regression test working_tree_diff_stats_with_untracked_counts_untracked_and_preserves_real_index still passes (real-index-not-mutated invariant).
  • cargo run -- hook pre-merge --yes clean (3824 tests + clippy + fmt + lychee + cargo-lock + custom lint hooks).

🤖 Generated with Claude Code

A worktree whose `<gitdir>/index` file is absent used to surface as
`working-tree-conflicts (Failed to copy index file)` in `wt list`,
because `WorkingTree::temp_index()` and `stage_to_temp_index()` both
errored when `std::fs::copy` hit ENOENT on the source. Git itself
treats a missing index as empty (`git add` populates it on demand);
mirror that — on ENOENT, remove the freshly-created tempfile (a
0-byte file is rejected as "smaller than expected") and let the
first `git` call against `GIT_INDEX_FILE` create a fresh valid
index at that path. Other copy errors still bubble up unchanged.

Tests cover the missing-index path at three layers:
- unit: `temp_index()` succeeds, `git add -A` + `write-tree`
  produces a tree containing the working-tree files, real index
  remains absent.
- integration: `wt list --full` against a dirty worktree with
  no index emits no `Failed to copy index file` footer.
- integration: `wt step commit --dry-run` against a missing
  index now succeeds.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
worktrunk-bot
worktrunk-bot previously approved these changes May 23, 2026
@worktrunk-bot

Copy link
Copy Markdown
Collaborator

CI flagged codecov/patch (95.34% vs project 97.26%, default auto target). Two uncovered lines, both the catch-all error arm in the new match:

  • src/git/repository/working_tree.rs:501: Err(e) => return Err(e).context("Failed to copy index file")
  • src/commands/step/commit.rs:139: same arm

Per CLAUDE.md, codecov/patch is a merge gate even though it's marked non-required, so dismissing my approval until this is resolved.

The non-ENOENT path is reachable in tests by making the real index a directory — fs::copy then fails with EISDIR, which is not NotFound, so it falls through to the catch-all. A one-shot test along the lines of:

#[test]
fn temp_index_propagates_non_not_found_copy_errors() {
    let test = TestRepo::with_initial_commit();
    let repo = Repository::at(test.root_path()).unwrap();
    let wt = repo.worktree_at(test.root_path());
    let real_index = wt.git_dir().unwrap().join("index");
    std::fs::remove_file(&real_index).unwrap();
    std::fs::create_dir(&real_index).unwrap(); // makes fs::copy return EISDIR

    let err = wt.temp_index().unwrap_err();
    assert!(err.to_string().contains("Failed to copy index file"));
}

would cover the working_tree.rs arm. The stage_to_temp_index arm is identical and probably not worth a parallel test if you extract the helper or treat the two arms as one logical line — happy to leave that judgment to you.

One unrelated note: affected tests (linux, advisory) failed at 11s in the "Compute merge-base for cache key" step with fatal: could not read Username for 'https://github.com'. That's a credential blip on the runner — not related to this PR; a rerun should clear it.

@worktrunk-bot
worktrunk-bot dismissed their stale review May 23, 2026 18:40

codecov/patch failed (95.34% vs project 97.26%) — see PR comment for the uncovered branches and a test sketch.

Collapse the missing-index handling: clear the 0-byte tempfile first,
then copy the real index back only when it exists. Removes the
uncovered `Err(e) => return Err(e).context(...)` arm that codecov flagged
and that no test exercises. Behaviour is unchanged on every path the
existing tests cover — missing index produces a fresh git-created
index; present index gets copied; non-ENOENT copy failures surface
through the same `.context("Failed to copy index file")` as before.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
worktrunk-bot
worktrunk-bot previously approved these changes May 23, 2026
@worktrunk-bot

Copy link
Copy Markdown
Collaborator

affected tests (linux, advisory) failed with working_tree_diff_stats_with_untracked_counts_untracked_and_preserves_real_index asserting left: 2, right: 3 (run 77541692695). The same test passes on the required test (linux) job for this SHA (3824 / 3824 passed) and on affected tests (macos, advisory) + affected tests (windows, advisory) — looks like a flake, not a regression from the refactor. The advisory job has now flaked twice in a row on this PR with two different shapes (last time was the credential blip). Approval stands; happy to dig in if it recurs.

NamedTempFile holds an open file handle. On Windows, deleting a file
with an open handle leaves the name delete-pending — neither the
following fs::copy nor git can recreate GIT_INDEX_FILE at that path
until the handle closes. Convert via into_temp_path() up-front so the
file handle is dropped while the unique path remains reserved (and
cleaned up on TempPath drop).

Caller-visible: stage_to_temp_index returns TempPath; TempIndex.temp
holds TempPath. Both impl Deref<Target = Path>, so callers using
.path() / & continue to work via deref coercion (`as_deref()` for the
Option<&Path>). No behaviour change on Unix-like systems.

Reported by Codex review.

Co-Authored-By: Claude Opus 4.7 (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.

lint is failing — cargo fmt wants the new stage_to_temp_index signature on a single line now that the return type fits within the width budget (tempfile::TempPath is shorter than tempfile::NamedTempFile). Inline suggestion below; happy to push the one-line fix if you'd rather not round-trip.

The Windows handle-close fix itself looks right — NamedTempFile::into_temp_path() drops the open File before we unlink, which is the documented pattern for unlinking a tempfile-managed path on Windows. The codecov gap from the previous round is gone with the if real_index.exists() form, so dismissing only on the lint failure.

Comment thread src/commands/step/commit.rs Resolved
@worktrunk-bot
worktrunk-bot dismissed their stale review May 23, 2026 19:02

cargo fmt regression introduced by the type swap — see follow-up review for the one-line fix.

Signature fits on one line after the TempPath return-type rename.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@max-sixty
max-sixty merged commit ba518b4 into main Jun 5, 2026
33 checks passed
@max-sixty
max-sixty deleted the missing-index branch June 5, 2026 07:11
@max-sixty max-sixty mentioned this pull request Jun 9, 2026
max-sixty added a commit that referenced this pull request Jun 9, 2026
v0.57.0.

`cargo semver-checks` reports breaking library API changes: the squash
refactor (#2983) renamed `Repository::commit_subjects` to
`commit_message_details`, and the new `GitError::UnbornDefaultBranch`
variant (#2990) shifts the discriminants of the exhaustive `GitError`
enum. Neither is intentional public API, but together they force a minor
bump pre-1.0.

## CHANGELOG

### Improved

- **`wt step diff --branch`**: `wt step diff` gained a `-b`/`--branch`
flag, mirroring `wt step commit`, so the diff can target another
worktree's branch without leaving the current one. The branch must have
a checked-out worktree.
([#2995](#2995))

- **Squash templates can use commit bodies**: The squash commit-message
template gains an experimental `{{ commit_details }}` variable — a list
of `{ subject, body }` objects for the commits being squashed —
alongside the existing `{{ commits }}` (now documented as the commit
subjects). Templates can incorporate full commit bodies, not just
subject lines.
([#2983](#2983), thanks
@florianilch)

- **Recommended Claude Code commit command drops the `CLAUDECODE=`
prefix**: Claude Code removed the nested-session check that rejected
`claude -p` launched from inside another session, so the workaround is
gone. The recommended `[commit.generation]` command shown by `wt config
create` no longer carries a leading `CLAUDECODE=`, and `wt` no longer
strips `CLAUDECODE` from the environment before running
commit-generation commands.
([#2979](#2979))

### Fixed

- **Nushell wrapper installs where Nushell actually autoloads it**: `wt
config shell install nu` wrote `wt.nu` to
`$nu.default-config-dir/vendor/autoload`, which Nushell never autoloads
— on Linux the wrapper was written but silently never loaded, so `wt`
was never wrapped (it happened to work on macOS/Windows by coincidence
of path layout). It now installs to `$nu.vendor-autoload-dirs | last`,
and install/uninstall clean up any worktrunk wrapper stranded at the old
location. ([#2992](#2992),
thanks @nnutter for reporting)

- **Claude Code hooks work for Fish shell users**: The plugin's hook
commands used `${CLAUDE_PLUGIN_ROOT}` brace syntax, which Fish doesn't
expand; they now use `$CLAUDE_PLUGIN_ROOT`, so the activity and
worktree-lifecycle hooks fire correctly under Fish.
([#2962](#2962), thanks @amw)

- **Pager no longer wedges the terminal on Ctrl-C (Windows)**:
Interrupting the `--help` pager (`less`) with Ctrl-C on Windows could
leave the terminal in a broken state; `less` now quits cleanly on
interrupt. ([#2969](#2969),
thanks @ofek for reporting)

- **Clearer error when the default branch has no commits**: In a freshly
initialized repo whose default branch is unborn,
`merge`/`rebase`/`squash`/`push` (and the diff report) failed with
`Default branch main does not exist locally` plus a misleading hint to
reset the cached value. They now report that the branch has no commits
yet, without the wrong cache-reset suggestion.
([#2990](#2990))

- **`diagnostic.md` uploads as a gist again**: The `-vv` diagnostic
report inlined raw NUL bytes from NUL-separated git output, so `gh gist
create` rejected it as a binary file. Control bytes in the subprocess
preview are now escaped.
([#2991](#2991))

- **`wt list` tolerates a missing index file**: A repo with no
`<gitdir>/index` (nothing ever staged) made the temp-index probe fail; a
missing index is now treated as an empty one, matching git's own
behavior. ([#2884](#2884))

- **Inline code renders in `--help` section headings**: Terminal
`--help` showed literal backticks in headings authored with inline code
(e.g. the `wt config state logs` heading `Command log
(`commands.jsonl`)`). Headings now reduce inline code to plain text
under the heading's uniform style. The `--stage`/`--dry-run` subsection
headings in `wt step commit`/`squash` were also renamed to sentence case
("Staging", "Dry run").
([#3003](#3003))

### Documentation

- **`wt switch` docs give forge PR/MR URLs equal billing with
`pr:`/`mr:`**: The switch docs now present the full forge-URL form
alongside the `pr:N` shortcut.
([#2970](#2970))

- **New FAQ entry on moving uncommitted changes to a new worktree**.
([#3002](#3002))

### Internal

- **Bare-repo prompt opt-out stored as a hint**:
`worktrunk.skip-bare-repo-prompt` moved under the `worktrunk.hints.`
namespace, so it now lists under `wt config state hints` and clears with
`wt config state clear` (previously a top-level key that escaped both).
Clean cutover: users who already opted out are re-prompted once on their
next `wt switch --create` in a dotted-name bare repo.
([#3001](#3001))

> _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.57.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 step diff --branch`**: `wt step diff` gained a `-b`/`--branch` flag, mirroring `wt step commit`, so the diff can target another worktree's branch without leaving the current one. The branch must have a checked-out worktree. ([#2995](max-sixty/worktrunk#2995))

- **Squash templates can use commit bodies**: The squash commit-message template gains an experimental `{{ commit_details }}` variable — a list of `{ subject, body }` objects for the commits being squashed — alongside the existing `{{ commits }}` (now documented as the commit subjects). Templates can incorporate full commit bodies, not just subject lines. ([#2983](max-sixty/worktrunk#2983), thanks @florianilch)

- **Recommended Claude Code commit command drops the `CLAUDECODE=` prefix**: Claude Code removed the nested-session check that rejected `claude -p` launched from inside another session, so the workaround is gone. The recommended `[commit.generation]` command shown by `wt config create` no longer carries a leading `CLAUDECODE=`, and `wt` no longer strips `CLAUDECODE` from the environment before running commit-generation commands. ([#2979](max-sixty/worktrunk#2979))

### Fixed

- **Nushell wrapper installs where Nushell actually autoloads it**: `wt config shell install nu` wrote `wt.nu` to `$nu.default-config-dir/vendor/autoload`, which Nushell never autoloads — on Linux the wrapper was written but silently never loaded, so `wt` was never wrapped (it happened to work on macOS/Windows by coincidence of path layout). It now installs to `$nu.vendor-autoload-dirs | last`, and install/uninstall clean up any worktrunk wrapper stranded at the old location. ([#2992](max-sixty/worktrunk#2992), thanks @nnutter for reporting)

- **Claude Code hooks work for Fish shell users**: The plugin's hook commands used `${CLAUDE_PLUGIN_ROOT}` brace syntax, which Fish doesn't expand; they now use `$CLAUDE_PLUGIN_ROOT`, so the activity and worktree-lifecycle hooks fire correctly under Fish. ([#2962](max-sixty/worktrunk#2962), thanks @amw)

- **Pager no longer wedges the terminal on Ctrl-C (Windows)**: Interrupting the `--help` pager (`less`) with Ctrl-C on Windows could leave the terminal in a broken state; `less` now quits cleanly on interrupt. ([#2969](max-sixty/worktrunk#2969), thanks @ofek for reporting)

- **Clearer error when the default branch has no commits**: In a freshly initialized repo whose default branch is unborn, `merge`/`rebase`/`squash`/`push` (and the diff report) failed with `Default branch main does not exist locally` plus a misleading hint to reset the cached value. They now report that the branch has no commits yet, without the wrong cache-reset suggestion. ([#2990](max-sixty/worktrunk#2990))

- **`diagnostic.md` uploads as a gist again**: The `-vv` diagnostic report inlined raw NUL bytes from NUL-separated git output, so `gh gist create` rejected it as a binary file. Control bytes in the subprocess preview are now escaped. ([#2991](max-sixty/worktrunk#2991))

- **`wt list` tolerates a missing index file**: A repo with no `<gitdir>/index` (nothing ever staged) made the temp-index probe fail; a missing index is now treated as an empty one, matching git's own behavior. ([#2884](max-sixty/worktrunk#2884))

- **Inline code renders in `--help` section headings**: Terminal `--help` showed literal backticks in headings authored with inline code (e.g. the `wt config state logs` heading `Command log (`commands.jsonl`)`). Headings now reduce inline code to plain text under the heading's uniform style. The `--stage`/`--dry-run` subsection headings in `wt step commit`/`squash` were also renamed to sentence case ("Staging", "Dry run"). ([#3003](max-sixty/worktrunk#3003))

### Documentation

- **`wt switch` docs give forge PR/MR URLs equal billing with `pr:`/`mr:`**: The switch docs now present the full forge-URL form alongside the `pr:N` shortcut. ([#2970](max-sixty/worktrunk#2970))

- **New FAQ entry on moving uncommitted changes to a new worktree**. ([#3002](max-sixty/worktrunk#3002))

### Internal

- **Bare-repo prompt opt-out stored as a hint**: `worktrunk.skip-bare-repo-prompt` moved under the `worktrunk.hints.` namespace, so it now lists under `wt config state hints` and clears with `wt config state clear` (previously a top-level key that escaped both). Clean cutover: users who already opted out are re-prompted once on their next `wt switch --create` in a dotted-name bare repo. ([#3001](max-sixty/worktrunk#3001))

## Install worktrunk 0.57.0

### Install prebuilt binaries via shell script

```sh
curl --proto '=https' --tlsv1.2 -LsSf https://github.com/max-sixty/worktrunk/releases/download/v0.57.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.57.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.57.0

|  File  | Platform | Checksum |
|--------|----------|----------|
| [worktrunk-aarch64-apple-darwin.tar.xz](https://github.com/max-sixty/worktrunk/releases/download/v0.57.0/worktrunk-aarch64-apple-darwin.tar.xz) | Apple Silicon macOS | [checksum](https://github.com/max-sixty/worktrunk/releases/download/v0.57.0/worktrunk-aarch64-apple-darwin.tar.xz.sha256) |
| [worktrunk-x86_64-apple-darwin.tar.xz](https://github.com/max-sixty/worktrunk/releases/download/v0.57.0/worktrunk-x86_64-apple-darwin.tar.xz) | Intel macOS | [checksum](https://github.com/max-sixty/worktrunk/releases/download/v0.57.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.57.0/worktrunk-x86_64-pc-windows-msvc.zip) | x64 Windows | [checksum](https://github.com/max-sixty/worktrunk/releases/download/v0.57.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.57.0/worktrunk-aarch64-unknown-linux-musl.tar.xz) | ARM64 MUSL Linux | [checksum](https://github.com/max-sixty/worktrunk/releases/download/v0.57.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.57.0/worktrunk-x86_64-unknown-linux-musl.tar.xz) | x64 MUSL Linux | [checksum](https://github.com/max-sixty/worktrunk/releases/download/v0.57.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.57.0">https://github.com/max-sixty/worktrunk/releases/tag/v0.57.0</a>.</p>
</details>
<hr>

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