From ac854bf8c79293e8153f8e568532933d1c9b7a19 Mon Sep 17 00:00:00 2001 From: Oliver Davies Date: Sat, 26 Aug 2023 22:19:35 +0100 Subject: [PATCH] feat(git): automatically add `refs` to commit ...messages Add a `prepare-commit-msg` hook to automaically add `Refs` values, such as issue IDs or commit SHAs to a Git commit message body by replacing an `ISSUE_ID` placeholder that can be added to a commit message template. --- templates/git-hooks/prepare-commit-msg.twig | 25 +++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100755 templates/git-hooks/prepare-commit-msg.twig diff --git a/templates/git-hooks/prepare-commit-msg.twig b/templates/git-hooks/prepare-commit-msg.twig new file mode 100755 index 0000000..70d73bb --- /dev/null +++ b/templates/git-hooks/prepare-commit-msg.twig @@ -0,0 +1,25 @@ +#!/usr/bin/env bash + +# {{ managedText }} + +set -euo pipefail + +# Load the issue ID from an `.issue-id` file within the project and replace the +# `ISSUE_ID` placeholder within a Git commit message. +# +# For example, running `echo "OD-123" > .issue-id` will add `Refs: OD-123` to +# the commit message. +# +# This also works with multiple issue IDs in the same string, e.g. +# "OD-123 OD-456". + +PROJECT_DIR=$(git rev-parse --show-toplevel) # Get the root directory of the repo +ISSUE_FILE="$PROJECT_DIR/.issue-id" + +if [ -f "${ISSUE_FILE}" ]; then + ISSUE_ID=$(cat "${ISSUE_FILE}" | sed 's/ /, /g') + + if [ -n "${ISSUE_ID}" ]; then + sed -i.bak "s/# ISSUE_ID/Refs: $ISSUE_ID/" "$1" + fi +fi