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