Skip to content

fix(recover): find the bare repository when a deleted CWD has no .git ancestor - #4067

Merged
max-sixty merged 5 commits into
mainfrom
nightly/clean-34678483266-bare-recover
Sep 13, 2026
Merged

max-sixty merged 5 commits into
mainfrom
nightly/clean-34678483266-bare-recover

Conversation

@worktrunk-bot

@worktrunk-bot worktrunk-bot commented Sep 12, 2026 •

Copy link
Copy Markdown
Collaborator

Recovering from a deleted current directory never worked in a bare repository. recover_from_path walks up from $PWD looking for a .git directory, and a bare repo has no .git entry at all — neither at the bare directory itself nor at any ancestor — so the walk ran to the filesystem root and returned nothing. Every caller then degraded: wt switch and the picker surfaced git's raw "cannot resolve CWD" error instead of recovering, and cwd_removed_hint() fell back to the bare Current directory was removed line with no wt switch ^ suggestion. Bare repos are a first-class layout here — and their worktrees usually sit inside the bare directory, so the walk has nowhere else to land.

try_repo_at now also accepts a directory that is itself a bare repository, using git's own is_git_directory heuristic (a HEAD file, an objects/ directory, a refs/ directory). The probe is a filesystem check rather than a Repository::at() call because at() discovers upward: asking it whether an arbitrary ancestor is a repository answers for some enclosing repository instead of reporting "not one here". The existing was_worktree_of validation is unchanged, so a repository found this way still has to list the deleted path among its worktrees before recovery accepts it.

The probe lives in git::is_bare_repo_dir, shared with holds_git_data in src/git/repository/worktrees.rs, which already ended in the same three checks — one home for the shape, so a later correction to it lands once.

Verified by temporarily reverting the one-line predicate with the tests in place: test_recover_from_path_bare_repo_worktree and test_try_repo_at_accepts_bare_repo_dir both fail without it and pass with it.

Local test runs

cargo test --lib git::recover (23 passed), cargo test --test integration bare_repository (37 passed), cargo test --test integration recover (1 passed), cargo fmt --check, cargo clippy --lib --all-features — all clean.

One unrelated unit test, copy::tests::test_copy_dir_recursive_does_not_count_a_non_regular_file, fails in the tend sandbox with PermissionDenied from UnixListener::bind — the sandbox blocks AF_UNIX sockets. It is green on main in CI and untouched here.

… ancestor

Deleted-CWD recovery walked ancestors looking for a .git directory, which a bare repo never has — so every bare layout fell through to 'no repository found' and wt switch reported the raw CWD error instead of recovering.
@worktrunk-bot worktrunk-bot added the nightly-cleanup Issues found by nightly code quality sweep label Sep 12, 2026

@worktrunk-bot worktrunk-bot left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

One finding: the new bare-repo predicate duplicates one the crate already has.

holds_git_data in src/git/repository/worktrees.rs ends in the same three checks and its docstring cites the same source — "The bare test is git's own from is_git_directory(): HEAD, plus objects and refs directories." The two copies diverged where they were written: the existing one tests HEAD with exists(), the new one with is_file(). So the heuristic now has two homes and no single place to correct when git's shape moves — the reftable backend came close to retiring the refs/ directory both copies depend on.

Pushing a commit that lifts the predicate into src/git/mod.rs as pub(crate) fn is_bare_repo_dir and has both call sites use it; holds_git_data keeps its own reasoning about the .git probe and just names the shared test.

Comment thread src/git/recover.rs Resolved
`holds_git_data` in `src/git/repository/worktrees.rs` already ended in git's
`is_git_directory()` triple, so the new recovery probe was a second copy of it
— and the copies already differed in how they tested `HEAD` (`exists()` vs
`is_file()`). Lift the predicate to `git::is_bare_repo_dir` and have both call
sites use it, so a later correction to the shape has one place to land.

@worktrunk-bot worktrunk-bot left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

The duplication finding from the earlier review is resolved — one is_bare_repo_dir, both call sites on it, and the is_file()/exists() drift gone with the second copy. Three smaller things the consolidation left behind; I'm pushing all three, since none sits inside a diff hunk.

