1#!/bin/sh 2# 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. 8# 9# To enable this hook, make this file executable. 10 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" 14 15# This example catches duplicate Signed-off-by lines. 16 17base_dir=$(dirname $0) 18MSG="$1" 19 20abort() { 21 cp $1 $1.save 22 cat >&2 <<EOF 23Commit aborted, your commit message was saved as '$1.save'. 24 25Reason: $2 26 27EOF 28 exit 1 29} 30 31test "" = "$(grep '^Signed-off-by: ' "$1" | 32 sort | uniq -c | sed -e '/^[ ]*1[ ]/d')" || { 33 abort "$1" "Duplicate Signed-off-by lines." 34} 35 36# Check that the first line exists, and is not an asterisk 37 38if [ -z "`head -n 1 $1 | grep -v '^[[:blank:]]*\*$'`" ] ; then 39 abort "$1" "Please provide the general description on the first line." 40fi 41 42# ...and that it is not too long 43 44if [ "`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." 46fi 47 48fdo_regex='fdo#[0-9]+' 49if egrep -q "$fdo_regex" $1; then 50 for bugid in `head -n 1 $1 |egrep -o "$fdo_regex" |sed 's/fdo#//'` 51 do 52 if [ "`echo $bugid |sed 's/fdo#//'`" -gt 88775 ]; then 53 abort "$1" "The first line contains a suspicious fdo# rereference: 'fdo#$bugid', did you mean tdf#?" 54 fi 55 done 56fi 57 58# ...and that it does not continue on the second line 59if [ "`wc -l < $1`" -gt 1 -a -n "`head -n 2 $1 | tail -n 1 | sed 's/^#.*//'`" ] ; then 60 abort "$1" "The second line is not empty - maybe the first line continues there?" 61fi 62 63# Check that the message is not a ChangeLog-like one 64 65if [ -n "`head -n 1 $1 | grep '^[0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\}.*<.*@.*>'`" ] ; then 66 abort "$1" "The commit message looks like ChangeLog, please use the git form." 67fi 68 69# Check for whitespace in front of *'s 70 71if [ -n "`sed '/^#/,$d' $1 | grep '^[[:space:]]\+\*.*:'`" -a -z "`grep '^\*' $1`" ] ; then 72 abort "$1" "Please don't use whitespace in front of '* file: Description.' entries." 73fi 74 75# Check that lines do not start with '#<something>' (possibly accidental commit, 76# such as starting the message with '#ifdef', git commits start with '#<whitespace>'. 77 78if [ -n "`grep '^#[^[:blank:]]' $1`" ] ; then 79 abort "$1" "Possible accidental comment in the commit message (leading # without space)." 80fi 81 82 83#------------------ copied gerrit commit-msg hook to handle ChangeId --> 84# From Gerrit Code Review 2.3 85# 86# Part of Gerrit Code Review (http://code.google.com/p/gerrit/) 87# 88# Copyright (C) 2009 The Android Open Source Project 89# 90# Licensed under the Apache License, Version 2.0 (the "License"); 91# you may not use this file except in compliance with the License. 92# You may obtain a copy of the License at 93# 94# http://www.apache.org/licenses/LICENSE-2.0 95# 96# Unless required by applicable law or agreed to in writing, software 97# distributed under the License is distributed on an "AS IS" BASIS, 98# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 99# See the License for the specific language governing permissions and 100# limitations under the License. 101# 102 103CHANGE_ID_AFTER="Bug|Issue" 104 105# Check for, and add if missing, a unique Change-Id 106# 107add_ChangeId() { 108 clean_message=`sed -e ' 109 /^diff --git a\/.*/{ 110 s/// 111 q 112 } 113 /^Signed-off-by:/d 114 /^#/d 115 ' "$MSG" | git stripspace` 116 if test -z "$clean_message" 117 then 118 return 119 fi 120 121 id=`grep -i '^Change-Id:' "$MSG" | sed -e "s/.*: I//"` 122 temp_msg=`grep -v -i '^Change-Id:' "$MSG"` 123 echo "$temp_msg" > "$MSG" 124 125 if test -z "$id" 126 then 127 id=`_gen_ChangeId` 128 fi 129 perl -e ' 130 $MSG = shift; 131 $id = shift; 132 $CHANGE_ID_AFTER = shift; 133 134 undef $/; 135 open(I, $MSG); $_ = <I>; close I; 136 s|^diff --git a/.*||ms; 137 s|^#.*$||mg; 138 exit unless $_; 139 140 @message = split /\n/; 141 $haveFooter = 0; 142 $startFooter = @message; 143 for($line = @message - 1; $line >= 0; $line--) { 144 $_ = $message[$line]; 145 146 if (/^[a-zA-Z0-9-]+: /) { 147 $haveFooter++; 148 next; 149 } 150 next if /^[ []/; 151 $startFooter = $line if ($haveFooter && /^\r?$/); 152 last; 153 } 154 155 @footer = @message[$startFooter+1..@message]; 156 @message = @message[0..$startFooter]; 157 push(@footer, "") unless @footer; 158 159 for ($line = 0; $line < @footer; $line++) { 160 $_ = $footer[$line]; 161 next if /^($CHANGE_ID_AFTER):/i; 162 last; 163 } 164 splice(@footer, $line, 0, "Change-Id: I$id"); 165 166 $_ = join("\n", @message, @footer); 167 open(O, ">$MSG"); print O; close O; 168 ' "$MSG" "$id" "$CHANGE_ID_AFTER" 169} 170_gen_ChangeIdInput() { 171 echo "tree `git write-tree`" 172 if parent=`git rev-parse HEAD^0 2>/dev/null` 173 then 174 echo "parent $parent" 175 fi 176 echo "author `git var GIT_AUTHOR_IDENT`" 177 echo "committer `git var GIT_COMMITTER_IDENT`" 178 echo 179 printf '%s' "$clean_message" 180} 181_gen_ChangeId() { 182 _gen_ChangeIdInput | 183 git hash-object -t commit --stdin 184} 185 186 187add_ChangeId 188#------------------ copied gerrit commit-msg hook to handle ChangeId <-- 189 190 191exit 0 192 193# vi:set shiftwidth=4 expandtab: 194