---
name: step-init
description: Initialize GitFlow configuration and branches
next_step: null
---

# Step: INIT - Initialize GitFlow

## YOUR TASK:

Set up GitFlow configuration with bare repository, worktrees, and branches.

**ULTRA THINK before creating configuration.**

---

## MANDATORY QUESTIONS

**⛔ ALWAYS ask these 3 questions, even if values can be auto-detected:**

| Question | Variable | Auto-detect from |
|----------|----------|------------------|
| Repository URL | `{REPO_URL}` | `git remote get-url origin` |
| Root folder | `{ROOT_FOLDER}` | Parent of current directory |
| Project name | `{PROJECT_NAME}` | Repo name from URL or folder |

---

## EXECUTION SEQUENCE:

### 1. Detect Environment (for defaults only)

```bash
# Check if we're in a git repository
IS_GIT_REPO=$(git rev-parse --git-dir 2>/dev/null && echo "true" || echo "false")

# Auto-detect values for defaults (will be confirmed by user)
DETECTED_URL=$(git remote get-url origin 2>/dev/null || echo "")
DETECTED_FOLDER=$(dirname "$(pwd)")
DETECTED_NAME=$(basename -s .git "$DETECTED_URL" 2>/dev/null || basename "$(pwd)")

# Detect git provider
if [[ "$DETECTED_URL" == *"github.com"* ]]; then
  GIT_PROVIDER="github"
elif [[ "$DETECTED_URL" == *"dev.azure.com"* ]]; then
  GIT_PROVIDER="azuredevops"
else
  GIT_PROVIDER="unknown"
fi

# Check if already initialized
ALREADY_INITIALIZED=$([ -f ".claude/gitflow/config.json" ] && echo "true" || echo "false")
```

### 2. Handle Existing Configuration

**If `ALREADY_INITIALIZED` = true:**

Display current configuration and ask what to do:

```yaml
AskUserQuestion:
  - header: "Existing"
    question: "GitFlow is already initialized. Current config will be shown. What do you want to do?"
    options:
      - label: "Reconfigure (Recommended)"
        description: "Review and update all settings"
      - label: "Repair"
        description: "Fix missing directories only"
      - label: "View only"
        description: "Display current config and exit"
      - label: "Cancel"
        description: "Exit without changes"
    multiSelect: false
```

**Handle choices:**
- **Reconfigure**: Continue to step 3 (will ask all questions again)
- **Repair**: Check directories, fix missing ones, exit
- **View only**: Display config, exit
- **Cancel**: Exit immediately

### 3. MANDATORY: Ask Repository URL

**⛔ ALWAYS ask, even if URL was detected:**

```yaml
AskUserQuestion:
  - header: "Repository"
    question: "Repository URL? (detected: {DETECTED_URL})"
    options:
      - label: "Use detected URL (Recommended)"
        description: "{DETECTED_URL}"
      - label: "Enter GitHub URL"
        description: "https://github.com/org/repo.git"
      - label: "Enter Azure DevOps URL"
        description: "https://dev.azure.com/org/project/_git/repo"
      - label: "Local only (no remote)"
        description: "Initialize without remote (limited features)"
    multiSelect: false
```

**If user selects "Enter GitHub URL" or "Enter Azure DevOps URL":**
- Capture URL via "Other" input
- Validate format
- Update `{REPO_URL}` and `{GIT_PROVIDER}`

### 4. MANDATORY: Ask Root Folder

**⛔ ALWAYS ask where to create the project structure:**

```yaml
AskUserQuestion:
  - header: "Location"
    question: "Root folder for project? (All worktrees will be created here)"
    options:
      - label: "Use parent folder (Recommended)"
        description: "{DETECTED_FOLDER}/"
      - label: "Use current folder"
        description: "$(pwd)/ - Will create subfolders here"
      - label: "Enter custom path"
        description: "Specify a different location"
    multiSelect: false
```

**Explain the structure that will be created:**

```
{ROOT_FOLDER}/
├── .bare/                 # Git bare repository
├── 01-Main/               # Worktree: main branch
├── 02-Develop/            # Worktree: develop branch
├── features/              # Feature branch worktrees
├── releases/              # Release branch worktrees
└── hotfixes/              # Hotfix branch worktrees
```

### 5. MANDATORY: Ask Project Name

**⛔ ALWAYS ask to confirm project name:**

```yaml
AskUserQuestion:
  - header: "Project"
    question: "Project name? (detected: {DETECTED_NAME})"
    options:
      - label: "Use detected name (Recommended)"
        description: "Project: {DETECTED_NAME}"
      - label: "Enter custom name"
        description: "Specify a different project name"
    multiSelect: false
```

### 6. Ask Worktree Mode

```yaml
AskUserQuestion:
  - header: "Worktrees"
    question: "How should worktrees be organized?"
    options:
      - label: "Organized (Recommended)"
        description: "01-Main, 02-Develop, features/, releases/, hotfixes/"
      - label: "Simple"
        description: "main/, develop/, features/, releases/, hotfixes/"
      - label: "Disabled"
        description: "No worktrees, use git checkout (not recommended)"
    multiSelect: false
```

### 7. Create Directory Structure

**⛔ Create the COMPLETE structure:**

