xref: /freebsd/sys/contrib/openzfs/scripts/commitcheck.sh (revision 6be3386466ab79a84b48429ae66244f21526d3df)
1#!/bin/sh
2
3REF="HEAD"
4
5# test commit body for length
6# lines containing urls are exempt for the length limit.
7test_commit_bodylength()
8{
9    length="72"
10    body=$(git log -n 1 --pretty=%b "$REF" | grep -Ev "http(s)*://" | grep -E -m 1 ".{$((length + 1))}")
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
19# check for a tagged line
20check_tagged_line()
21{
22    regex='^[[:space:]]*'"$1"':[[:space:]][[:print:]]+[[:space:]]<[[:graph:]]+>$'
23    foundline=$(git log -n 1 "$REF" | grep -E -m 1 "$regex")
24    if [ -z "$foundline" ]; then
25        echo "error: missing \"$1\""
26        return 1
27    fi
28
29    return 0
30}
31
32# check commit message for a normal commit
33new_change_commit()
34{
35    error=0
36
37    # subject is not longer than 72 characters
38    long_subject=$(git log -n 1 --pretty=%s "$REF" | grep -E -m 1 '.{73}')
39    if [ -n "$long_subject" ]; then
40        echo "error: commit subject over 72 characters"
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
50    if ! test_commit_bodylength ; then
51        error=1
52    fi
53
54    return $error
55}
56
57is_coverity_fix()
58{
59    # subject starts with Fix coverity defects means it's a coverity fix
60    subject=$(git log -n 1 --pretty=%s "$REF" | grep -E -m 1 '^Fix coverity defects')
61    if [ -n "$subject" ]; then
62        return 0
63    fi
64
65    return 1
66}
67
68coverity_fix_commit()
69{
70    error=0
71
72    # subject starts with Fix coverity defects: CID dddd, dddd...
73    subject=$(git log -n 1 --pretty=%s "$REF" |
74        grep -E -m 1 'Fix coverity defects: CID [[:digit:]]+(, [[:digit:]]+)*')
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
87    IFS='
88'
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
92        if [ $? -ne 0 ]; then
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
107if [ -n "$1" ]; then
108    REF="$1"
109fi
110
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
120# have a normal commit
121if ! new_change_commit ; then
122    exit 1
123fi
124
125exit 0
126