2 # From Gerrit Code Review 2.8.1
4 # Part of Gerrit Code Review (http://code.google.com/p/gerrit/)
6 # Copyright (C) 2009 The Android Open Source Project
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
12 # http://www.apache.org/licenses/LICENSE-2.0
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.
23 CHANGE_ID_AFTER="Bug|Issue"
26 # Check for, and add if missing, a unique Change-Id
29 clean_message=`sed -e '
36 ' "$MSG" | git stripspace`
37 if test -z "$clean_message"
42 if test "false" = "`git config --bool --get gerrit.createChangeId`"
47 # Does Change-Id: already exist? if so, exit (no change).
48 if grep -i '^Change-Id:' "$MSG" >/dev/null
56 if [ -x /usr/xpg4/bin/awk ]; then
57 # Solaris AWK is just too broken
62 # - parse the commit message as (textLine+ blankLine*)*
63 # - assume textLine+ to be a footer until proven otherwise
64 # - exception: the first block is not footer (as it is the title)
65 # - read textLine+ into a variable
66 # - then count blankLines
67 # - once the next textLine appears, print textLine+ blankLine* as these
69 # - in END, the last textLine+ block is available for footer parsing
72 # while we start with the assumption that textLine+
73 # is a footer, the first block is not.
79 # Skip lines starting with "#" without any spaces before it.
82 # Skip the line starting with the diff command and everything after it,
83 # up to the end of the file, assuming it is only patch data.
84 # If more than one line before the diff was empty, strip all but one.
91 # Count blank lines outside footer comments
92 /^$/ && (footerComment == 0) {
97 # Catch footer comment
98 /^\[[a-zA-Z0-9-]+:/ && (isFooter == 1) {
102 /]$/ && (footerComment == 1) {
106 # We have a non-blank line after blank lines. Handle this.
109 for (i = 0; i < blankLines; i++) {
119 # Detect that the current block is not the footer
120 (footerComment == 0) && (!/^\[?[a-zA-Z0-9-]+:/ || /^[a-zA-Z0-9-]+:\/\//) {
125 # We need this information about the current last comment line
126 if (footerComment == 2) {
136 # If the last block is considered a footer, splice in the Change-Id at the
138 # Look for the right place to inject Change-Id by considering
139 # CHANGE_ID_AFTER. Keys listed in it (case insensitive) come first,
140 # then Change-Id, then everything else (eg. Signed-off-by:).
142 # Otherwise just print the last block, a new line and the Change-Id as a
150 changeIdAfter = "^(" tolower("'"$CHANGE_ID_AFTER"'") "):"
151 numlines = split(lines, footer, "\n")
152 for (line = 1; line <= numlines; line++) {
153 if (unprinted && match(tolower(footer[line]), changeIdAfter) != 1) {
155 print "Change-Id: I'"$id"'"
160 print "Change-Id: I'"$id"'"
162 }' "$MSG" > "$T" && mv "$T" "$MSG" || rm -f "$T"
164 _gen_ChangeIdInput() {
165 echo "tree `git write-tree`"
166 if parent=`git rev-parse "HEAD^0" 2>/dev/null`
168 echo "parent $parent"
170 echo "author `git var GIT_AUTHOR_IDENT`"
171 echo "committer `git var GIT_COMMITTER_IDENT`"
173 printf '%s' "$clean_message"
177 git hash-object -t commit --stdin
182 SOB=$(git var GIT_AUTHOR_IDENT | sed -n 's/^\(.*>\).*$/Signed-off-by: \1/p')
183 grep -qs "^$SOB" "$1" || echo "$SOB" >> "$1"