Skip to content
Shell
887 B
Raw Blame
#!/bin/bash
# Protect Main Branch Hook
# Blocks file edits when on main/master branch
# Exit code 2 = block the tool call

# Get project directory from environment or use PWD
PROJECT_DIR="${CLAUDE_PROJECT_DIR:-$PWD}"

# Check if we're in a git repo
if ! git -C "$PROJECT_DIR" rev-parse --git-dir > /dev/null 2>&1; then
  # Not a git repo, allow the edit
  exit 0
fi

# Get current branch
branch=$(git -C "$PROJECT_DIR" branch --show-current 2>/dev/null)

# Block edits on main or master
if [[ "$branch" == "main" ]] || [[ "$branch" == "master" ]]; then
  echo "BLOCKED: Cannot edit files on '$branch' branch." >&2
  echo "" >&2
  echo "Please create a feature branch first:" >&2
  echo "  git checkout -b feature/your-feature-name" >&2
  echo "" >&2
  echo "Or use the git-workflow plugin:" >&2
  echo "  /git-workflow:feature your-feature-name" >&2
  exit 2
fi

# Allow the edit
exit 0