find_validated_repo_near's loop still comments // Check immediate children for .git directories. The children arm now matches a bare repo too — that is what makes the <root>/.bare plus sibling-worktree layout recoverable, since there the bare directory is a child rather than the ancestor. The function's own docstring was updated for exactly this in the same commit, so the inner comment now contradicts it.

is_bare_repo_dir's docstring argues its shallowness is cheap because the recovery caller "hands the path to Repository::at, which fails on a non-repository" — but five lines above it warns that at() answers upward, which is why it isn't used as the probe. Those can't both hold: a false positive nested inside a real repository makes at() succeed on the enclosing one. What actually bounds the cost is was_worktree_of, which still requires the deleted path in the repository's worktree list. Naming that instead keeps a later reader from taking the predicate for self-guarding and dropping the validation.

No ## Unreleased → ### Fixed entry. Recovery from a deleted CWD never worked in a bare layout, and bare is a first-class layout here, so wt switch from a removed worktree went from git's raw "cannot resolve CWD" to recovering — visible enough to belong in the release notes next to the other wt switch fixes.

Nothing else: the HEAD probe moving from exists() to is_file() differs only for a non-regular-file HEAD, which is not a git directory (an_unresolvable_git_entry_still_counts_as_git_data and the lone-HEAD decoy assertion both still hold), and the bare entry list_worktrees would otherwise contribute to was_worktree_of is filtered by its !wt.bare predicate, so widening try_repo_at can't make an arbitrary deleted path inside a bare directory look like a worktree of it.

…the changelog entry

The children loop still said it checked for `.git` directories, which the bare arm made wrong — that arm is what a sibling layout needs, where the repository is a child rather than an ancestor.

`is_bare_repo_dir`'s docstring justified its shallowness with `Repository::at` failing on a non-repository, contradicting its own note that `at()` discovers upward: a false positive inside a real repository makes it succeed. The bound is `was_worktree_of`, so name that instead.
@max-sixty

Copy link
Copy Markdown
Owner

resolve conflicts, then we can merge

@worktrunk-bot

Copy link
Copy Markdown
Collaborator Author

Conflicts resolved and pushed as a4edf0b. The only conflict was in CHANGELOG.md, where #4073's locked-worktree entry landed at the same position under ### Fixed; both entries are kept, #4073 first. The merge changes nothing else — git diff main..HEAD is still just the four files this PR touches (CHANGELOG.md, src/git/mod.rs, src/git/recover.rs, src/git/repository/worktrees.rs).

All four required checks pass at that head — test (linux), test (macos), test (windows), fast-checks — along with codecov/patch. affected tests (macos, advisory) was still running when I stopped polling. git merge-tree against the current main (48626f2) is clean.

@max-sixty
max-sixty merged commit 28399c2 into main Sep 13, 2026
37 checks passed
@max-sixty
max-sixty deleted the nightly/clean-34678483266-bare-recover branch September 13, 2026 03:52
@max-sixty max-sixty mentioned this pull request Sep 16, 2026
social4hyq pushed a commit to social4hyq/homebrew-core that referenced this pull request Sep 20, 2026
worktrunk 0.78.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

