From 3bdb1d9f3feae3f6b32130ae65e9fd7f6b7ea6fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Den=20Delimarsky=20=F0=9F=8C=BA?= <53200638+localden@users.noreply.github.com> Date: Sat, 20 Sep 2025 20:14:11 -0700 Subject: [PATCH] Root detection logic --- scripts/bash/create-new-feature.sh | 23 ++++++++++++++++++++--- scripts/powershell/create-new-feature.ps1 | 13 ++++++++----- 2 files changed, 28 insertions(+), 8 deletions(-) diff --git a/scripts/bash/create-new-feature.sh b/scripts/bash/create-new-feature.sh index 6a8e913..6670550 100644 --- a/scripts/bash/create-new-feature.sh +++ b/scripts/bash/create-new-feature.sh @@ -18,16 +18,33 @@ if [ -z "$FEATURE_DESCRIPTION" ]; then exit 1 fi +# Function to find the repository root by searching for existing project markers +find_repo_root() { + local dir="$1" + while [ "$dir" != "/" ]; do + if [ -d "$dir/.git" ] || [ -d "$dir/.specify" ]; then + echo "$dir" + return 0 + fi + dir="$(dirname "$dir")" + done + return 1 +} + # Resolve repository root. Prefer git information when available, but fall back -# to the script location so the workflow still functions in repositories that +# to searching for repository markers so the workflow still functions in repositories that # were initialised with --no-git. SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" -FALLBACK_ROOT="$(cd "$SCRIPT_DIR/../../.." && pwd)" + if git rev-parse --show-toplevel >/dev/null 2>&1; then REPO_ROOT=$(git rev-parse --show-toplevel) HAS_GIT=true else - REPO_ROOT="$FALLBACK_ROOT" + REPO_ROOT="$(find_repo_root "$SCRIPT_DIR")" + if [ -z "$REPO_ROOT" ]; then + echo "Error: Could not determine repository root. Please run this script from within the repository." >&2 + exit 1 + fi HAS_GIT=false fi diff --git a/scripts/powershell/create-new-feature.ps1 b/scripts/powershell/create-new-feature.ps1 index 780f399..f1c8e04 100644 --- a/scripts/powershell/create-new-feature.ps1 +++ b/scripts/powershell/create-new-feature.ps1 @@ -15,12 +15,12 @@ if (-not $FeatureDescription -or $FeatureDescription.Count -eq 0) { $featureDesc = ($FeatureDescription -join ' ').Trim() # Resolve repository root. Prefer git information when available, but fall back -# to the script location so the workflow still functions in repositories that +# to searching for repository markers so the workflow still functions in repositories that # were initialised with --no-git. function Find-RepositoryRoot { param( [string]$StartDir, - [string[]]$Markers = @('.git', 'README.md') + [string[]]$Markers = @('.git', '.specify') ) $current = Resolve-Path $StartDir while ($true) { @@ -31,14 +31,17 @@ function Find-RepositoryRoot { } $parent = Split-Path $current -Parent if ($parent -eq $current) { - break + # Reached filesystem root without finding markers + return $null } $current = $parent } - # If no marker found, fall back to script root - return (Resolve-Path $StartDir) } $fallbackRoot = (Find-RepositoryRoot -StartDir $PSScriptRoot) +if (-not $fallbackRoot) { + Write-Error "Error: Could not determine repository root. Please run this script from within the repository." + exit 1 +} try { $repoRoot = git rev-parse --show-toplevel 2>$null