1
1
#! /usr/bin/env bash
2
- # shellcheck disable=SC2059
3
2
#
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.
11
12
#
12
13
# 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
16
17
# commitlint -20 # last 20 commits
17
18
# commitlint master.. # commits in this branch
18
19
# commitlint 1.21..1.22 | grep -F [FAIL] # bad commits in 1.22 release cycle
20
+ # commitlint # unpushed commits
19
21
20
22
if [[ -n ${NO_COLOR+set} ]]
21
23
then
32
34
function checkcommit()
33
35
{
34
36
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"
36
53
37
54
# Does the summary have a type: prefix ?
38
55
# Can begin with ; and/or end with !, some spaces are tolerated.
39
- FMT=" %s %-60s %b${NRM} \n"
40
56
if ! echo " $SUMMARY " | grep -qE ' ^(; *)?\w+:[ \w]+!?'
41
57
then
58
+ # shellcheck disable=SC2059
42
59
printf " $FMT " " $HASH " " $SUMMARY " " ${RED} [FAIL]"
43
60
STATUS=1
44
61
else
62
+ # shellcheck disable=SC2059
45
63
printf " $FMT " " $HASH " " $SUMMARY " " ${GRN} [ok]"
46
64
fi
47
65
}
48
66
67
+ STATUS=
49
68
RANGE=${*:- " @{u}.." } # @{u} = upstream
50
69
51
- if [[ ! $RANGE =~ (-| \.\. ) ]]
70
+ if [[ $RANGE =~ (-| \.\. | \^ ! ) ]]
52
71
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 "
54
77
fi
55
78
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
79
if [[ -z $STATUS ]]
64
80
then
65
81
printf " " # "${GRN}Ok${NRM}\n"
66
82
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"
68
85
cat << EOF
69
86
---------------------------------------------------------------------------
70
87
Commit messages should follow this format:
@@ -80,15 +97,16 @@ The subject line should have a type: prefix. Common types:
80
97
cha pkg lib - packager/builder/lib-user changes (->changelogs)
81
98
dev doc test ci ref cln - developer changes
82
99
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 ...
84
102
(see https://hledger.org/CONTRIBUTING.html#open-issues -> COMPONENT)
85
103
86
104
Mention any related issues, usually parenthesised at end of summary: (#1234)
87
105
! indicates a breaking change.
88
106
; skips expensive CI tests.
89
107
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.
92
110
More detail: https://hledger.org/CONTRIBUTING.html#commit-messages
93
111
---------------------------------------------------------------------------
94
112
EOF
0 commit comments