Skip to content

Instantly share code, notes, and snippets.

@tricantivu
Created July 5, 2023 04:36
Show Gist options
  • Save tricantivu/589b7f5d92c1098abe9f0267a2f02482 to your computer and use it in GitHub Desktop.
Save tricantivu/589b7f5d92c1098abe9f0267a2f02482 to your computer and use it in GitHub Desktop.
My commit-msg hook
#!/usr/bin/env bash
readonly MAX_SUBJ_LINE_LEN=75
readonly MAX_BODY_LINE_LEN=72
read -r subject < "$1"
if [[ "${subject}" =~ ^Revert ]]; then
# Skip checking commit message generated by git-revert(1)
exit 0
else
# Verify Conventional Commits Specification compliance
[[ "${subject}" =~ ^(ci|rm|fix|feat|docs|test|perf|build|chore|style|refactor)\(?.*\)?:' '.+$ ]] || {
echo 'Bad message subject' >&2
exit 1
}
(( "${#subject}" > MAX_SUBJ_LINE_LEN )) && {
echo "Message subject exceeds ${MAX_SUBJ_LINE_LEN} characters" >&2
exit 1
}
fi
l=1
while read -r line; do
[[ "${line}" == "${subject}" ]] && {
(( l++ ))
continue
}
(( "${#line}" )) || {
(( l++ ))
continue
}
[[ "${line}" =~ ^# ]] && continue
(( "${#line}" > MAX_BODY_LINE_LEN )) && {
echo "Message line ${l} exceeds ${MAX_BODY_LINE_LEN} characters"
exit 1
}
done < "$1"
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment