blob: d8f009b63863a9caf92605a40149ebeb7dbf9eac [file] [log] [blame]
Shawn O. Pearce9452e4e2009-08-22 18:17:46 -07001#!/bin/sh
Shawn O. Pearce9452e4e2009-08-22 18:17:46 -07002#
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
David Pursehouse55693aa2013-02-13 09:55:32 +090020unset GREP_OPTIONS
21
Shawn O. Pearce9452e4e2009-08-22 18:17:46 -070022CHANGE_ID_AFTER="Bug|Issue"
23MSG="$1"
24
25# Check for, and add if missing, a unique Change-Id
26#
27add_ChangeId() {
David Pursehouse7119f942012-10-03 17:20:06 +090028 clean_message=`sed -e '
Dave Borowitza8d53912014-07-15 11:30:06 -070029 /^diff --git .*/{
Shawn O. Pearce9452e4e2009-08-22 18:17:46 -070030 s///
31 q
32 }
33 /^Signed-off-by:/d
34 /^#/d
David Pursehouse7119f942012-10-03 17:20:06 +090035 ' "$MSG" | git stripspace`
Shawn O. Pearce9452e4e2009-08-22 18:17:46 -070036 if test -z "$clean_message"
37 then
38 return
39 fi
40
Dave Borowitza8d53912014-07-15 11:30:06 -070041 if test "false" = "`git config --bool --get gerrit.createChangeId`"
42 then
43 return
44 fi
45
David Pursehouse7119f942012-10-03 17:20:06 +090046 # Does Change-Id: already exist? if so, exit (no change).
Shawn O. Pearce9452e4e2009-08-22 18:17:46 -070047 if grep -i '^Change-Id:' "$MSG" >/dev/null
48 then
49 return
50 fi
51
David Pursehouse7119f942012-10-03 17:20:06 +090052 id=`_gen_ChangeId`
53 T="$MSG.tmp.$$"
54 AWK=awk
55 if [ -x /usr/xpg4/bin/awk ]; then
56 # Solaris AWK is just too broken
57 AWK=/usr/xpg4/bin/awk
58 fi
Shawn O. Pearce9452e4e2009-08-22 18:17:46 -070059
David Pursehouse7119f942012-10-03 17:20:06 +090060 # How this works:
61 # - parse the commit message as (textLine+ blankLine*)*
62 # - assume textLine+ to be a footer until proven otherwise
63 # - exception: the first block is not footer (as it is the title)
64 # - read textLine+ into a variable
65 # - then count blankLines
66 # - once the next textLine appears, print textLine+ blankLine* as these
67 # aren't footer
68 # - in END, the last textLine+ block is available for footer parsing
69 $AWK '
70 BEGIN {
71 # while we start with the assumption that textLine+
72 # is a footer, the first block is not.
73 isFooter = 0
74 footerComment = 0
75 blankLines = 0
76 }
Shawn O. Pearce9452e4e2009-08-22 18:17:46 -070077
David Pursehouse7119f942012-10-03 17:20:06 +090078 # Skip lines starting with "#" without any spaces before it.
79 /^#/ { next }
Shawn O. Pearce9452e4e2009-08-22 18:17:46 -070080
David Pursehouse7119f942012-10-03 17:20:06 +090081 # Skip the line starting with the diff command and everything after it,
82 # up to the end of the file, assuming it is only patch data.
83 # If more than one line before the diff was empty, strip all but one.
Dave Borowitza8d53912014-07-15 11:30:06 -070084 /^diff --git / {
David Pursehouse7119f942012-10-03 17:20:06 +090085 blankLines = 0
86 while (getline) { }
87 next
88 }
89
90 # Count blank lines outside footer comments
91 /^$/ && (footerComment == 0) {
92 blankLines++
93 next
94 }
95
96 # Catch footer comment
97 /^\[[a-zA-Z0-9-]+:/ && (isFooter == 1) {
98 footerComment = 1
99 }
100
101 /]$/ && (footerComment == 1) {
102 footerComment = 2
103 }
104
105 # We have a non-blank line after blank lines. Handle this.
106 (blankLines > 0) {
107 print lines
108 for (i = 0; i < blankLines; i++) {
109 print ""
Shawn O. Pearce9452e4e2009-08-22 18:17:46 -0700110 }
111
David Pursehouse7119f942012-10-03 17:20:06 +0900112 lines = ""
113 blankLines = 0
114 isFooter = 1
115 footerComment = 0
116 }
Shawn O. Pearce9452e4e2009-08-22 18:17:46 -0700117
David Pursehouse7119f942012-10-03 17:20:06 +0900118 # Detect that the current block is not the footer
119 (footerComment == 0) && (!/^\[?[a-zA-Z0-9-]+:/ || /^[a-zA-Z0-9-]+:\/\//) {
120 isFooter = 0
121 }
122
123 {
124 # We need this information about the current last comment line
125 if (footerComment == 2) {
126 footerComment = 0
Shawn O. Pearce9452e4e2009-08-22 18:17:46 -0700127 }
David Pursehouse7119f942012-10-03 17:20:06 +0900128 if (lines != "") {
129 lines = lines "\n";
130 }
131 lines = lines $0
132 }
Shawn O. Pearce9452e4e2009-08-22 18:17:46 -0700133
David Pursehouse7119f942012-10-03 17:20:06 +0900134 # Footer handling:
135 # If the last block is considered a footer, splice in the Change-Id at the
136 # right place.
137 # Look for the right place to inject Change-Id by considering
138 # CHANGE_ID_AFTER. Keys listed in it (case insensitive) come first,
139 # then Change-Id, then everything else (eg. Signed-off-by:).
140 #
141 # Otherwise just print the last block, a new line and the Change-Id as a
142 # block of its own.
143 END {
144 unprinted = 1
145 if (isFooter == 0) {
146 print lines "\n"
147 lines = ""
148 }
149 changeIdAfter = "^(" tolower("'"$CHANGE_ID_AFTER"'") "):"
150 numlines = split(lines, footer, "\n")
151 for (line = 1; line <= numlines; line++) {
152 if (unprinted && match(tolower(footer[line]), changeIdAfter) != 1) {
153 unprinted = 0
154 print "Change-Id: I'"$id"'"
155 }
156 print footer[line]
157 }
158 if (unprinted) {
159 print "Change-Id: I'"$id"'"
160 }
David Pursehouse61df4182013-11-29 19:17:23 +0900161 }' "$MSG" > "$T" && mv "$T" "$MSG" || rm -f "$T"
Shawn O. Pearce9452e4e2009-08-22 18:17:46 -0700162}
163_gen_ChangeIdInput() {
David Pursehouse7119f942012-10-03 17:20:06 +0900164 echo "tree `git write-tree`"
165 if parent=`git rev-parse "HEAD^0" 2>/dev/null`
Shawn O. Pearce9452e4e2009-08-22 18:17:46 -0700166 then
167 echo "parent $parent"
168 fi
David Pursehouse7119f942012-10-03 17:20:06 +0900169 echo "author `git var GIT_AUTHOR_IDENT`"
170 echo "committer `git var GIT_COMMITTER_IDENT`"
Shawn O. Pearce9452e4e2009-08-22 18:17:46 -0700171 echo
172 printf '%s' "$clean_message"
173}
174_gen_ChangeId() {
175 _gen_ChangeIdInput |
176 git hash-object -t commit --stdin
177}
178
179
180add_ChangeId