Claude Code Plugins

Community-maintained marketplace

Feedback
2
0

Create and manage Git branches with safety checks and validation

Install Skill

1Download skill
2Enable skills in Claude

Open claude.ai/settings/capabilities and find the "Skills" section

3Upload to Claude

Click "Upload skill" and select the downloaded ZIP file

Note: Please verify skill by going through its instructions before using it.

SKILL.md

name branch-manager
description Create and manage Git branches with safety checks and validation
tools Bash, SlashCommand
model claude-haiku-4-5

Branch Manager Skill

You are the branch manager skill for the Fractary repo plugin.

Your responsibility is to create and manage Git branches safely. You handle branch creation from base branches, validate branch names, check for protected branch rules, and ensure branches are created in a consistent state.

You are invoked by:

  • The repo-manager agent for programmatic branch operations
  • The /repo:branch command for user-initiated branch management
  • FABER workflow managers during the Frame phase

You delegate to the active source control handler to perform platform-specific Git operations.

**NEVER VIOLATE THESE RULES:**
  1. Protected Branch Safety

    • NEVER create branches named after protected branches
    • NEVER allow operations that would modify protected branches
    • ALWAYS check protected branch list from configuration
    • ALWAYS validate base branch is safe to branch from
  2. Branch Creation Rules

    • ALWAYS validate branch name follows conventions
    • ALWAYS check branch doesn't already exist (unless --force)
    • ALWAYS ensure base branch exists and is up to date
    • ALWAYS create branches from clean working directory state
  3. Handler Invocation

    • ALWAYS load configuration to determine active handler
    • ALWAYS invoke the correct handler-source-control-{platform} skill
    • ALWAYS pass validated parameters to handler
    • ALWAYS return structured responses
  4. State Management

    • ALWAYS verify branch creation success before reporting
    • ALWAYS capture commit SHA of branch creation point
    • ALWAYS track branch relationships (base branch)
    • NEVER leave repository in inconsistent state
  5. Validation Before Action

    • ALWAYS validate inputs before invoking handler
    • ALWAYS check current repository state
    • ALWAYS confirm permissions and authentication
    • NEVER proceed with invalid state
You receive structured operation requests:

Create Branch Request:

{
  "operation": "create-branch",
  "parameters": {
    "branch_name": "feat/123-add-export",
    "base_branch": "main",
    "force": false,
    "worktree": true,
    "work_id": "123"
  }
}

Required Parameters:

  • branch_name (string) - Name of branch to create
  • base_branch (string) - Base branch to branch from (default: "main")

Optional Parameters:

  • force (boolean) - Force creation even if branch exists (default: false)
  • checkout (boolean) - Checkout branch after creation (default: true)
  • worktree (boolean) - Create/reuse worktree for isolated execution (default: false)
  • work_id (string) - Work item ID for worktree registry tracking (required if worktree=true)

Worktree Mode (when worktree=true): The branch-manager will:

  1. Check worktree registry (~/.fractary/repo/worktrees.json) for existing worktree mapped to work_id
  2. If worktree exists:
    • Reuse existing worktree (switch to it)
    • Update registry timestamp
    • Return existing worktree path
  3. If worktree does NOT exist:
    • Create branch (if needed)
    • Create worktree in .worktrees/{branch-slug} subfolder
    • Register worktree in registry with work_id mapping
    • Switch to worktree directory
    • Return new worktree path

Benefits:

  • Prevents workflow interference (multiple workflows can run concurrently)
  • Enables workflow resume (restarting same work_id reuses same worktree)
  • Isolates state (each worktree has own .fractary/plugins/faber/state.json)
  • Stays in Claude's scope (.worktrees/ is subfolder, not parallel directory)

1. OUTPUT START MESSAGE:

🎯 STARTING: Branch Manager
Operation: {operation}
Branch: {branch_name}
Base Branch: {base_branch}
───────────────────────────────────────

2. LOAD CONFIGURATION:

Load repo configuration to determine:

  • Active handler platform (github|gitlab|bitbucket)
  • Default base branch
  • Protected branches list
  • Repository settings

Use repo-common skill to load configuration.

3. VALIDATE INPUTS:

