Stefan Tauner | 7634708 | 2016-11-27 17:45:49 +0100 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | # |
Stefan Tauner | 3a937b7 | 2017-10-01 19:15:10 +0200 | [diff] [blame^] | 3 | # Change-ID amending from Gerrit Code Review 2.14.2 |
| 4 | # |
| 5 | # Part of Gerrit Code Review (https://www.gerritcodereview.com/) |
Stefan Tauner | 7634708 | 2016-11-27 17:45:49 +0100 | [diff] [blame] | 6 | # |
| 7 | # Copyright (C) 2009 The Android Open Source Project |
| 8 | # |
| 9 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 10 | # you may not use this file except in compliance with the License. |
| 11 | # You may obtain a copy of the License at |
| 12 | # |
| 13 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 14 | # |
| 15 | # Unless required by applicable law or agreed to in writing, software |
| 16 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 17 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 18 | # See the License for the specific language governing permissions and |
| 19 | # limitations under the License. |
| 20 | # |
| 21 | |
Stefan Tauner | 3a937b7 | 2017-10-01 19:15:10 +0200 | [diff] [blame^] | 22 | unset GREP_OPTIONS |
| 23 | |
| 24 | CHANGE_ID_AFTER="Bug|Depends-On|Issue|Test|Feature|Fixes|Fixed|Staging-ID" |
Stefan Tauner | 7634708 | 2016-11-27 17:45:49 +0100 | [diff] [blame] | 25 | MSG="$1" |
| 26 | |
| 27 | # Check for, and add if missing, a unique Change-Id |
| 28 | # |
| 29 | add_ChangeId() { |
| 30 | clean_message=`sed -e ' |
Stefan Tauner | 3a937b7 | 2017-10-01 19:15:10 +0200 | [diff] [blame^] | 31 | /^diff --git .*/{ |
Stefan Tauner | 7634708 | 2016-11-27 17:45:49 +0100 | [diff] [blame] | 32 | s/// |
| 33 | q |
| 34 | } |
| 35 | /^Signed-off-by:/d |
| 36 | /^#/d |
| 37 | ' "$MSG" | git stripspace` |
| 38 | if test -z "$clean_message" |
| 39 | then |
| 40 | return |
| 41 | fi |
| 42 | |
Stefan Tauner | 3a937b7 | 2017-10-01 19:15:10 +0200 | [diff] [blame^] | 43 | # *Do* add Change-Id to temp commits (original code bails out here) |
| 44 | # if echo "$clean_message" | head -1 | grep -q '^\(fixup\|squash\)!' |
| 45 | # then |
| 46 | # return |
| 47 | # fi |
| 48 | |
| 49 | if test "false" = "`git config --bool --get gerrit.createChangeId`" |
| 50 | then |
| 51 | return |
| 52 | fi |
| 53 | |
Stefan Tauner | 7634708 | 2016-11-27 17:45:49 +0100 | [diff] [blame] | 54 | # Does Change-Id: already exist? if so, exit (no change). |
Stefan Tauner | 3a937b7 | 2017-10-01 19:15:10 +0200 | [diff] [blame^] | 55 | if grep -i '^Change-Id:' "$MSG" >/dev/null |
Stefan Tauner | 7634708 | 2016-11-27 17:45:49 +0100 | [diff] [blame] | 56 | then |
| 57 | return |
| 58 | fi |
| 59 | |
| 60 | id=`_gen_ChangeId` |
| 61 | T="$MSG.tmp.$$" |
| 62 | AWK=awk |
| 63 | if [ -x /usr/xpg4/bin/awk ]; then |
| 64 | # Solaris AWK is just too broken |
| 65 | AWK=/usr/xpg4/bin/awk |
| 66 | fi |
| 67 | |
Stefan Tauner | 3a937b7 | 2017-10-01 19:15:10 +0200 | [diff] [blame^] | 68 | # Get core.commentChar from git config or use default symbol |
| 69 | commentChar=`git config --get core.commentChar` |
| 70 | commentChar=${commentChar:-#} |
| 71 | |
Stefan Tauner | 7634708 | 2016-11-27 17:45:49 +0100 | [diff] [blame] | 72 | # How this works: |
| 73 | # - parse the commit message as (textLine+ blankLine*)* |
| 74 | # - assume textLine+ to be a footer until proven otherwise |
| 75 | # - exception: the first block is not footer (as it is the title) |
| 76 | # - read textLine+ into a variable |
| 77 | # - then count blankLines |
| 78 | # - once the next textLine appears, print textLine+ blankLine* as these |
| 79 | # aren't footer |
| 80 | # - in END, the last textLine+ block is available for footer parsing |
| 81 | $AWK ' |
| 82 | BEGIN { |
| 83 | # while we start with the assumption that textLine+ |
| 84 | # is a footer, the first block is not. |
| 85 | isFooter = 0 |
| 86 | footerComment = 0 |
| 87 | blankLines = 0 |
| 88 | } |
| 89 | |
Stefan Tauner | 3a937b7 | 2017-10-01 19:15:10 +0200 | [diff] [blame^] | 90 | # Skip lines starting with commentChar without any spaces before it. |
| 91 | /^'"$commentChar"'/ { next } |
Stefan Tauner | 7634708 | 2016-11-27 17:45:49 +0100 | [diff] [blame] | 92 | |
| 93 | # Skip the line starting with the diff command and everything after it, |
| 94 | # up to the end of the file, assuming it is only patch data. |
| 95 | # If more than one line before the diff was empty, strip all but one. |
Stefan Tauner | 3a937b7 | 2017-10-01 19:15:10 +0200 | [diff] [blame^] | 96 | /^diff --git / { |
Stefan Tauner | 7634708 | 2016-11-27 17:45:49 +0100 | [diff] [blame] | 97 | blankLines = 0 |
| 98 | while (getline) { } |
| 99 | next |
| 100 | } |
| 101 | |
| 102 | # Count blank lines outside footer comments |
| 103 | /^$/ && (footerComment == 0) { |
| 104 | blankLines++ |
| 105 | next |
| 106 | } |
| 107 | |
| 108 | # Catch footer comment |
| 109 | /^\[[a-zA-Z0-9-]+:/ && (isFooter == 1) { |
| 110 | footerComment = 1 |
| 111 | } |
| 112 | |
| 113 | /]$/ && (footerComment == 1) { |
| 114 | footerComment = 2 |
| 115 | } |
| 116 | |
| 117 | # We have a non-blank line after blank lines. Handle this. |
| 118 | (blankLines > 0) { |
| 119 | print lines |
| 120 | for (i = 0; i < blankLines; i++) { |
| 121 | print "" |
| 122 | } |
| 123 | |
| 124 | lines = "" |
| 125 | blankLines = 0 |
| 126 | isFooter = 1 |
| 127 | footerComment = 0 |
| 128 | } |
| 129 | |
| 130 | # Detect that the current block is not the footer |
| 131 | (footerComment == 0) && (!/^\[?[a-zA-Z0-9-]+:/ || /^[a-zA-Z0-9-]+:\/\//) { |
| 132 | isFooter = 0 |
| 133 | } |
| 134 | |
| 135 | { |
| 136 | # We need this information about the current last comment line |
| 137 | if (footerComment == 2) { |
| 138 | footerComment = 0 |
| 139 | } |
| 140 | if (lines != "") { |
| 141 | lines = lines "\n"; |
| 142 | } |
| 143 | lines = lines $0 |
| 144 | } |
| 145 | |
| 146 | # Footer handling: |
| 147 | # If the last block is considered a footer, splice in the Change-Id at the |
| 148 | # right place. |
| 149 | # Look for the right place to inject Change-Id by considering |
| 150 | # CHANGE_ID_AFTER. Keys listed in it (case insensitive) come first, |
| 151 | # then Change-Id, then everything else (eg. Signed-off-by:). |
| 152 | # |
| 153 | # Otherwise just print the last block, a new line and the Change-Id as a |
| 154 | # block of its own. |
| 155 | END { |
| 156 | unprinted = 1 |
| 157 | if (isFooter == 0) { |
| 158 | print lines "\n" |
| 159 | lines = "" |
| 160 | } |
| 161 | changeIdAfter = "^(" tolower("'"$CHANGE_ID_AFTER"'") "):" |
| 162 | numlines = split(lines, footer, "\n") |
| 163 | for (line = 1; line <= numlines; line++) { |
| 164 | if (unprinted && match(tolower(footer[line]), changeIdAfter) != 1) { |
| 165 | unprinted = 0 |
| 166 | print "Change-Id: I'"$id"'" |
| 167 | } |
| 168 | print footer[line] |
| 169 | } |
| 170 | if (unprinted) { |
| 171 | print "Change-Id: I'"$id"'" |
| 172 | } |
Stefan Tauner | 3a937b7 | 2017-10-01 19:15:10 +0200 | [diff] [blame^] | 173 | }' "$MSG" > "$T" && mv "$T" "$MSG" || rm -f "$T" |
Stefan Tauner | 7634708 | 2016-11-27 17:45:49 +0100 | [diff] [blame] | 174 | } |
| 175 | _gen_ChangeIdInput() { |
| 176 | echo "tree `git write-tree`" |
| 177 | if parent=`git rev-parse "HEAD^0" 2>/dev/null` |
| 178 | then |
| 179 | echo "parent $parent" |
| 180 | fi |
| 181 | echo "author `git var GIT_AUTHOR_IDENT`" |
| 182 | echo "committer `git var GIT_COMMITTER_IDENT`" |
| 183 | echo |
| 184 | printf '%s' "$clean_message" |
| 185 | } |
| 186 | _gen_ChangeId() { |
| 187 | _gen_ChangeIdInput | |
| 188 | git hash-object -t commit --stdin |
| 189 | } |
| 190 | |
| 191 | |
| 192 | add_ChangeId |