Root detection logic

This commit is contained in:
Den Delimarsky 🌺
2025-09-20 20:14:11 -07:00
parent 0e5f7cee9a
commit 3bdb1d9f3f
2 changed files with 28 additions and 8 deletions

View File

@@ -18,16 +18,33 @@ if [ -z "$FEATURE_DESCRIPTION" ]; then
exit 1 exit 1
fi 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 # 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. # were initialised with --no-git.
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" 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 if git rev-parse --show-toplevel >/dev/null 2>&1; then
REPO_ROOT=$(git rev-parse --show-toplevel) REPO_ROOT=$(git rev-parse --show-toplevel)
HAS_GIT=true HAS_GIT=true
else 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 HAS_GIT=false
fi fi

View File

@@ -15,12 +15,12 @@ if (-not $FeatureDescription -or $FeatureDescription.Count -eq 0) {
$featureDesc = ($FeatureDescription -join ' ').Trim() $featureDesc = ($FeatureDescription -join ' ').Trim()
# Resolve repository root. Prefer git information when available, but fall back # 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. # were initialised with --no-git.
function Find-RepositoryRoot { function Find-RepositoryRoot {
param( param(
[string]$StartDir, [string]$StartDir,
[string[]]$Markers = @('.git', 'README.md') [string[]]$Markers = @('.git', '.specify')
) )
$current = Resolve-Path $StartDir $current = Resolve-Path $StartDir
while ($true) { while ($true) {
@@ -31,14 +31,17 @@ function Find-RepositoryRoot {
} }
$parent = Split-Path $current -Parent $parent = Split-Path $current -Parent
if ($parent -eq $current) { if ($parent -eq $current) {
break # Reached filesystem root without finding markers
return $null
} }
$current = $parent $current = $parent
} }
# If no marker found, fall back to script root
return (Resolve-Path $StartDir)
} }
$fallbackRoot = (Find-RepositoryRoot -StartDir $PSScriptRoot) $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 { try {
$repoRoot = git rev-parse --show-toplevel 2>$null $repoRoot = git rev-parse --show-toplevel 2>$null