xref: /freebsd/sys/contrib/openzfs/scripts/commitcheck.sh (revision e92ffd9b626833ebdbf2742c8ffddc6cd94b963e)
17877fdebSMatt Macy#!/bin/sh
2eda14cbcSMatt Macy
3eda14cbcSMatt MacyREF="HEAD"
4eda14cbcSMatt Macy
5eda14cbcSMatt Macy# test commit body for length
6eda14cbcSMatt Macy# lines containing urls are exempt for the length limit.
77877fdebSMatt Macytest_commit_bodylength()
8eda14cbcSMatt Macy{
9eda14cbcSMatt Macy    length="72"
103ff01b23SMartin Matuska    body=$(git log --no-show-signature -n 1 --pretty=%b "$REF" | grep -Ev "http(s)*://" | grep -E -m 1 ".{$((length + 1))}")
11eda14cbcSMatt Macy    if [ -n "$body" ]; then
12eda14cbcSMatt Macy        echo "error: commit message body contains line over ${length} characters"
13eda14cbcSMatt Macy        return 1
14eda14cbcSMatt Macy    fi
15eda14cbcSMatt Macy
16eda14cbcSMatt Macy    return 0
17eda14cbcSMatt Macy}
18eda14cbcSMatt Macy
19eda14cbcSMatt Macy# check for a tagged line
207877fdebSMatt Macycheck_tagged_line()
21eda14cbcSMatt Macy{
227877fdebSMatt Macy    regex='^[[:space:]]*'"$1"':[[:space:]][[:print:]]+[[:space:]]<[[:graph:]]+>$'
233ff01b23SMartin Matuska    foundline=$(git log --no-show-signature -n 1 "$REF" | grep -E -m 1 "$regex")
24eda14cbcSMatt Macy    if [ -z "$foundline" ]; then
25eda14cbcSMatt Macy        echo "error: missing \"$1\""
26eda14cbcSMatt Macy        return 1
27eda14cbcSMatt Macy    fi
28eda14cbcSMatt Macy
29eda14cbcSMatt Macy    return 0
30eda14cbcSMatt Macy}
31eda14cbcSMatt Macy
32eda14cbcSMatt Macy# check commit message for a normal commit
337877fdebSMatt Macynew_change_commit()
34eda14cbcSMatt Macy{
35eda14cbcSMatt Macy    error=0
36eda14cbcSMatt Macy
37eda14cbcSMatt Macy    # subject is not longer than 72 characters
383ff01b23SMartin Matuska    long_subject=$(git log --no-show-signature -n 1 --pretty=%s "$REF" | grep -E -m 1 '.{73}')
39eda14cbcSMatt Macy    if [ -n "$long_subject" ]; then
40eda14cbcSMatt Macy        echo "error: commit subject over 72 characters"
41eda14cbcSMatt Macy        error=1
42eda14cbcSMatt Macy    fi
43eda14cbcSMatt Macy
44eda14cbcSMatt Macy    # need a signed off by
45eda14cbcSMatt Macy    if ! check_tagged_line "Signed-off-by" ; then
46eda14cbcSMatt Macy        error=1
47eda14cbcSMatt Macy    fi
48eda14cbcSMatt Macy
49eda14cbcSMatt Macy    # ensure that no lines in the body of the commit are over 72 characters
50eda14cbcSMatt Macy    if ! test_commit_bodylength ; then
51eda14cbcSMatt Macy        error=1
52eda14cbcSMatt Macy    fi
53eda14cbcSMatt Macy
54*e92ffd9bSMartin Matuska    return "$error"
55eda14cbcSMatt Macy}
56eda14cbcSMatt Macy
577877fdebSMatt Macyis_coverity_fix()
58eda14cbcSMatt Macy{
59eda14cbcSMatt Macy    # subject starts with Fix coverity defects means it's a coverity fix
603ff01b23SMartin Matuska    subject=$(git log --no-show-signature -n 1 --pretty=%s "$REF" | grep -E -m 1 '^Fix coverity defects')
61eda14cbcSMatt Macy    if [ -n "$subject" ]; then
62eda14cbcSMatt Macy        return 0
63eda14cbcSMatt Macy    fi
64eda14cbcSMatt Macy
65eda14cbcSMatt Macy    return 1
66eda14cbcSMatt Macy}
67eda14cbcSMatt Macy
687877fdebSMatt Macycoverity_fix_commit()
69eda14cbcSMatt Macy{
70eda14cbcSMatt Macy    error=0
71eda14cbcSMatt Macy
72eda14cbcSMatt Macy    # subject starts with Fix coverity defects: CID dddd, dddd...
733ff01b23SMartin Matuska    subject=$(git log --no-show-signature -n 1 --pretty=%s "$REF" |
74eda14cbcSMatt Macy        grep -E -m 1 'Fix coverity defects: CID [[:digit:]]+(, [[:digit:]]+)*')
75eda14cbcSMatt Macy    if [ -z "$subject" ]; then
76eda14cbcSMatt Macy        echo "error: Coverity defect fixes must have a subject line that starts with \"Fix coverity defects: CID dddd\""
77eda14cbcSMatt Macy        error=1
78eda14cbcSMatt Macy    fi
79eda14cbcSMatt Macy
80eda14cbcSMatt Macy    # need a signed off by
81eda14cbcSMatt Macy    if ! check_tagged_line "Signed-off-by" ; then
82eda14cbcSMatt Macy        error=1
83eda14cbcSMatt Macy    fi
84eda14cbcSMatt Macy
85eda14cbcSMatt Macy    # test each summary line for the proper format
86eda14cbcSMatt Macy    OLDIFS=$IFS
877877fdebSMatt Macy    IFS='
887877fdebSMatt Macy'
893ff01b23SMartin Matuska    for line in $(git log --no-show-signature -n 1 --pretty=%b "$REF" | grep -E '^CID'); do
9016038816SMartin Matuska        if ! echo "$line" | grep -qE '^CID [[:digit:]]+: ([[:graph:]]+|[[:space:]])+ \(([[:upper:]]|\_)+\)'; then
91eda14cbcSMatt Macy            echo "error: commit message has an improperly formatted CID defect line"
92eda14cbcSMatt Macy            error=1
93eda14cbcSMatt Macy        fi
94eda14cbcSMatt Macy    done
95eda14cbcSMatt Macy    IFS=$OLDIFS
96eda14cbcSMatt Macy
97eda14cbcSMatt Macy    # ensure that no lines in the body of the commit are over 72 characters
98eda14cbcSMatt Macy    if ! test_commit_bodylength; then
99eda14cbcSMatt Macy        error=1
100eda14cbcSMatt Macy    fi
101eda14cbcSMatt Macy
102*e92ffd9bSMartin Matuska    return "$error"
103eda14cbcSMatt Macy}
104eda14cbcSMatt Macy
105eda14cbcSMatt Macyif [ -n "$1" ]; then
106eda14cbcSMatt Macy    REF="$1"
107eda14cbcSMatt Macyfi
108eda14cbcSMatt Macy
109eda14cbcSMatt Macy# if coverity fix, test against that
110eda14cbcSMatt Macyif is_coverity_fix; then
111eda14cbcSMatt Macy    if ! coverity_fix_commit; then
112eda14cbcSMatt Macy        exit 1
113eda14cbcSMatt Macy    else
114eda14cbcSMatt Macy        exit 0
115eda14cbcSMatt Macy    fi
116eda14cbcSMatt Macyfi
117eda14cbcSMatt Macy
118eda14cbcSMatt Macy# have a normal commit
119eda14cbcSMatt Macyif ! new_change_commit ; then
120eda14cbcSMatt Macy    exit 1
121eda14cbcSMatt Macyfi
122eda14cbcSMatt Macy
123eda14cbcSMatt Macyexit 0
124