Skip to content

Commit 103813f

Browse files
committed
;dev: bin: commitlint: also work a git commit-msg hook
1 parent 215e90a commit 103813f

File tree

1 file changed

+44
-26
lines changed

1 file changed

+44
-26
lines changed

bin/commitlint

Lines changed: 44 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
11
#!/usr/bin/env bash
2-
# shellcheck disable=SC2059
32
#
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.
3+
# commitlint [GITRANGE|FILE]
4+
5+
# Check unpushed commits, or commits in the specified range, or a
6+
# commit message in FILE, for compliance with hledger's conventions
7+
# (https://hledger.org/CONTRIBUTING.html#commit-messages). If the
8+
# argument contains - or .. or ^! it's a range, otherwise a file
9+
# containing a proposed commit message.
10+
# Run interactively, or symlink as .git/hooks/commit-msg to check
11+
# your messages before commit.
1112
#
1213
# Examples:
13-
# commitlint # unpushed commits
14-
# commitlint HEAD # last commit
15-
# commitlint d5d19f841 # this commit
14+
# commitlint foo # commit message in ./foo
15+
# commitlint HEAD^! # last commit
16+
# commitlint d5d19f841^! # this commit
1617
# commitlint -20 # last 20 commits
1718
# commitlint master.. # commits in this branch
1819
# commitlint 1.21..1.22 | grep -F [FAIL] # bad commits in 1.22 release cycle
20+
# commitlint # unpushed commits
1921

2022
if [[ -n ${NO_COLOR+set} ]]
2123
then
@@ -32,39 +34,54 @@ fi
3234
function checkcommit()
3335
{
3436
HASH=${1}
35-
SUMMARY=$(git log -1 "$HASH" --pretty=format:"%s")
37+
MSG=$(git log -1 "$HASH" --pretty=format:"%s%n%b")
38+
checkmsg "$MSG"
39+
}
40+
41+
# checkmsg MSG [GITHASH] - check this commit message and print result
42+
function checkmsg()
43+
{
44+
MSG=${1}
45+
HASH=${2}
46+
if [[ -n $HASH ]]
47+
then
48+
HASH="$HASH "
49+
fi
50+
51+
SUMMARY=$(echo "$MSG" | head -1)
52+
FMT="%s%-60s %b${NRM}\n"
3653

3754
# Does the summary have a type: prefix ?
3855
# Can begin with ; and/or end with !, some spaces are tolerated.
39-
FMT="%s %-60s %b${NRM}\n"
4056
if ! echo "$SUMMARY" | grep -qE '^(; *)?\w+:[ \w]+!?'
4157
then
58+
# shellcheck disable=SC2059
4259
printf "$FMT" "$HASH" "$SUMMARY" "${RED}[FAIL]"
4360
STATUS=1
4461
else
62+
# shellcheck disable=SC2059
4563
printf "$FMT" "$HASH" "$SUMMARY" "${GRN}[ok]"
4664
fi
4765
}
4866

67+
STATUS=
4968
RANGE=${*:-"@{u}.."} # @{u} = upstream
5069

51-
if [[ ! $RANGE =~ (-|\.\.) ]]
70+
if [[ $RANGE =~ (-|\.\.|\^!) ]]
5271
then
53-
RANGE=$RANGE^! # assume range is a single commit
72+
HASHES=$(git log --abbrev-commit --pretty=format:%h "$RANGE")
73+
for HASH in $HASHES; do checkcommit "$HASH"; done
74+
else
75+
MSG=$(cat "$1")
76+
checkmsg "$MSG"
5477
fi
5578

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-
6379
if [[ -z $STATUS ]]
6480
then
6581
printf "" # "${GRN}Ok${NRM}\n"
6682
else
67-
printf "\n${RED}Some commits are not in preferred style.${NRM}\n"
83+
# shellcheck disable=SC2059
84+
printf "\n${RED}Commit(s) not in preferred style.${NRM}\n"
6885
cat <<EOF
6986
---------------------------------------------------------------------------
7087
Commit messages should follow this format:
@@ -80,15 +97,16 @@ The subject line should have a type: prefix. Common types:
8097
cha pkg lib - packager/builder/lib-user changes (->changelogs)
8198
dev doc test ci ref cln - developer changes
8299
83-
It may additionally have a topic prefix such as:
100+
It may additionally have a topic(s) prefix such as:
101+
bin examples install cli ui web print reg bal bs balcmds journal csv ...
84102
(see https://hledger.org/CONTRIBUTING.html#open-issues -> COMPONENT)
85103
86104
Mention any related issues, usually parenthesised at end of summary: (#1234)
87105
! indicates a breaking change.
88106
; skips expensive CI tests.
89107
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.
108+
These conventions are evolving. In practice, any type or topic will do.
109+
Use your best judgement and we'll polish during code review.
92110
More detail: https://hledger.org/CONTRIBUTING.html#commit-messages
93111
---------------------------------------------------------------------------
94112
EOF

0 commit comments

Comments
 (0)