The changes are only shown in the editor and do not land in the final commit message. For that setting the git-commit-last-msg function and the commit-msg hook had to be updated. The function is now a standalone function instead of anonymous and uses every line until the first comment in COMMIT_EDITMSG discarding the new information too. The hook breaks now when checking line lengths when the changes start since for some weird reason they are passed together with the rest of the message instead of being deleted like the comments.
55 lines
1.6 KiB
Bash
Executable File
55 lines
1.6 KiB
Bash
Executable File
#!/bin/bash
|
|
#
|
|
# A hook script to check the commit log message.
|
|
# Called by "git commit" with one argument, the name of the file that has the
|
|
# commit message.
|
|
# The hook should exit with non-zero status after issuing an appropriate message
|
|
# if it wants to stop the commit.
|
|
# The hook is allowed to edit the commit message file.
|
|
#
|
|
# To enable this hook, save this file in ".git/hooks/commit-msg".
|
|
|
|
die() {
|
|
printf "$1" >&2
|
|
exit ${2:-1}
|
|
}
|
|
|
|
subject="$(head -1 "$1")"
|
|
body="$(tail +2 "$1" | grep -v "^#")"
|
|
|
|
[[ ${#subject} -le 50 ]] || die "Subject too long. (<= 50)\n"
|
|
|
|
# The subject line has to match "${pats[@]}", but to be more verbose different
|
|
# error messages are printed for the different 'levels' of the pattern.
|
|
declare -a pats msg
|
|
pats=(
|
|
"^([-_,*(){}./a-zA-Z0-9]+:)+ "
|
|
"[A-Z]"
|
|
".*[^.]$"
|
|
)
|
|
msg=(
|
|
"Specify which program was modified. (e.g. \"zsh:p10k: <subject>\")\n"
|
|
"Start subject with a capital letter.\n"
|
|
"Remove punctuation mark from end.\n"
|
|
)
|
|
[[ ${#msg[@]} -ge ${#pats[@]} ]] || die "Something went wrong internally.\n"
|
|
for ((i = 0; i < ${#pats[@]}; i++)); do
|
|
if ! grep -qE "$(printf "%s" "${pats[@]:0:$i+1}")" <<<"$subject"; then
|
|
die "${msg[$i]}"
|
|
fi
|
|
done
|
|
|
|
BKP_IFS="$IFS"
|
|
IFS='
|
|
'
|
|
# The -v flag or commit.verbose setting add the changes of the commit to the
|
|
# bottom of the commit message. But when the changes contain lines longer than
|
|
# 72 characters this hook will fail when not breaking the loop before the end
|
|
# start of the patch.
|
|
verbose_start_line="$(git diff --staged -u | head -1)"
|
|
for line in $body; do
|
|
[[ "$line" != "$verbose_start_line" ]] || break
|
|
[[ ${#line} -le 72 ]] || die "Body lines too long. (<= 72)\n"
|
|
done
|
|
IFS="$BKP_IFS"
|