Skip to content

fix: resolve main repo root correctly from inside worktrees#126

Merged
helizaga merged 1 commit intomainfrom
fix/discover-repo-root-from-worktree
Feb 15, 2026
Merged

fix: resolve main repo root correctly from inside worktrees#126
helizaga merged 1 commit intomainfrom
fix/discover-repo-root-from-worktree

Conversation

@helizaga
Copy link
Collaborator

@helizaga helizaga commented Feb 14, 2026

Summary

Closes #123

  • discover_repo_root() used git rev-parse --show-toplevel which returns the worktree's own root when called from inside a worktree, breaking list, go 1, and all other commands
  • Switched to git rev-parse --git-common-dir to always resolve the true main repo root (same technique already used by _gtrconfig_path() in lib/config.sh)
  • Added 5 tests covering discover_repo_root, cmd_list, and cmd_go from inside a worktree

Test plan

  • bats tests/core_resolve_target.bats — new discover_repo_root tests pass
  • bats tests/cmd_list.bats — new from-worktree tests pass
  • bats tests/cmd_go.bats — new from-worktree test passes
  • bats tests/ — full suite (267 tests) passes with no regressions

Summary by CodeRabbit

Release Notes

  • Bug Fixes

    • Improved repository root detection to properly support git worktrees, ensuring all commands correctly identify the main repository when executed from within a worktree.
  • Tests

    • Added comprehensive test coverage validating command behavior when executed inside git worktrees, including both standard and porcelain output modes.

discover_repo_root() used git rev-parse --show-toplevel which returns
the worktree's own root when called from inside a worktree. Switch to
--git-common-dir to always resolve the true main repo root, fixing
list, go 1, and all other commands when run from a worktree.
@helizaga helizaga requested a review from NatoBoram as a code owner February 14, 2026 21:46
@coderabbitai
Copy link

coderabbitai bot commented Feb 14, 2026

Walkthrough

The pull request fixes incorrect repository root detection when commands are run from inside git worktrees. The discover_repo_root function now uses git_common_dir to properly distinguish between main repositories and worktrees, enabling accurate directory resolution from any worktree location.

Changes

Cohort / File(s) Summary
Core Repository Root Detection
lib/core.sh
Refactored discover_repo_root to use git_common_dir for distinguishing main repos from worktrees. When git_common_dir is .git, falls back to git rev-parse --show-toplevel; when it's an absolute path (worktree), derives root by stripping /.git. Adds error logging when not in a git repo.
Worktree Navigation Tests
tests/cmd_go.bats
Added test case verifying cmd_go 1 executes successfully from within a worktree and outputs the correct main repository path.
Worktree List Command Tests
tests/cmd_list.bats
Added two test cases: verifying cmd_list from worktree shows all worktrees with correct main repo identification, and cmd_list --porcelain correctly includes main repo when run from worktree.
Repository Root Resolution Tests
tests/core_resolve_target.bats
Added two test cases confirming discover_repo_root correctly returns the main repository root when executed from either a worktree or the main repo.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

🐰 Hopping through the worktrees with glee,
No more confusion 'tween repos and thee!
Git common_dir shows the way,
To the main repo, hip-hooray! 🌿✨

🚥 Pre-merge checks | ✅ 6
✅ Passed checks (6 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the main change: fixing the discover_repo_root function to correctly resolve the main repository root when executed from inside a git worktree.
Linked Issues check ✅ Passed The changes address all coding requirements from issue #123: discover_repo_root now uses git rev-parse --git-common-dir to distinguish between main repo and worktrees, and comprehensive tests validate the fix for cmd_list, cmd_go, and discover_repo_root.
Out of Scope Changes check ✅ Passed All changes are directly related to fixing discover_repo_root and its impact on cmd_list and cmd_go; no unrelated modifications are present.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Merge Conflict Detection ✅ Passed ✅ No merge conflicts detected when merging into main

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch fix/discover-repo-root-from-worktree

No actionable comments were generated in the recent review. 🎉

🧹 Recent nitpick comments
lib/core.sh (1)

13-41: Good fix for worktree root discovery.

The approach using git rev-parse --git-common-dir correctly identifies the main repository root from inside worktrees. The logic is sound and matches the pattern used elsewhere in the codebase.

However, there's a subtle inconsistency in symlink handling:

  • Main repo case (line 30): git rev-parse --show-toplevel returns a canonical path with symlinks resolved.
  • Worktree case (line 32): The path is derived by stripping /.git from git_common_dir, which may retain symlinks.

The test at tests/core_resolve_target.bats:92-93 uses pwd -P to resolve symlinks before comparison, which works around this. However, for full consistency, consider canonicalizing the worktree-derived path as well.

🔧 Optional: Canonicalize worktree path for consistency
   if [ "$git_common_dir" = ".git" ]; then
     root=$(git rev-parse --show-toplevel 2>/dev/null)
   else
-    root="${git_common_dir%/.git}"
+    # Strip /.git suffix and canonicalize to match --show-toplevel behavior
+    root=$(cd -P "${git_common_dir%/.git}" 2>/dev/null && pwd -P) || root="${git_common_dir%/.git}"
   fi

Comment @coderabbitai help to get the list of available commands and usage tips.

@helizaga helizaga merged commit 57e7167 into main Feb 15, 2026
3 checks passed
@helizaga helizaga deleted the fix/discover-repo-root-from-worktree branch February 15, 2026 00:46
@fmal
Copy link

fmal commented Feb 16, 2026

@helizaga yay thanks, pls cut a release whenever possible ❤️

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.

git gtr list shows incorrect results when run from inside a worktree

2 participants