3 # An example hook script to check the commit log message.
4 # Called by git-commit with one argument, the name of the file
5 # that has the commit message. The hook should exit with non-zero
6 # status after issuing an appropriate message if it wants to stop the
7 # commit. The hook is allowed to edit the commit message file.
9 # To enable this hook, make this file executable.
11 # Uncomment the below to add a Signed-off-by line to the message.
12 # SOB=$(git var GIT_AUTHOR_IDENT | sed -n 's/^\(.*>\).*$/Signed-off-by: \1/p')
13 # grep -qs "^$SOB" "$1" || echo "$SOB" >> "$1"
15 # This example catches duplicate Signed-off-by lines.
17 base_dir
=$
(dirname $0)
23 Commit aborted, your commit message was saved as '$1.save'.
31 test "" = "$(grep '^Signed-off-by: ' "$1" |
32 sort | uniq -c | sed -e '/^[ ]*1[ ]/d')" ||
{
33 abort
"$1" "Duplicate Signed-off-by lines."
36 # Check that the first line exists, and is not an asterisk
38 if [ -z "`head -n 1 $1 | grep -v '^[[:blank:]]*\*$'`" ] ; then
39 abort
"$1" "Please provide the general description on the first line."
42 # ...and that it is not too long
44 if [ "`head -n 1 $1 | wc -c`" -gt 79 ] ; then
45 abort
"$1" "The first line is too long, please try to fit into 79 characters."
48 # ...and that it does not continue on the second line
49 if [ "`wc -l < $1`" -gt 1 -a -n "`head -n 2 $1 | tail -n 1 | sed 's/^#.*//'`" ] ; then
50 abort
"$1" "The second line is not empty - maybe the first line continues there?"
53 # Check that the message is not a ChangeLog-like one
55 if [ -n "`head -n 1 $1 | grep '^[0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\}.*<.*@.*>'`" ] ; then
56 abort
"$1" "The commit message looks like ChangeLog, please use the git form."
59 # Check for whitespace in front of *'s
61 if [ -n "`sed '/^#/,$d' $1 | grep '^[[:space:]]\+\*.*:'`" -a -z "`grep '^\*' $1`" ] ; then
62 abort
"$1" "Please don't use whitespace in front of '* file: Description.' entries."
65 # Check that lines do not start with '#<something>' (possibly accidental commit,
66 # such as starting the message with '#ifdef', git commits start with '#<whitespace>'.
68 if [ -n "`grep '^#[^[:blank:]]' $1`" ] ; then
69 abort
"$1" "Possible accidental comment in the commit message (leading # without space)."
73 #------------------ copied gerrit commit-msg hook to handle ChangeId -->
74 # From Gerrit Code Review 2.3
76 # Part of Gerrit Code Review (http://code.google.com/p/gerrit/)
78 # Copyright (C) 2009 The Android Open Source Project
80 # Licensed under the Apache License, Version 2.0 (the "License");
81 # you may not use this file except in compliance with the License.
82 # You may obtain a copy of the License at
84 # http://www.apache.org/licenses/LICENSE-2.0
86 # Unless required by applicable law or agreed to in writing, software
87 # distributed under the License is distributed on an "AS IS" BASIS,
88 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
89 # See the License for the specific language governing permissions and
90 # limitations under the License.
93 CHANGE_ID_AFTER
="Bug|Issue"
95 # Check for, and add if missing, a unique Change-Id
98 clean_message
=`sed -e '
105 ' "$MSG" | git stripspace`
106 if test -z "$clean_message"
111 id
=`grep -i '^Change-Id:' "$MSG" | sed -e "s/.*: I//"`
112 temp_msg
=`grep -v -i '^Change-Id:' "$MSG"`
113 echo "$temp_msg" > "$MSG"
122 $CHANGE_ID_AFTER = shift;
125 open(I, $MSG); $_ = <I>; close I;
126 s|^diff --git a/.*||ms;
130 @message = split /\n/;
132 $startFooter = @message;
133 for($line = @message - 1; $line >= 0; $line--) {
134 $_ = $message[$line];
136 if (/^[a-zA-Z0-9-]+: /) {
141 $startFooter = $line if ($haveFooter && /^\r?$/);
145 @footer = @message[$startFooter+1..@message];
146 @message = @message[0..$startFooter];
147 push(@footer, "") unless @footer;
149 for ($line = 0; $line < @footer; $line++) {
151 next if /^($CHANGE_ID_AFTER):/i;
154 splice(@footer, $line, 0, "Change-Id: I$id");
156 $_ = join("\n", @message, @footer);
157 open(O, ">$MSG"); print O; close O;
158 ' "$MSG" "$id" "$CHANGE_ID_AFTER"
160 _gen_ChangeIdInput
() {
161 echo "tree `git write-tree`"
162 if parent
=`git rev-parse HEAD^0 2>/dev/null`
164 echo "parent $parent"
166 echo "author `git var GIT_AUTHOR_IDENT`"
167 echo "committer `git var GIT_COMMITTER_IDENT`"
169 printf '%s' "$clean_message"
173 git hash-object
-t commit
--stdin
178 #------------------ copied gerrit commit-msg hook to handle ChangeId <--