Whamcloud - gitweb
LU-624 Kernel update [RHEL6.1 2.6.32-131.17.1.el6]
[fs/lustre-release.git] / build / commit-msg
1 #!/bin/bash
2 #
3 # A hook script to check the commit log message to ensure that it has
4 # a well-formed commit summary and body, a valid Signed-off-by: line,
5 # and a Gerrit Change-Id: line (added automatically if missing).
6 #
7 # Called by git-commit with one argument, the name of the file
8 # that has the commit message.  The hook should exit with non-zero
9 # status after issuing an appropriate message if it wants to stop the
10 # commit.  The hook is allowed to edit the commit message file.
11 #
12 # Should be installed as .git/hooks/commit-msg.
13 #
14
15 ORIGINAL="$1"
16 REVISED="$(mktemp "$1.XXXXXX")"
17 SIGNOFF="Signed-off-by:"
18 CHANGEID="Change-Id:"
19 WIDTH_SUM=64
20 WIDTH_REG=70
21
22 # Check for, and add if missing, a unique Change-Id
23 new_changeid() {
24         {
25                 git var GIT_AUTHOR_IDENT
26                 git var GIT_COMMITTER_IDENT
27                 git write-tree
28                 git rev-parse HEAD 2>/dev/null
29                 grep -v "$SIGNOFF" "$ORIGINAL" | git stripspace -s
30         } | git hash-object --stdin
31 }
32
33 usage() {
34         [ "$LINE" ] && echo "error:$NUM: $LINE" 1>&2 && echo "" 1>&2
35
36         cat 1>&2 <<- EOF
37         Commit message $1
38         $2
39         See http://wiki.whamcloud.com/display/PUB/Submitting+Changes
40         for full details.  An example valid commit comment is:
41
42         LU-nnn component: short description of change under 64 columns
43
44         A more detailed explanation.  This can be as detailed as you'd like.
45         Please explain both what problem was solved and a good high-level
46         description of how it was solved.  Wrap at $WIDTH_REG columns or less.
47
48         $SIGNOFF Your Real Name <your_email@domain.name>
49         $CHANGEID Ixxxx(added automatically if missing)xxxx
50         EOF
51
52         exit 1
53 }
54
55 export HAS_SIGNOFF=false
56 export HAS_SUMMARY=false
57 export HAS_BLANK=false
58 export HAS_COMMENTS=false
59 export HAS_DIFF=false
60
61 grep -q "^$CHANGEID" "$ORIGINAL" && HAS_CHANGEID=true || HAS_CHANGEID=false
62
63 export LINE=""
64 export NUM=1
65
66 IFS=""
67 while read LINE; do
68         WIDTH=$(($(echo $LINE | wc -c) - 1)) # -1 for end-of-line character
69
70         case "$LINE" in
71         $SIGNOFF*)
72                 $HAS_SUMMARY || usage "missing summary before $SIGNOFF."
73                 $HAS_BLANK || usage "missing blank line before $SIGNOFF."
74                 # Signed-off-by: First Last <email@host.domain>
75                 GOOD=$(echo "$LINE" | grep "^$SIGNOFF .* .* <.*@[^.]*\..*>")
76                 [ -z "$GOOD" ] &&
77                         usage "missing valid commit summary line to show" \
78                               "agreement with code submission requirements at"
79                 HAS_SIGNOFF=true
80                 echo $LINE
81                 $HAS_CHANGEID || echo "$CHANGEID I$(new_changeid)"
82                 ;;
83         $CHANGEID*)
84                 $HAS_SUMMARY || usage "missing summary before $CHANGEID line."
85                 $HAS_BLANK || usage "missing blank line before $CHANGEID line."
86                 # Change-Id: I762ab50568f25527176ae54e92c446cf06112097
87                 GOOD=$(echo "$LINE" | grep "^$CHANGEID I[0-9a-fA-F]\{40\}")
88                 [ -z "$GOOD" ] &&
89                         usage "missing valid $CHANGEID line for Gerrit tracking"
90                 HAS_CHANGEID=true
91                 echo $LINE
92                 ;;
93         "")     [ $HAS_SUMMARY -a $NUM -eq 2 ] && HAS_BLANK=true
94                 echo $LINE
95                 ;;
96         diff*|index*) # beginning of uncommented diffstat from "commit -v"
97                 # diff --git a/build/commit-msg b/build/commit-msg
98                 # index 80a3442..acb4c50 100755
99                 DIFF=$(echo "$LINE" | grep -- "^diff --git a/")
100                 [ "$DIFF" ] && HAS_DIFF=true && continue
101                 INDEX=$(echo "$LINE" | grep -- "^index [0-9a-fA-F]\{6,\}\.\.")
102                 [ $HAS_DIFF -a "$INDEX" ] && break || HAS_DIFF=false
103                 ;;
104         \#*)    HAS_COMMENTS=true
105                 continue
106                 ;;
107         *)      if [ $NUM -eq 1 ]; then
108                         FMT="^[A-Z]\{2,5\}-[0-9]\{1,5\} [-a-z0-9]\{2,9\}: "
109                         GOOD=$(echo "$LINE" | grep "$FMT")
110                         [ $WIDTH -gt $WIDTH_SUM ] &&
111                                 usage "summary longer than $WIDTH_SUM columns."
112                         if [ -z "$GOOD" ]; then
113                                 FMT="^[A-Z]\{2,5\}-[0-9]\{1,5\} "
114                                 NO_SUBSYS=$(echo "$LINE" | grep "$FMT")
115                                 [ "$NO_SUBSYS" ] &&
116                                         usage "has no subsys: in commit summary"
117                                 usage "missing valid commit summary line."
118                         fi
119                         HAS_SUMMARY=true
120                 elif [ $WIDTH -gt $WIDTH_REG ]; then
121                         usage "has lines longer than $WIDTH_REG columns."
122                 fi
123                 HAS_DIFF=false
124                 echo $LINE
125                 ;;
126         esac
127
128         NUM=$((NUM + 1))
129 done < "$ORIGINAL" > "$REVISED"
130
131 [ $NUM -eq 1 ] && exit 1 # empty file
132
133 LINE=""
134 $HAS_SUMMARY || usage "missing commit summary line."
135 $HAS_SIGNOFF || usage "missing $SIGNOFF line."
136 $HAS_CHANGEID && rm "$REVISED" || mv "$REVISED" "$ORIGINAL"