- **Hook scripts read renamed context keys**: the JSON piped to every hook drops `worktree`, `repo_root`, `main_worktree`, and `main_worktree_path` in favour of `worktree_path`, `repo_path`, `repo`, and `primary_worktree_path`. Config templates migrate on load, so hooks written with the old names keep working; scripts reading the JSON, `--execute` templates, and `--var` overrides need updating. (Breaking.) ([#4080](max-sixty/worktrunk#4080))

- **`wt config plugins pi` installs for Pi; oh-my-pi moves to `wt config plugins omp`**: `pi install` wrote an oh-my-pi hook, which Pi never loads; Pi users re-run it to get a Pi extension. Existing oh-my-pi hooks keep working, listed as outdated until `wt config plugins omp install`. (Breaking: scripts that run `wt config plugins pi` for oh-my-pi need `omp`.) [Docs](https://worktrunk.dev/claude-code/#oh-my-pi) ([#4135](max-sixty/worktrunk#4135), thanks @ashebanow)

- **Interactive prompts no longer open with a blank line**: `wt config shell install`, `wt config plugins claude install`, and the commit-message setup offer in `wt merge` began with one. ([#4059](max-sixty/worktrunk#4059))

- **`wt config show` gives a single `wt config shell install` hint**: an outdated wrapper, a fish wrapper at the deprecated `conf.d` path, and missing fish completions each printed their own hint. The zsh `compinit` snippet is now syntax-highlighted. ([#4059](max-sixty/worktrunk#4059))

- **`wt config update --output <path>` confirms the write**: it now prints `✓ Wrote user config migration @ ~/migrated.toml` where it printed nothing. `--output=-` stays silent. ([#4053](max-sixty/worktrunk#4053))

### Fixed

- **Removal no longer discards untracked files hidden by `status.showUntrackedFiles = no`**: under that setting, a worktree holding only untracked files looked clean, so `wt remove`, `wt merge`, and `wt step prune` removed it without `--force`. Safety checks now always count untracked files, and auto-staging warnings list every file they stage. ([#4111](max-sixty/worktrunk#4111), thanks @Duang777)

- **`wt remove` and `wt merge` check the right worktree under an inherited `GIT_DIR`**: run from a `!wt` git alias or a git hook, which pass `GIT_DIR` down, `wt remove` could delete a dirty worktree without `--force`, and `wt merge` could refuse a clean merge. Both now check the worktree they act on. ([#4082](max-sixty/worktrunk#4082), thanks @Duang777)

- **`wt merge` no longer deletes a worktree locked with `git worktree lock`**: the merge completes and prints `Worktree preserved (locked)`. Every Worktrunk removal refuses a locked worktree, even with `--force`; run `git worktree unlock <path>` first. ([#4073](max-sixty/worktrunk#4073), thanks @Duang777)

- **`wt config update` no longer overwrites a config edited while its prompt is open**: it wrote the pre-prompt snapshot over the newer file and reported success. It now fails with the path and leaves the newer file; re-running previews the current contents. ([#4127](max-sixty/worktrunk#4127), thanks @zach-hammad-vs for reporting)

- **`wt step prune` no longer deletes a branch created minutes ago from an older commit**: `--min-age` aged a branch without a worktree by its commit's date, so a new branch off a day-old commit was pruned on the next run. Age now comes from the branch's oldest reflog entry. ([#4077](max-sixty/worktrunk#4077))

- **A comment or blank line above an inline config section no longer breaks the config**: a save or migration that rewrote `commit = { … }` as `[commit]` put that line inside the brackets. `wt switch`, `wt merge`, `wt remove`, and several `wt step` commands then failed with `Failed to load config`; `wt list` warned and ran without the config. Saves also dropped unrecognized keys from inline sections. ([#4120](max-sixty/worktrunk#4120), thanks @zach-hammad-vs for reporting)

- **Config saves keep the trailing comment on a value they rewrite**: a save such as answering the commit-generation offer dropped the comment after each value it rewrote, including one after an inline section's closing brace. ([#4138](max-sixty/worktrunk#4138))

- **Ignored project-config keys warn from every worktree**: an unknown key in `.config/wt.toml`, or one that belongs in user config such as `[merge]`, warned only in the main worktree, so `wt merge` from a linked worktree ignored it silently. ([#4145](max-sixty/worktrunk#4145), thanks @ee-prog for reporting)

- **`wt merge` skips `post-commit` when it removes that hook's worktree**: before, the hook ran in the primary worktree when the removed one sat inside the repository, and otherwise didn't run. The merge now prints `▲ Skipped post-commit`; move work that must finish there to `pre-remove`. ([#4049](max-sixty/worktrunk#4049))

- **`wt switch` recovers from a removed worktree in a bare repository**: `wt switch` and the picker surfaced git's raw `fatal: Unable to read current working directory` instead, and the removed-directory message lacked `wt switch ^`. ([#4067](max-sixty/worktrunk#4067))

- **`wt config shell install fish` puts the wrapper where fish reads it under a custom `$XDG_CONFIG_HOME`**: the wrapper always went to `~/.config/fish/functions/`, so install reported success while `wt switch` changed no directory. On Windows, the fish completion moves from `%APPDATA%\fish\completions` to the wrapper's directory. ([#4107](max-sixty/worktrunk#4107))

- **`wt config shell install zsh` honours `$ZDOTDIR` only when it is absolute**: an empty or relative value resolved against the current directory, so install appended to a `.zshrc` under the current directory and reported success while the file zsh reads went untouched. A non-absolute value now falls back to `$HOME`. ([#4085](max-sixty/worktrunk#4085))

- **`wt config shell install` for Nushell no longer deletes a `wt.nu` under the current directory**: an empty or relative `$XDG_CONFIG_HOME` resolved against it, so install removed a `nushell/vendor/autoload/wt.nu` beneath it unread, as a stale wrapper. It now falls back to `~/.config`, which also fixes unread `tea` logins that let `wt switch pr:<n>` treat a Gitea host as GitHub. ([#4104](max-sixty/worktrunk#4104))

- **`wt switch --create` names the branch a failed worktree add leaves behind**: `git worktree add -b` writes the branch ref before populating the worktree, so a failure in between left the branch with no worktree, and the next attempt reported a name collision. The error now says how to delete or reuse it. ([#4109](max-sixty/worktrunk#4109), thanks @technicalpickles for reporting)

- **`wt config shell install` no longer tells an already-wrapped shell to restart**: reinstalling from a shell with the wrapper loaded, such as after a version bump, printed `↳ Restart shell to activate shell integration`. ([#4059](max-sixty/worktrunk#4059))

- **`wt config shell install` migrates a deprecated `conf.d` fish wrapper when `~/.config/fish/functions` is missing**: before, only `wt config shell install fish` migrated it. `wt config show` now reports that wrapper even when fish isn't on `PATH`. ([#4059](max-sixty/worktrunk#4059))

- **Plugin status and uninstall checks ask the agent CLIs**: `wt config show` could list an installed Claude Code plugin or Gemini CLI extension as not installed, and `wt config plugins claude|codex uninstall` could report success over a marketplace still configured. A repeat Claude uninstall now runs both removals instead of stopping at `Plugin not installed`. ([#4048](max-sixty/worktrunk#4048), [#4054](max-sixty/worktrunk#4054))

- **`wt config plugins opencode install` no longer writes the plugin into the current directory**: with `OPENCODE_CONFIG_DIR` set but empty, it wrote `plugins/worktrunk.ts` there, typically inside a repository, reporting success though OpenCode never saw it. An empty value now counts as unset. ([#4084](max-sixty/worktrunk#4084))

- **The OpenCode plugin clears its activity marker when OpenCode 1 disposes it**: only explicit session deletion cleared it, so a finished session kept its `💬` and `wt list` showed it as active until `wt config state marker clear` was run by hand. ([#4129](max-sixty/worktrunk#4129), thanks @drewdas for reporting)

- **`wt config update` migrates `[ci] platform` into a `[forge]` section that only sets `hostname`**: that config, common with GitHub Enterprise and self-hosted GitLab, kept the deprecated key with no deprecation warning. ([#4061](max-sixty/worktrunk#4061))

- **Deprecated keys in an inline `[projects]` entry now migrate**: `"example.com/org/repo" = { merge = { no-ff = true } }` skipped the migration the section form got, so the per-project setting was ignored. ([#4122](max-sixty/worktrunk#4122), thanks @zach-hammad-vs for reporting)

- **A deprecated config section the migration can't rewrite now warns**: `select = "not a table"`, or a `[select]` whose `[switch.picker]` destination is already set, was ignored without a message. It now reports `unknown field select (will be ignored)`. ([#4121](max-sixty/worktrunk#4121), thanks @zach-hammad-vs for reporting)

- **`wt config update` no longer changes what a template renders**: it could rename `{{ repo_root }}` inside a quoted s

See merge request: Harmonybrew/homebrew-core!20435
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

nightly-cleanup Issues found by nightly code quality sweep

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants