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).
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.
12 # Should be installed as .git/hooks/commit-msg.
16 REVISED="$(mktemp "$1.XXXXXX")"
17 SIGNOFF="Signed-off-by:"
22 # Check for, and add if missing, a unique Change-Id
25 git var GIT_AUTHOR_IDENT
26 git var GIT_COMMITTER_IDENT
28 git rev-parse HEAD 2>/dev/null
29 grep -v "$SIGNOFF" "$ORIGINAL" | git stripspace -s
30 } | git hash-object --stdin
34 [ "$LINE" ] && echo "error:$NUM: $LINE" 1>&2 && echo "" 1>&2
39 See http://wiki.whamcloud.com/display/PUB/Submitting+Changes
40 for full details. An example valid commit comment is:
42 LU-nnn component: short description of change under 64 columns
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.
48 $SIGNOFF Your Real Name <your_email@domain.name>
49 $CHANGEID Ixxxx(added automatically if missing)xxxx
55 export HAS_SIGNOFF=false
56 export HAS_SUMMARY=false
57 export HAS_BLANK=false
58 export HAS_COMMENTS=false
61 grep -q "^$CHANGEID" "$ORIGINAL" && HAS_CHANGEID=true || HAS_CHANGEID=false
68 WIDTH=$(($(echo $LINE | wc -c) - 1)) # -1 for end-of-line character
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 .* .* <.*@[^.]*\..*>")
77 usage "missing valid commit summary line to show" \
78 "agreement with code submission requirements at"
81 $HAS_CHANGEID || echo "$CHANGEID I$(new_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\}")
89 usage "missing valid $CHANGEID line for Gerrit tracking"
93 "") [ $HAS_SUMMARY -a $NUM -eq 2 ] && HAS_BLANK=true
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
104 \#*) HAS_COMMENTS=true
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")
116 usage "has no subsys: in commit summary"
117 usage "missing valid commit summary line."
120 elif [ $WIDTH -gt $WIDTH_REG ]; then
121 usage "has lines longer than $WIDTH_REG columns."
129 done < "$ORIGINAL" > "$REVISED"
131 [ $NUM -eq 1 ] && exit 1 # empty file
134 $HAS_SUMMARY || usage "missing commit summary line."
135 $HAS_SIGNOFF || usage "missing $SIGNOFF line."
136 $HAS_CHANGEID && rm "$REVISED" || mv "$REVISED" "$ORIGINAL"