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