|
| 1 | +#!/usr/bin/env bash |
| 2 | +# Common functions and variables for all scripts |
| 3 | + |
| 4 | +# Get repository root, with fallback for non-git repositories |
| 5 | +get_repo_root() { |
| 6 | + if git rev-parse --show-toplevel >/dev/null 2>&1; then |
| 7 | + 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) |
| 12 | + fi |
| 13 | +} |
| 14 | + |
| 15 | +# Get current branch, with fallback for non-git repositories |
| 16 | +get_current_branch() { |
| 17 | + # First check if SPECIFY_FEATURE environment variable is set |
| 18 | + if [[ -n "${SPECIFY_FEATURE:-}" ]]; then |
| 19 | + echo "$SPECIFY_FEATURE" |
| 20 | + return |
| 21 | + fi |
| 22 | + |
| 23 | + # Then check git if available |
| 24 | + if git rev-parse --abbrev-ref HEAD >/dev/null 2>&1; then |
| 25 | + git rev-parse --abbrev-ref HEAD |
| 26 | + return |
| 27 | + fi |
| 28 | + |
| 29 | + # For non-git repos, try to find the latest feature directory |
| 30 | + local repo_root=$(get_repo_root) |
| 31 | + local specs_dir="$repo_root/specs" |
| 32 | + |
| 33 | + if [[ -d "$specs_dir" ]]; then |
| 34 | + local latest_feature="" |
| 35 | + local highest=0 |
| 36 | + |
| 37 | + for dir in "$specs_dir"/*; do |
| 38 | + if [[ -d "$dir" ]]; then |
| 39 | + local dirname=$(basename "$dir") |
| 40 | + if [[ "$dirname" =~ ^([0-9]{3})- ]]; then |
| 41 | + local number=${BASH_REMATCH[1]} |
| 42 | + number=$((10#$number)) |
| 43 | + if [[ "$number" -gt "$highest" ]]; then |
| 44 | + highest=$number |
| 45 | + latest_feature=$dirname |
| 46 | + fi |
| 47 | + fi |
| 48 | + fi |
| 49 | + done |
| 50 | + |
| 51 | + if [[ -n "$latest_feature" ]]; then |
| 52 | + echo "$latest_feature" |
| 53 | + return |
| 54 | + fi |
| 55 | + fi |
| 56 | + |
| 57 | + echo "main" # Final fallback |
| 58 | +} |
| 59 | + |
| 60 | +# Check if we have git available |
| 61 | +has_git() { |
| 62 | + git rev-parse --show-toplevel >/dev/null 2>&1 |
| 63 | +} |
| 64 | + |
| 65 | +check_feature_branch() { |
| 66 | + local branch="$1" |
| 67 | + local has_git_repo="$2" |
| 68 | + |
| 69 | + # For non-git repos, we can't enforce branch naming but still provide output |
| 70 | + if [[ "$has_git_repo" != "true" ]]; then |
| 71 | + echo "[specify] Warning: Git repository not detected; skipped branch validation" >&2 |
| 72 | + return 0 |
| 73 | + fi |
| 74 | + |
| 75 | + if [[ ! "$branch" =~ ^[0-9]{3}- ]]; then |
| 76 | + echo "ERROR: Not on a feature branch. Current branch: $branch" >&2 |
| 77 | + echo "Feature branches should be named like: 001-feature-name" >&2 |
| 78 | + return 1 |
| 79 | + fi |
| 80 | + |
| 81 | + return 0 |
| 82 | +} |
| 83 | + |
| 84 | +get_feature_dir() { echo "$1/specs/$2"; } |
| 85 | + |
| 86 | +# Find feature directory by numeric prefix instead of exact branch match |
| 87 | +# This allows multiple branches to work on the same spec (e.g., 004-fix-bug, 004-add-feature) |
| 88 | +find_feature_dir_by_prefix() { |
| 89 | + local repo_root="$1" |
| 90 | + local branch_name="$2" |
| 91 | + local specs_dir="$repo_root/specs" |
| 92 | + |
| 93 | + # Extract numeric prefix from branch (e.g., "004" from "004-whatever") |
| 94 | + if [[ ! "$branch_name" =~ ^([0-9]{3})- ]]; then |
| 95 | + # If branch doesn't have numeric prefix, fall back to exact match |
| 96 | + echo "$specs_dir/$branch_name" |
| 97 | + return |
| 98 | + fi |
| 99 | + |
| 100 | + local prefix="${BASH_REMATCH[1]}" |
| 101 | + |
| 102 | + # Search for directories in specs/ that start with this prefix |
| 103 | + local matches=() |
| 104 | + if [[ -d "$specs_dir" ]]; then |
| 105 | + for dir in "$specs_dir"/"$prefix"-*; do |
| 106 | + if [[ -d "$dir" ]]; then |
| 107 | + matches+=("$(basename "$dir")") |
| 108 | + fi |
| 109 | + done |
| 110 | + fi |
| 111 | + |
| 112 | + # Handle results |
| 113 | + if [[ ${#matches[@]} -eq 0 ]]; then |
| 114 | + # No match found - return the branch name path (will fail later with clear error) |
| 115 | + echo "$specs_dir/$branch_name" |
| 116 | + elif [[ ${#matches[@]} -eq 1 ]]; then |
| 117 | + # Exactly one match - perfect! |
| 118 | + echo "$specs_dir/${matches[0]}" |
| 119 | + else |
| 120 | + # Multiple matches - this shouldn't happen with proper naming convention |
| 121 | + echo "ERROR: Multiple spec directories found with prefix '$prefix': ${matches[*]}" >&2 |
| 122 | + echo "Please ensure only one spec directory exists per numeric prefix." >&2 |
| 123 | + echo "$specs_dir/$branch_name" # Return something to avoid breaking the script |
| 124 | + fi |
| 125 | +} |
| 126 | + |
| 127 | +get_feature_paths() { |
| 128 | + local repo_root=$(get_repo_root) |
| 129 | + local current_branch=$(get_current_branch) |
| 130 | + local has_git_repo="false" |
| 131 | + |
| 132 | + if has_git; then |
| 133 | + has_git_repo="true" |
| 134 | + fi |
| 135 | + |
| 136 | + # Use prefix-based lookup to support multiple branches per spec |
| 137 | + local feature_dir=$(find_feature_dir_by_prefix "$repo_root" "$current_branch") |
| 138 | + |
| 139 | + cat <<EOF |
| 140 | +REPO_ROOT='$repo_root' |
| 141 | +CURRENT_BRANCH='$current_branch' |
| 142 | +HAS_GIT='$has_git_repo' |
| 143 | +FEATURE_DIR='$feature_dir' |
| 144 | +FEATURE_SPEC='$feature_dir/spec.md' |
| 145 | +IMPL_PLAN='$feature_dir/plan.md' |
| 146 | +TASKS='$feature_dir/tasks.md' |
| 147 | +RESEARCH='$feature_dir/research.md' |
| 148 | +DATA_MODEL='$feature_dir/data-model.md' |
| 149 | +QUICKSTART='$feature_dir/quickstart.md' |
| 150 | +CONTRACTS_DIR='$feature_dir/contracts' |
| 151 | +EOF |
| 152 | +} |
| 153 | + |
| 154 | +check_file() { [[ -f "$1" ]] && echo " ✓ $2" || echo " ✗ $2"; } |
| 155 | +check_dir() { [[ -d "$1" && -n $(ls -A "$1" 2>/dev/null) ]] && echo " ✓ $2" || echo " ✗ $2"; } |
| 156 | + |
0 commit comments