Add local copy of commit-msg hook
[coreboot.git] / util / gitconfig / commit-msg
1 #!/bin/sh
2 # From Gerrit Code Review 2.1.7-rc2
3 #
4 # Part of Gerrit Code Review (http://code.google.com/p/gerrit/)
5 #
6 # Copyright (C) 2009 The Android Open Source Project
7 #
8 # Licensed under the Apache License, Version 2.0 (the "License");
9 # you may not use this file except in compliance with the License.
10 # You may obtain a copy of the License at
11 #
12 # http://www.apache.org/licenses/LICENSE-2.0
13 #
14 # Unless required by applicable law or agreed to in writing, software
15 # distributed under the License is distributed on an "AS IS" BASIS,
16 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 # See the License for the specific language governing permissions and
18 # limitations under the License.
19 #
20
21 CHANGE_ID_AFTER="Bug|Issue"
22 MSG="$1"
23
24 # Check for, and add if missing, a unique Change-Id
25 #
26 add_ChangeId() {
27         clean_message=`sed -e '
28                 /^diff --git a\/.*/{
29                         s///
30                         q
31                 }
32                 /^Signed-off-by:/d
33                 /^#/d
34         ' "$MSG" | git stripspace`
35         if test -z "$clean_message"
36         then
37                 return
38         fi
39
40         if grep -i '^Change-Id:' "$MSG" >/dev/null
41         then
42                 return
43         fi
44
45         id=`_gen_ChangeId`
46         perl -e '
47                 $MSG = shift;
48                 $id = shift;
49                 $CHANGE_ID_AFTER = shift;
50
51                 undef $/;
52                 open(I, $MSG); $_ = <I>; close I;
53                 s|^diff --git a/.*||ms;
54                 s|^#.*$||mg;
55                 exit unless $_;
56
57                 @message = split /\n/;
58                 $haveFooter = 0;
59                 $startFooter = @message;
60                 for($line = @message - 1; $line >= 0; $line--) {
61                         $_ = $message[$line];
62
63                         if (/^[a-zA-Z0-9-]+:/ && !m,^[a-z0-9-]+://,) {
64                                 $haveFooter++;
65                                 next;
66                         }
67                         next if /^[ []/;
68                         $startFooter = $line if ($haveFooter && /^\r?$/);
69                         last;
70                 }
71
72                 @footer = @message[$startFooter+1..@message];
73                 @message = @message[0..$startFooter];
74                 push(@footer, "") unless @footer;
75
76                 for ($line = 0; $line < @footer; $line++) {
77                         $_ = $footer[$line];
78                         next if /^($CHANGE_ID_AFTER):/i;
79                         last;
80                 }
81                 splice(@footer, $line, 0, "Change-Id: I$id");
82
83                 $_ = join("\n", @message, @footer);
84                 open(O, ">$MSG"); print O; close O;
85         ' "$MSG" "$id" "$CHANGE_ID_AFTER"
86 }
87 _gen_ChangeIdInput() {
88         echo "tree `git write-tree`"
89         if parent=`git rev-parse HEAD^0 2>/dev/null`
90         then
91                 echo "parent $parent"
92         fi
93         echo "author `git var GIT_AUTHOR_IDENT`"
94         echo "committer `git var GIT_COMMITTER_IDENT`"
95         echo
96         printf '%s' "$clean_message"
97 }
98 _gen_ChangeId() {
99         _gen_ChangeIdInput |
100         git hash-object -t commit --stdin
101 }
102
103
104 add_ChangeId