Skip to content

perf(repo): skip submodule rev-parse probe via core.worktree in bulk config - #2350

Merged
max-sixty merged 2 commits into
mainfrom
2322-submodule-probe
Apr 21, 2026
Merged

max-sixty merged 2 commits into
mainfrom
2322-submodule-probe

Conversation

@max-sixty

Copy link
Copy Markdown
Owner

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

…k config

Replace the failing `git rev-parse --show-toplevel` probe in `repo_path()`
with a direct read of `core.worktree` from the bulk config map populated
once per command. For non-submodule repos the probe fails (roughly 5ms on
healthy macOS, ~200ms on Mark's system in #2322), so removing it wins on
every invocation.

The new control flow (`is_bare()` → `core.worktree` → `parent(git_common_dir)`)
matches git's own resolution rules: submodules have `core.worktree` set
explicitly because their data lives in the parent's `.git/modules/`, normal
repos leave it unset so `parent(.git)` is correct, and bare repos are caught
by `is_bare()` first. The #1070 invariant (submodule path correction in
`list_worktrees()`) continues to hold — verified by
`test_repo_path_in_submodule` and `wt list` inside a real submodule.

hyperfine A/B, 200 runs, `-N`:
- `wt noop` in normal repo: 53.2ms → 48.8ms (1.09×)
- `wt noop` in submodule:   62.1ms → 55.5ms (1.12×)

Co-Authored-By: Claude <[email protected]>
worktrunk-bot
worktrunk-bot previously approved these changes Apr 21, 2026

@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.

codecov/patch failed at 90.9% (10/11). The uncovered line is the absolute-path branch inside the core.worktree block:

return Ok(if path.is_absolute() {
    path                                      // ← this line
} else {
    self.git_common_dir.join(path).normalize()
});

Git writes core.worktree as a relative path for submodules (../../sub), so the existing test_repo_path_in_submodule integration test only exercises the else arm. No test sets core.worktree to an absolute path (no matches for core.worktree in tests/).

A unit test in src/git/repository/tests.rs that pre-populates the bulk config map with an absolute core.worktree (matching the pattern used by the existing parse_config_list_z_* tests) would close the gap. Happy to push one if you'd like — dismissing the approval until this is resolved.

@worktrunk-bot
worktrunk-bot dismissed their stale review April 21, 2026 05:10

codecov/patch failed at 90.9% — absolute-path branch uncovered (see follow-up COMMENT review)

Reviewer flagged the absolute-path arm as uncovered — git writes relative
paths for submodules, so the existing integration test only exercises the
else branch. Pre-populate the bulk config cache with an absolute value to
cover the other arm without needing a filesystem fixture.

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

Copy link
Copy Markdown
Owner Author

Addressed in b4cefb2 — pre-populates the bulk config cache with an absolute core.worktree value to cover the branch, matching the pattern used by the existing parse_config_list_z_* unit tests. No filesystem fixture needed.

@max-sixty
max-sixty merged commit 363e310 into main Apr 21, 2026
25 checks passed
@max-sixty
max-sixty deleted the 2322-submodule-probe branch April 21, 2026 05:24
max-sixty added a commit that referenced this pull request Apr 21, 2026
…2362)

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

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