Skip to content

Commit 215e90a

Browse files
committed
;dev: bin: commitlint: check for preferred style in commit messages
1 parent 8b1650c commit 215e90a

File tree

1 file changed

+98
-0
lines changed

1 file changed

+98
-0
lines changed

bin/commitlint

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
#!/usr/bin/env bash
2+
# shellcheck disable=SC2059
3+
#
4+
# Check git commits for compliance with hledger's conventions,
5+
# described at https://hledger.org/CONTRIBUTING.html#commit-messages.
6+
#
7+
# Usage:
8+
# commitlint [GITREV|GITRANGE]
9+
# Checks unpushed commits, or the specified commit or range.
10+
# If the argument contains - or .. it's a range, otherwise a single commit.
11+
#
12+
# Examples:
13+
# commitlint # unpushed commits
14+
# commitlint HEAD # last commit
15+
# commitlint d5d19f841 # this commit
16+
# commitlint -20 # last 20 commits
17+
# commitlint master.. # commits in this branch
18+
# commitlint 1.21..1.22 | grep -F [FAIL] # bad commits in 1.22 release cycle
19+
20+
if [[ -n ${NO_COLOR+set} ]]
21+
then
22+
RED=""
23+
GRN=""
24+
NRM=""
25+
else
26+
RED="\033[31m"
27+
GRN="\033[32m"
28+
NRM="\033[0m"
29+
fi
30+
31+
# checkcommit GITHASH - check this commit's message and print result
32+
function checkcommit()
33+
{
34+
HASH=${1}
35+
SUMMARY=$(git log -1 "$HASH" --pretty=format:"%s")
36+
37+
# Does the summary have a type: prefix ?
38+
# Can begin with ; and/or end with !, some spaces are tolerated.
39+
FMT="%s %-60s %b${NRM}\n"
40+
if ! echo "$SUMMARY" | grep -qE '^(; *)?\w+:[ \w]+!?'
41+
then
42+
printf "$FMT" "$HASH" "$SUMMARY" "${RED}[FAIL]"
43+
STATUS=1
44+
else
45+
printf "$FMT" "$HASH" "$SUMMARY" "${GRN}[ok]"
46+
fi
47+
}
48+
49+
RANGE=${*:-"@{u}.."} # @{u} = upstream
50+
51+
if [[ ! $RANGE =~ (-|\.\.) ]]
52+
then
53+
RANGE=$RANGE^! # assume range is a single commit
54+
fi
55+
56+
HASHES=$(git log --abbrev-commit --pretty=format:%h "$RANGE")
57+
58+
STATUS=
59+
for HASH in $HASHES; do
60+
checkcommit "$HASH"
61+
done
62+
63+
if [[ -z $STATUS ]]
64+
then
65+
printf "" # "${GRN}Ok${NRM}\n"
66+
else
67+
printf "\n${RED}Some commits are not in preferred style.${NRM}\n"
68+
cat <<EOF
69+
---------------------------------------------------------------------------
70+
Commit messages should follow this format:
71+
72+
[;]type[!]: [topic:] summary
73+
74+
[Optional description, ready for release notes/changelog.]
75+
76+
Explanation:
77+
78+
The subject line should have a type: prefix. Common types:
79+
feat imp fix - end-user changes (->release notes & changelogs)
80+
cha pkg lib - packager/builder/lib-user changes (->changelogs)
81+
dev doc test ci ref cln - developer changes
82+
83+
It may additionally have a topic prefix such as:
84+
(see https://hledger.org/CONTRIBUTING.html#open-issues -> COMPONENT)
85+
86+
Mention any related issues, usually parenthesised at end of summary: (#1234)
87+
! indicates a breaking change.
88+
; skips expensive CI tests.
89+
90+
These conventions are evolving. In practice, any type or topic will
91+
do. Use your best judgement and we'll polish during code review.
92+
More detail: https://hledger.org/CONTRIBUTING.html#commit-messages
93+
---------------------------------------------------------------------------
94+
EOF
95+
96+
fi
97+
98+
exit 0"$STATUS"

0 commit comments

Comments
 (0)