initialize-repo.ps1 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #!/usr/bin/env pwsh
  2. # Git extension: initialize-repo.ps1
  3. # Initialize a Git repository with an initial commit.
  4. # Customizable — replace this script to add .gitignore templates,
  5. # default branch config, git-flow, LFS, signing, etc.
  6. $ErrorActionPreference = 'Stop'
  7. # Find project root
  8. function Find-ProjectRoot {
  9. param([string]$StartDir)
  10. $current = Resolve-Path $StartDir
  11. while ($true) {
  12. foreach ($marker in @('.specify', '.git')) {
  13. if (Test-Path (Join-Path $current $marker)) {
  14. return $current
  15. }
  16. }
  17. $parent = Split-Path $current -Parent
  18. if ($parent -eq $current) { return $null }
  19. $current = $parent
  20. }
  21. }
  22. $repoRoot = Find-ProjectRoot -StartDir $PSScriptRoot
  23. if (-not $repoRoot) { $repoRoot = Get-Location }
  24. Set-Location $repoRoot
  25. # Read commit message from extension config, fall back to default
  26. $commitMsg = "[Spec Kit] Initial commit"
  27. $configFile = Join-Path $repoRoot ".specify/extensions/git/git-config.yml"
  28. if (Test-Path $configFile) {
  29. foreach ($line in Get-Content $configFile) {
  30. if ($line -match '^init_commit_message:\s*(.+)$') {
  31. $val = $matches[1].Trim() -replace '^["'']' -replace '["'']$'
  32. if ($val) { $commitMsg = $val }
  33. break
  34. }
  35. }
  36. }
  37. # Check if git is available
  38. if (-not (Get-Command git -ErrorAction SilentlyContinue)) {
  39. Write-Warning "[specify] Warning: Git not found; skipped repository initialization"
  40. exit 0
  41. }
  42. # Check if already a git repo
  43. try {
  44. git rev-parse --is-inside-work-tree 2>$null | Out-Null
  45. if ($LASTEXITCODE -eq 0) {
  46. Write-Warning "[specify] Git repository already initialized; skipping"
  47. exit 0
  48. }
  49. } catch { }
  50. # Initialize
  51. try {
  52. $out = git init -q 2>&1 | Out-String
  53. if ($LASTEXITCODE -ne 0) { throw "git init failed: $out" }
  54. $out = git add . 2>&1 | Out-String
  55. if ($LASTEXITCODE -ne 0) { throw "git add failed: $out" }
  56. $out = git commit --allow-empty -q -m $commitMsg 2>&1 | Out-String
  57. if ($LASTEXITCODE -ne 0) { throw "git commit failed: $out" }
  58. } catch {
  59. Write-Warning "[specify] Error: $_"
  60. exit 1
  61. }
  62. Write-Host "✓ Git repository initialized"