xref: /freebsd/sys/contrib/openzfs/scripts/commitcheck.sh (revision 8ddb146abcdf061be9f2c0db7e391697dafad85c)
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 --no-show-signature -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 --no-show-signature -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 --no-show-signature -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 --no-show-signature -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 --no-show-signature -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 --no-show-signature -n 1 --pretty=%b "$REF" | grep -E '^CID'); do
90        if ! echo "$line" | grep -qE '^CID [[:digit:]]+: ([[:graph:]]+|[[:space:]])+ \(([[:upper:]]|\_)+\)'; then
91            echo "error: commit message has an improperly formatted CID defect line"
92            error=1
93        fi
94    done
95    IFS=$OLDIFS
96
97    # ensure that no lines in the body of the commit are over 72 characters
98    if ! test_commit_bodylength; then
99        error=1
100    fi
101
102    return "$error"
103}
104
105if [ -n "$1" ]; then
106    REF="$1"
107fi
108
109# if coverity fix, test against that
110if is_coverity_fix; then
111    if ! coverity_fix_commit; then
112        exit 1
113    else
114        exit 0
115    fi
116fi
117
118# have a normal commit
119if ! new_change_commit ; then
120    exit 1
121fi
122
123exit 0
124