# GitFlow - Shared Functions

> **Usage:** This file contains reusable functions. Reference with `{{REF:section_name}}` in other files.

---

## DETECT_PROVIDER

Detects GitHub or Azure DevOps from remote URL.

```bash
detect_git_provider() {
  REMOTE_URL=$(git remote get-url origin 2>/dev/null || echo "")

  if [[ "$REMOTE_URL" == *"dev.azure.com"* ]] || [[ "$REMOTE_URL" == *"visualstudio.com"* ]]; then
    GIT_PROVIDER="azuredevops"
    [[ "$REMOTE_URL" =~ dev\.azure\.com/([^/]+)/([^/]+)/_git/([^/]+) ]] && {
      AZURE_ORG="${BASH_REMATCH[1]}"
      AZURE_PROJECT="${BASH_REMATCH[2]}"
      AZURE_REPO="${BASH_REMATCH[3]}"
    }
  elif [[ "$REMOTE_URL" == *"github.com"* ]]; then
    GIT_PROVIDER="github"
  else
    GIT_PROVIDER="unknown"
  fi
}
```

---

## NORMALIZE_NAME

Normalizes a branch name (accents, spaces, special characters).

```bash
normalize_branch_name() {
  local input="$1" max_length="${2:-50}"
  echo "$input" | tr '[:upper:]' '[:lower:]' | \
    sed 'y/àâäéèêëïîôùûüçœæ/aaaeeeeiioouucoa/' | \
    sed "s/[ _']/-/g" | sed 's/[^a-z0-9-]//g' | \
    sed 's/--*/-/g' | sed 's/^-//;s/-$//' | cut -c1-$max_length | sed 's/-$//'
}
```

---

## DETECT_WORKTREE_MODE

Detects worktree mode (organized, adjacent, none).

```bash
detect_worktree_mode() {
  if [ -d "../features" ] && [ -d "../releases" ] && [ -d "../hotfixes" ]; then
    WORKTREE_MODE="organized"
  elif [ -d "../01-Main" ] && [ -d "../02-Develop" ]; then
    WORKTREE_MODE="organized"
  elif [ -d "../worktrees" ]; then
    WORKTREE_MODE="adjacent"
  else
    CONFIG_FILE=".claude/gitflow/config.json"
    [ -f "$CONFIG_FILE" ] && WORKTREE_MODE=$(grep -oP '"mode":\s*"\K[^"]+' "$CONFIG_FILE" | head -1)
    WORKTREE_MODE=${WORKTREE_MODE:-adjacent}
  fi
}
```

---

## CLEANUP_WORKTREE

Cleans up a worktree for a given branch.

```bash
cleanup_worktree_for_branch() {
  local BRANCH=$1 WORKTREE_BASE="../worktrees" WORKTREE_PATH=""

  case "$BRANCH" in
    feature/*) WORKTREE_PATH="$WORKTREE_BASE/features/${BRANCH#feature/}" ;;
    release/*) WORKTREE_PATH="$WORKTREE_BASE/releases/${BRANCH#release/}" ;;
    hotfix/*)  WORKTREE_PATH="$WORKTREE_BASE/hotfixes/${BRANCH#hotfix/}" ;;
  esac

  # Organized mode
  [ -d "../features" ] && case "$BRANCH" in
    feature/*) WORKTREE_PATH="../features/${BRANCH#feature/}" ;;
    release/*) WORKTREE_PATH="../releases/${BRANCH#release/}" ;;
    hotfix/*)  WORKTREE_PATH="../hotfixes/${BRANCH#hotfix/}" ;;
  esac

  [ -d "$WORKTREE_PATH" ] && {
    git worktree remove "$WORKTREE_PATH" --force 2>/dev/null || rm -rf "$WORKTREE_PATH"
    git worktree prune
    echo "✓ Worktree cleaned: $WORKTREE_PATH"
  }
}
```

---

## SYNC_REMOTE

Syncs with remote and displays status.

```bash
sync_with_remote() {
  git fetch --all --quiet
  local BRANCH=$(git branch --show-current)
  local AHEAD=$(git rev-list --count origin/$BRANCH..HEAD 2>/dev/null || echo "0")
  local BEHIND=$(git rev-list --count HEAD..origin/$BRANCH 2>/dev/null || echo "0")

  [ "$BEHIND" -gt 0 ] && echo "⚠️ $BRANCH: $BEHIND commit(s) behind"
  [ "$AHEAD" -gt 0 ] && echo "ℹ️ $BRANCH: $AHEAD commit(s) not pushed"
}
```

---

## READ_GITFLOW_CONFIG

Reads GitFlow configuration.

```bash
read_gitflow_config() {
  local CONFIG_FILE=".claude/gitflow/config.json"
  [ ! -f "$CONFIG_FILE" ] && return 1

  # Exported variables
  GF_LANG=$(grep -oP '"code":\s*"\K[^"]+' "$CONFIG_FILE" | head -1)
  GF_WORKTREE_MODE=$(grep -oP '"mode":\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)

  GF_LANG=${GF_LANG:-en}
  GF_WORKTREE_MODE=${GF_WORKTREE_MODE:-adjacent}
  GF_MAIN_BRANCH=${GF_MAIN_BRANCH:-main}
  GF_DEVELOP_BRANCH=${GF_DEVELOP_BRANCH:-develop}
}
```

---

## CONFIG_TEMPLATE

GitFlow configuration JSON template (minimal version).

```json
{
  "version": "1.3.0",
  "branches": { "main": "main", "develop": "develop" },
  "prefixes": { "feature": "feature/", "release": "release/", "hotfix": "hotfix/" },
  "worktrees": { "mode": "organized" },
  "language": { "code": "en" }
}
```

**worktrees.mode options:**
- `organized`: `../features/`, `../releases/`, `../hotfixes/`
- `adjacent`: `../worktrees/{type}/`
- `disabled`: No worktrees

---

## PR_COMMANDS

PR commands by provider.

### GitHub
```bash
gh pr create --title "$TITLE" --body "$BODY" --base "$BASE"
gh pr view $NUMBER --json state,mergeable,reviews
gh pr merge $NUMBER --squash --delete-branch
```

### Azure DevOps
```bash
az repos pr create --title "$TITLE" --description "$BODY" --source-branch "$SOURCE" --target-branch "$BASE"
az repos pr show --id $NUMBER --query "{status:status,mergeStatus:mergeStatus}"
az repos pr update --id $NUMBER --status completed --squash true --delete-source-branch true
```

---

## OUTPUT_FORMAT

Standard output format (replaces ASCII art).

```bash
output_header() { echo -e "\n## $1\n"; }
output_section() { echo "### $1"; }
output_item() { echo "- $1"; }
output_success() { echo "✓ $1"; }
output_warning() { echo "⚠️ $1"; }
output_error() { echo "❌ $1"; }
output_next() { echo "→ Next: $1"; }
```
