blob: 172a178117414f6fe15e03b36783fd1524106a38 [file] [log] [blame]
Shawn O. Pearce9452e4e2009-08-22 18:17:46 -07001#!/bin/sh
David Pursehouse7119f942012-10-03 17:20:06 +09002# From Gerrit Code Review 2.5-rc0
Shawn O. Pearce9452e4e2009-08-22 18:17:46 -07003#
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
21CHANGE_ID_AFTER="Bug|Issue"
22MSG="$1"
23
24# Check for, and add if missing, a unique Change-Id
25#
26add_ChangeId() {
David Pursehouse7119f942012-10-03 17:20:06 +090027 clean_message=`sed -e '
Shawn O. Pearce9452e4e2009-08-22 18:17:46 -070028 /^diff --git a\/.*/{
29 s///
30 q
31 }
32 /^Signed-off-by:/d
33 /^#/d
David Pursehouse7119f942012-10-03 17:20:06 +090034 ' "$MSG" | git stripspace`
Shawn O. Pearce9452e4e2009-08-22 18:17:46 -070035 if test -z "$clean_message"
36 then
37 return
38 fi
39
David Pursehouse7119f942012-10-03 17:20:06 +090040 # Does Change-Id: already exist? if so, exit (no change).
Shawn O. Pearce9452e4e2009-08-22 18:17:46 -070041 if grep -i '^Change-Id:' "$MSG" >/dev/null
42 then
43 return
44 fi
45
David Pursehouse7119f942012-10-03 17:20:06 +090046 id=`_gen_ChangeId`
47 T="$MSG.tmp.$$"
48 AWK=awk
49 if [ -x /usr/xpg4/bin/awk ]; then
50 # Solaris AWK is just too broken
51 AWK=/usr/xpg4/bin/awk
52 fi
Shawn O. Pearce9452e4e2009-08-22 18:17:46 -070053
David Pursehouse7119f942012-10-03 17:20:06 +090054 # How this works:
55 # - parse the commit message as (textLine+ blankLine*)*
56 # - assume textLine+ to be a footer until proven otherwise
57 # - exception: the first block is not footer (as it is the title)
58 # - read textLine+ into a variable
59 # - then count blankLines
60 # - once the next textLine appears, print textLine+ blankLine* as these
61 # aren't footer
62 # - in END, the last textLine+ block is available for footer parsing
63 $AWK '
64 BEGIN {
65 # while we start with the assumption that textLine+
66 # is a footer, the first block is not.
67 isFooter = 0
68 footerComment = 0
69 blankLines = 0
70 }
Shawn O. Pearce9452e4e2009-08-22 18:17:46 -070071
David Pursehouse7119f942012-10-03 17:20:06 +090072 # Skip lines starting with "#" without any spaces before it.
73 /^#/ { next }
Shawn O. Pearce9452e4e2009-08-22 18:17:46 -070074
David Pursehouse7119f942012-10-03 17:20:06 +090075 # Skip the line starting with the diff command and everything after it,
76 # up to the end of the file, assuming it is only patch data.
77 # If more than one line before the diff was empty, strip all but one.
78 /^diff --git a/ {
79 blankLines = 0
80 while (getline) { }
81 next
82 }
83
84 # Count blank lines outside footer comments
85 /^$/ && (footerComment == 0) {
86 blankLines++
87 next
88 }
89
90 # Catch footer comment
91 /^\[[a-zA-Z0-9-]+:/ && (isFooter == 1) {
92 footerComment = 1
93 }
94
95 /]$/ && (footerComment == 1) {
96 footerComment = 2
97 }
98
99 # We have a non-blank line after blank lines. Handle this.
100 (blankLines > 0) {
101 print lines
102 for (i = 0; i < blankLines; i++) {
103 print ""
Shawn O. Pearce9452e4e2009-08-22 18:17:46 -0700104 }
105
David Pursehouse7119f942012-10-03 17:20:06 +0900106 lines = ""
107 blankLines = 0
108 isFooter = 1
109 footerComment = 0
110 }
Shawn O. Pearce9452e4e2009-08-22 18:17:46 -0700111
David Pursehouse7119f942012-10-03 17:20:06 +0900112 # Detect that the current block is not the footer
113 (footerComment == 0) && (!/^\[?[a-zA-Z0-9-]+:/ || /^[a-zA-Z0-9-]+:\/\//) {
114 isFooter = 0
115 }
116
117 {
118 # We need this information about the current last comment line
119 if (footerComment == 2) {
120 footerComment = 0
Shawn O. Pearce9452e4e2009-08-22 18:17:46 -0700121 }
David Pursehouse7119f942012-10-03 17:20:06 +0900122 if (lines != "") {
123 lines = lines "\n";
124 }
125 lines = lines $0
126 }
Shawn O. Pearce9452e4e2009-08-22 18:17:46 -0700127
David Pursehouse7119f942012-10-03 17:20:06 +0900128 # Footer handling:
129 # If the last block is considered a footer, splice in the Change-Id at the
130 # right place.
131 # Look for the right place to inject Change-Id by considering
132 # CHANGE_ID_AFTER. Keys listed in it (case insensitive) come first,
133 # then Change-Id, then everything else (eg. Signed-off-by:).
134 #
135 # Otherwise just print the last block, a new line and the Change-Id as a
136 # block of its own.
137 END {
138 unprinted = 1
139 if (isFooter == 0) {
140 print lines "\n"
141 lines = ""
142 }
143 changeIdAfter = "^(" tolower("'"$CHANGE_ID_AFTER"'") "):"
144 numlines = split(lines, footer, "\n")
145 for (line = 1; line <= numlines; line++) {
146 if (unprinted && match(tolower(footer[line]), changeIdAfter) != 1) {
147 unprinted = 0
148 print "Change-Id: I'"$id"'"
149 }
150 print footer[line]
151 }
152 if (unprinted) {
153 print "Change-Id: I'"$id"'"
154 }
155 }' "$MSG" > $T && mv $T "$MSG" || rm -f $T
Shawn O. Pearce9452e4e2009-08-22 18:17:46 -0700156}
157_gen_ChangeIdInput() {
David Pursehouse7119f942012-10-03 17:20:06 +0900158 echo "tree `git write-tree`"
159 if parent=`git rev-parse "HEAD^0" 2>/dev/null`
Shawn O. Pearce9452e4e2009-08-22 18:17:46 -0700160 then
161 echo "parent $parent"
162 fi
David Pursehouse7119f942012-10-03 17:20:06 +0900163 echo "author `git var GIT_AUTHOR_IDENT`"
164 echo "committer `git var GIT_COMMITTER_IDENT`"
Shawn O. Pearce9452e4e2009-08-22 18:17:46 -0700165 echo
166 printf '%s' "$clean_message"
167}
168_gen_ChangeId() {
169 _gen_ChangeIdInput |
170 git hash-object -t commit --stdin
171}
172
173
174add_ChangeId