Enforce a Mandatory Ticket-ID Prefix on Worktree Names

A cheap naming-convention enforcement that pays off the first time someone — human or agent — is trying to figure out which of a dozen worktrees corresponds to which piece of work. Blocks creation if the name doesn't start with a ticket-ID-shaped prefix.

Worktree & Git Safety Event: WorktreeCreate Claude Code Open source · Free

1. Save this as the hook script

#!/usr/bin/env python3
"""WorktreeCreate hook — blocks creation unless worktree_name starts with
a ticket-ID pattern like PROJ-123."""
import json, re, sys

data = json.load(sys.stdin)
name = data.get("worktree_name", "")

if not re.match(r"^[A-Z]{2,6}-\d{2,6}", name):
    print(f"BLOCKED: worktree name '{name}' doesn't start with a ticket-ID "
          f"prefix (e.g. PROJ-123).", file=sys.stderr)
    sys.exit(1)

sys.exit(0)

Save to a path inside your project, e.g. .claude/hooks/enforce-ticket-prefix-worktree-names.sh, then chmod +x it.

2. Wire it into .claude/settings.json

{
  "hooks": {
    "WorktreeCreate": [
      {
        "hooks": [
          {"type": "command", "command": "python3 .claude/hooks/enforce-ticket-prefix-worktree-names.py"}
        ]
      }
    ]
  }
}

Before you use this

A hook runs with your local shell's permissions — review the script above before adding it, the same way you'd review any script you didn't write yourself. See our guide on hook security risks for the specific CVEs this category of feature has already produced.

Related hooks

← All hooks
FEEDBACK