548f373522edbd9f1a440f41de82e6dca148c81e
[coreboot.git] / util / gitconfig / commit-msg
1 #!/bin/sh
2 #
3 # Part of Gerrit Code Review (http://code.google.com/p/gerrit/)
4 #
5 # Copyright (C) 2009 The Android Open Source Project
6 #
7 # Licensed under the Apache License, Version 2.0 (the "License");
8 # you may not use this file except in compliance with the License.
9 # You may obtain a copy of the License at
10 #
11 # http://www.apache.org/licenses/LICENSE-2.0
12 #
13 # Unless required by applicable law or agreed to in writing, software
14 # distributed under the License is distributed on an "AS IS" BASIS,
15 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 # See the License for the specific language governing permissions and
17 # limitations under the License.
18 #
19
20 CHANGE_ID_AFTER="Bug|Issue"
21 MSG="$1"
22
23 # Check for, and add if missing, a unique Change-Id
24 #
25 add_ChangeId() {
26         clean_message=`sed -e '
27                 /^diff --git a\/.*/{
28                         s///
29                         q
30                 }
31                 /^Signed-off-by:/d
32                 /^#/d
33         ' "$MSG" | git stripspace`
34         if test -z "$clean_message"
35         then
36                 return
37         fi
38
39         # Does Change-Id: already exist? if so, exit (no change).
40         if grep -i '^Change-Id:' "$MSG" >/dev/null
41         then
42                 return
43         fi
44
45         id=`_gen_ChangeId`
46         T="$MSG.tmp.$$"
47         AWK=awk
48         if [ -x /usr/xpg4/bin/awk ]; then
49                 # Solaris AWK is just too broken
50                 AWK=/usr/xpg4/bin/awk
51         fi
52         $AWK '
53         # Skip lines starting with "#" without any spaces before it.
54         /^#/ { next }
55
56         # Skip the line starting with the diff command and everything after it,
57         # up to the end of the file, assuming it is only patch data.
58         # If more than one line before the diff was empty, strip all but one.
59         /^diff --git a/ {
60                 if (blankLines > 1) {
61                         blankLines = 1
62                 }
63                 while (getline) { }
64                 next
65         }
66
67         # Handle comments and continuations in tags ([foo: bar] etc)
68         (caught == 1) && /^[ []/ {
69                 if (lines != "") {
70                         lines = lines "\n"
71                 }
72                 lines = lines $0
73                 next
74         }
75
76         # Handle normal lines (ie. not starting with some tag like "Signed-off-by:").
77         # If normal text appears after tags were "caught", handle them as normal text, too.
78         # Also count blank lines in blankLines.
79         !/^[a-zA-Z0-9-]+:/ || /^[a-zA-Z0-9-]+:\/\// {
80                 if ($0 == "") {
81                         blankLines++
82                         next
83                 } else {
84                         for (i = 0; i < blankLines; i++) {
85                                 print ""
86                         }
87                         blankLines = 0
88                 }
89                 if (caught == 1) {
90                         caught = 0
91                         print lines
92                         lines = ""
93                 }
94                 print $0
95                 next
96         }
97
98         # Handle tags.  They are "caught" and collected in the "lines" variable
99         {
100                 caught = 1
101                 if (lines != "") {
102                         lines = lines "\n";
103                 }
104                 lines = lines $0
105         }
106
107         # Tag handling:
108         # If last line before tags was not blank, there were no tags.
109         # In that case, print everything, plus a blank line, followed by Change-Id.
110         # Otherwise there were tags. Look for the right place to inject Change-Id,
111         # by considering CHANGE_ID_AFTER. Tags listed in it (case insensitive) come first,
112         # then Change-Id, then everything else (eg. Signed-off-by:).
113         END {
114                 unprinted = 1
115                 if (blankLines == 0) {
116                         if (lines == "") {
117                                  print ""
118                         } else {
119                                 print lines "\n"
120                         }
121                 } else {
122                         for (i = 0; i < blankLines; i++) {
123                                 print ""
124                         }
125                         changeIdAfter = "^(" tolower("'"$CHANGE_ID_AFTER"'") "):"
126                         numlines = split(lines, footer, "\n")
127                         for (line = 1; line <= numlines; line++) {
128                                 if (unprinted && match(tolower(footer[line]), changeIdAfter) != 1) {
129                                         unprinted = 0
130                                         print "Change-Id: I'"$id"'"
131                                 }
132                                 print footer[line]
133                         }
134                 }
135                 if (unprinted) {
136                         print "Change-Id: I'"$id"'"
137                 }
138         }' "$MSG" > $T && mv $T "$MSG" || rm -f $T
139 }
140 _gen_ChangeIdInput() {
141         echo "tree `git write-tree`"
142         if parent=`git rev-parse "HEAD^0" 2>/dev/null`
143         then
144                 echo "parent $parent"
145         fi
146         echo "author `git var GIT_AUTHOR_IDENT`"
147         echo "committer `git var GIT_COMMITTER_IDENT`"
148         echo
149         printf '%s' "$clean_message"
150 }
151 _gen_ChangeId() {
152         _gen_ChangeIdInput |
153         git hash-object -t commit --stdin
154 }
155
156
157 add_ChangeId