-
Notifications
You must be signed in to change notification settings - Fork 0
/
commitlint
executable file
·62 lines (50 loc) · 1.59 KB
/
commitlint
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/bin/sh
# commitlint SUBJECT - lint your commits by conventionalcommits spec
#
# https://www.conventionalcommits.org/en/v1.0.0/
#
# EXAMPLE:
# git show --no-patch --pretty='format:%s' | commitlint
# commitlint "feat: foo"
#
# SPDX-License-Identifier: 0BSD
set -eu
error() {
printf 'ERROR: "%s" - %s\n' "$(head -n1 "$SUBJECT")" "$@" >&2
exit 1
}
lint() {
pattern="$1"
head -n1 "$SUBJECT" | grep -q -E "$pattern"
}
if [ -p /dev/stdin ]; then
# save piped data to file, as we need to read it multiple times
f="$(mktemp)" && trap 'rm $f' 0
cat /dev/stdin >"$f"
set -- "$f"
elif [ "$#" -eq 1 ]; then
# pipe the argument into ourself
printf '%s' "$1" | "$0"
exit $?
else
error "Invalid input"
fi
# global file reference used by lint()
SUBJECT="$f"
# custom rules
lint "^initial commit$" && exit
lint '^Revert ".+"' && exit
# https://github.com/conventional-changelog/commitlint/tree/b12ec06/%40commitlint/config-conventional
scope="(|\([[:alnum:]\/_-]+\))"
## type
lint "^[a-zA-Z]+.*:" || error "missing TYPE. use '{TYPE}: {SUBJECT}'"
lint "^[a-z]+.*:" || error "TYPE needs to start with lowercase word"
lint "^(build|chore|ci|docs?|feat|fix|perf|refactor|revert|style|tests?)[^[:alnum:]]+" || error "invalid TYPE"
# breaking indicator
lint "^[a-z]+(|\(.*\))(|!):" || error "Breaking Indicator malformed"
## scope
lint "^[a-z]+${scope}(|!):" || error "SCOPE malformed"
## subject
lint "^[a-z]+.*: [a-z]" || error "SUBJECT needs to start with lowercase word"
lint "^[a-z]+.*: [a-z].{2,}" || error "SUBJECT is expected to be at least 3 chars"
lint "[^\.]$" || error "SUBJECT should not end with full-stop"