Skip to content

Commit 43a311d

Browse files
authored
Merge pull request #256 from charliechiou/fix-git-hook
Ensure proper footer sorting of footer
2 parents d64ab4b + 7381c74 commit 43a311d

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

scripts/commit-msg.hook

+21-3
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,16 @@ get_all_match_positions() {
130130
done <<< "$targets"
131131
}
132132

133+
KNOWN_TRAILERS_BY=(
134+
'Signed-off-by'
135+
'Reviewed-by'
136+
'Co-authored-by'
137+
'Acked-by'
138+
'Suggested-by'
139+
'Tested-by'
140+
'Reported-by'
141+
)
142+
133143
# Build regex for detecting commit trailers.
134144
build_commit_trailer_regex() {
135145
local -a keys specials standalones trailers
@@ -145,12 +155,17 @@ build_commit_trailer_regex() {
145155
'Signed-off-by' 'Suggested-by' 'Tested-by'
146156
)
147157

158+
trailers_test=(
159+
'CC' 'Change-Id' 'Closes'
160+
"${KNOWN_TRAILERS_BY[@]}"
161+
)
148162
# Standalone keys (those that do not require a value).
149163
standalones=(
150164
'(Doc|Upgrade|Security)Impact'
151165
"Git-Dch[$separators] (Ignore|Short|Full)"
152166
)
153167

168+
154169
# Read custom trailer keys from git config and add them either to specials or trailers.
155170
# This loop reads lines matching 'trailer.*.key'.
156171
while read -r _ key; do
@@ -170,6 +185,10 @@ build_commit_trailer_regex() {
170185
fi
171186
done < <(git config --get-regexp 'trailer.*.key')
172187

188+
if [[ ${#trailers_by[@]} -eq 0 ]]; then
189+
trailers_by=("${DEFAULT_TRAILERS_BY[@]}")
190+
fi
191+
173192
# Possible trailers :
174193
# - Acked-by
175194
# - Co-authored-by
@@ -178,8 +197,7 @@ build_commit_trailer_regex() {
178197
# - Signed-off-by
179198
# - Suggested-by
180199
# - Tested-by
181-
TRAILERS_BY_REGEX="^($(IFS='|'; echo "${trailers_by[*]}")):"
182-
200+
TRAILERS_BY_REGEX="/($(IFS='|'; echo "${trailers_by[*]}" | tr '[:upper:]' '[:lower:]'))/"
183201
# Begin constructing the regex.
184202
TRAILER_REGEX='^('
185203

@@ -610,7 +628,7 @@ add_change_id() {
610628
other_footer = ""
611629
612630
for (line = 1; line <= numlines; line++) {
613-
if (match(tolower(footer[line]), TRAILERS_BY_REGEX)) {
631+
if (match(tolower(footer[line]), '"$TRAILERS_BY_REGEX"')) {
614632
trailers = trailers footer[line] "\n"
615633
} else {
616634
other_footer = other_footer footer[line] "\n"

0 commit comments

Comments
 (0)