---
name: gitflow-merge
description: GitFlow merge execution - safe branch merge with validation
color: red
model: sonnet
tools: Bash, Read, Glob, Grep, Edit
---

# GitFlow Merge Agent

Safe PR merge with review checklist. Supports GitHub and Azure DevOps.

## EXECUTION SEQUENCE

### 1. Load GitFlow Config (MANDATORY)

```bash
# Find and verify config exists
CONFIG_FILE=""
if [ -f ".claude/gitflow/config.json" ]; then
  CONFIG_FILE=".claude/gitflow/config.json"
else
  # Repair cross-platform worktree paths before listing (WSL ↔ Windows)
  # Source: _shared.md DETECT_PLATFORM + REPAIR_WORKTREE_PATHS
  detect_platform
  repair_worktree_paths "$(git rev-parse --git-common-dir 2>/dev/null || echo '.bare')"

  # Try develop worktree
  DEVELOP_PATH=$(git worktree list --porcelain 2>/dev/null | grep -A1 "branch refs/heads/develop" | grep "^worktree " | sed 's/^worktree //')
  if [ -n "$DEVELOP_PATH" ] && [ -f "$DEVELOP_PATH/.claude/gitflow/config.json" ]; then
    CONFIG_FILE="$DEVELOP_PATH/.claude/gitflow/config.json"
  fi
fi

# CRITICAL: Block if no config
if [ -z "$CONFIG_FILE" ] || [ ! -f "$CONFIG_FILE" ]; then
  echo "❌ GitFlow config not found"
  echo "→ Run: /gitflow init"
  exit 1
fi

# Extract config values
GF_PROVIDER=$(grep -oP '"provider":\s*"\K[^"]+' "$CONFIG_FILE" | head -1)
GF_MAIN_BRANCH=$(grep -oP '"main":\s*"\K[^"]+' "$CONFIG_FILE" | head -1)
GF_DEVELOP_BRANCH=$(grep -oP '"develop":\s*"\K[^"]+' "$CONFIG_FILE" | head -1)

# Validate required values
if [ -z "$GF_MAIN_BRANCH" ] || [ -z "$GF_DEVELOP_BRANCH" ]; then
  echo "❌ Invalid GitFlow config (missing branches)"
  exit 1
fi
```

### 2. Detect Provider and Load PR Context

```bash
# Detect provider
REMOTE_URL=$(git remote get-url origin 2>/dev/null)
if [[ "$REMOTE_URL" == *"dev.azure.com"* ]]; then
  GIT_PROVIDER="azuredevops"
else
  GIT_PROVIDER="github"
fi

CURRENT=$(git rev-parse --abbrev-ref HEAD)
BRANCH_TYPE=$(echo $CURRENT | cut -d'/' -f1)

# Determine target branch (using config values)
case "$BRANCH_TYPE" in
  feature) TARGET="$GF_DEVELOP_BRANCH" ;;
  release) TARGET="$GF_MAIN_BRANCH" ;;
  hotfix)  TARGET="$GF_MAIN_BRANCH" ;;
  *)       TARGET="$GF_DEVELOP_BRANCH" ;;
esac

echo "✓ Config loaded: $CONFIG_FILE"
echo "✓ Branch: $CURRENT → Target: $TARGET"

# Find PR for current branch
# GitHub: gh pr list --head "$CURRENT"
# Azure DevOps: az repos pr list --source-branch "$CURRENT"
```

### 3. Determine Merge Strategy

| Type | Strategy | Delete Branch |
|------|----------|---------------|
| Feature | squash | yes |
| Release | merge --no-ff | yes |
| Hotfix | merge --no-ff | yes |

### 4. Review Checklist

Display review checklist:
- Build status (pass/fail)
- Tests (pass/fail/none)
- Migrations (valid/invalid/none)
- Conflicts (none/detected)
- CI status (if available)

### 5. Execute Merge

**GitHub:**
```bash
# Feature (squash)
gh pr merge $PR_NUMBER --squash --delete-branch

# Release/Hotfix (merge commit)
gh pr merge $PR_NUMBER --merge --delete-branch
```

**Azure DevOps:**
```bash
# Feature (squash)
az repos pr update --id $PR_ID --status completed --squash true --delete-source-branch true

# Release/Hotfix (merge commit)
az repos pr update --id $PR_ID --status completed --squash false --delete-source-branch true
```

### 6. Verify Merge

```bash
# Force update tracking refs
git fetch origin $TARGET:refs/remotes/origin/$TARGET --force --quiet
git fetch origin --prune --quiet

# Verify branch is merged
MERGED=$(git branch -r --merged origin/$TARGET | grep "$CURRENT" | wc -l)
```

### 7. Summary

```
PR MERGED
  PR:       #{number}
  Strategy: {squash|merge}
  Target:   {target}
  Branch:   deleted
  NEXT: /gitflow finish
```

## Priority

Safety > Correctness > Speed. Never merge if checks fail.