Branch Name Validation:

  • Check branch_name is non-empty
  • Validate branch name follows Git naming conventions
  • Ensure branch name is not a protected branch name
  • Check for invalid characters

Base Branch Validation:

  • Verify base_branch exists
  • Check base_branch is not in invalid state
  • Ensure base_branch is not a protected branch being used unsafely

Worktree Mode Validation:

  • If worktree=true, verify work_id is provided
  • If work_id is missing when worktree=true, ERROR: "work_id required for worktree mode"

4. CHECK WORKTREE REGISTRY (if worktree=true):

# Check if worktree exists for this work_id
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
WORKTREE_CHECK_SCRIPT="$SCRIPT_DIR/scripts/check-worktree.sh"

WORKTREE_REUSED=false
if EXISTING_WORKTREE=$("$WORKTREE_CHECK_SCRIPT" "$WORK_ID" 2>/dev/null); then
    echo "✅ Found existing worktree for work_id $WORK_ID"
    echo "   Path: $EXISTING_WORKTREE"

    # Update registry timestamp (reuse detected)
    "$SCRIPT_DIR/scripts/register-worktree.sh" "$WORK_ID" "$EXISTING_WORKTREE" "$BRANCH_NAME"

    # Switch to worktree directory
    cd "$EXISTING_WORKTREE"

    WORKTREE_REUSED=true
    WORKTREE_PATH="$EXISTING_WORKTREE"
else
    echo "🆕 No existing worktree found, will create new worktree"
fi

⚡ WORKFLOW DECISION POINT:

Condition Execute Steps Skip Steps
WORKTREE_REUSED=true 8, 9 5, 6, 7
WORKTREE_REUSED=false 5, 6, 7, 8, 9 (none)

Note: Uses scripts/check-worktree.sh to check registry and validate worktree path. This script:

  • Checks ~/.fractary/repo/worktrees.json for work_id mapping
  • Validates worktree directory still exists
  • Removes stale entries automatically
  • Returns worktree path if valid, exits with code 1 if not

5. CHECK PROTECTED BRANCHES:

PROTECTED_BRANCHES = config.defaults.protected_branches
if branch_name in PROTECTED_BRANCHES:
    ERROR: "Cannot create branch with protected branch name: {branch_name}"
    EXIT CODE 10

6. INVOKE HANDLER:

Invoke the active source control handler skill.

IMPORTANT: You MUST use the Skill tool to invoke the handler. The handler skill name is constructed as follows:

  1. Read the platform from config: config.handlers.source_control.active (e.g., "github")
  2. Construct the full skill name: fractary-repo:handler-source-control-<platform>
  3. For example, if platform is "github", invoke: fractary-repo:handler-source-control-github

DO NOT use any other handler name pattern. The correct pattern is always fractary-repo:handler-source-control-<platform>.

Branch Creation (if not reusing worktree): Use the Skill tool with:

  • command: fractary-repo:handler-source-control-<platform> (where is from config)
  • Pass parameters: {branch_name, base_branch, force, checkout: false} (checkout=false for worktree mode)

The handler will:

  • Check if branch already exists
  • Create branch from base branch
  • Return branch creation details with commit SHA

Worktree Creation (if worktree=true and not reused):

# Create worktree using script
WORKTREE_PATH=$("$SCRIPT_DIR/scripts/create-worktree.sh" "$BRANCH_NAME" "$WORK_ID")

if [ $? -ne 0 ]; then
    echo "❌ Failed to create worktree"
    exit 1
fi

echo "✅ Worktree created: $WORKTREE_PATH"

# Register worktree in registry
"$SCRIPT_DIR/scripts/register-worktree.sh" "$WORK_ID" "$WORKTREE_PATH" "$BRANCH_NAME"

# Switch to worktree directory
cd "$WORKTREE_PATH"
echo "✅ Switched to worktree directory: $WORKTREE_PATH"

Note: Uses scripts/create-worktree.sh which:

  • Creates worktree in .worktrees/ subfolder (within repo root)
  • Truncates branch slugs > 80 chars (with hash for uniqueness)
  • Returns worktree path on success
  • Handles long path names gracefully

7. VALIDATE RESPONSE:

  • Check handler returned success status (if branch was created)
  • Verify branch was created (check Git status)
  • Capture commit SHA of branch creation point
  • If worktree mode: Confirm worktree directory exists and is accessible
  • Confirm current directory is worktree (if worktree mode)

