blob: fa250f388104fdd4a604b7a8733d9c49861021ac [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
Nico Huberc3b02dc2023-08-12 01:13:45 +020031DEV_GUIDELINES_URL="https://www.flashprog.org/Development_Guidelines"
David Hendricks28d08192019-09-08 13:59:42 -070032
Stefan Tauner76347082016-11-27 17:45:49 +010033# Check for, and add if missing, a unique Change-Id
34#
35add_ChangeId() {
36 clean_message=`sed -e '
Stefan Tauner3a937b72017-10-01 19:15:10 +020037 /^diff --git .*/{
Stefan Tauner76347082016-11-27 17:45:49 +010038 s///
39 q
40 }
41 /^Signed-off-by:/d
42 /^#/d
43 ' "$MSG" | git stripspace`
44 if test -z "$clean_message"
45 then
46 return
47 fi
48
Stefan Tauner3a937b72017-10-01 19:15:10 +020049 # *Do* add Change-Id to temp commits (original code bails out here)
50 # if echo "$clean_message" | head -1 | grep -q '^\(fixup\|squash\)!'
51 # then
Elyes HAOUASac01baa2018-05-28 16:52:21 +020052 # return
Stefan Tauner3a937b72017-10-01 19:15:10 +020053 # fi
54
55 if test "false" = "`git config --bool --get gerrit.createChangeId`"
56 then
57 return
58 fi
59
Stefan Tauner76347082016-11-27 17:45:49 +010060 # Does Change-Id: already exist? if so, exit (no change).
Stefan Tauner3a937b72017-10-01 19:15:10 +020061 if grep -i '^Change-Id:' "$MSG" >/dev/null
Stefan Tauner76347082016-11-27 17:45:49 +010062 then
63 return
64 fi
65
66 id=`_gen_ChangeId`
67 T="$MSG.tmp.$$"
68 AWK=awk
69 if [ -x /usr/xpg4/bin/awk ]; then
70 # Solaris AWK is just too broken
71 AWK=/usr/xpg4/bin/awk
72 fi
73
Stefan Tauner3a937b72017-10-01 19:15:10 +020074 # Get core.commentChar from git config or use default symbol
75 commentChar=`git config --get core.commentChar`
76 commentChar=${commentChar:-#}
77
Stefan Tauner76347082016-11-27 17:45:49 +010078 # How this works:
79 # - parse the commit message as (textLine+ blankLine*)*
80 # - assume textLine+ to be a footer until proven otherwise
81 # - exception: the first block is not footer (as it is the title)
82 # - read textLine+ into a variable
83 # - then count blankLines
84 # - once the next textLine appears, print textLine+ blankLine* as these
85 # aren't footer
86 # - in END, the last textLine+ block is available for footer parsing
87 $AWK '
88 BEGIN {
89 # while we start with the assumption that textLine+
90 # is a footer, the first block is not.
91 isFooter = 0
92 footerComment = 0
93 blankLines = 0
94 }
95
Stefan Tauner3a937b72017-10-01 19:15:10 +020096 # Skip lines starting with commentChar without any spaces before it.
97 /^'"$commentChar"'/ { next }
Stefan Tauner76347082016-11-27 17:45:49 +010098
99 # Skip the line starting with the diff command and everything after it,
100 # up to the end of the file, assuming it is only patch data.
101 # If more than one line before the diff was empty, strip all but one.
Stefan Tauner3a937b72017-10-01 19:15:10 +0200102 /^diff --git / {
Stefan Tauner76347082016-11-27 17:45:49 +0100103 blankLines = 0
104 while (getline) { }
105 next
106 }
107
108 # Count blank lines outside footer comments
109 /^$/ && (footerComment == 0) {
110 blankLines++
111 next
112 }
113
114 # Catch footer comment
115 /^\[[a-zA-Z0-9-]+:/ && (isFooter == 1) {
116 footerComment = 1
117 }
118
119 /]$/ && (footerComment == 1) {
120 footerComment = 2
121 }
122
123 # We have a non-blank line after blank lines. Handle this.
124 (blankLines > 0) {
125 print lines
126 for (i = 0; i < blankLines; i++) {
127 print ""
128 }
129
130 lines = ""
131 blankLines = 0
132 isFooter = 1
133 footerComment = 0
134 }
135
136 # Detect that the current block is not the footer
137 (footerComment == 0) && (!/^\[?[a-zA-Z0-9-]+:/ || /^[a-zA-Z0-9-]+:\/\//) {
138 isFooter = 0
139 }
140
141 {
142 # We need this information about the current last comment line
143 if (footerComment == 2) {
144 footerComment = 0
145 }
146 if (lines != "") {
147 lines = lines "\n";
148 }
149 lines = lines $0
150 }
151
152 # Footer handling:
153 # If the last block is considered a footer, splice in the Change-Id at the
154 # right place.
155 # Look for the right place to inject Change-Id by considering
156 # CHANGE_ID_AFTER. Keys listed in it (case insensitive) come first,
157 # then Change-Id, then everything else (eg. Signed-off-by:).
158 #
159 # Otherwise just print the last block, a new line and the Change-Id as a
160 # block of its own.
161 END {
162 unprinted = 1
163 if (isFooter == 0) {
164 print lines "\n"
165 lines = ""
166 }
167 changeIdAfter = "^(" tolower("'"$CHANGE_ID_AFTER"'") "):"
168 numlines = split(lines, footer, "\n")
169 for (line = 1; line <= numlines; line++) {
170 if (unprinted && match(tolower(footer[line]), changeIdAfter) != 1) {
171 unprinted = 0
172 print "Change-Id: I'"$id"'"
173 }
174 print footer[line]
175 }
176 if (unprinted) {
177 print "Change-Id: I'"$id"'"
178 }
Stefan Tauner3a937b72017-10-01 19:15:10 +0200179 }' "$MSG" > "$T" && mv "$T" "$MSG" || rm -f "$T"
Stefan Tauner76347082016-11-27 17:45:49 +0100180}
181_gen_ChangeIdInput() {
182 echo "tree `git write-tree`"
183 if parent=`git rev-parse "HEAD^0" 2>/dev/null`
184 then
185 echo "parent $parent"
186 fi
187 echo "author `git var GIT_AUTHOR_IDENT`"
188 echo "committer `git var GIT_COMMITTER_IDENT`"
189 echo
190 printf '%s' "$clean_message"
191}
192_gen_ChangeId() {
193 _gen_ChangeIdInput |
194 git hash-object -t commit --stdin
195}
196
David Hendricks28d08192019-09-08 13:59:42 -0700197test_signoff() {
198 if ! grep -qi '^[[:space:]]*\+Signed-off-by:' "$MSG"; then
199 printf "\nError: No Signed-off-by line in the commit message.\n"
200 printf "See: ${DEV_GUIDELINES_URL}\n"
201 exit 1
202 fi
203}
204
Stefan Taunerf3f996e2017-10-04 02:56:31 +0200205# Test for duplicate signoffs/acks
206test_duplicate_signoffs_acks() {
207 test "" = "$(grep -i '^(Signed-off-by|Acked-by): ' "$MSG" |
208 sort | uniq -c | sed -e '/^[[:space:]]*1[[:space:]]/d')" || {
209 echo "Duplicate Signed-off-by or Acked-by lines." >&2
210 exit 1
211 }
212}
Stefan Tauner76347082016-11-27 17:45:49 +0100213
Stefan Taunerf3f996e2017-10-04 02:56:31 +0200214main() {
David Hendricks28d08192019-09-08 13:59:42 -0700215 test_signoff
Stefan Taunerf3f996e2017-10-04 02:56:31 +0200216 test_duplicate_signoffs_acks
217 add_ChangeId
218}
219
220main