|
10 | 10 | # 5. Environment variables |
11 | 11 | # 6. Fallback values |
12 | 12 |
|
| 13 | +# Resolve the main repo root from the current git context. |
| 14 | +# Works from the main repo root, a subdirectory, or a linked worktree. |
| 15 | +# Returns: absolute path to main repo root or empty on failure |
| 16 | +_resolve_main_repo_root() { |
| 17 | + local git_common_dir repo_root |
| 18 | + git_common_dir=$(git rev-parse --git-common-dir 2>/dev/null) || return 1 |
| 19 | + |
| 20 | + [ -n "$git_common_dir" ] || return 1 |
| 21 | + |
| 22 | + case "$git_common_dir" in |
| 23 | + /*) |
| 24 | + repo_root="${git_common_dir%/.git}" |
| 25 | + ;; |
| 26 | + *) |
| 27 | + repo_root=$( |
| 28 | + unset CDPATH |
| 29 | + cd -P -- "$git_common_dir/.." 2>/dev/null && pwd -P |
| 30 | + ) || return 1 |
| 31 | + ;; |
| 32 | + esac |
| 33 | + |
| 34 | + [ -n "$repo_root" ] || return 1 |
| 35 | + printf "%s" "$repo_root" |
| 36 | +} |
| 37 | + |
13 | 38 | # Get the path to .gtrconfig file in main repo root |
14 | 39 | # Usage: _gtrconfig_path |
15 | 40 | # Returns: path to .gtrconfig or empty if not in a repo |
16 | | -# Note: Uses --git-common-dir to find main repo even from worktrees |
| 41 | +# Note: Uses _resolve_main_repo_root to find main repo even from worktrees/subdirectories |
17 | 42 | _gtrconfig_path() { |
18 | | - local git_common_dir repo_root |
19 | | - git_common_dir=$(git rev-parse --git-common-dir 2>/dev/null) || return 0 |
20 | | - |
21 | | - # git-common-dir returns: |
22 | | - # - ".git" when in main repo (relative) |
23 | | - # - "/absolute/path/to/repo/.git" when in worktree (absolute) |
24 | | - if [ "$git_common_dir" = ".git" ]; then |
25 | | - # In main repo - use show-toplevel |
26 | | - repo_root=$(git rev-parse --show-toplevel 2>/dev/null) || return 0 |
27 | | - else |
28 | | - # In worktree - strip /.git suffix from absolute path |
29 | | - repo_root="${git_common_dir%/.git}" |
30 | | - fi |
| 43 | + local repo_root |
| 44 | + repo_root=$(_resolve_main_repo_root) || return 0 |
31 | 45 |
|
32 | 46 | printf "%s/.gtrconfig" "$repo_root" |
33 | 47 | } |
|
0 commit comments