Skip to content

Commit 952a91d

Browse files
Dynamically exclude "-by" trailers in regex
This commit refactors trailer handling by dynamically extracting all "-by" suffix trailers. It constructs 'TRAILERS_BY_REGEX' to match these trailers automatically.Also, the awk script is updated to use 'TRAILERS_BY_REGEX'. Co-authored-by: EricccTaiwan <[email protected]> Change-Id: I55166d2ecad85e9e3371ff354cefb2c648b49919
1 parent af3bf61 commit 952a91d

File tree

1 file changed

+9
-25
lines changed

1 file changed

+9
-25
lines changed

scripts/commit-msg.hook

Lines changed: 9 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -154,29 +154,23 @@ build_commit_trailer_regex() {
154154
# Read custom trailer keys from git config and add them either to specials or trailers.
155155
# This loop reads lines matching 'trailer.*.key'.
156156
while read -r _ key; do
157-
# Skip if key already exists in trailers or specials.
157+
if [[ "$key" =~ -by$ ]]; then
158+
trailers_by+=("$key")
159+
continue
160+
fi
161+
158162
for each in "${trailers[@]}" "${specials[@]}"; do
159-
if [ "$key" = "$each" ]; then
160-
continue 2
161-
fi
163+
[[ "$key" == "$each" ]] && continue 2
162164
done
163-
# If key ends with a separator character, add to specials; otherwise, to trailers.
165+
164166
if [[ $key =~ [${separators}]$ ]]; then
165167
specials+=("$key")
166168
else
167169
trailers+=("$key")
168170
fi
169171
done < <(git config --get-regexp 'trailer.*.key')
170172

171-
# Read custom trailer keys again into the 'keys' array (if needed).
172-
while IFS=. read -r _ key _; do
173-
for each in "${keys[@]}"; do
174-
if [ "$key" = "$each" ]; then
175-
continue 2
176-
fi
177-
done
178-
keys+=("$key")
179-
done < <(git config --get-regexp 'trailer.*.key')
173+
TRAILERS_BY_REGEX="^($(IFS='|'; echo "${trailers_by[*]}")):"
180174

181175
# Begin constructing the regex.
182176
TRAILER_REGEX='^('
@@ -209,16 +203,6 @@ build_commit_trailer_regex() {
209203
TRAILER_REGEX="${TRAILER_REGEX%|})"
210204
fi
211205

212-
# Append additional keys.
213-
if ((${#keys[@]} > 0)); then
214-
TRAILER_REGEX+='|(('
215-
for each in "${keys[@]}"; do
216-
TRAILER_REGEX+="$each|"
217-
done
218-
# Use the second character of separators (if available) as a separator for keys.
219-
TRAILER_REGEX="${TRAILER_REGEX%|})[${separators:1:1}[:blank:]])"
220-
fi
221-
222206
# End the regex.
223207
TRAILER_REGEX+=")"
224208
}
@@ -618,7 +602,7 @@ add_change_id() {
618602
other_footer = ""
619603
620604
for (line = 1; line <= numlines; line++) {
621-
if (match(tolower(footer[line]), /^(signed-off-by|acked-by|co-authored-by|reviewed-by|tested-by|suggested-by|reported-by):/)) {
605+
if (match(tolower(footer[line]), TRAILERS_BY_REGEX)) {
622606
trailers = trailers footer[line] "\n"
623607
} else {
624608
other_footer = other_footer footer[line] "\n"

0 commit comments

Comments
 (0)