| 1 |
#!/usr/bin/env bash |
| 2 |
# Create a branch name from an issue number and title. |
| 3 |
# Usage: make-issue-branch.sh <issue-number> "<issue-title>" [prefix] [base-ref] |
| 4 |
# prefix: fix (default), enhancement, test |
| 5 |
# |
| 6 |
# base-ref: pass an explicit ref such as "origin/develop" when running inside a |
| 7 |
# worktree or after a git fetch to avoid inheriting unexpected commits. |
| 8 |
# Defaults to "origin/develop" when omitted. |
| 9 |
set -euo pipefail |
| 10 |
|
| 11 |
# Required arguments. |
| 12 |
ISSUE_NUMBER="${1:?issue number required}" |
| 13 |
TITLE="${2:?issue title required}" |
| 14 |
|
| 15 |
# Optional prefix — determines branch type per naming convention. |
| 16 |
PREFIX="${3:-fix}" |
| 17 |
|
| 18 |
# Validate prefix. |
| 19 |
case "$PREFIX" in |
| 20 |
fix|enhancement|test) ;; |
| 21 |
*) PREFIX="fix" ;; |
| 22 |
esac |
| 23 |
|
| 24 |
# Optional base ref — defaults to origin/develop (Imagify base branch). |
| 25 |
BASE_REF="${4:-origin/develop}" |
| 26 |
|
| 27 |
# Build a short, URL-safe slug from the title (first 4 words max). |
| 28 |
SLUG="$(printf '%s' "$TITLE" \ |
| 29 |
| tr '[:upper:]' '[:lower:]' \ |
| 30 |
| sed -E 's/[^a-z0-9]+/-/g; s/^-+//; s/-+$//' \ |
| 31 |
| cut -d- -f1-4 \ |
| 32 |
| sed 's/-*$//')" |
| 33 |
|
| 34 |
# Branch naming convention: <prefix>/<issue>-<slug> |
| 35 |
BRANCH="${PREFIX}/${ISSUE_NUMBER}-${SLUG}" |
| 36 |
|
| 37 |
# Create and switch to the branch (idempotent — safe if branch already exists). |
| 38 |
if git show-ref --verify --quiet "refs/heads/${BRANCH}"; then |
| 39 |
git checkout "$BRANCH" |
| 40 |
elif [[ -n "$BASE_REF" ]]; then |
| 41 |
git checkout -b "$BRANCH" "$BASE_REF" |
| 42 |
else |
| 43 |
git checkout -b "$BRANCH" |
| 44 |
fi |
| 45 |
echo "$BRANCH" |
| 46 |
|