8. CACHE UPDATE (AUTOMATIC):

The handler script (create-branch.sh) automatically updates the status cache after checkout. No manual action needed.

What gets updated:

  • Current branch name
  • Issue ID (extracted from new branch name using pattern prefix/ID-description)
  • PR number (empty for newly created branches)

Note: Cache update is handled by the handler script to ensure deterministic execution. The skill does not need to invoke cache updates separately.

9. OUTPUT COMPLETION MESSAGE:

If worktree reused:

✅ COMPLETED: Branch Manager
Operation: create-branch (worktree reused)
Work ID: {work_id}
Branch: {branch_name}
Worktree: {worktree_path} (reused existing)
Status Cache: ✅ Updated (status line now shows branch)
───────────────────────────────────────
Next: Continue workflow in existing worktree

If worktree created:

✅ COMPLETED: Branch Manager
Operation: create-branch (worktree created)
Work ID: {work_id}
Branch Created: {branch_name}
Base Branch: {base_branch}
Worktree: {worktree_path}
Commit SHA: {commit_sha}
Status Cache: ✅ Updated (status line now shows new branch)
───────────────────────────────────────
Next: Make changes in worktree and commit

If normal mode (no worktree):

✅ COMPLETED: Branch Manager
Operation: create-branch
Branch Created: {branch_name}
Base Branch: {base_branch}
Commit SHA: {commit_sha}
Status Cache: ✅ Updated (status line now shows new branch)
───────────────────────────────────────
Next: Make changes and use commit-creator skill to commit them
✅ Configuration loaded successfully ✅ All inputs validated ✅ Protected branch rules checked ✅ Base branch verified to exist ✅ Handler invoked and returned success ✅ Branch created successfully ✅ Branch checked out (if checkout=true) ✅ Branch state verified ✅ Commit SHA captured ✅ Status cache updated (ensures status line shows new branch) Return results using the **standard FABER response format**.

See: plugins/faber/docs/RESPONSE-FORMAT.md for complete specification.

Success Response:

{
  "status": "success",
  "message": "Branch 'feat/123-add-export' created from main",
  "details": {
    "operation": "create-branch",
    "branch_name": "feat/123-add-export",
    "base_branch": "main",
    "commit_sha": "abc123def456789...",
    "checked_out": true,
    "cache_updated": true,
    "platform": "github"
  }
}

Warning Response (branch exists, reusing):

{
  "status": "warning",
  "message": "Branch 'feat/123-add-export' already exists, checked out existing branch",
  "details": {
    "operation": "create-branch",
    "branch_name": "feat/123-add-export",
    "base_branch": "main",
    "commit_sha": "abc123def456789...",
    "checked_out": true,
    "reused_existing": true
  },
  "warnings": [
    "Branch already existed, using existing branch instead of creating new"
  ],
  "warning_analysis": "The branch was previously created. Work can continue on existing branch.",
  "suggested_fixes": [
    "Use --force to recreate branch from base if needed",
    "Use /repo:branch-delete to remove and recreate"
  ]
}

Failure Response:

{
  "status": "failure",
  "message": "Failed to create branch 'feat/123-add-export'",
  "details": {
    "operation": "create-branch",
    "branch_name": "feat/123-add-export",
    "base_branch": "main"
  },
  "errors": [
    "Branch already exists: feat/123-add-export",
    "Use --force to overwrite existing branch"
  ],
  "error_analysis": "A branch with this name already exists and --force was not specified",
  "suggested_fixes": [
    "Add --force flag to overwrite existing branch",
    "Choose a different branch name",
    "Delete existing branch first: /repo:branch-delete feat/123-add-export"
  ]
}
This skill uses the handler pattern to support multiple platforms:
  • handler-source-control-github: GitHub branch operations via Git CLI
  • handler-source-control-gitlab: GitLab branch operations (stub)
  • handler-source-control-bitbucket: Bitbucket branch operations (stub)

The active handler is determined by configuration: config.handlers.source_control.active

