Skip to content

Commit 83f60ba

Browse files
committed
hook: Ignore commit trailers in body
1 parent 178bfd8 commit 83f60ba

File tree

1 file changed

+85
-1
lines changed

1 file changed

+85
-1
lines changed

hook.sh

Lines changed: 85 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,88 @@ set_colors() {
3636
fi
3737
}
3838

39+
#
40+
# Build regex for detecting commit trailers
41+
#
42+
43+
build_commit_trailer_regex() {
44+
local -a keys specials standalones trailers
45+
local _ each key seperators
46+
seperators=$(git config --get trailer.separators || echo ':')
47+
trailers=(
48+
'Acked-by' 'Bug' 'CC'
49+
'Change-Id' 'Closes' 'Closes-Bug'
50+
'Co-Authored-By' 'Implements' 'Partial-Bug'
51+
'Related-Bug' 'Reported-by' 'Reviewed-by'
52+
'Signed-off-by' 'Suggested-by' 'Tested-by'
53+
'Thanks'
54+
)
55+
standalones=(
56+
'(Doc|Upgrade|Security)Impact'
57+
"Git-Dch[$seperators] (Ignore|Short|Full)"
58+
)
59+
60+
# read custom trailers
61+
while read -r _ key;do
62+
for each in "${trailers[@]}" "${specials[@]}";do
63+
test "$key" = "$each" && continue 2
64+
done
65+
if [[ $key =~ [$seperators]$ ]];then
66+
specials+=("$key")
67+
else
68+
trailers+=("$key")
69+
fi
70+
done < <(git config --get-regexp 'trailer.*.key')
71+
72+
# read custom trailer keys
73+
while IFS=. read -r _ key _;do
74+
for each in "${keys[@]}";do
75+
test "$key" = "$each" && continue 2
76+
done
77+
keys+=("$key")
78+
done < <(git config --get-regexp 'trailer.*.key')
79+
80+
# start
81+
TRAILER_REGEX='^('
82+
83+
# trailers
84+
if ((${#trailers[@]}>0));then
85+
TRAILER_REGEX+='(('
86+
for each in "${trailers[@]}";do
87+
TRAILER_REGEX+="$each|"
88+
done
89+
TRAILER_REGEX="${TRAILER_REGEX%|*})[$seperators][[:blank:]]*)"
90+
fi
91+
if ((${#standalones[@]}>0));then
92+
TRAILER_REGEX+='|(('
93+
for each in "${standalones[@]}";do
94+
TRAILER_REGEX+="$each|"
95+
done
96+
TRAILER_REGEX="${TRAILER_REGEX%|*})$)"
97+
fi
98+
99+
# specials
100+
if ((${#specials[@]}>0));then
101+
TRAILER_REGEX+='|('
102+
for each in "${specials[@]}";do
103+
TRAILER_REGEX+="$each|"
104+
done
105+
TRAILER_REGEX="${TRAILER_REGEX%|*})"
106+
fi
107+
108+
# keys
109+
if ((${#keys[@]}>0));then
110+
TRAILER_REGEX+='|(('
111+
for each in "${keys[@]}";do
112+
TRAILER_REGEX+="$each|"
113+
done
114+
TRAILER_REGEX="${TRAILER_REGEX%|*})[${seperators:1:1}[:blank:]])"
115+
fi
116+
117+
# end
118+
TRAILER_REGEX+=")"
119+
}
120+
39121
#
40122
# Set the hook editor, using the same approach as git.
41123
#
@@ -242,7 +324,7 @@ validate_commit_message() {
242324

243325
for i in "${!COMMIT_MSG_LINES[@]}"; do
244326
LINE_NUMBER=$((i+1))
245-
test "${#COMMIT_MSG_LINES[$i]}" -le 72 || [[ ${COMMIT_MSG_LINES[$i]} =~ $URL_REGEX ]]
327+
test "${#COMMIT_MSG_LINES[$i]}" -le 72 || [[ ${COMMIT_MSG_LINES[$i]} =~ $URL_REGEX ]] || [[ ${COMMIT_MSG_LINES[$i]} =~ $TRAILER_REGEX ]]
246328
test $? -eq 0 || add_warning $LINE_NUMBER "Wrap the body at 72 characters (${#COMMIT_MSG_LINES[$i]} chars)"
247329
done
248330

@@ -273,6 +355,8 @@ set_colors
273355

274356
set_editor
275357

358+
build_commit_trailer_regex
359+
276360
if tty >/dev/null 2>&1; then
277361
TTY=$(tty)
278362
else

0 commit comments

Comments
 (0)