mirror of
https://github.com/github/spec-kit.git
synced 2026-03-25 14:53:08 +00:00
* fix(scripts): prioritize .specify over git for repo root detection When spec-kit is initialized in a subdirectory that doesn't have its own .git, but a parent directory does, spec-kit was incorrectly using the parent's git repository root. This caused specs to be created in the wrong location. The fix changes repo root detection to prioritize .specify directory over git rev-parse, ensuring spec-kit respects its own initialization boundary rather than inheriting a parent git repo. Fixes #1932 Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * fix: address code review feedback - Normalize paths in find_specify_root to prevent infinite loop with relative paths - Use -PathType Container in PowerShell to only match .specify directories - Improve has_git/Test-HasGit to check git command availability and validate work tree - Handle git worktrees/submodules where .git can be a file - Remove dead fallback code in create-new-feature scripts Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * fix: check .specify before termination in find_specify_root Fixes edge case where project root is at filesystem root (common in containers). The loop now checks for .specify before checking the termination condition. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * fix: scope git operations to spec-kit root & remove unused helpers - get_current_branch now uses has_git check and runs git with -C to prevent using parent git repo branch names in .specify-only projects - Same fix applied to PowerShell Get-CurrentBranch - Removed unused find_repo_root() from create-new-feature.sh - Removed unused Find-RepositoryRoot from create-new-feature.ps1 Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * fix: use cd -- to handle paths starting with dash Prevents cd from interpreting directory names like -P or -L as options. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * fix: check git command exists before calling get_repo_root in has_git Avoids unnecessary work when git isn't installed since get_repo_root may internally call git rev-parse. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * fix(powershell): use LiteralPath and check git before Get-RepoRoot - Use -LiteralPath in Find-SpecifyRoot to handle paths with wildcard characters ([, ], *, ?) - Check Get-Command git before calling Get-RepoRoot in Test-HasGit to avoid unnecessary work when git isn't installed Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * fix(powershell): use LiteralPath for .git check in Test-HasGit Prevents Test-Path from treating wildcard characters in paths as globs. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * fix(powershell): use LiteralPath in Get-RepoRoot fallback Prevents Resolve-Path from treating wildcard characters as patterns. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> --------- Co-authored-by: iamaeroplane <michal.bachorik@gmail.com> Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
264 lines
9.0 KiB
PowerShell
264 lines
9.0 KiB
PowerShell
#!/usr/bin/env pwsh
|
|
# Common PowerShell functions analogous to common.sh
|
|
|
|
# Find repository root by searching upward for .specify directory
|
|
# This is the primary marker for spec-kit projects
|
|
function Find-SpecifyRoot {
|
|
param([string]$StartDir = (Get-Location).Path)
|
|
|
|
# Normalize to absolute path to prevent issues with relative paths
|
|
# Use -LiteralPath to handle paths with wildcard characters ([, ], *, ?)
|
|
$current = (Resolve-Path -LiteralPath $StartDir -ErrorAction SilentlyContinue)?.Path
|
|
if (-not $current) { return $null }
|
|
|
|
while ($true) {
|
|
if (Test-Path -LiteralPath (Join-Path $current ".specify") -PathType Container) {
|
|
return $current
|
|
}
|
|
$parent = Split-Path $current -Parent
|
|
if ([string]::IsNullOrEmpty($parent) -or $parent -eq $current) {
|
|
return $null
|
|
}
|
|
$current = $parent
|
|
}
|
|
}
|
|
|
|
# Get repository root, prioritizing .specify directory over git
|
|
# This prevents using a parent git repo when spec-kit is initialized in a subdirectory
|
|
function Get-RepoRoot {
|
|
# First, look for .specify directory (spec-kit's own marker)
|
|
$specifyRoot = Find-SpecifyRoot
|
|
if ($specifyRoot) {
|
|
return $specifyRoot
|
|
}
|
|
|
|
# Fallback to git if no .specify found
|
|
try {
|
|
$result = git rev-parse --show-toplevel 2>$null
|
|
if ($LASTEXITCODE -eq 0) {
|
|
return $result
|
|
}
|
|
} catch {
|
|
# Git command failed
|
|
}
|
|
|
|
# Final fallback to script location for non-git repos
|
|
# Use -LiteralPath to handle paths with wildcard characters
|
|
return (Resolve-Path -LiteralPath (Join-Path $PSScriptRoot "../../..")).Path
|
|
}
|
|
|
|
function Get-CurrentBranch {
|
|
# First check if SPECIFY_FEATURE environment variable is set
|
|
if ($env:SPECIFY_FEATURE) {
|
|
return $env:SPECIFY_FEATURE
|
|
}
|
|
|
|
# Then check git if available at the spec-kit root (not parent)
|
|
$repoRoot = Get-RepoRoot
|
|
if (Test-HasGit) {
|
|
try {
|
|
$result = git -C $repoRoot rev-parse --abbrev-ref HEAD 2>$null
|
|
if ($LASTEXITCODE -eq 0) {
|
|
return $result
|
|
}
|
|
} catch {
|
|
# Git command failed
|
|
}
|
|
}
|
|
|
|
# For non-git repos, try to find the latest feature directory
|
|
$specsDir = Join-Path $repoRoot "specs"
|
|
|
|
if (Test-Path $specsDir) {
|
|
$latestFeature = ""
|
|
$highest = 0
|
|
$latestTimestamp = ""
|
|
|
|
Get-ChildItem -Path $specsDir -Directory | ForEach-Object {
|
|
if ($_.Name -match '^(\d{8}-\d{6})-') {
|
|
# Timestamp-based branch: compare lexicographically
|
|
$ts = $matches[1]
|
|
if ($ts -gt $latestTimestamp) {
|
|
$latestTimestamp = $ts
|
|
$latestFeature = $_.Name
|
|
}
|
|
} elseif ($_.Name -match '^(\d{3})-') {
|
|
$num = [int]$matches[1]
|
|
if ($num -gt $highest) {
|
|
$highest = $num
|
|
# Only update if no timestamp branch found yet
|
|
if (-not $latestTimestamp) {
|
|
$latestFeature = $_.Name
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if ($latestFeature) {
|
|
return $latestFeature
|
|
}
|
|
}
|
|
|
|
# Final fallback
|
|
return "main"
|
|
}
|
|
|
|
# Check if we have git available at the spec-kit root level
|
|
# Returns true only if git is installed and the repo root is inside a git work tree
|
|
# Handles both regular repos (.git directory) and worktrees/submodules (.git file)
|
|
function Test-HasGit {
|
|
# First check if git command is available (before calling Get-RepoRoot which may use git)
|
|
if (-not (Get-Command git -ErrorAction SilentlyContinue)) {
|
|
return $false
|
|
}
|
|
$repoRoot = Get-RepoRoot
|
|
# Check if .git exists (directory or file for worktrees/submodules)
|
|
# Use -LiteralPath to handle paths with wildcard characters
|
|
if (-not (Test-Path -LiteralPath (Join-Path $repoRoot ".git"))) {
|
|
return $false
|
|
}
|
|
# Verify it's actually a valid git work tree
|
|
try {
|
|
$null = git -C $repoRoot rev-parse --is-inside-work-tree 2>$null
|
|
return ($LASTEXITCODE -eq 0)
|
|
} catch {
|
|
return $false
|
|
}
|
|
}
|
|
|
|
function Test-FeatureBranch {
|
|
param(
|
|
[string]$Branch,
|
|
[bool]$HasGit = $true
|
|
)
|
|
|
|
# For non-git repos, we can't enforce branch naming but still provide output
|
|
if (-not $HasGit) {
|
|
Write-Warning "[specify] Warning: Git repository not detected; skipped branch validation"
|
|
return $true
|
|
}
|
|
|
|
if ($Branch -notmatch '^[0-9]{3}-' -and $Branch -notmatch '^\d{8}-\d{6}-') {
|
|
Write-Output "ERROR: Not on a feature branch. Current branch: $Branch"
|
|
Write-Output "Feature branches should be named like: 001-feature-name or 20260319-143022-feature-name"
|
|
return $false
|
|
}
|
|
return $true
|
|
}
|
|
|
|
function Get-FeatureDir {
|
|
param([string]$RepoRoot, [string]$Branch)
|
|
Join-Path $RepoRoot "specs/$Branch"
|
|
}
|
|
|
|
function Get-FeaturePathsEnv {
|
|
$repoRoot = Get-RepoRoot
|
|
$currentBranch = Get-CurrentBranch
|
|
$hasGit = Test-HasGit
|
|
$featureDir = Get-FeatureDir -RepoRoot $repoRoot -Branch $currentBranch
|
|
|
|
[PSCustomObject]@{
|
|
REPO_ROOT = $repoRoot
|
|
CURRENT_BRANCH = $currentBranch
|
|
HAS_GIT = $hasGit
|
|
FEATURE_DIR = $featureDir
|
|
FEATURE_SPEC = Join-Path $featureDir 'spec.md'
|
|
IMPL_PLAN = Join-Path $featureDir 'plan.md'
|
|
TASKS = Join-Path $featureDir 'tasks.md'
|
|
RESEARCH = Join-Path $featureDir 'research.md'
|
|
DATA_MODEL = Join-Path $featureDir 'data-model.md'
|
|
QUICKSTART = Join-Path $featureDir 'quickstart.md'
|
|
CONTRACTS_DIR = Join-Path $featureDir 'contracts'
|
|
}
|
|
}
|
|
|
|
function Test-FileExists {
|
|
param([string]$Path, [string]$Description)
|
|
if (Test-Path -Path $Path -PathType Leaf) {
|
|
Write-Output " ✓ $Description"
|
|
return $true
|
|
} else {
|
|
Write-Output " ✗ $Description"
|
|
return $false
|
|
}
|
|
}
|
|
|
|
function Test-DirHasFiles {
|
|
param([string]$Path, [string]$Description)
|
|
if ((Test-Path -Path $Path -PathType Container) -and (Get-ChildItem -Path $Path -ErrorAction SilentlyContinue | Where-Object { -not $_.PSIsContainer } | Select-Object -First 1)) {
|
|
Write-Output " ✓ $Description"
|
|
return $true
|
|
} else {
|
|
Write-Output " ✗ $Description"
|
|
return $false
|
|
}
|
|
}
|
|
|
|
# Resolve a template name to a file path using the priority stack:
|
|
# 1. .specify/templates/overrides/
|
|
# 2. .specify/presets/<preset-id>/templates/ (sorted by priority from .registry)
|
|
# 3. .specify/extensions/<ext-id>/templates/
|
|
# 4. .specify/templates/ (core)
|
|
function Resolve-Template {
|
|
param(
|
|
[Parameter(Mandatory=$true)][string]$TemplateName,
|
|
[Parameter(Mandatory=$true)][string]$RepoRoot
|
|
)
|
|
|
|
$base = Join-Path $RepoRoot '.specify/templates'
|
|
|
|
# Priority 1: Project overrides
|
|
$override = Join-Path $base "overrides/$TemplateName.md"
|
|
if (Test-Path $override) { return $override }
|
|
|
|
# Priority 2: Installed presets (sorted by priority from .registry)
|
|
$presetsDir = Join-Path $RepoRoot '.specify/presets'
|
|
if (Test-Path $presetsDir) {
|
|
$registryFile = Join-Path $presetsDir '.registry'
|
|
$sortedPresets = @()
|
|
if (Test-Path $registryFile) {
|
|
try {
|
|
$registryData = Get-Content $registryFile -Raw | ConvertFrom-Json
|
|
$presets = $registryData.presets
|
|
if ($presets) {
|
|
$sortedPresets = $presets.PSObject.Properties |
|
|
Sort-Object { if ($null -ne $_.Value.priority) { $_.Value.priority } else { 10 } } |
|
|
ForEach-Object { $_.Name }
|
|
}
|
|
} catch {
|
|
# Fallback: alphabetical directory order
|
|
$sortedPresets = @()
|
|
}
|
|
}
|
|
|
|
if ($sortedPresets.Count -gt 0) {
|
|
foreach ($presetId in $sortedPresets) {
|
|
$candidate = Join-Path $presetsDir "$presetId/templates/$TemplateName.md"
|
|
if (Test-Path $candidate) { return $candidate }
|
|
}
|
|
} else {
|
|
# Fallback: alphabetical directory order
|
|
foreach ($preset in Get-ChildItem -Path $presetsDir -Directory -ErrorAction SilentlyContinue | Where-Object { $_.Name -notlike '.*' }) {
|
|
$candidate = Join-Path $preset.FullName "templates/$TemplateName.md"
|
|
if (Test-Path $candidate) { return $candidate }
|
|
}
|
|
}
|
|
}
|
|
|
|
# Priority 3: Extension-provided templates
|
|
$extDir = Join-Path $RepoRoot '.specify/extensions'
|
|
if (Test-Path $extDir) {
|
|
foreach ($ext in Get-ChildItem -Path $extDir -Directory -ErrorAction SilentlyContinue | Where-Object { $_.Name -notlike '.*' } | Sort-Object Name) {
|
|
$candidate = Join-Path $ext.FullName "templates/$TemplateName.md"
|
|
if (Test-Path $candidate) { return $candidate }
|
|
}
|
|
}
|
|
|
|
# Priority 4: Core templates
|
|
$core = Join-Path $base "$TemplateName.md"
|
|
if (Test-Path $core) { return $core }
|
|
|
|
return $null
|
|
}
|
|
|