2021-07-06 07:57:10 +03:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
#
|
2021-07-07 04:33:18 +03:00
|
|
|
# commitlint [GITRANGE|FILE]
|
|
|
|
|
|
|
|
# Check unpushed commits, or commits in the specified range, or a
|
|
|
|
# commit message in FILE, for compliance with hledger's conventions
|
|
|
|
# (https://hledger.org/CONTRIBUTING.html#commit-messages). If the
|
|
|
|
# argument contains - or .. or ^! it's a range, otherwise a file
|
|
|
|
# containing a proposed commit message.
|
|
|
|
# Run interactively, or symlink as .git/hooks/commit-msg to check
|
|
|
|
# your messages before commit.
|
2021-07-06 07:57:10 +03:00
|
|
|
#
|
|
|
|
# Examples:
|
2021-07-07 04:33:18 +03:00
|
|
|
# commitlint foo # commit message in ./foo
|
|
|
|
# commitlint HEAD^! # last commit
|
|
|
|
# commitlint d5d19f841^! # this commit
|
2021-07-06 07:57:10 +03:00
|
|
|
# commitlint -20 # last 20 commits
|
|
|
|
# commitlint master.. # commits in this branch
|
|
|
|
# commitlint 1.21..1.22 | grep -F [FAIL] # bad commits in 1.22 release cycle
|
2021-07-07 04:33:18 +03:00
|
|
|
# commitlint # unpushed commits
|
2021-07-06 07:57:10 +03:00
|
|
|
|
2021-07-11 11:03:09 +03:00
|
|
|
set -e
|
|
|
|
|
2021-07-06 07:57:10 +03:00
|
|
|
if [[ -n ${NO_COLOR+set} ]]
|
|
|
|
then
|
|
|
|
RED=""
|
|
|
|
GRN=""
|
|
|
|
NRM=""
|
|
|
|
else
|
|
|
|
RED="\033[31m"
|
|
|
|
GRN="\033[32m"
|
|
|
|
NRM="\033[0m"
|
|
|
|
fi
|
|
|
|
|
|
|
|
# checkcommit GITHASH - check this commit's message and print result
|
|
|
|
function checkcommit()
|
|
|
|
{
|
|
|
|
HASH=${1}
|
2021-07-07 04:33:18 +03:00
|
|
|
MSG=$(git log -1 "$HASH" --pretty=format:"%s%n%b")
|
|
|
|
checkmsg "$MSG"
|
|
|
|
}
|
|
|
|
|
|
|
|
# checkmsg MSG [GITHASH] - check this commit message and print result
|
|
|
|
function checkmsg()
|
|
|
|
{
|
|
|
|
MSG=${1}
|
|
|
|
HASH=${2}
|
|
|
|
if [[ -n $HASH ]]
|
|
|
|
then
|
|
|
|
HASH="$HASH "
|
|
|
|
fi
|
|
|
|
|
|
|
|
SUMMARY=$(echo "$MSG" | head -1)
|
|
|
|
FMT="%s%-60s %b${NRM}\n"
|
2021-07-06 07:57:10 +03:00
|
|
|
|
2021-07-14 00:35:20 +03:00
|
|
|
# Is this some boring commit, eg a hard-to avoid github merge commit ?
|
|
|
|
# Ignore those.
|
2021-09-27 16:22:00 +03:00
|
|
|
if echo "$SUMMARY" | grep -qE '^Merge'
|
2021-07-14 00:35:20 +03:00
|
|
|
then
|
|
|
|
# shellcheck disable=SC2059
|
|
|
|
printf "$FMT" "$HASH" "$SUMMARY" "[ignored]"
|
2021-07-12 03:05:35 +03:00
|
|
|
# Does the summary follow convention ?
|
|
|
|
# [;]type[!]: [topic: [subtopic: ...]] subject
|
|
|
|
# spaces after ; and ! and : are optional (also before, but that should be discouraged)
|
|
|
|
# the type prefix is required and must be all word characters
|
|
|
|
# there can be zero or more topic prefixes of increasing depth
|
|
|
|
# a topic prefix must begin with a word character, can contain spaces/slashes/commas
|
|
|
|
# (so potentially multiple topic labels, eg "imp: bs, cf, is: cli/doc: blah blah")
|
2021-07-14 00:35:20 +03:00
|
|
|
elif ! echo "$SUMMARY" | grep -qE '^( *; *)?\w+( *!)? *: *(\w[\w,/ ]* *: *)*'
|
2021-07-06 07:57:10 +03:00
|
|
|
then
|
2021-07-07 04:33:18 +03:00
|
|
|
# shellcheck disable=SC2059
|
2021-07-06 07:57:10 +03:00
|
|
|
printf "$FMT" "$HASH" "$SUMMARY" "${RED}[FAIL]"
|
|
|
|
STATUS=1
|
2021-07-14 00:35:20 +03:00
|
|
|
# Looks like a good commit.
|
2021-07-06 07:57:10 +03:00
|
|
|
else
|
2021-07-07 04:33:18 +03:00
|
|
|
# shellcheck disable=SC2059
|
2021-07-06 07:57:10 +03:00
|
|
|
printf "$FMT" "$HASH" "$SUMMARY" "${GRN}[ok]"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2021-07-07 04:33:18 +03:00
|
|
|
STATUS=
|
2021-07-06 07:57:10 +03:00
|
|
|
RANGE=${*:-"@{u}.."} # @{u} = upstream
|
|
|
|
|
2021-07-07 04:33:18 +03:00
|
|
|
if [[ $RANGE =~ (-|\.\.|\^!) ]]
|
2021-07-06 07:57:10 +03:00
|
|
|
then
|
2021-07-07 04:33:18 +03:00
|
|
|
HASHES=$(git log --abbrev-commit --pretty=format:%h "$RANGE")
|
|
|
|
for HASH in $HASHES; do checkcommit "$HASH"; done
|
|
|
|
else
|
|
|
|
MSG=$(cat "$1")
|
|
|
|
checkmsg "$MSG"
|
2021-07-06 07:57:10 +03:00
|
|
|
fi
|
|
|
|
|
|
|
|
if [[ -z $STATUS ]]
|
|
|
|
then
|
|
|
|
printf "" # "${GRN}Ok${NRM}\n"
|
|
|
|
else
|
2021-07-07 04:33:18 +03:00
|
|
|
# shellcheck disable=SC2059
|
|
|
|
printf "\n${RED}Commit(s) not in preferred style.${NRM}\n"
|
2021-07-06 07:57:10 +03:00
|
|
|
cat <<EOF
|
|
|
|
---------------------------------------------------------------------------
|
|
|
|
Commit messages should follow this format:
|
|
|
|
|
|
|
|
[;]type[!]: [topic:] summary
|
|
|
|
|
|
|
|
[Optional description, ready for release notes/changelog.]
|
|
|
|
|
|
|
|
Explanation:
|
|
|
|
|
|
|
|
The subject line should have a type: prefix. Common types:
|
|
|
|
feat imp fix - end-user changes (->release notes & changelogs)
|
|
|
|
cha pkg lib - packager/builder/lib-user changes (->changelogs)
|
2021-07-12 03:05:35 +03:00
|
|
|
dev doc test ci ref cln - developer changes (->just commit log, mostly)
|
2021-07-06 07:57:10 +03:00
|
|
|
|
2021-07-12 03:05:35 +03:00
|
|
|
It can additionally have a topic prefix (and optionally subtopics), such as:
|
2021-07-07 04:33:18 +03:00
|
|
|
bin examples install cli ui web print reg bal bs balcmds journal csv ...
|
2021-07-06 07:57:10 +03:00
|
|
|
(see https://hledger.org/CONTRIBUTING.html#open-issues -> COMPONENT)
|
2021-07-12 03:05:35 +03:00
|
|
|
Space, comma and slash are also tolerated inside topics for now. Eg:
|
|
|
|
imp: bs, cf, is: cli/doc: blah blah blah
|
2021-07-06 07:57:10 +03:00
|
|
|
|
|
|
|
Mention any related issues, usually parenthesised at end of summary: (#1234)
|
|
|
|
! indicates a breaking change.
|
|
|
|
; skips expensive CI tests.
|
|
|
|
|
2021-07-07 04:33:18 +03:00
|
|
|
These conventions are evolving. In practice, any type or topic will do.
|
|
|
|
Use your best judgement and we'll polish during code review.
|
2021-07-07 05:06:07 +03:00
|
|
|
More context: https://hledger.org/CONTRIBUTING.html#commit-messages
|
|
|
|
|
|
|
|
You can set up this script to check your commit messages locally:
|
|
|
|
1. before committing:
|
|
|
|
a. safer but must redo: cp bin/commitlint .git/hooks/commit-msg
|
|
|
|
b. more convenient: ln -s ../../bin/commitlint .git/hooks/commit-msg
|
|
|
|
2. before pushing: bin/commitlint && git push
|
2021-07-06 07:57:10 +03:00
|
|
|
---------------------------------------------------------------------------
|
|
|
|
EOF
|
|
|
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
exit 0"$STATUS"
|