Whamcloud - gitweb
LU-17744 ldiskfs: mballoc stats fixes
[fs/lustre-release.git] / contrib / git-hooks / prepare-commit-msg
1 #!/bin/bash
2 # SPDX-License-Identifier: GPL-2.0
3
4 #
5 # This file is part of Lustre, http://www.lustre.org/
6 #
7 # contrib/git-hooks/prepare-commit-msg
8 #
9 # A Git hook script to prepare the commit log message.  Install into
10 # lustre/.git/hooks/prepare-commit-msg to enable for Lustre commits.
11 #
12 # Called by git-commit with the name of the file that has the
13 # commit message, followed by the description of the commit
14 # message's source.  The hook's purpose is to edit the commit
15 # message file.  If the hook fails with a non-zero status,
16 # the commit is aborted.
17 #
18 # Commit hook to check the patch against the Lustre coding style.
19 # It adds any checkpatch warnings/errors as commit comments, which
20 # means that they can currently be ignored, but are at least visible.
21 #
22
23 CHECKPATCH=${CHECKPATCH:-contrib/scripts/checkpatch.pl}
24 CHECKPATCH_OPTS=${CHECKPATCH_OPTS:-"--no-signoff --no-tree"}
25 SHELLCHECK=${SHELLCHECK:-$(which shellcheck)}
26 SHELLCHECK_OPTS=${SHELLCHECK_OPTS:-" -f gcc "}
27 SHELLCHECK_RUN=${SHELLCHECK_RUN:-"yes"} # run everytime by default
28
29 # If there are no comments in the commit, it is likely a rebase and
30 # this shouldn't be adding new comments, or they appear in the commit.
31 grep -q "^#" "$1" || exit 0
32
33 # Add a commented-out Test-Parameters: line. This will let the developer
34 # uncomment it out if they think it's appropriate.
35 echo "#" >> "$1"
36 echo "# If this patch makes only non-functional changes, or test-only" >> "$1"
37 echo "# changes, consider adding this 'Test-Parameters' in the" >> "$1"
38 echo "# commit message:" >> "$1"
39 echo "#" >> "$1"
40 echo "# Test-Parameters: trivial" >> "$1"
41 echo "#" >> "$1"
42 echo "# If you need to repeat the same subtest many times, consider" >> "$1"
43 echo "# this instead:" >> "$1"
44 echo "#" >> "$1"
45 echo "# Test-Parameters: trivial testlist=sanity env=ONLY=1,ONLY_REPEAT=100" >> "$1"
46 echo "#" >> "$1"
47 echo "# https://wiki.whamcloud.com/display/PUB/Changing+Test+Parameters+with+Gerrit+Commit+Messages" >> "$1"
48 echo "#" >> "$1"
49
50 # Add a commented-out Signed-off-by: line.  This shouldn't be added in an
51 # uncommented form, otherwise sanity checking for an emtpy commit fails.
52 # The developer should uncomment it to include it in the commit message.
53 echo "#" >> "$1"
54 echo "# The 'Signed-off-by' line signifies that you agree to the Developer" >> "$1"
55 echo "# Certificate of Origin - https://developercertificate.org/" >> "$1"
56 echo "#" >> "$1"
57 SIGNOFF=$(git var GIT_AUTHOR_IDENT | sed -n 's/^\(.*>\).*$/Signed-off-by: \1/p')
58 grep -qs "^$SIGNOFF" "$1" || echo "# $SIGNOFF" >> "$1"
59
60 # Add the checkpatch.pl output as comments, but don't cause a commit error
61 # yet, as there may be exceptions and it is better let a person decide.
62 if [ -x "$CHECKPATCH" ]; then
63         echo "" >> "$1"
64         echo "#" >> "$1"
65         [ -d ".git/rebase-apply" -o -d ".git/rebase-merge" ] &&
66                 DIFFOPT="HEAD" || DIFFOPT="--cached"
67         git diff $DIFFOPT | $CHECKPATCH $CHECKPATCH_OPTS - |
68                 sed -e 's/^/# /' >> "$1"
69 fi
70
71 # Add the shellcheck output as comments, but don't cause a commit error.
72 if [[ -x "$SHELLCHECK" && "$SHELLCHECK_RUN" == "yes" ]]; then
73         # Add header
74         echo "#" >> "$1"
75         echo "# shellcheck output:" >> "$1"
76         echo "#" >> "$1"
77         # Get file that needs to be shellchecked. We only consider *.sh files
78         # Example Start --
79         #       modified:   lustre/tests/sanity.sh
80         #       modified:   contrib/git-hooks/prepare-commit-msg
81         #            new:   lustre/utils/liblustreapi_error.c
82         # Example End   --
83         mod_files=($(awk '/modified:.*\.sh|new:.*\.sh/ { print $3 }' $1))
84         for cur_file in ${mod_files[@]}; do
85                 # Shebangs should prefer bash over regular sh. This allows
86                 # scripts to freely use bash-isms and lowers the risks of
87                 # failures on Debian based platforms. Output a small warning
88                 # if #!/bin/sh is found.
89                 echo "#" $(grep -Hn "#\!/bin/sh" "$cur_file" && \
90                         echo " : Please don't use sh in shebangs.") >> "$1"
91
92                 # Get start/end range of lines that needs to be reported
93                 # range presently not used will come in handy when dealing with
94                 # warnings reported by shellcheck
95                 # Example 'git show' output Start --
96                 #       @@ -29,6 +29,7 @@ DEF_STRIPE_COUNT=-1
97                 # Example 'git show' output End   --
98                 range=($(git show $cur_file | awk '/^@@/ { print $3 }' | \
99                         sed -e 's/+//' -e 's/,/ /'))
100                 start_line=${range[0]}
101                 end_line=$((start_line + ${range[1]}))
102
103                 # 'shellcheck' reports errors/warnings/notes as below:
104                 # Example start ---
105                 #        sanity-lnet.sh:2319:13: error: Double quote array \
106                 #        expansions to avoid re-splitting elements. [SC2068]
107                 #
108                 #        sanity-lnet.sh:2421:27: note: Double quote to prevent \
109                 #        globbing and word splitting. [SC2086]
110                 #
111                 #        sanity-lnet.sh:2411:8: warning: Declare and assign \
112                 #        separately to avoid masking return values. [SC2155]
113                 # Example end   ---
114                 $SHELLCHECK $SHELLCHECK_OPTS $cur_file |
115                         while IFS=": " read -r file line char mtype msg; do
116                         # Only filter out "errors" reported by shellcheck
117                         # We ignore "warnings" and "notes" for now.
118                         # TODO "Warnings" reported by shellcheck will come later
119                         [[ "$mtype" == "error" ]] || continue
120                         # Empty line we skip it.
121                         [[ -n "$line" ]] || continue
122                         echo "# $file:$line:$char $mtype: $msg" >> "$1"
123                 done
124         done
125         echo "#" >> "$1"
126 fi
127
128 # Cause Vim to wrap text at 70 columns to match commit message style.
129 # Adding a matching emacs modeline would be good, if I knew how to do that.
130 echo "# vim:textwidth=70:" >> "$1"
131
132 # Add these comments at the end
133 # So that Vim does not pretend
134 # The "echo" above is actually
135 # A modeline for this file. Savvy?