Skip to content

Commit 85bc4e5

Browse files
committed
Fix REPO_ROOT priority: prefer .specify walk-up over git root
In monorepos the git toplevel may differ from the project root that contains .specify/. The previous fix still preferred git rev-parse over the walk-up result. Bash scripts (20): prefer the discovered _root when it contains .specify/; only accept git root if it also contains .specify/. PowerShell scripts (20): validate git root contains .specify/ before using it; fall back to walking up from script directory otherwise.
1 parent 499ae8b commit 85bc4e5

40 files changed

Lines changed: 340 additions & 60 deletions

src/specify_cli/integrations/amp/scripts/update-context.ps1

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,12 @@ $ErrorActionPreference = 'Stop'
1010
# Derive repo root from script location (walks up to find .specify/)
1111
$scriptDir = Split-Path -Parent $MyInvocation.MyCommand.Definition
1212
$repoRoot = git rev-parse --show-toplevel 2>$null
13-
if (-not $repoRoot) {
13+
# If git did not return a repo root, or the git root does not contain .specify,
14+
# fall back to walking up from the script directory to find the initialized project root.
15+
if (-not $repoRoot -or -not (Test-Path (Join-Path $repoRoot '.specify'))) {
1416
$repoRoot = $scriptDir
15-
while ($repoRoot -ne [System.IO.Path]::GetPathRoot($repoRoot) -and -not (Test-Path (Join-Path $repoRoot '.specify'))) {
17+
$fsRoot = [System.IO.Path]::GetPathRoot($repoRoot)
18+
while ($repoRoot -and $repoRoot -ne $fsRoot -and -not (Test-Path (Join-Path $repoRoot '.specify'))) {
1619
$repoRoot = Split-Path -Parent $repoRoot
1720
}
1821
}

src/specify_cli/integrations/amp/scripts/update-context.sh

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,17 @@ set -euo pipefail
1212
_script_dir="$(cd "$(dirname "$0")" && pwd)"
1313
_root="$_script_dir"
1414
while [ "$_root" != "/" ] && [ ! -d "$_root/.specify" ]; do _root="$(dirname "$_root")"; done
15-
REPO_ROOT="${REPO_ROOT:-$(git rev-parse --show-toplevel 2>/dev/null || echo "$_root")}"
15+
if [ -z "${REPO_ROOT:-}" ]; then
16+
if [ -d "$_root/.specify" ]; then
17+
REPO_ROOT="$_root"
18+
else
19+
git_root="$(git rev-parse --show-toplevel 2>/dev/null || true)"
20+
if [ -n "$git_root" ] && [ -d "$git_root/.specify" ]; then
21+
REPO_ROOT="$git_root"
22+
else
23+
REPO_ROOT="$_root"
24+
fi
25+
fi
26+
fi
1627

1728
exec "$REPO_ROOT/.specify/scripts/bash/update-agent-context.sh" amp

src/specify_cli/integrations/auggie/scripts/update-context.ps1

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,12 @@ $ErrorActionPreference = 'Stop'
1010
# Derive repo root from script location (walks up to find .specify/)
1111
$scriptDir = Split-Path -Parent $MyInvocation.MyCommand.Definition
1212
$repoRoot = git rev-parse --show-toplevel 2>$null
13-
if (-not $repoRoot) {
13+
# If git did not return a repo root, or the git root does not contain .specify,
14+
# fall back to walking up from the script directory to find the initialized project root.
15+
if (-not $repoRoot -or -not (Test-Path (Join-Path $repoRoot '.specify'))) {
1416
$repoRoot = $scriptDir
15-
while ($repoRoot -ne [System.IO.Path]::GetPathRoot($repoRoot) -and -not (Test-Path (Join-Path $repoRoot '.specify'))) {
17+
$fsRoot = [System.IO.Path]::GetPathRoot($repoRoot)
18+
while ($repoRoot -and $repoRoot -ne $fsRoot -and -not (Test-Path (Join-Path $repoRoot '.specify'))) {
1619
$repoRoot = Split-Path -Parent $repoRoot
1720
}
1821
}

src/specify_cli/integrations/auggie/scripts/update-context.sh

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,17 @@ set -euo pipefail
1212
_script_dir="$(cd "$(dirname "$0")" && pwd)"
1313
_root="$_script_dir"
1414
while [ "$_root" != "/" ] && [ ! -d "$_root/.specify" ]; do _root="$(dirname "$_root")"; done
15-
REPO_ROOT="${REPO_ROOT:-$(git rev-parse --show-toplevel 2>/dev/null || echo "$_root")}"
15+
if [ -z "${REPO_ROOT:-}" ]; then
16+
if [ -d "$_root/.specify" ]; then
17+
REPO_ROOT="$_root"
18+
else
19+
git_root="$(git rev-parse --show-toplevel 2>/dev/null || true)"
20+
if [ -n "$git_root" ] && [ -d "$git_root/.specify" ]; then
21+
REPO_ROOT="$git_root"
22+
else
23+
REPO_ROOT="$_root"
24+
fi
25+
fi
26+
fi
1627

1728
exec "$REPO_ROOT/.specify/scripts/bash/update-agent-context.sh" auggie

src/specify_cli/integrations/bob/scripts/update-context.ps1

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,12 @@ $ErrorActionPreference = 'Stop'
1010
# Derive repo root from script location (walks up to find .specify/)
1111
$scriptDir = Split-Path -Parent $MyInvocation.MyCommand.Definition
1212
$repoRoot = git rev-parse --show-toplevel 2>$null
13-
if (-not $repoRoot) {
13+
# If git did not return a repo root, or the git root does not contain .specify,
14+
# fall back to walking up from the script directory to find the initialized project root.
15+
if (-not $repoRoot -or -not (Test-Path (Join-Path $repoRoot '.specify'))) {
1416
$repoRoot = $scriptDir
15-
while ($repoRoot -ne [System.IO.Path]::GetPathRoot($repoRoot) -and -not (Test-Path (Join-Path $repoRoot '.specify'))) {
17+
$fsRoot = [System.IO.Path]::GetPathRoot($repoRoot)
18+
while ($repoRoot -and $repoRoot -ne $fsRoot -and -not (Test-Path (Join-Path $repoRoot '.specify'))) {
1619
$repoRoot = Split-Path -Parent $repoRoot
1720
}
1821
}

src/specify_cli/integrations/bob/scripts/update-context.sh

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,17 @@ set -euo pipefail
1212
_script_dir="$(cd "$(dirname "$0")" && pwd)"
1313
_root="$_script_dir"
1414
while [ "$_root" != "/" ] && [ ! -d "$_root/.specify" ]; do _root="$(dirname "$_root")"; done
15-
REPO_ROOT="${REPO_ROOT:-$(git rev-parse --show-toplevel 2>/dev/null || echo "$_root")}"
15+
if [ -z "${REPO_ROOT:-}" ]; then
16+
if [ -d "$_root/.specify" ]; then
17+
REPO_ROOT="$_root"
18+
else
19+
git_root="$(git rev-parse --show-toplevel 2>/dev/null || true)"
20+
if [ -n "$git_root" ] && [ -d "$git_root/.specify" ]; then
21+
REPO_ROOT="$git_root"
22+
else
23+
REPO_ROOT="$_root"
24+
fi
25+
fi
26+
fi
1627

1728
exec "$REPO_ROOT/.specify/scripts/bash/update-agent-context.sh" bob

src/specify_cli/integrations/claude/scripts/update-context.ps1

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,12 @@ $ErrorActionPreference = 'Stop'
1010
# Derive repo root from script location (walks up to find .specify/)
1111
$scriptDir = Split-Path -Parent $MyInvocation.MyCommand.Definition
1212
$repoRoot = git rev-parse --show-toplevel 2>$null
13-
if (-not $repoRoot) {
13+
# If git did not return a repo root, or the git root does not contain .specify,
14+
# fall back to walking up from the script directory to find the initialized project root.
15+
if (-not $repoRoot -or -not (Test-Path (Join-Path $repoRoot '.specify'))) {
1416
$repoRoot = $scriptDir
15-
while ($repoRoot -ne [System.IO.Path]::GetPathRoot($repoRoot) -and -not (Test-Path (Join-Path $repoRoot '.specify'))) {
17+
$fsRoot = [System.IO.Path]::GetPathRoot($repoRoot)
18+
while ($repoRoot -and $repoRoot -ne $fsRoot -and -not (Test-Path (Join-Path $repoRoot '.specify'))) {
1619
$repoRoot = Split-Path -Parent $repoRoot
1720
}
1821
}

src/specify_cli/integrations/claude/scripts/update-context.sh

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,17 @@ set -euo pipefail
1212
_script_dir="$(cd "$(dirname "$0")" && pwd)"
1313
_root="$_script_dir"
1414
while [ "$_root" != "/" ] && [ ! -d "$_root/.specify" ]; do _root="$(dirname "$_root")"; done
15-
REPO_ROOT="${REPO_ROOT:-$(git rev-parse --show-toplevel 2>/dev/null || echo "$_root")}"
15+
if [ -z "${REPO_ROOT:-}" ]; then
16+
if [ -d "$_root/.specify" ]; then
17+
REPO_ROOT="$_root"
18+
else
19+
git_root="$(git rev-parse --show-toplevel 2>/dev/null || true)"
20+
if [ -n "$git_root" ] && [ -d "$git_root/.specify" ]; then
21+
REPO_ROOT="$git_root"
22+
else
23+
REPO_ROOT="$_root"
24+
fi
25+
fi
26+
fi
1627

1728
exec "$REPO_ROOT/.specify/scripts/bash/update-agent-context.sh" claude

src/specify_cli/integrations/codebuddy/scripts/update-context.ps1

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,12 @@ $ErrorActionPreference = 'Stop'
1010
# Derive repo root from script location (walks up to find .specify/)
1111
$scriptDir = Split-Path -Parent $MyInvocation.MyCommand.Definition
1212
$repoRoot = git rev-parse --show-toplevel 2>$null
13-
if (-not $repoRoot) {
13+
# If git did not return a repo root, or the git root does not contain .specify,
14+
# fall back to walking up from the script directory to find the initialized project root.
15+
if (-not $repoRoot -or -not (Test-Path (Join-Path $repoRoot '.specify'))) {
1416
$repoRoot = $scriptDir
15-
while ($repoRoot -ne [System.IO.Path]::GetPathRoot($repoRoot) -and -not (Test-Path (Join-Path $repoRoot '.specify'))) {
17+
$fsRoot = [System.IO.Path]::GetPathRoot($repoRoot)
18+
while ($repoRoot -and $repoRoot -ne $fsRoot -and -not (Test-Path (Join-Path $repoRoot '.specify'))) {
1619
$repoRoot = Split-Path -Parent $repoRoot
1720
}
1821
}

src/specify_cli/integrations/codebuddy/scripts/update-context.sh

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,17 @@ set -euo pipefail
1212
_script_dir="$(cd "$(dirname "$0")" && pwd)"
1313
_root="$_script_dir"
1414
while [ "$_root" != "/" ] && [ ! -d "$_root/.specify" ]; do _root="$(dirname "$_root")"; done
15-
REPO_ROOT="${REPO_ROOT:-$(git rev-parse --show-toplevel 2>/dev/null || echo "$_root")}"
15+
if [ -z "${REPO_ROOT:-}" ]; then
16+
if [ -d "$_root/.specify" ]; then
17+
REPO_ROOT="$_root"
18+
else
19+
git_root="$(git rev-parse --show-toplevel 2>/dev/null || true)"
20+
if [ -n "$git_root" ] && [ -d "$git_root/.specify" ]; then
21+
REPO_ROOT="$git_root"
22+
else
23+
REPO_ROOT="$_root"
24+
fi
25+
fi
26+
fi
1627

1728
exec "$REPO_ROOT/.specify/scripts/bash/update-agent-context.sh" codebuddy

0 commit comments

Comments
 (0)