diff options
author | Robin Jarry <robin@jarry.cc> | 2024-01-09 00:34:58 +0100 |
---|---|---|
committer | Robin Jarry <robin@jarry.cc> | 2024-01-17 11:52:51 +0100 |
commit | 4239f0d80204a2d1f7a2475adfd62b39cd86d075 (patch) | |
tree | 3a67e992dbf77fd8692c565558f25114551d3710 | |
parent | ec0f4a50cf777895ff970fff2900265c8357c2bc (diff) | |
download | aerc-4239f0d80204a2d1f7a2475adfd62b39cd86d075.tar.gz |
contrib: add commit-msg git hook
Add a script that deals with the proper formatting and ordering of git
trailers. Install it as commit-msg git hook so that everyone can benefit
from it.
Signed-off-by: Robin Jarry <robin@jarry.cc>
Tested-by: Bence Ferdinandy <bence@ferdinandy.com>
-rw-r--r-- | CONTRIBUTING.md | 1 | ||||
-rw-r--r-- | GNUmakefile | 2 | ||||
-rwxr-xr-x | contrib/commit-msg | 80 |
3 files changed, 83 insertions, 0 deletions
diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 9337a3c2..1a4d0bd2 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -76,6 +76,7 @@ defaults: git config format.notes true git config notes.rewriteRef refs/notes/commits git config notes.rewriteMode concatenate + ln -s ../../contrib/commit-msg .git/hooks/commit-msg + ln -s ../../contrib/sendemail-validate .git/hooks/sendemail-validate + git config sendemail.validate true diff --git a/GNUmakefile b/GNUmakefile index cd86c096..274f2c6d 100644 --- a/GNUmakefile +++ b/GNUmakefile @@ -114,6 +114,8 @@ gitconfig: git config notes.rewriteRef refs/notes/commits git config notes.rewriteMode concatenate @mkdir -p .git/hooks + @rm -f .git/hooks/commit-msg* + ln -s ../../contrib/commit-msg .git/hooks/commit-msg @rm -f .git/hooks/sendemail-validate* @if grep -q GIT_SENDEMAIL_FILE_COUNTER `git --exec-path`/git-send-email 2>/dev/null; then \ set -xe; \ diff --git a/contrib/commit-msg b/contrib/commit-msg new file mode 100755 index 00000000..add67177 --- /dev/null +++ b/contrib/commit-msg @@ -0,0 +1,80 @@ +#!/bin/sh + +set -e + +debug() { + if [ "$GIT_TRAILER_DEBUG" = 1 ]; then + "$@" >&2 + fi +} + +trailer_order=" +Closes: +Fixes: +Implements: +References: +Link: +Changelog-added: +Changelog-fixed: +Changelog-changed: +Changelog-deprecated: +Cc: +Suggested-by: +Requested-by: +Reported-by: +Co-authored-by: +Signed-off-by: +Tested-by: +Reviewed-by: +Acked-by: +" +file=${1?file} +tmp=$(mktemp) +trap "rm -f $tmp" EXIT + +# Read unfolded trailers and normalize case. +git interpret-trailers --parse --trim-empty "$file" | +while read -r key value; do + # Force title case on trailer key. + first_letter=$(echo "$key" | sed 's/^\(.\).*/\1/' | tr '[:lower:]' '[:upper:]') + other_letters=$(echo "$key" | sed 's/^.\(.*\)/\1/' | tr '[:upper:]' '[:lower:]') + key="$first_letter$other_letters" + + # Find sort order of this key. + order=$(echo "$trailer_order" | grep -Fxn "$key" | sed -n 's/^\([0-9]\+\):.*/\1/p') + if [ -z "$order" ]; then + echo "warning: unknown trailer '$key'" >&2 + # Unknown trailers are always first. + order="0" + fi + + echo "$order $key $value" +done | +# Sort trailers according to their numeric order, trim the numeric order. +LC_ALL=C sort -n | sed 's/^[0-9]\+ //' > "$tmp" + +debug echo ==== sanitized trailers ==== +debug cat "$tmp" + +# Unfortunately, reordering trailers is not possible at the moment. Delete all +# trailers first. The only way to do it is to force replace existing trailers +# with empty values and trim empty trailers one by one. +while read -r key value; do + git interpret-trailers --in-place --if-exists=replace \ + --trailer="$key " "$file" + git interpret-trailers --in-place --trim-empty "$file" +done < "$tmp" + +set -- +while read -r trailer; do + case "$trailer" in + Changelog-*) + # Wrap changelog entries indenting with a space. + trailer=$(echo "$trailer" | fmt -w 72 | sed '2,$s/^/ /') + ;; + esac + set -- "$@" --trailer="$trailer" +done < "$tmp" + +# Remove duplicate "key: value" trailers (e.g. duplicate signed-off-by). +git interpret-trailers --in-place --if-exists=addIfDifferent "$@" "$file" |