```bash
cd "{ROOT_FOLDER}"

# Create bare repository if not exists
if [ ! -d ".bare" ]; then
  git clone --bare "{REPO_URL}" .bare
  # Configure bare repo for worktrees
  cd .bare
  git config core.bare false
  git config core.worktree ..
  cd ..
fi

# Create worktree directories
mkdir -p features releases hotfixes

# Organized mode: numbered main folders
if [ "$WORKTREE_MODE" = "organized" ]; then
  # Create main worktree
  [ ! -d "01-Main" ] && git worktree add 01-Main main

  # Create develop worktree
  [ ! -d "02-Develop" ] && git worktree add 02-Develop develop
fi

# Simple mode: named folders
if [ "$WORKTREE_MODE" = "simple" ]; then
  [ ! -d "main" ] && git worktree add main main
  [ ! -d "develop" ] && git worktree add develop develop
fi

# Create gitflow config directory (in develop worktree)
DEVELOP_PATH=$([ "$WORKTREE_MODE" = "organized" ] && echo "02-Develop" || echo "develop")
mkdir -p "$DEVELOP_PATH/.claude/gitflow/{plans,logs,cache,backup}"
```

### 8. Create Configuration

**Write `.claude/gitflow/config.json` in develop worktree:**

```json
{
  "version": "2.0.0",
  "repository": {
    "name": "{PROJECT_NAME}",
    "rootFolder": "{ROOT_FOLDER}",
    "remoteUrl": "{REPO_URL}"
  },
  "git": {
    "provider": "{GIT_PROVIDER}",
    "branches": {
      "main": "main",
      "develop": "develop"
    },
    "prefixes": {
      "feature": "feature/",
      "release": "release/",
      "hotfix": "hotfix/"
    }
  },
  "worktrees": {
    "enabled": true,
    "mode": "{WORKTREE_MODE}",
    "structure": {
      "main": "{ROOT_FOLDER}/01-Main",
      "develop": "{ROOT_FOLDER}/02-Develop",
      "features": "{ROOT_FOLDER}/features",
      "releases": "{ROOT_FOLDER}/releases",
      "hotfixes": "{ROOT_FOLDER}/hotfixes"
    }
  },
  "versioning": {
    "strategy": "semver",
    "current": "{VERSION}",
    "tagPrefix": "v",
    "sources": ["csproj", "package.json", "VERSION"]
  },
  "efcore": {
    "enabled": true,
    "validateOnCommit": true,
    "blockDestructive": true
  },
  "workflow": {
    "push": {
      "afterCommit": "worktree"
    },
    "pr": {
      "autoLabels": true,
      "requireReview": true
    }
  }
}
```

### 9. Detect Version

```bash
# In develop worktree
cd "$DEVELOP_PATH"

# Priority: csproj > Directory.Build.props > package.json > VERSION > tag
VERSION=$(grep -oP '<Version>\K[^<]+' *.csproj 2>/dev/null | head -1)
[ -z "$VERSION" ] && VERSION=$(grep -oP '<Version>\K[^<]+' Directory.Build.props 2>/dev/null)
[ -z "$VERSION" ] && VERSION=$(grep -oP '"version":\s*"\K[^"]+' package.json 2>/dev/null)
[ -z "$VERSION" ] && VERSION=$(cat VERSION 2>/dev/null)
[ -z "$VERSION" ] && VERSION=$(git describe --tags --abbrev=0 2>/dev/null | sed 's/^v//')
[ -z "$VERSION" ] && VERSION="0.1.0"
```

### 10. Summary

```
╔══════════════════════════════════════════════════════════════════╗
║                    GITFLOW INITIALIZED                            ║
╠══════════════════════════════════════════════════════════════════╣
║  Project:    {PROJECT_NAME}                                       ║
║  Root:       {ROOT_FOLDER}                                        ║
║  Remote:     {REPO_URL}                                           ║
║  Provider:   {GIT_PROVIDER}                                       ║
║  Version:    {VERSION}                                            ║
╠══════════════════════════════════════════════════════════════════╣
║  Structure:                                                        ║
║  ├── .bare/           (git bare repository)                       ║
║  ├── 01-Main/         (main branch)         ✅                    ║
║  ├── 02-Develop/      (develop branch)      ✅                    ║
║  ├── features/        (feature worktrees)   ✅                    ║
║  ├── releases/        (release worktrees)   ✅                    ║
║  └── hotfixes/        (hotfix worktrees)    ✅                    ║
╠══════════════════════════════════════════════════════════════════╣
║  Config: 02-Develop/.claude/gitflow/config.json                   ║
╠══════════════════════════════════════════════════════════════════╣
║  NEXT: cd 02-Develop && /gitflow -f <feature-name>                ║
╚══════════════════════════════════════════════════════════════════╝
```

---

## VALIDATION CHECKLIST:

Before completing init, verify:

- [ ] Repository URL confirmed by user
- [ ] Root folder confirmed by user
- [ ] Project name confirmed by user
- [ ] .bare/ directory exists
- [ ] 01-Main/ worktree exists and is on main branch
- [ ] 02-Develop/ worktree exists and is on develop branch
- [ ] features/, releases/, hotfixes/ directories exist
- [ ] .claude/gitflow/config.json created in develop worktree

---

## ERROR HANDLING:

| Error | Solution |
|-------|----------|
| Clone fails | Check URL, network, credentials |
| Worktree exists | Skip or offer to recreate |
| Permission denied | Check folder permissions |
| Branch missing | Create from default branch |

---

## SUCCESS CRITERIA:

- ✅ All 3 mandatory questions asked
- ✅ Complete folder structure created
- ✅ Both main and develop worktrees functional
- ✅ Configuration file created
- ✅ User can navigate to 02-Develop and start working

## NEXT STEP:

Init is standalone. User should `cd 02-Develop` then run `/gitflow -f <feature-name>`.
