| 1 |
#!/bin/sh |
| 2 |
|
| 3 |
set -e |
| 4 |
|
| 5 |
debug() { |
| 6 |
if [ "$GIT_TRAILER_DEBUG" = 1 ]; then |
| 7 |
"$@" >&2 |
| 8 |
fi |
| 9 |
} |
| 10 |
|
| 11 |
trailer_order=" |
| 12 |
Closes: |
| 13 |
Fixes: |
| 14 |
Implements: |
| 15 |
References: |
| 16 |
Link: |
| 17 |
Changelog-added: |
| 18 |
Changelog-fixed: |
| 19 |
Changelog-changed: |
| 20 |
Changelog-deprecated: |
| 21 |
Cc: |
| 22 |
Suggested-by: |
| 23 |
Requested-by: |
| 24 |
Reported-by: |
| 25 |
Co-authored-by: |
| 26 |
Signed-off-by: |
| 27 |
Tested-by: |
| 28 |
Reviewed-by: |
| 29 |
Acked-by: |
| 30 |
" |
| 31 |
file=${1?file} |
| 32 |
tmp=$(mktemp) |
| 33 |
trap "rm -f $tmp" EXIT |
| 34 |
|
| 35 |
# Read unfolded trailers and normalize case. |
| 36 |
git interpret-trailers --parse --trim-empty "$file" | |
| 37 |
while read -r key value; do |
| 38 |
# Force title case on trailer key. |
| 39 |
first_letter=$(echo "$key" | sed 's/^\(.\).*/\1/' | tr '[:lower:]' '[:upper:]') |
| 40 |
other_letters=$(echo "$key" | sed 's/^.\(.*\)/\1/' | tr '[:upper:]' '[:lower:]') |
| 41 |
key="$first_letter$other_letters" |
| 42 |
|
| 43 |
# Find sort order of this key. |
| 44 |
order=$(echo "$trailer_order" | grep -Fxn "$key" | sed -nE 's/^([0-9]+):.*/\1/p') |
| 45 |
if [ -z "$order" ]; then |
| 46 |
echo "warning: unknown trailer '$key'" >&2 |
| 47 |
# Unknown trailers are always first. |
| 48 |
order="0" |
| 49 |
fi |
| 50 |
|
| 51 |
echo "$order $key $value" |
| 52 |
done | |
| 53 |
# Sort trailers according to their numeric order, trim the numeric order. |
| 54 |
LC_ALL=C sort -n | sed -E 's/^[0-9]+ //' > "$tmp" |
| 55 |
|
| 56 |
debug echo ==== sanitized trailers ==== |
| 57 |
debug cat "$tmp" |
| 58 |
|
| 59 |
# Unfortunately, reordering trailers is not possible at the moment. Delete all |
| 60 |
# trailers first. The only way to do it is to force replace existing trailers |
| 61 |
# with empty values and trim empty trailers one by one. |
| 62 |
while read -r key value; do |
| 63 |
git interpret-trailers --in-place --if-exists=replace \ |
| 64 |
--trailer="$key " "$file" |
| 65 |
git interpret-trailers --in-place --trim-empty "$file" |
| 66 |
done < "$tmp" |
| 67 |
|
| 68 |
set -- |
| 69 |
while read -r trailer; do |
| 70 |
case "$trailer" in |
| 71 |
Changelog-*) |
| 72 |
# Wrap changelog entries indenting with a space. |
| 73 |
trailer=$(echo "$trailer" | fmt -w 72 | sed '2,$s/^/ /') |
| 74 |
;; |
| 75 |
esac |
| 76 |
set -- "$@" --trailer="$trailer" |
| 77 |
done < "$tmp" |
| 78 |
|
| 79 |
# Remove duplicate "key: value" trailers (e.g. duplicate signed-off-by). |
| 80 |
git interpret-trailers --in-place --if-exists=addIfDifferent "$@" "$file" |