3 # Part of Gerrit Code Review (http://code.google.com/p/gerrit/)
5 # Copyright (C) 2009 The Android Open Source Project
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
11 # http://www.apache.org/licenses/LICENSE-2.0
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.
20 CHANGE_ID_AFTER="Bug|Issue"
23 # Check for, and add if missing, a unique Change-Id
26 clean_message=`sed -e '
33 ' "$MSG" | git stripspace`
34 if test -z "$clean_message"
39 # Does Change-Id: already exist? if so, exit (no change).
40 if grep -i '^Change-Id:' "$MSG" >/dev/null
48 if [ -x /usr/xpg4/bin/awk ]; then
49 # Solaris AWK is just too broken
53 # Skip lines starting with "#" without any spaces before it.
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.
67 # Handle comments and continuations in tags ([foo: bar] etc)
68 (caught == 1) && /^[ []/ {
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-]+:\/\// {
84 for (i = 0; i < blankLines; i++) {
98 # Handle tags. They are "caught" and collected in the "lines" variable
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:).
115 if (blankLines == 0) {
122 for (i = 0; i < blankLines; i++) {
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) {
130 print "Change-Id: I'"$id"'"
136 print "Change-Id: I'"$id"'"
138 }' "$MSG" > $T && mv $T "$MSG" || rm -f $T
140 _gen_ChangeIdInput() {
141 echo "tree `git write-tree`"
142 if parent=`git rev-parse "HEAD^0" 2>/dev/null`
144 echo "parent $parent"
146 echo "author `git var GIT_AUTHOR_IDENT`"
147 echo "committer `git var GIT_COMMITTER_IDENT`"
149 printf '%s' "$clean_message"
153 git hash-object -t commit --stdin