initialize-repo.sh 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #!/usr/bin/env bash
  2. # Git extension: initialize-repo.sh
  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. set -e
  7. SCRIPT_DIR="$(CDPATH="" cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
  8. # Find project root
  9. _find_project_root() {
  10. local dir="$1"
  11. while [ "$dir" != "/" ]; do
  12. if [ -d "$dir/.specify" ] || [ -d "$dir/.git" ]; then
  13. echo "$dir"
  14. return 0
  15. fi
  16. dir="$(dirname "$dir")"
  17. done
  18. return 1
  19. }
  20. REPO_ROOT=$(_find_project_root "$SCRIPT_DIR") || REPO_ROOT="$(pwd)"
  21. cd "$REPO_ROOT"
  22. # Read commit message from extension config, fall back to default
  23. COMMIT_MSG="[Spec Kit] Initial commit"
  24. _config_file="$REPO_ROOT/.specify/extensions/git/git-config.yml"
  25. if [ -f "$_config_file" ]; then
  26. _msg=$(grep '^init_commit_message:' "$_config_file" 2>/dev/null | sed 's/^init_commit_message:[[:space:]]*//' | sed 's/^["'\'']//' | sed 's/["'\'']*$//')
  27. if [ -n "$_msg" ]; then
  28. COMMIT_MSG="$_msg"
  29. fi
  30. fi
  31. # Check if git is available
  32. if ! command -v git >/dev/null 2>&1; then
  33. echo "[specify] Warning: Git not found; skipped repository initialization" >&2
  34. exit 0
  35. fi
  36. # Check if already a git repo
  37. if git rev-parse --is-inside-work-tree >/dev/null 2>&1; then
  38. echo "[specify] Git repository already initialized; skipping" >&2
  39. exit 0
  40. fi
  41. # Initialize
  42. _git_out=$(git init -q 2>&1) || { echo "[specify] Error: git init failed: $_git_out" >&2; exit 1; }
  43. _git_out=$(git add . 2>&1) || { echo "[specify] Error: git add failed: $_git_out" >&2; exit 1; }
  44. _git_out=$(git commit --allow-empty -q -m "$COMMIT_MSG" 2>&1) || { echo "[specify] Error: git commit failed: $_git_out" >&2; exit 1; }
  45. echo "✓ Git repository initialized" >&2