Skip to content

Commit 0155c71

Browse files
authored
Merge pull request #251 from charliechiou/fix-git-hook
Ensure all trailers remain after Change-Id
2 parents f4b4bc2 + d83b210 commit 0155c71

File tree

1 file changed

+32
-45
lines changed

1 file changed

+32
-45
lines changed

scripts/commit-msg.hook

+32-45
Original file line numberDiff line numberDiff line change
@@ -154,29 +154,31 @@ 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+
# Possible trailers :
174+
# - Acked-by
175+
# - Co-authored-by
176+
# - Reported-by
177+
# - Reviewed-by
178+
# - Signed-off-by
179+
# - Suggested-by
180+
# - Tested-by
181+
TRAILERS_BY_REGEX="^($(IFS='|'; echo "${trailers_by[*]}")):"
180182

181183
# Begin constructing the regex.
182184
TRAILER_REGEX='^('
@@ -209,16 +211,6 @@ build_commit_trailer_regex() {
209211
TRAILER_REGEX="${TRAILER_REGEX%|})"
210212
fi
211213

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-
222214
# End the regex.
223215
TRAILER_REGEX+=")"
224216
}
@@ -611,35 +603,30 @@ add_change_id() {
611603
print lines "\n"
612604
lines = ""
613605
}
614-
changeIdAfter = "^(" tolower("'"$CHANGE_ID_AFTER"'") "):"
606+
615607
numlines = split(lines, footer, "\n")
616608
617-
# Find the last line that starts with a comment character.
618-
coauthorIndex = 0
619-
for (line = 1; line <= numlines; line++) {
620-
if (match(tolower(footer[line]), /^co-authored-by:/)) {
621-
coauthorIndex = line
622-
}
623-
}
609+
trailers = ""
610+
other_footer = ""
624611
625612
for (line = 1; line <= numlines; line++) {
626-
if (unprinted && match(tolower(footer[line]), changeIdAfter) != 1) {
627-
# If the Change-Id is the first line in the footer, print it first.
628-
if (coauthorIndex == 0 || line > coauthorIndex) {
629-
print "Change-Id: I'"$id"'"
630-
unprinted = 0
631-
}
613+
if (match(tolower(footer[line]), TRAILERS_BY_REGEX)) {
614+
trailers = trailers footer[line] "\n"
615+
} else {
616+
other_footer = other_footer footer[line] "\n"
632617
}
633-
print footer[line]
618+
}
634619
635-
if(line == coauthorIndex && unprinted) {
636-
print "Change-Id: I'"$id"'"
637-
unprinted = 0
638-
}
620+
if (other_footer != "") {
621+
printf "%s", other_footer
639622
}
640-
if (unprinted) {
641-
print "Change-Id: I'"$id"'"
623+
624+
if (trailers != "") {
625+
printf "%s", trailers
642626
}
627+
628+
printf "Change-Id: I'"$id"'\n"
629+
643630
}' "$MSG" > "$T" && mv "$T" "$MSG" || rm -f "$T"
644631
}
645632

0 commit comments

Comments
 (0)