Skip to content

fix(repo): honor git's scope rules for core.worktree in repo_path() - #2362

Merged
max-sixty merged 1 commit into
mainfrom
2322-submodule-probe
Apr 21, 2026
Merged

max-sixty merged 1 commit into
mainfrom
2322-submodule-probe

Conversation

@max-sixty

Copy link
Copy Markdown
Owner

Summary

#2350 introduced a divergence for a narrow case: normal non-bare repos where a user has set core.worktree in global/system config. git config --list -z merges scopes into the bulk map, but git itself only honors core.worktree from local config for worktree discovery — so we'd return (e.g.) a stray global path as the repo root while git would return the actual worktree.

Fix: when the bulk map reports core.worktree, delegate to git rev-parse --show-toplevel so git applies its own scope rules. The common case (no core.worktree anywhere — the vast majority of repos) still skips the subprocess entirely; that was the whole point of #2350. Submodules pay one fork back, matching pre-#2350 behavior for that minority case.

Caught by Codex review of the merged #2350.

Correctness

Verified against git (all from inside .git):

core.worktree scope git rev-parse --show-toplevel Pre-fix (#2350) Post-fix
Not set anywhere fails → fall through parent(.git) ✓ parent(.git) ✓
Local (submodule / user-set) returns resolved path wrong for global; ok for local matches git ✓
Global only fails → git ignores it returned global value ❌ matches git ✓

Tests

  • New unit test repo_path_ignores_non_local_core_worktree — pre-populates the bulk config cache with a core.worktree value but leaves the on-disk .git/config empty, so rev-parse --show-toplevel fails and we fall through to parent(.git). Simulates the global-only scope case.
  • Existing test_repo_path_in_submodule integration test still passes (local core.worktree in a real submodule).
  • All 1023 lib tests and the 4 submodule integration tests pass.

Performance

No regression for the common case (no core.worktree anywhere): still skips the subprocess. Submodules pay one fork — same as pre-#2350.

Ref #2322

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

#2350 introduced a divergence for normal repos where the user has set
`core.worktree` in global/system config: `git config --list -z` merges
all scopes into the bulk map, but git itself only honors `core.worktree`
from local config for worktree discovery. We were trusting the merged
value and returning (e.g.) a stray global path as the repo root.

Delegate to `git rev-parse --show-toplevel` only when the bulk map
reports `core.worktree` — git then applies its own scope rules. The
common case (no `core.worktree` anywhere) still skips the subprocess,
which is the whole point of this path. Submodules now pay one fork
back, matching pre-#2350 behavior for that minority case.

Caught by Codex review of #2350.

Co-Authored-By: Claude <[email protected]>
@max-sixty
max-sixty merged commit cb4000e into main Apr 21, 2026
25 checks passed
@max-sixty
max-sixty deleted the 2322-submodule-probe branch April 21, 2026 21:46
max-sixty added a commit that referenced this pull request Apr 21, 2026
Doc-only follow-up to #2350 / #2362. The docstring on
`Repository::repo_path()` had grown to ~45 lines during the
scope-handling fix; trimming it to ~25 without dropping any of the
reasoning that would cause accidental regressions.

Kept (load-bearing):
- Why we anchor on `git_common_dir` (linked-worktree invariant)
- The three-case resolution table
- Why submodules need `core.worktree` (or someone removes the probe)
- Why we delegate to `rev-parse` instead of trusting the bulk map
directly (or someone re-introduces the #2350 scope bug)
- The "common case skips the subprocess" perf note (or someone always
probes)

Trimmed:
- "# Why we anchor" heading → folded into the lead-in
- "(or `[includeIf]`-imported)" parenthetical
- One redundant sentence restating that git ignores global
`core.worktree`
- Minor phrasing cleanup

> _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