---
name: step-plan
description: Generate integration plan with EF Core analysis
next_step: null
---

# Step: PLAN - Integration Strategy

## YOUR TASK:

Analyze branch state, detect migration conflicts, and generate a detailed integration plan.

**ULTRA THINK about merge strategy and migration handling.**

---

## EXECUTION SEQUENCE:

### 1. Analyze Branch Context

```bash
CURRENT=$(git rev-parse --abbrev-ref HEAD)
git fetch origin --quiet

# Determine target based on branch type
case "$CURRENT" in
  feature/*)
    BASE_BRANCH="develop"
    MERGE_STRATEGY="squash"
    ;;
  release/*)
    BASE_BRANCH="main"
    MERGE_STRATEGY="merge"
    MERGE_BACK="develop"
    ;;
  hotfix/*)
    BASE_BRANCH="main"
    MERGE_STRATEGY="merge"
    MERGE_BACK="develop"
    ;;
esac

# Get commits
COMMITS=$(git rev-list --count origin/$BASE_BRANCH..HEAD)
COMMIT_LIST=$(git log --oneline origin/$BASE_BRANCH..HEAD)
```

### 2. Analyze EF Core Migrations

```bash
# Find migration files in branch
BRANCH_MIGRATIONS=$(git diff --name-only origin/$BASE_BRANCH...HEAD | grep "Migrations/")

# Find migrations in base branch
BASE_MIGRATIONS=$(git ls-tree -r --name-only origin/$BASE_BRANCH | grep "Migrations/")

# Check for snapshot divergence
SNAPSHOT_BRANCH=$(git show HEAD:$(find . -name "*ModelSnapshot.cs" -path "*/Migrations/*" | head -1) 2>/dev/null | md5sum)
SNAPSHOT_BASE=$(git show origin/$BASE_BRANCH:$(find . -name "*ModelSnapshot.cs" -path "*/Migrations/*" | head -1) 2>/dev/null | md5sum)

SNAPSHOT_DIVERGED=$([ "$SNAPSHOT_BRANCH" != "$SNAPSHOT_BASE" ] && echo "true" || echo "false")
```

### 3. Detect Conflicts

**Migration conflicts:**
```bash
if [ "$SNAPSHOT_DIVERGED" = "true" ]; then
  echo "⚠️ ModelSnapshot has diverged from $BASE_BRANCH"

  # Check if base has new migrations we don't have
  NEW_BASE_MIGRATIONS=$(git diff --name-only HEAD...origin/$BASE_BRANCH | grep "Migrations/" | grep -v "ModelSnapshot")

  [ -n "$NEW_BASE_MIGRATIONS" ] && {
    CONFLICT_TYPE="migration"
    CONFLICT_RESOLUTION="rebase-snapshot"
  }
fi
```

**Code conflicts:**
```bash
# Dry-run merge to detect conflicts
git merge-tree $(git merge-base HEAD origin/$BASE_BRANCH) HEAD origin/$BASE_BRANCH > /tmp/merge-tree 2>&1
CODE_CONFLICTS=$(grep -c "<<<<<<" /tmp/merge-tree || echo "0")
```

### 4. Determine Integration Strategy

| Scenario | Strategy |
|----------|----------|
| No conflicts | Direct merge |
| Code conflicts only | Rebase + resolve |
| Migration conflicts | `/efcore:rebase-snapshot` first |
| Both | Migration first, then code |

### 5. Generate Plan Document

See [references/plan-template.md](../references/plan-template.md) for the complete plan document template written to `.claude/gitflow/plans/{branch-name}_{date}.md`:
- Summary table (source, target, commits, strategy, merge-back)
- Commits list, EF Core analysis table
- Pre-integration steps (conditional on migration/code conflicts)
- Integration steps (conditional on release/hotfix)
- Post-integration checklist

### 6. Display Plan Summary

```
╔══════════════════════════════════════════════════════════════════╗
║                    INTEGRATION PLAN                               ║
╠══════════════════════════════════════════════════════════════════╣
║  {CURRENT} → {BASE_BRANCH}                                        ║
╠══════════════════════════════════════════════════════════════════╣
║  Commits:    {COMMITS}                                            ║
║  Strategy:   {MERGE_STRATEGY}                                     ║
║  Migrations: {migration_status}                                   ║
║  Conflicts:  {conflict_status}                                    ║
╠══════════════════════════════════════════════════════════════════╣
{if conflicts}
║  ⚠️ PRE-REQUISITES:                                               ║
║  {list of required steps}                                         ║
╠══════════════════════════════════════════════════════════════════╣
{endif}
║  Plan saved: .claude/gitflow/plans/{filename}                     ║
╠══════════════════════════════════════════════════════════════════╣
║  NEXT: {/efcore:rebase-snapshot | /gitflow pr}                    ║
╚══════════════════════════════════════════════════════════════════╝
```

---

## BLOCKING CONDITIONS:

- **Migration conflict** → Must run `/efcore:rebase-snapshot` first
- **Destructive migrations** → Require explicit confirmation
- **Build failure** → Fix before proceeding

---

## SUCCESS CRITERIA:

- Branch context analyzed
- Migration status determined
- Conflicts identified
- Plan document generated
- Clear next steps provided

## NEXT STEP:

- If conflicts: Resolve per plan
- If clean: `/gitflow pr`
