Skip to content

💫 Toolkit to help you get started with SDD or any other process!

Root detection logic

Den Delimarsky 🌺3bdb1d9f3fea
diff --git a/scripts/bash/create-new-feature.sh b/scripts/bash/create-new-feature.sh
index 6a8e913af..6670550e4 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 780f39992..f1c8e04e3 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