Skip to content

Commit 7381c74

Browse files
Ensure proper footer sorting of footer
When no trailer was configured in git configuration, TRAILERS_BY_REGEX remained empty. This caused the subsequent match condition to always evaluate to true, leading to incorrect footer sorting regardless of their content. To address this issue, the update ensures that when the configuration is empty, default terms are used to initialize TRAILERS_BY_REGEX. The sorting mechanism is then adjusted to prioritize other footers, configured trailers, and the change-id. Co-authored-by: EricccTaiwan <[email protected]> Change-Id: I6df5438b7ec6c85f5438f869b44cd14589c03339 Refactor commit trailer regex Refactor commit trailer regex by renaming default_trailers_by to DEFAULT_TRAILERS_BY to follow the naming convention for global variables.Then refine build_commit_trailer_regex by reusing the array. Co-authored-by: EricccTaiwan <[email protected]> Change-Id: I7c7e9367f59a6da0ff84605904d08343ad558f42
1 parent bd9c149 commit 7381c74

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)