Skip to content

fix: propagate repo_path() errors via Result instead of fallback - #1280

Merged
max-sixty merged 5 commits into
mainfrom
fix/repo-path-result-propagation
Mar 5, 2026
Merged

max-sixty merged 5 commits into
mainfrom
fix/repo-path-result-propagation

Conversation

@worktrunk-bot

Copy link
Copy Markdown
Collaborator

Summary

Alternative approach to #1225, as requested by @max-sixty.

Instead of silently falling back when is_bare() fails in repo_path(), this PR changes repo_path() to return Result<&Path> and propagates errors to callers:

  • Changes repo_path() from get_or_init to get_or_try_init, returning anyhow::Result<&Path>
  • Propagates the error via ? at ~20 call sites across 18 files
  • build_hook_context() and format_path() also updated to return Result
  • Best-effort contexts (deprecation warnings, log formatter) use .ok() / let Ok(..) else to silently handle failures

Tradeoff vs #1225: More code churn but a cleaner failure mode — git failures surface early with clear errors instead of caching a potentially wrong path.

Test plan

  • All 554 unit tests pass
  • All 1144 integration tests pass (including 21 bare repo tests)
  • Clippy clean, no warnings
  • CI (linux, macos, windows)

🤖 Generated with Claude Code

Alternative to #1225. Changes `repo_path()` to return `anyhow::Result<&Path>`
using `get_or_try_init`, propagating `is_bare()` failures to callers instead
of silently caching a fallback value.

This surfaces git failures (e.g., timeouts) early with clear errors rather
than caching a potentially wrong path. The tradeoff is more code churn
(18 files, ~20 call sites) but a cleaner failure mode.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
worktrunk-bot and others added 2 commits March 5, 2026 07:18
@max-sixty

Copy link
Copy Markdown
Owner

do we snapshot the error message when is_bare fails? please could we do so if not?

@worktrunk-bot

Copy link
Copy Markdown
Collaborator Author

Good call — we didn't have one. Added a snapshot test in ce7ebb0 that constructs a Repository with a non-existent git_common_dir, which makes both --show-toplevel and is_bare() fail. The snapshotted error:

failed to check if repository is bare: No such file or directory (os error 2)

@max-sixty

Copy link
Copy Markdown
Owner

@worktrunk-bot please fix tests

…essage

The repo_path() error snapshot fails on Windows because the OS error
text differs ("No such file or directory" vs "The directory name is
invalid."). Replace the insta snapshot with a prefix assertion on the
stable part of the message.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
@worktrunk-bot

Copy link
Copy Markdown
Collaborator Author

Fixed in fc18cc9. The snapshot was using an OS-specific error message — "No such file or directory (os error 2)" on Linux vs "The directory name is invalid. (os error 267)" on Windows. Replaced the insta snapshot with a prefix assertion on the stable part ("failed to check if repository is bare: ").

CI is now green across all platforms.

@max-sixty
max-sixty merged commit 79361c6 into main Mar 5, 2026
21 checks passed
@max-sixty
max-sixty deleted the fix/repo-path-result-propagation branch March 5, 2026 16:15
worktrunk-bot added a commit that referenced this pull request Mar 15, 2026
The fix in 5b67d38 added Repository::repo_name() to resolve the
project name correctly for nested bare repos (project/.git → "project").
Commit 79361c6 (#1280) inadvertently removed repo_name() while
refactoring repo_path() to return Result, reverting all call sites
back to repo_path().file_name() which returns ".git" for bare repos.

Re-add repo_name() adapted for the Result-returning repo_path() API,
and restore the regression test.

Closes #1279

Co-Authored-By: Claude Opus 4.6 <[email protected]>
max-sixty added a commit that referenced this pull request Apr 21, 2026
…config (#2350)

## Summary

Replaces the failing `git rev-parse --show-toplevel` probe in
`Repository::repo_path()` with a direct read of `core.worktree` from the
bulk config map. On non-submodule repos the probe fails unconditionally,
costing roughly 5ms per call on healthy macOS — or ~200ms on systems
with expensive `execve` like the one reported in #2322. `repo_path()`
fires ~2× during parent-side alias dispatch, so the win compounds.

The new control flow matches git's own rules for locating the working
tree:

| `git_common_dir` layout | Signal | Resolution |

|-----------------------------|----------------------------|----------------------------------|
| Bare `.git` | `core.bare = true` | `git_common_dir` is the repo |
| Submodule `.git/modules/X` | `core.worktree` set by git | join +
normalize vs common dir |
| Normal `.git` | neither set | `parent(git_common_dir)` |

Submodules need `core.worktree` because their data lives in the parent's
`.git/modules/`, so the implicit `parent(.git)` rule normal repos rely
on would point at the wrong directory. Reading `core.worktree` from the
bulk map is git's own signal for this — no `rev-parse` subprocess
required.

## Correctness

The #762 → #1070 → #1280 chain of submodule fixes established two
invariants: `repo_path()` must return the submodule working directory
(not `.git/modules/sub`), and `list_worktrees()` depends on that to
correct git's own mis-report for submodule main worktrees. Both still
hold:

- `test_repo_path_in_submodule` (integration) passes — exercises
`repo_path()`, `list_worktrees()`, and `worktree_for_branch()` in a real
parent/submodule setup.
- `wt list` from inside a real submodule at `/tmp/subm-test/parent/sub`
reports the submodule working directory, not the git data path.
- All 4 submodule-adjacent tests (`test_repo_path_in_submodule`,
`test_push_{no_ff_,}with_submodule_recurse_config`,
`test_remove_foreground_with_submodules`) pass.
- Bare repos still hit the `is_bare()` short-circuit; linked worktrees
still fall through to `parent()` because the shared `.git` common dir
has no `core.worktree`.

Errors still propagate via `Result` (from `all_config()` instead of the
removed subprocess), preserving the #1280 invariant.

## Performance

hyperfine A/B on a healthy macOS system (200 runs each, `-N` bypasses
shell):

| Context | Before | After | Speedup |

|--------------------------|-----------------|-----------------|---------|
| `wt noop` in normal repo | 53.2 ± 5.3 ms | 48.8 ± 6.1 ms | 1.09× |
| `wt noop` in submodule | 62.1 ± 12.6 ms | 55.5 ± 11.7 ms | 1.12× |

Trace confirmation: `git rev-parse --show-toplevel [.git]` no longer
appears in `RUST_LOG=debug` output for alias dispatch. The unrelated
`rev-parse --show-toplevel [parent]` from `WorkingTree::root()`
(legitimately successful) remains and is out of scope.

`benches/alias` results were too noisy to cite confidently on my machine
during this session (`wt_version` — which this change doesn't touch —
varied ±60% between runs), but the hyperfine measurement is clean.

Ref #2322

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

---------

Co-authored-by: Claude <[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