git-common.ps1 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #!/usr/bin/env pwsh
  2. # Git-specific common functions for the git extension.
  3. # Extracted from scripts/powershell/common.ps1 — contains only git-specific
  4. # branch validation and detection logic.
  5. function Test-HasGit {
  6. param([string]$RepoRoot = (Get-Location))
  7. try {
  8. if (-not (Test-Path (Join-Path $RepoRoot '.git'))) { return $false }
  9. if (-not (Get-Command git -ErrorAction SilentlyContinue)) { return $false }
  10. git -C $RepoRoot rev-parse --is-inside-work-tree 2>$null | Out-Null
  11. return ($LASTEXITCODE -eq 0)
  12. } catch {
  13. return $false
  14. }
  15. }
  16. function Get-SpecKitEffectiveBranchName {
  17. param([string]$Branch)
  18. if ($Branch -match '^([^/]+)/([^/]+)$') {
  19. return $Matches[2]
  20. }
  21. return $Branch
  22. }
  23. function Test-FeatureBranch {
  24. param(
  25. [string]$Branch,
  26. [bool]$HasGit = $true
  27. )
  28. # For non-git repos, we can't enforce branch naming but still provide output
  29. if (-not $HasGit) {
  30. Write-Warning "[specify] Warning: Git repository not detected; skipped branch validation"
  31. return $true
  32. }
  33. $raw = $Branch
  34. $Branch = Get-SpecKitEffectiveBranchName $raw
  35. # Accept sequential prefix (3+ digits) but exclude malformed timestamps
  36. # Malformed: 7-or-8 digit date + 6-digit time with no trailing slug (e.g. "2026031-143022" or "20260319-143022")
  37. $hasMalformedTimestamp = ($Branch -match '^[0-9]{7}-[0-9]{6}-') -or ($Branch -match '^(?:\d{7}|\d{8})-\d{6}$')
  38. $isSequential = ($Branch -match '^[0-9]{3,}-') -and (-not $hasMalformedTimestamp)
  39. if (-not $isSequential -and $Branch -notmatch '^\d{8}-\d{6}-') {
  40. [Console]::Error.WriteLine("ERROR: Not on a feature branch. Current branch: $raw")
  41. [Console]::Error.WriteLine("Feature branches should be named like: 001-feature-name, 1234-feature-name, or 20260319-143022-feature-name")
  42. return $false
  43. }
  44. return $true
  45. }