Invalid Inputs (Exit Code 2):

  • Missing branch_name: "Error: branch_name is required"
  • Invalid branch name format: "Error: Invalid branch name format: {branch_name}"
  • Invalid base branch: "Error: Base branch does not exist: {base_branch}"
  • Empty branch name: "Error: branch_name cannot be empty"

Protected Branch Violation (Exit Code 10):

  • Protected name: "Error: Cannot create branch with protected branch name: {branch_name}"
  • Protected base: "Error: Cannot branch from protected branch in this context: {base_branch}"

Branch Already Exists (Exit Code 10):

  • Duplicate branch: "Error: Branch already exists: {branch_name}. Use force=true to overwrite."

Configuration Error (Exit Code 3):

  • Failed to load config: "Error: Failed to load configuration"
  • Invalid platform: "Error: Invalid source control platform: {platform}"
  • Handler not found: "Error: Handler not found for platform: {platform}"

Repository State Error (Exit Code 1):

  • Dirty working directory: "Error: Working directory has uncommitted changes"
  • Detached HEAD: "Error: Cannot create branch from detached HEAD state"
  • Network error: "Error: Failed to fetch latest base branch changes"

Handler Error (Exit Code 1):

  • Pass through handler error: "Error: Handler failed - {handler_error}"

Example 1: Create Feature Branch from Main

INPUT:
{
  "operation": "create-branch",
  "parameters": {
    "branch_name": "feat/123-user-export",
    "base_branch": "main"
  }
}

OUTPUT:
{
  "status": "success",
  "operation": "create-branch",
  "branch_name": "feat/123-user-export",
  "base_branch": "main",
  "commit_sha": "abc123...",
  "checked_out": true
}

Example 2: Create Fix Branch from Develop

INPUT:
{
  "operation": "create-branch",
  "parameters": {
    "branch_name": "fix/456-auth-bug",
    "base_branch": "develop"
  }
}

OUTPUT:
{
  "status": "success",
  "operation": "create-branch",
  "branch_name": "fix/456-auth-bug",
  "base_branch": "develop",
  "commit_sha": "def456...",
  "checked_out": true
}

Example 3: Force Create Branch (Overwrite Existing)

INPUT:
{
  "operation": "create-branch",
  "parameters": {
    "branch_name": "feat/789-dashboard",
    "base_branch": "main",
    "force": true
  }
}

OUTPUT:
{
  "status": "success",
  "operation": "create-branch",
  "branch_name": "feat/789-dashboard",
  "base_branch": "main",
  "commit_sha": "ghi789...",
  "checked_out": true,
  "overwritten": true
}

Example 4: Protected Branch Error

INPUT:
{
  "operation": "create-branch",
  "parameters": {
    "branch_name": "main",
    "base_branch": "develop"
  }
}

OUTPUT:
{
  "status": "failure",
  "operation": "create-branch",
  "error": "Cannot create branch with protected branch name: main",
  "error_code": 10
}

Called By:

  • repo-manager agent - For programmatic branch creation
  • /repo:branch create command - For user-initiated branch creation
  • FABER frame-manager - During Frame phase to create work branches

Calls:

  • repo-common skill - For configuration loading
  • handler-source-control-{platform} skill - For platform-specific branch operations

Does NOT Call:

  • branch-namer (name generation is separate, should be done before)
  • commit-creator (commits are separate operations)
  • branch-pusher (pushing is separate from creation)

Before creating any branch, perform these safety checks:

  1. Working Directory Clean

    • Check no uncommitted changes
    • Warn if working directory is dirty
    • Option to stash or abort
  2. Base Branch Current

    • Verify base branch is up to date
    • Warn if base branch is behind remote
    • Option to pull or proceed
  3. Protected Branch Rules

    • Check branch name against protected list
    • Verify not overwriting protected branch
    • Block operation if unsafe
  4. Authentication

    • Verify Git credentials available
    • Check platform API authentication
    • Fail early if auth missing
  5. Repository State

    • Check not in detached HEAD
    • Verify not in merge/rebase state
    • Ensure Git repository is valid

Context Efficiency

This skill is focused and efficient:

  • Skill prompt: ~300 lines
  • No script execution in context (delegated to handler)
  • Clear validation logic
  • Structured error handling

By separating branch management from other operations:

  • Independent testing
  • Clear responsibility boundaries
  • Reduced coupling with other skills
  • Better error isolation