blob: d9bb188bb4a7f1d294e383b708ebed660663b770 [file] [log] [blame]
Shawn O. Pearce9452e4e2009-08-22 18:17:46 -07001#!/bin/sh
Dana Dahlstromcbe8aeb2017-12-06 10:39:49 -08002# From Gerrit Code Review 2.14.6
Shawn O. Pearce9452e4e2009-08-22 18:17:46 -07003#
Mike Bjorge93229642016-02-29 11:48:15 -08004# Part of Gerrit Code Review (https://www.gerritcodereview.com/)
Shawn O. Pearce9452e4e2009-08-22 18:17:46 -07005#
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
David Pursehouse55693aa2013-02-13 09:55:32 +090021unset GREP_OPTIONS
22
Dana Dahlstromcbe8aeb2017-12-06 10:39:49 -080023CHANGE_ID_AFTER="Bug|Depends-On|Issue|Test|Feature|Fixes|Fixed"
Shawn O. Pearce9452e4e2009-08-22 18:17:46 -070024MSG="$1"
25
26# Check for, and add if missing, a unique Change-Id
27#
28add_ChangeId() {
David Pursehouse7119f942012-10-03 17:20:06 +090029 clean_message=`sed -e '
Dave Borowitza8d53912014-07-15 11:30:06 -070030 /^diff --git .*/{
Shawn O. Pearce9452e4e2009-08-22 18:17:46 -070031 s///
32 q
33 }
34 /^Signed-off-by:/d
35 /^#/d
David Pursehouse7119f942012-10-03 17:20:06 +090036 ' "$MSG" | git stripspace`
Shawn O. Pearce9452e4e2009-08-22 18:17:46 -070037 if test -z "$clean_message"
38 then
39 return
40 fi
41
Mike Bjorge93229642016-02-29 11:48:15 -080042 # Do not add Change-Id to temp commits
43 if echo "$clean_message" | head -1 | grep -q '^\(fixup\|squash\)!'
44 then
45 return
46 fi
47
Dave Borowitza8d53912014-07-15 11:30:06 -070048 if test "false" = "`git config --bool --get gerrit.createChangeId`"
49 then
50 return
51 fi
52
David Pursehouse7119f942012-10-03 17:20:06 +090053 # Does Change-Id: already exist? if so, exit (no change).
Shawn O. Pearce9452e4e2009-08-22 18:17:46 -070054 if grep -i '^Change-Id:' "$MSG" >/dev/null
55 then
56 return
57 fi
58
David Pursehouse7119f942012-10-03 17:20:06 +090059 id=`_gen_ChangeId`
60 T="$MSG.tmp.$$"
61 AWK=awk
62 if [ -x /usr/xpg4/bin/awk ]; then
63 # Solaris AWK is just too broken
64 AWK=/usr/xpg4/bin/awk
65 fi
Shawn O. Pearce9452e4e2009-08-22 18:17:46 -070066
Mike Bjorge93229642016-02-29 11:48:15 -080067 # Get core.commentChar from git config or use default symbol
68 commentChar=`git config --get core.commentChar`
69 commentChar=${commentChar:-#}
70
David Pursehouse7119f942012-10-03 17:20:06 +090071 # How this works:
72 # - parse the commit message as (textLine+ blankLine*)*
73 # - assume textLine+ to be a footer until proven otherwise
74 # - exception: the first block is not footer (as it is the title)
75 # - read textLine+ into a variable
76 # - then count blankLines
77 # - once the next textLine appears, print textLine+ blankLine* as these
78 # aren't footer
79 # - in END, the last textLine+ block is available for footer parsing
80 $AWK '
81 BEGIN {
82 # while we start with the assumption that textLine+
83 # is a footer, the first block is not.
84 isFooter = 0
85 footerComment = 0
86 blankLines = 0
87 }
Shawn O. Pearce9452e4e2009-08-22 18:17:46 -070088
Mike Bjorge93229642016-02-29 11:48:15 -080089 # Skip lines starting with commentChar without any spaces before it.
90 /^'"$commentChar"'/ { next }
Shawn O. Pearce9452e4e2009-08-22 18:17:46 -070091
David Pursehouse7119f942012-10-03 17:20:06 +090092 # Skip the line starting with the diff command and everything after it,
93 # up to the end of the file, assuming it is only patch data.
94 # If more than one line before the diff was empty, strip all but one.
Dave Borowitza8d53912014-07-15 11:30:06 -070095 /^diff --git / {
David Pursehouse7119f942012-10-03 17:20:06 +090096 blankLines = 0
97 while (getline) { }
98 next
99 }
100
101 # Count blank lines outside footer comments
102 /^$/ && (footerComment == 0) {
103 blankLines++
104 next
105 }
106
107 # Catch footer comment
108 /^\[[a-zA-Z0-9-]+:/ && (isFooter == 1) {
109 footerComment = 1
110 }
111
112 /]$/ && (footerComment == 1) {
113 footerComment = 2
114 }
115
116 # We have a non-blank line after blank lines. Handle this.
117 (blankLines > 0) {
118 print lines
119 for (i = 0; i < blankLines; i++) {
120 print ""
Shawn O. Pearce9452e4e2009-08-22 18:17:46 -0700121 }
122
David Pursehouse7119f942012-10-03 17:20:06 +0900123 lines = ""
124 blankLines = 0
125 isFooter = 1
126 footerComment = 0
127 }
Shawn O. Pearce9452e4e2009-08-22 18:17:46 -0700128
David Pursehouse7119f942012-10-03 17:20:06 +0900129 # Detect that the current block is not the footer
130 (footerComment == 0) && (!/^\[?[a-zA-Z0-9-]+:/ || /^[a-zA-Z0-9-]+:\/\//) {
131 isFooter = 0
132 }
133
134 {
135 # We need this information about the current last comment line
136 if (footerComment == 2) {
137 footerComment = 0
Shawn O. Pearce9452e4e2009-08-22 18:17:46 -0700138 }
David Pursehouse7119f942012-10-03 17:20:06 +0900139 if (lines != "") {
140 lines = lines "\n";
141 }
142 lines = lines $0
143 }
Shawn O. Pearce9452e4e2009-08-22 18:17:46 -0700144
David Pursehouse7119f942012-10-03 17:20:06 +0900145 # Footer handling:
146 # If the last block is considered a footer, splice in the Change-Id at the
147 # right place.
148 # Look for the right place to inject Change-Id by considering
149 # CHANGE_ID_AFTER. Keys listed in it (case insensitive) come first,
150 # then Change-Id, then everything else (eg. Signed-off-by:).
151 #
152 # Otherwise just print the last block, a new line and the Change-Id as a
153 # block of its own.
154 END {
155 unprinted = 1
156 if (isFooter == 0) {
157 print lines "\n"
158 lines = ""
159 }
160 changeIdAfter = "^(" tolower("'"$CHANGE_ID_AFTER"'") "):"
161 numlines = split(lines, footer, "\n")
162 for (line = 1; line <= numlines; line++) {
163 if (unprinted && match(tolower(footer[line]), changeIdAfter) != 1) {
164 unprinted = 0
165 print "Change-Id: I'"$id"'"
166 }
167 print footer[line]
168 }
169 if (unprinted) {
170 print "Change-Id: I'"$id"'"
171 }
David Pursehouse61df4182013-11-29 19:17:23 +0900172 }' "$MSG" > "$T" && mv "$T" "$MSG" || rm -f "$T"
Shawn O. Pearce9452e4e2009-08-22 18:17:46 -0700173}
174_gen_ChangeIdInput() {
David Pursehouse7119f942012-10-03 17:20:06 +0900175 echo "tree `git write-tree`"
176 if parent=`git rev-parse "HEAD^0" 2>/dev/null`
Shawn O. Pearce9452e4e2009-08-22 18:17:46 -0700177 then
178 echo "parent $parent"
179 fi
David Pursehouse7119f942012-10-03 17:20:06 +0900180 echo "author `git var GIT_AUTHOR_IDENT`"
181 echo "committer `git var GIT_COMMITTER_IDENT`"
Shawn O. Pearce9452e4e2009-08-22 18:17:46 -0700182 echo
183 printf '%s' "$clean_message"
184}
185_gen_ChangeId() {
186 _gen_ChangeIdInput |
187 git hash-object -t commit --stdin
188}
189
190
191add_ChangeId