Skip to content

fix: use --show-toplevel for repo_path() to support submodules - #762

Merged
max-sixty merged 2 commits into
max-sixty:mainfrom
lajarre:fix/submodule-repo-path
Jan 21, 2026
Merged

max-sixty merged 2 commits into
max-sixty:mainfrom
lajarre:fix/submodule-repo-path

Conversation

@lajarre

@lajarre lajarre commented Jan 20, 2026

Copy link
Copy Markdown
Contributor

The previous implementation derived repo_path from git_common_dir's parent, which fails for submodules where git data is stored in the parent repo's .git/modules/ directory.

Bug report (previous implementation)

Problem: When running wt switch --create inside a git submodule, worktrees are created in the wrong location.

Reproduction:

# Inside a git submodule at /parent/submodule
wt switch --create feature

# Expected: /parent/submodule/.worktrees/feature
# Actual:   /parent/.git/modules/.worktrees/feature

Note: This affects configurations with relative worktree-path templates like .worktrees/{{ branch | sanitize }} or .worktrees/{{ branch | sanitize }}. The default sibling-directory config (../{{ repo }}.{{ branch | sanitize }}) may also be affected since it's relative to the incorrectly computed repo root.

Root Cause: repo_path() derived the working directory from git_common_dir.parent(), assuming .git is always inside the working tree. For submodules, git stores data in parent/.git/modules/sub, so parent() returns the wrong path.

Fix: Use git rev-parse --show-toplevel which correctly returns the working tree root regardless of where git stores its data.

@lajarre
lajarre marked this pull request as draft January 20, 2026 12:36
@lajarre
lajarre force-pushed the fix/submodule-repo-path branch from 066c899 to 5669b8a Compare January 20, 2026 12:38
The previous implementation derived repo_path from git_common_dir's
parent, which fails for submodules where git data is stored in the
parent repo's .git/modules/ directory.
@lajarre
lajarre force-pushed the fix/submodule-repo-path branch from 5669b8a to 9adbbfb Compare January 20, 2026 12:45
@lajarre
lajarre marked this pull request as ready for review January 20, 2026 12:56
Replace fragile path-based submodule detection (.contains(".git/modules/"))
with a try-fallback approach: try --show-toplevel first (works for submodules
due to explicit core.worktree config), fall back to parent() for normal repos.

Added detailed documentation explaining:
- Why we run from git_common_dir (stable reference across worktrees)
- Why try-fallback works (core.worktree asymmetry between normal/submodule)

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

Copy link
Copy Markdown
Owner

thank you @lajarre !

I made a small change to avoid looking at the paths. does that look OK to you?

@lajarre

lajarre commented Jan 21, 2026

Copy link
Copy Markdown
Contributor Author

I made a small change to avoid looking at the paths. does that look OK to you?

Much better, lgtm!

@max-sixty
max-sixty merged commit 890c08c into max-sixty:main Jan 21, 2026
@max-sixty

Copy link
Copy Markdown
Owner

thanks a lot @lajarre

@lajarre
lajarre deleted the fix/submodule-repo-path branch January 21, 2026 11:12
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