Skip to content

fix repo root resolution from subdirectories#166

Merged
helizaga merged 2 commits intomainfrom
codex/fix-subdir-root-resolution
Apr 9, 2026
Merged

fix repo root resolution from subdirectories#166
helizaga merged 2 commits intomainfrom
codex/fix-subdir-root-resolution

Conversation

@helizaga
Copy link
Copy Markdown
Collaborator

@helizaga helizaga commented Apr 9, 2026

Summary

  • resolve the main repo root from git rev-parse --git-common-dir even when Git returns a relative path from a main-repo subdirectory
  • reuse that shared helper for both discover_repo_root() and .gtrconfig lookup so subdirectory execution is consistent
  • add regressions for cmd_list, discover_repo_root, and _gtrconfig_path from a main-repo subdirectory

Root Cause

git rev-parse --git-common-dir returns relative paths like ../.git when gtr runs from a subdirectory of the main repository. The existing code only handled .git and absolute worktree paths, so it collapsed the repo root to .. and mis-resolved .gtrconfig.

Validation

  • shellcheck lib/config.sh lib/core.sh
  • bats tests/config.bats tests/core_resolve_target.bats tests/cmd_list.bats
  • bats tests/
  • manual smoke check: (cd lib && /Users/tommy/Documents/GitHub/git-worktree-runner/bin/gtr list)

Closes #161.

Summary by CodeRabbit

  • Refactor

    • Consolidated repository root detection logic for more reliable root resolution across worktrees.
  • Tests

    • Added integration tests verifying correct behavior when running from nested subdirectories, including resolution of repo-root config paths and command listing output.
    • Added tests ensuring correct failure when run outside a repository and added conditional teardown to clean up test state.

@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Apr 9, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 0dbef427-8b54-4752-ba5b-a9415191a749

📥 Commits

Reviewing files that changed from the base of the PR and between ce2cfc5 and 5dd7fa4.

📒 Files selected for processing (2)
  • lib/core.sh
  • tests/core_resolve_target.bats
🚧 Files skipped from review as they are similar to previous changes (2)
  • tests/core_resolve_target.bats
  • lib/core.sh

Walkthrough

Introduces a centralized _resolve_main_repo_root() helper to compute the repository root from git rev-parse --git-common-dir; refactors _gtrconfig_path() and discover_repo_root() to use it; adds tests exercising resolution and gtr behavior from nested subdirectories and a test teardown hook.

Changes

Cohort / File(s) Summary
Repository root resolution refactor
lib/config.sh, lib/core.sh
Add _resolve_main_repo_root() to derive absolute main repo root from git rev-parse --git-common-dir; refactor _gtrconfig_path() and discover_repo_root() to call the helper instead of duplicating parsing logic.
Integration tests & teardown
tests/config.bats, tests/cmd_list.bats, tests/core_resolve_target.bats
Add tests that run from nested subdirectories to assert _resolve_main_repo_root(), _gtrconfig_path(), and discover_repo_root() resolve to the physical repo root; add a conditional teardown() hook to clean up TEST_REPO state.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

🐰 I tunneled down through folders deep,
Found the root where secrets sleep,
One small helper, neatly planned,
Now every path understands. 🌱

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 60.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'fix repo root resolution from subdirectories' directly describes the main change: fixing root resolution to work correctly from subdirectories, which is the core objective of the PR.
Linked Issues check ✅ Passed The PR comprehensively addresses issue #161 by adding helper functions for consistent root resolution, refactoring both discover_repo_root and gtrconfig lookup, and adding regression tests for all affected code paths.
Out of Scope Changes check ✅ Passed All changes are directly scoped to the root resolution issue: helper function addition, refactoring of affected discovery logic, and adding comprehensive regression tests for the fix.

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

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch codex/fix-subdir-root-resolution

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

@helizaga helizaga marked this pull request as ready for review April 9, 2026 23:41
@helizaga helizaga requested a review from NatoBoram as a code owner April 9, 2026 23:41
Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@lib/core.sh`:
- Line 19: The assignment to root using _resolve_main_repo_root can cause an
early exit under set -e if the command fails; guard the call by checking its
exit status (e.g., use if ! root=$(_resolve_main_repo_root); then ... fi or
root=$(_resolve_main_repo_root) || true) so failures are handled gracefully
instead of triggering set -e; update the block around the root variable
assignment and ensure subsequent logic that depends on root handles the
empty/failure case appropriately.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 178ea85b-96e7-4e4d-8d75-4c85c29035e1

📥 Commits

Reviewing files that changed from the base of the PR and between c765e62 and ce2cfc5.

📒 Files selected for processing (5)
  • lib/config.sh
  • lib/core.sh
  • tests/cmd_list.bats
  • tests/config.bats
  • tests/core_resolve_target.bats

@helizaga helizaga changed the title [codex] fix repo root resolution from subdirectories fix repo root resolution from subdirectories Apr 9, 2026
@helizaga helizaga merged commit f079e1c into main Apr 9, 2026
4 checks passed
@helizaga helizaga deleted the codex/fix-subdir-root-resolution branch April 9, 2026 23:55
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.

Running gtr in a subdirectory should behave the same as running from root of repository

1 participant