Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ignore autogenerated trailers in commit message #38

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
86 changes: 85 additions & 1 deletion hook.sh
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,88 @@ set_colors() {
fi
}

#
# Build regex for detecting commit trailers
#

build_commit_trailer_regex() {
local -a keys specials standalones trailers
local _ each key seperators
seperators=$(git config --get trailer.separators || echo ':')
trailers=(
'Acked-by' 'Bug' 'CC'
'Change-Id' 'Closes' 'Closes-Bug'
'Co-Authored-By' 'Implements' 'Partial-Bug'
'Related-Bug' 'Reported-by' 'Reviewed-by'
'Signed-off-by' 'Suggested-by' 'Tested-by'
'Thanks'
)
standalones=(
'(Doc|Upgrade|Security)Impact'
"Git-Dch[$seperators] (Ignore|Short|Full)"
)

# read custom trailers
while read -r _ key;do
for each in "${trailers[@]}" "${specials[@]}";do
test "$key" = "$each" && continue 2
done
if [[ $key =~ [$seperators]$ ]];then
specials+=("$key")
else
trailers+=("$key")
fi
done < <(git config --get-regexp 'trailer.*.key')

# read custom trailer keys
while IFS=. read -r _ key _;do
for each in "${keys[@]}";do
test "$key" = "$each" && continue 2
done
keys+=("$key")
done < <(git config --get-regexp 'trailer.*.key')

# start
TRAILER_REGEX='^('

# trailers
if ((${#trailers[@]}>0));then
TRAILER_REGEX+='(('
for each in "${trailers[@]}";do
TRAILER_REGEX+="$each|"
done
TRAILER_REGEX="${TRAILER_REGEX%|*})[$seperators][[:blank:]]*)"
fi
if ((${#standalones[@]}>0));then
TRAILER_REGEX+='|(('
for each in "${standalones[@]}";do
TRAILER_REGEX+="$each|"
done
TRAILER_REGEX="${TRAILER_REGEX%|*})$)"
fi

# specials
if ((${#specials[@]}>0));then
TRAILER_REGEX+='|('
for each in "${specials[@]}";do
TRAILER_REGEX+="$each|"
done
TRAILER_REGEX="${TRAILER_REGEX%|*})"
fi

# keys
if ((${#keys[@]}>0));then
TRAILER_REGEX+='|(('
for each in "${keys[@]}";do
TRAILER_REGEX+="$each|"
done
TRAILER_REGEX="${TRAILER_REGEX%|*})[${seperators:1:1}[:blank:]])"
fi

# end
TRAILER_REGEX+=")"
}

#
# Set the hook editor, using the same approach as git.
#
Expand Down Expand Up @@ -242,7 +324,7 @@ validate_commit_message() {

for i in "${!COMMIT_MSG_LINES[@]}"; do
LINE_NUMBER=$((i+1))
test "${#COMMIT_MSG_LINES[$i]}" -le 72 || [[ ${COMMIT_MSG_LINES[$i]} =~ $URL_REGEX ]]
test "${#COMMIT_MSG_LINES[$i]}" -le 72 || [[ ${COMMIT_MSG_LINES[$i]} =~ $URL_REGEX ]] || [[ ${COMMIT_MSG_LINES[$i]} =~ $TRAILER_REGEX ]]
test $? -eq 0 || add_warning $LINE_NUMBER "Wrap the body at 72 characters (${#COMMIT_MSG_LINES[$i]} chars)"
done

Expand Down Expand Up @@ -273,6 +355,8 @@ set_colors

set_editor

build_commit_trailer_regex

if tty >/dev/null 2>&1; then
TTY=$(tty)
else
Expand Down
1 change: 1 addition & 0 deletions test/test_helper.bash
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ setup() {
git -c "init.templatedir=$TMP_DIRECTORY/templates" init
git config user.email "test@git-good-commit"
git config user.name "Git Good Commit Tests"
git config core.hooksPath '.git/hooks'
echo "Foo bar" > my_file
git add my_file
mkdir -p .git/hooks
Expand Down
25 changes: 25 additions & 0 deletions test/validation.bats
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,31 @@ EOF
assert_success
}


@test "validation: auto generated trailers does not show warnings" {
echo "n" > $FAKE_TTY
run git commit -m "$(cat <<EOF
Summarize change in around 50 characters or less

More detailed explanatory text, if necessary. Wrap it to about 72
characters or so. In some contexts, the first line is treated as the
subject of the commit and the rest of the text as the body. The
blank line separating the summary from the body is critical (unless
you omit the body entirely); various tools like `log`, `shortlog`
and `rebase` can get confused if you run the two together.


Bug: 123
See-also: HEAD
Co-Authored-by: John Doe <[email protected]>
Acked-by: Jane Doe <[email protected]>
Reference-to: f000e67 (Merge pull request #2 from tommarshall/ignore-trailing-whitespace, 2016-09-29)
See-also: f000e679cfe3ac (Merge pull request #2 from tommarshall/ignore-trailing-whitespace)
EOF
)"

assert_success
}
# 1. Separate subject from body with a blank line
# ------------------------------------------------------------------------------

Expand Down