]> git.proxmox.com Git - mirror_zfs.git/blame - scripts/commitcheck.sh
Replace ZoL with OpenZFS where applicable
[mirror_zfs.git] / scripts / commitcheck.sh
CommitLineData
777b8ccc 1#!/bin/sh
cb524aa2
GDN
2
3REF="HEAD"
4
8dcaf243 5# test commit body for length
a584ef26 6# lines containing urls are exempt for the length limit.
777b8ccc 7test_commit_bodylength()
8dcaf243
GDN
8{
9 length="72"
a584ef26 10 body=$(git log -n 1 --pretty=%b "$REF" | grep -Ev "http(s)*://" | grep -E -m 1 ".{$((length + 1))}")
8dcaf243
GDN
11 if [ -n "$body" ]; then
12 echo "error: commit message body contains line over ${length} characters"
13 return 1
14 fi
15
16 return 0
17}
18
cb524aa2 19# check for a tagged line
777b8ccc 20check_tagged_line()
cb524aa2 21{
777b8ccc 22 regex='^[[:space:]]*'"$1"':[[:space:]][[:print:]]+[[:space:]]<[[:graph:]]+>$'
3da3488e 23 foundline=$(git log -n 1 "$REF" | grep -E -m 1 "$regex")
cb524aa2
GDN
24 if [ -z "$foundline" ]; then
25 echo "error: missing \"$1\""
26 return 1
27 fi
28
29 return 0
30}
31
cb524aa2 32# check commit message for a normal commit
777b8ccc 33new_change_commit()
cb524aa2
GDN
34{
35 error=0
36
4efb48ee
NG
37 # subject is not longer than 72 characters
38 long_subject=$(git log -n 1 --pretty=%s "$REF" | grep -E -m 1 '.{73}')
cb524aa2 39 if [ -n "$long_subject" ]; then
4efb48ee 40 echo "error: commit subject over 72 characters"
cb524aa2
GDN
41 error=1
42 fi
43
44 # need a signed off by
45 if ! check_tagged_line "Signed-off-by" ; then
46 error=1
47 fi
48
49 # ensure that no lines in the body of the commit are over 72 characters
8dcaf243 50 if ! test_commit_bodylength ; then
cb524aa2
GDN
51 error=1
52 fi
53
54 return $error
55}
56
777b8ccc 57is_coverity_fix()
8dcaf243
GDN
58{
59 # subject starts with Fix coverity defects means it's a coverity fix
3da3488e 60 subject=$(git log -n 1 --pretty=%s "$REF" | grep -E -m 1 '^Fix coverity defects')
8dcaf243
GDN
61 if [ -n "$subject" ]; then
62 return 0
63 fi
64
65 return 1
66}
67
777b8ccc 68coverity_fix_commit()
8dcaf243
GDN
69{
70 error=0
71
72 # subject starts with Fix coverity defects: CID dddd, dddd...
73 subject=$(git log -n 1 --pretty=%s "$REF" |
3da3488e 74 grep -E -m 1 'Fix coverity defects: CID [[:digit:]]+(, [[:digit:]]+)*')
8dcaf243
GDN
75 if [ -z "$subject" ]; then
76 echo "error: Coverity defect fixes must have a subject line that starts with \"Fix coverity defects: CID dddd\""
77 error=1
78 fi
79
80 # need a signed off by
81 if ! check_tagged_line "Signed-off-by" ; then
82 error=1
83 fi
84
85 # test each summary line for the proper format
86 OLDIFS=$IFS
777b8ccc
RM
87 IFS='
88'
3da3488e
BB
89 for line in $(git log -n 1 --pretty=%b "$REF" | grep -E '^CID'); do
90 echo "$line" | grep -E '^CID [[:digit:]]+: ([[:graph:]]+|[[:space:]])+ \(([[:upper:]]|\_)+\)' > /dev/null
91 # shellcheck disable=SC2181
777b8ccc 92 if [ $? -ne 0 ]; then
8dcaf243
GDN
93 echo "error: commit message has an improperly formatted CID defect line"
94 error=1
95 fi
96 done
97 IFS=$OLDIFS
98
99 # ensure that no lines in the body of the commit are over 72 characters
100 if ! test_commit_bodylength; then
101 error=1
102 fi
103
104 return $error
105}
106
cb524aa2
GDN
107if [ -n "$1" ]; then
108 REF="$1"
109fi
110
8dcaf243
GDN
111# if coverity fix, test against that
112if is_coverity_fix; then
113 if ! coverity_fix_commit; then
114 exit 1
115 else
116 exit 0
117 fi
118fi
119
cb524aa2
GDN
120# have a normal commit
121if ! new_change_commit ; then
122 exit 1
123fi
124
125exit 0