Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 32 additions & 45 deletions scripts/commit-msg.hook
Original file line number Diff line number Diff line change
Expand Up @@ -154,29 +154,31 @@ build_commit_trailer_regex() {
# Read custom trailer keys from git config and add them either to specials or trailers.
# This loop reads lines matching 'trailer.*.key'.
while read -r _ key; do
# Skip if key already exists in trailers or specials.
if [[ "$key" =~ -by$ ]]; then
trailers_by+=("$key")
continue
fi

for each in "${trailers[@]}" "${specials[@]}"; do
if [ "$key" = "$each" ]; then
continue 2
fi
[[ "$key" == "$each" ]] && continue 2
done
# If key ends with a separator character, add to specials; otherwise, to trailers.

if [[ $key =~ [${separators}]$ ]]; then
specials+=("$key")
else
trailers+=("$key")
fi
done < <(git config --get-regexp 'trailer.*.key')

# Read custom trailer keys again into the 'keys' array (if needed).
while IFS=. read -r _ key _; do
for each in "${keys[@]}"; do
if [ "$key" = "$each" ]; then
continue 2
fi
done
keys+=("$key")
done < <(git config --get-regexp 'trailer.*.key')
# Possible trailers :
# - Acked-by
# - Co-authored-by
# - Reported-by
# - Reviewed-by
# - Signed-off-by
# - Suggested-by
# - Tested-by
TRAILERS_BY_REGEX="^($(IFS='|'; echo "${trailers_by[*]}")):"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add comments to make list for the possible trailers.


# Begin constructing the regex.
TRAILER_REGEX='^('
Expand Down Expand Up @@ -209,16 +211,6 @@ build_commit_trailer_regex() {
TRAILER_REGEX="${TRAILER_REGEX%|})"
fi

# Append additional keys.
if ((${#keys[@]} > 0)); then
TRAILER_REGEX+='|(('
for each in "${keys[@]}"; do
TRAILER_REGEX+="$each|"
done
# Use the second character of separators (if available) as a separator for keys.
TRAILER_REGEX="${TRAILER_REGEX%|})[${separators:1:1}[:blank:]])"
fi

# End the regex.
TRAILER_REGEX+=")"
}
Expand Down Expand Up @@ -611,35 +603,30 @@ add_change_id() {
print lines "\n"
lines = ""
}
changeIdAfter = "^(" tolower("'"$CHANGE_ID_AFTER"'") "):"

numlines = split(lines, footer, "\n")

# Find the last line that starts with a comment character.
coauthorIndex = 0
for (line = 1; line <= numlines; line++) {
if (match(tolower(footer[line]), /^co-authored-by:/)) {
coauthorIndex = line
}
}
trailers = ""
other_footer = ""

for (line = 1; line <= numlines; line++) {
if (unprinted && match(tolower(footer[line]), changeIdAfter) != 1) {
# If the Change-Id is the first line in the footer, print it first.
if (coauthorIndex == 0 || line > coauthorIndex) {
print "Change-Id: I'"$id"'"
unprinted = 0
}
if (match(tolower(footer[line]), TRAILERS_BY_REGEX)) {
trailers = trailers footer[line] "\n"
} else {
other_footer = other_footer footer[line] "\n"
}
print footer[line]
}

if(line == coauthorIndex && unprinted) {
print "Change-Id: I'"$id"'"
unprinted = 0
}
if (other_footer != "") {
printf "%s", other_footer
}
if (unprinted) {
print "Change-Id: I'"$id"'"

if (trailers != "") {
printf "%s", trailers
}

printf "Change-Id: I'"$id"'\n"

}' "$MSG" > "$T" && mv "$T" "$MSG" || rm -f "$T"
}

Expand Down
Loading