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