From c586b8cf2cc885821fe7f794a4902db6c434790d Mon Sep 17 00:00:00 2001 From: pyr0ball Date: Thu, 21 May 2026 08:18:18 -0700 Subject: [PATCH] feat: add prepare-commit-msg hook to auto-append ticket links Detects ticket number from branch name (patterns: feat/42-desc, fix/42-desc, 42-desc, issue-42-desc) and appends the full Forgejo issue URL to the commit body if not already present. Closes: https://git.opensourcesolarpunk.com/Circuit-Forge/circuitforge-infra/issues/20 --- hooks/prepare-commit-msg | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100755 hooks/prepare-commit-msg diff --git a/hooks/prepare-commit-msg b/hooks/prepare-commit-msg new file mode 100755 index 0000000..59c3732 --- /dev/null +++ b/hooks/prepare-commit-msg @@ -0,0 +1,39 @@ +#!/usr/bin/env bash +# prepare-commit-msg — auto-appends Forgejo ticket link when branch name contains a ticket number +# +# Branch name patterns detected: +# feat/42-description fix/42-desc 42-description issue-42-desc +# +# Appends to commit body: +# Closes: https://git.opensourcesolarpunk.com/Circuit-Forge//issues/ + +set -euo pipefail + +MSG_FILE="$1" +SOURCE="${2:-}" + +# Don't modify merge commits, squash commits, or amends that already have a body +[[ "$SOURCE" == "merge" || "$SOURCE" == "squash" ]] && exit 0 + +# Get the current branch name +BRANCH=$(git symbolic-ref --short HEAD 2>/dev/null || echo "") +[[ -z "$BRANCH" ]] && exit 0 + +# Extract ticket number — match digits after type-prefix slash or at start, before a dash/underscore +TICKET=$(echo "$BRANCH" | grep -oE '(^|/)(issue-)?([0-9]+)[-_]' | grep -oE '[0-9]+' | head -1 || true) +[[ -z "$TICKET" ]] && exit 0 + +# Derive repo name from origin remote +REMOTE_URL=$(git remote get-url origin 2>/dev/null || echo "") +REPO=$(echo "$REMOTE_URL" | sed -E 's|.*Circuit-Forge/||;s|\.git$||') +[[ -z "$REPO" ]] && exit 0 + +TICKET_URL="https://git.opensourcesolarpunk.com/Circuit-Forge/${REPO}/issues/${TICKET}" + +# Skip if the URL (or a bare #N ref) is already in the message +if grep -qF "$TICKET_URL" "$MSG_FILE" || grep -qE "^(Closes|Fixes|Refs|See):.*#${TICKET}" "$MSG_FILE"; then + exit 0 +fi + +# Append to message body (separated by blank line from subject) +printf "\nCloses: %s\n" "$TICKET_URL" >> "$MSG_FILE"