initialize-skills.sh 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #!/usr/bin/env bash
  2. set -euo pipefail
  3. SKILLS_DIR="${HOME}/.config/superpowers/skills"
  4. SKILLS_REPO="https://github.com/obra/superpowers-skills.git"
  5. # Check if skills directory exists and is a valid git repo
  6. if [ -d "$SKILLS_DIR/.git" ]; then
  7. cd "$SKILLS_DIR"
  8. # Get the remote name for the current tracking branch
  9. TRACKING_REMOTE=$(git rev-parse --abbrev-ref --symbolic-full-name @{u} 2>/dev/null | cut -d'/' -f1 || echo "")
  10. # Fetch from tracking remote if set, otherwise try upstream then origin
  11. if [ -n "$TRACKING_REMOTE" ]; then
  12. git fetch "$TRACKING_REMOTE" 2>/dev/null || true
  13. else
  14. git fetch upstream 2>/dev/null || git fetch origin 2>/dev/null || true
  15. fi
  16. # Check if we can fast-forward
  17. LOCAL=$(git rev-parse @ 2>/dev/null || echo "")
  18. REMOTE=$(git rev-parse @{u} 2>/dev/null || echo "")
  19. BASE=$(git merge-base @ @{u} 2>/dev/null || echo "")
  20. # Try to fast-forward merge first
  21. if [ -n "$LOCAL" ] && [ -n "$REMOTE" ] && [ "$LOCAL" != "$REMOTE" ]; then
  22. # Check if we can fast-forward (local is ancestor of remote)
  23. if [ "$LOCAL" = "$BASE" ]; then
  24. # Fast-forward merge is possible
  25. echo "Updating skills to latest version..."
  26. if git merge --ff-only @{u} 2>&1; then
  27. echo "✓ Skills updated successfully"
  28. echo "SKILLS_UPDATED=true"
  29. else
  30. echo "Failed to update skills"
  31. fi
  32. else
  33. # Can't fast-forward - will be reported at the end
  34. echo "SKILLS_BEHIND=true"
  35. fi
  36. fi
  37. exit 0
  38. fi
  39. # Skills directory doesn't exist or isn't a git repo - initialize it
  40. echo "Initializing skills repository..."
  41. # Handle migration from old installation
  42. if [ -d "${HOME}/.config/superpowers/.git" ]; then
  43. echo "Found existing installation. Backing up..."
  44. mv "${HOME}/.config/superpowers/.git" "${HOME}/.config/superpowers/.git.bak"
  45. if [ -d "${HOME}/.config/superpowers/skills" ]; then
  46. mv "${HOME}/.config/superpowers/skills" "${HOME}/.config/superpowers/skills.bak"
  47. echo "Your old skills are in ~/.config/superpowers/skills.bak"
  48. fi
  49. fi
  50. # Clone the skills repository
  51. mkdir -p "${HOME}/.config/superpowers"
  52. git clone "$SKILLS_REPO" "$SKILLS_DIR"
  53. cd "$SKILLS_DIR"
  54. # Offer to fork if gh is installed
  55. if command -v gh &> /dev/null; then
  56. echo ""
  57. echo "GitHub CLI detected. Would you like to fork superpowers-skills?"
  58. echo "Forking allows you to share skill improvements with the community."
  59. echo ""
  60. read -p "Fork superpowers-skills? (y/N): " -n 1 -r
  61. echo
  62. if [[ $REPLY =~ ^[Yy]$ ]]; then
  63. gh repo fork obra/superpowers-skills --remote=true
  64. echo "Forked! You can now contribute skills back to the community."
  65. else
  66. git remote add upstream "$SKILLS_REPO"
  67. fi
  68. else
  69. # No gh, just set up upstream remote
  70. git remote add upstream "$SKILLS_REPO"
  71. fi
  72. echo "Skills repository initialized at $SKILLS_DIR"