|
1 | 1 | #!/usr/bin/env bash |
2 | 2 | # Common functions and variables for all scripts |
3 | 3 |
|
4 | | -# Get repository root, with fallback for non-git repositories |
| 4 | +# Find repository root by searching upward for .specify directory |
| 5 | +# This is the primary marker for spec-kit projects |
| 6 | +find_specify_root() { |
| 7 | + local dir="${1:-$(pwd)}" |
| 8 | + while [ "$dir" != "/" ]; do |
| 9 | + if [ -d "$dir/.specify" ]; then |
| 10 | + echo "$dir" |
| 11 | + return 0 |
| 12 | + fi |
| 13 | + dir="$(dirname "$dir")" |
| 14 | + done |
| 15 | + return 1 |
| 16 | +} |
| 17 | + |
| 18 | +# Get repository root, prioritizing .specify directory over git |
| 19 | +# This prevents using a parent git repo when spec-kit is initialized in a subdirectory |
5 | 20 | get_repo_root() { |
| 21 | + # First, look for .specify directory (spec-kit's own marker) |
| 22 | + local specify_root |
| 23 | + if specify_root=$(find_specify_root); then |
| 24 | + echo "$specify_root" |
| 25 | + return |
| 26 | + fi |
| 27 | + |
| 28 | + # Fallback to git if no .specify found |
6 | 29 | if git rev-parse --show-toplevel >/dev/null 2>&1; then |
7 | 30 | git rev-parse --show-toplevel |
8 | | - else |
9 | | - # Fall back to script location for non-git repos |
10 | | - local script_dir="$(CDPATH="" cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
11 | | - (cd "$script_dir/../../.." && pwd) |
| 31 | + return |
12 | 32 | fi |
| 33 | + |
| 34 | + # Final fallback to script location for non-git repos |
| 35 | + local script_dir="$(CDPATH="" cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 36 | + (cd "$script_dir/../../.." && pwd) |
13 | 37 | } |
14 | 38 |
|
15 | 39 | # Get current branch, with fallback for non-git repositories |
@@ -57,9 +81,11 @@ get_current_branch() { |
57 | 81 | echo "main" # Final fallback |
58 | 82 | } |
59 | 83 |
|
60 | | -# Check if we have git available |
| 84 | +# Check if we have git available at the spec-kit root level |
| 85 | +# Returns true only if the .specify root has its own .git directory |
61 | 86 | has_git() { |
62 | | - git rev-parse --show-toplevel >/dev/null 2>&1 |
| 87 | + local repo_root=$(get_repo_root) |
| 88 | + [ -d "$repo_root/.git" ] |
63 | 89 | } |
64 | 90 |
|
65 | 91 | check_feature_branch() { |
|
0 commit comments