# Worktree Creation and Validation

## Determine Worktree Path

**Read worktree mode from config (already loaded by read_gitflow_config):**
```bash
WORKTREE_MODE="$GF_WORKTREE_MODE"
```

## Worktree Path Selection (Organized Mode)

> **FORBIDDEN — Numbered directories:**
> NEVER create directories like `03-Release`, `04-Feature`, `05-Hotfix`.
> Only `01-Main` and `02-Develop` use numbers (they are permanent, created at init).
> All other worktrees MUST use the paths from `config.json → worktrees.structure`:
> - `$GF_FEATURES_PATH/{name}` (e.g., `features/my-feature`, NOT `04-Feature`)
> - `$GF_RELEASES_PATH/v{version}` (e.g., `releases/v2.15.0`, NOT `03-Release`)
> - `$GF_HOTFIXES_PATH/{name}` (e.g., `hotfixes/fix-bug`, NOT `05-Hotfix`)

```bash
case "$branch_type" in
  feature) WORKTREE_PATH="$GF_FEATURES_PATH/$BRANCH_NAME" ;;
  release) WORKTREE_PATH="$GF_RELEASES_PATH/v$VERSION" ;;
  hotfix)  WORKTREE_PATH="$GF_HOTFIXES_PATH/$BRANCH_NAME" ;;
esac

mkdir -p "$(dirname "$WORKTREE_PATH")"
git worktree add -b "$FULL_BRANCH" "$WORKTREE_PATH" "origin/$BASE_BRANCH"

# Convert worktree paths to relative format (cross-platform: Windows + WSL)
repair_worktree_paths "$(git rev-parse --git-common-dir)"

# POST-CREATION VALIDATION (BLOCKING)
# Detect if worktree was accidentally created with a numbered prefix
CREATED_PATH=$(git worktree list | grep "$FULL_BRANCH" | awk '{print $1}')
validate_worktree_path "$CREATED_PATH" || {
  echo "BLOCKING: Recreating worktree at correct path..."
  git worktree remove "$CREATED_PATH" --force
  git branch -D "$FULL_BRANCH" 2>/dev/null
  git worktree add -b "$FULL_BRANCH" "$WORKTREE_PATH" "origin/$BASE_BRANCH"
  repair_worktree_paths "$(git rev-parse --git-common-dir)"
}
```

## No-Worktree Mode

```bash
git checkout "$BASE_BRANCH" && git pull origin "$BASE_BRANCH"
git checkout -b "$FULL_BRANCH"
```
