Skip to content
Shell
2.8 KB
Raw Blame
#!/bin/bash
# Suggest Feature Branch Hook
# Runs on user prompt submit to suggest creating a feature branch
# when on main/master/develop and starting new work

# Read the user's message from stdin
USER_MESSAGE=$(cat)

# Check if we're in a git repo
if ! git rev-parse --git-dir > /dev/null 2>&1; then
  exit 0
fi

CURRENT_BRANCH=$(git branch --show-current 2>/dev/null)

# Protected branches
PROTECTED_BRANCHES=("main" "master" "develop")

# Check if on protected branch
IS_PROTECTED=false
for protected in "${PROTECTED_BRANCHES[@]}"; do
  if [[ "$CURRENT_BRANCH" == "$protected" ]]; then
    IS_PROTECTED=true
    break
  fi
done

if [[ "$IS_PROTECTED" != "true" ]]; then
  exit 0
fi

# Keywords that suggest new work (case-insensitive check)
WORK_KEYWORDS="add|create|build|implement|fix|update|change|modify|refactor|delete|remove|write|make|set up|setup|configure|install|migrate|convert|redesign|optimize|improve|enhance|develop|integrate|connect|new feature|feature|task|issue|bug|hotfix"

# Check if the message suggests new work
if echo "$USER_MESSAGE" | grep -iE "($WORK_KEYWORDS)" > /dev/null 2>&1; then
  # Extract a suggested branch name from the message
  # Take first 50 chars, lowercase, replace non-alphanumeric with dashes
  SUGGESTED_NAME=$(echo "$USER_MESSAGE" | head -c 60 | tr '[:upper:]' '[:lower:]' | sed 's/[^a-z0-9]/-/g' | sed 's/--*/-/g' | sed 's/^-//' | sed 's/-$//' | cut -c1-40)

  echo ""
  echo "┌─────────────────────────────────────────────────────────────────┐"
  echo "│  🌿 You're on '$CURRENT_BRANCH' - Need a feature branch?        "
  echo "├─────────────────────────────────────────────────────────────────┤"
  echo "│                                                                 │"
  echo "│  Suggested branch: feature/$SUGGESTED_NAME"
  echo "│                                                                 │"
  echo "│  Quick options:                                                 │"
  echo "│    • Say: \"Create a feature branch for this\"                   │"
  echo "│    • Use: /feature $SUGGESTED_NAME                              │"
  echo "│    • Run: git checkout -b feature/$SUGGESTED_NAME               │"
  echo "│                                                                 │"
  echo "│  Or continue - I'll remind you before any file changes.        │"
  echo "└─────────────────────────────────────────────────────────────────┘"
  echo ""
fi

exit 0