#!/usr/bin/env bash # # Create a local branch for an issue. Branch name: issue/ (safe, short). # Base branch defaults to "test"; ensure it is up to date (fetch + reset to origin/base). # Usage: ./create-branch-for-issue.sh [base_branch] # set -euo pipefail GITEA_ISSUES_DIR="${GITEA_ISSUES_DIR:-$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)}" # shellcheck source=lib.sh source "${GITEA_ISSUES_DIR}/lib.sh" if [[ $# -lt 1 ]]; then log_err "Usage: $0 [base_branch]" exit 1 fi ISSUE_NUM="$1" BASE="${2:-test}" require_git_root || exit 1 if git show-ref --quiet "refs/heads/issue/${ISSUE_NUM}"; then log_info "Branch issue/${ISSUE_NUM} already exists. Checking it out." git checkout "issue/${ISSUE_NUM}" echo "issue/${ISSUE_NUM}" exit 0 fi if ! git show-ref --quiet "refs/heads/${BASE}"; then log_err "Base branch ${BASE} does not exist locally." exit 1 fi git fetch origin if git show-ref --quiet "refs/remotes/origin/${BASE}"; then git checkout "${BASE}" git reset --hard "origin/${BASE}" else git checkout "${BASE}" fi git checkout -b "issue/${ISSUE_NUM}" log_info "Created and checked out branch issue/${ISSUE_NUM} from ${BASE}." echo "issue/${ISSUE_NUM}"