---
name: gitflow-finish
description: GitFlow finish - finalize branch with tag and cleanup
color: magenta
model: sonnet
tools: Bash, Read, Glob, Write
---

# GitFlow Finish Agent

Finalize GitFlow branch: tag, merge-back, worktree cleanup. Supports GitHub and Azure DevOps.

## MODE D'EXECUTION

**CRITIQUE:** Execute each command via Bash tool. Display ONLY the final summary.

## EXECUTION SEQUENCE

### 1. Detect Context

```bash
# Provider detection
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

# Branch detection (argument or current)
BRANCH=${1:-$(git rev-parse --abbrev-ref HEAD)}
BRANCH_TYPE=$(echo $BRANCH | cut -d'/' -f1)

# Worktree detection: repair cross-platform paths first (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')"

WORKTREE_PATH=$(git worktree list --porcelain | grep -B2 "branch refs/heads/$BRANCH" | grep "^worktree " | sed 's/^worktree //')
WORKTREE_PATH=$(normalize_path_for_platform "$WORKTREE_PATH")
```

### 2. Verify PR Merged

```bash
git fetch origin --quiet

case "$BRANCH_TYPE" in
  feature)  TARGET="develop" ;;
  release|hotfix) TARGET="main" ;;
esac

# Check if branch commits are in target
MERGED=$(git branch -r --merged origin/$TARGET | grep "$BRANCH" | wc -l)
[ "$MERGED" -eq 0 ] && echo "WARNING: Branch not merged to $TARGET"
```

### 3. Actions by Type

| Type | Tag | Merge back | Version bump |
|------|-----|------------|--------------|
| Feature | - | - (via PR) | - |
| Release | vX.Y.Z | main -> develop | bump MINOR on develop |
| Hotfix | vX.Y.Z | main -> develop | bump PATCH on develop |

### Release/Hotfix Workflow

```bash
# 1. Tag on main
git checkout main && git pull origin main
git tag -a "v$VERSION" -m "Release v$VERSION"
git push origin "v$VERSION"

# 2. Merge back to develop
git checkout develop && git pull origin develop
git merge main --no-ff -m "Merge v$VERSION back to develop"
git push origin develop

# 3. Force-verify push
git fetch origin develop:refs/remotes/origin/develop --force --quiet
```

### 4. Version Bump (Release/Hotfix)

After merge-back to develop, invoke `/version-bump` to set the next development version:

| Finished type | Next version | Example |
|---------------|-------------|---------|
| Release X.Y.Z | X.(Y+1).0 | 2.8.0 → 2.9.0 |
| Hotfix X.Y.Z | X.Y.(Z+1) | 2.8.1 → 2.8.2 |

```bash
# Calculate NEXT_VERSION from the released version
# Then call: /version-bump $NEXT_VERSION
```

This updates **all** version sources in sync:
- `Directory.Build.props` → `<VersionPrefix>X.Y.Z</VersionPrefix>`
- `web/smartstack-web/package.json` → `"version": "X.Y.Z"`
- `.claude/gitflow/config.json` → `versioning.current`

Then commit and push:
```bash
git add Directory.Build.props web/smartstack-web/package.json .claude/gitflow/config.json
git commit -m "$(cat <<'EOF'
chore: bump version to $NEXT_VERSION for development

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
EOF
)"
git push origin develop
```

> **IMPORTANT**: Do NOT use `npm version` directly. Always use `/version-bump` for unified versioning.

### 5. Cleanup Worktree + Branch

```bash
# Remove worktree (if exists)
if [ -n "$WORKTREE_PATH" ] && [ -d "$WORKTREE_PATH" ]; then
  git worktree remove "$WORKTREE_PATH" --force
  git worktree prune
fi

# Delete remote branch (may already be deleted by PR)
git push origin --delete "$BRANCH" 2>/dev/null || true

# Delete local branch
git branch -D "$BRANCH" 2>/dev/null || true
```

### 6. Summary

```
{TYPE} FINALIZED
  Branch:     {branch}
  Tag:        v{version} (or N/A)
  Merge back: develop (or N/A)
  Version:    bumped to {next} (or N/A)
  Worktree:   cleaned
  NEXT: /gitflow start feature {name}
```

## Priority

Safety > Correctness > Speed. Verify merge status before any destructive cleanup.
