initialize-skills.sh 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 - local is behind
  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. elif [ "$REMOTE" != "$BASE" ]; then
  33. # Remote has changes (local is behind or diverged)
  34. echo "SKILLS_BEHIND=true"
  35. fi
  36. # If REMOTE = BASE, local is ahead - no action needed
  37. fi
  38. exit 0
  39. fi
  40. # Skills directory doesn't exist or isn't a git repo - initialize it
  41. echo "Initializing skills repository..."
  42. # Handle migration from old installation
  43. if [ -d "${HOME}/.config/superpowers/.git" ]; then
  44. echo "Found existing installation. Backing up..."
  45. mv "${HOME}/.config/superpowers/.git" "${HOME}/.config/superpowers/.git.bak"
  46. if [ -d "${HOME}/.config/superpowers/skills" ]; then
  47. mv "${HOME}/.config/superpowers/skills" "${HOME}/.config/superpowers/skills.bak"
  48. echo "Your old skills are in ~/.config/superpowers/skills.bak"
  49. fi
  50. fi
  51. # Clone the skills repository
  52. mkdir -p "${HOME}/.config/superpowers"
  53. git clone "$SKILLS_REPO" "$SKILLS_DIR"
  54. cd "$SKILLS_DIR"
  55. # Offer to fork if gh is installed
  56. if command -v gh &> /dev/null; then
  57. echo ""
  58. echo "GitHub CLI detected. Would you like to fork superpowers-skills?"
  59. echo "Forking allows you to share skill improvements with the community."
  60. echo ""
  61. read -p "Fork superpowers-skills? (y/N): " -n 1 -r
  62. echo
  63. if [[ $REPLY =~ ^[Yy]$ ]]; then
  64. gh repo fork obra/superpowers-skills --remote=true
  65. echo "Forked! You can now contribute skills back to the community."
  66. else
  67. git remote add upstream "$SKILLS_REPO"
  68. fi
  69. else
  70. # No gh, just set up upstream remote
  71. git remote add upstream "$SKILLS_REPO"
  72. fi
  73. echo "Skills repository initialized at $SKILLS_DIR"