blob: 08f04ffe8d22a548601a71fc8acc0573d989950c [file] [log] [blame]
Martin Roth0ad5fbd2020-12-24 12:06:38 -07001#!/usr/bin/env sh
Patrick Georgi3b81b9d2011-06-30 19:55:31 +02002# Part of Gerrit Code Review (http://code.google.com/p/gerrit/)
3#
Patrick Georgi3b81b9d2011-06-30 19:55:31 +02004# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15#
16
17CHANGE_ID_AFTER="Bug|Issue"
18MSG="$1"
19
20# Check for, and add if missing, a unique Change-Id
21#
22add_ChangeId() {
23 clean_message=`sed -e '
24 /^diff --git a\/.*/{
25 s///
26 q
27 }
28 /^Signed-off-by:/d
29 /^#/d
30 ' "$MSG" | git stripspace`
31 if test -z "$clean_message"
32 then
33 return
34 fi
35
Patrick Georgiad6331d2011-07-02 00:35:02 +020036 # Does Change-Id: already exist? if so, exit (no change).
Zheng Baoa5de9412012-08-27 16:58:39 +080037 if grep -i '^Change-Id: I[0-9a-f]\{40\}$' "$MSG" >/dev/null
Patrick Georgi3b81b9d2011-06-30 19:55:31 +020038 then
39 return
40 fi
41
42 id=`_gen_ChangeId`
Patrick Georgiad6331d2011-07-02 00:35:02 +020043 T="$MSG.tmp.$$"
44 AWK=awk
45 if [ -x /usr/xpg4/bin/awk ]; then
46 # Solaris AWK is just too broken
47 AWK=/usr/xpg4/bin/awk
48 fi
Patrick Georgic0ea5432012-03-07 09:30:03 +010049
50 # How this works:
51 # - parse the commit message as (textLine+ blankLine*)*
52 # - assume textLine+ to be a footer until proven otherwise
53 # - exception: the first block is not footer (as it is the title)
54 # - read textLine+ into a variable
55 # - then count blankLines
56 # - once the next textLine appears, print textLine+ blankLine* as these
57 # aren't footer
58 # - in END, the last textLine+ block is available for footer parsing
Patrick Georgiad6331d2011-07-02 00:35:02 +020059 $AWK '
Patrick Georgic0ea5432012-03-07 09:30:03 +010060 BEGIN {
61 # while we start with the assumption that textLine+
62 # is a footer, the first block is not.
63 isFooter = 0
64 footerComment = 0
65 blankLines = 0
66 }
67
Patrick Georgiad6331d2011-07-02 00:35:02 +020068 # Skip lines starting with "#" without any spaces before it.
69 /^#/ { next }
Patrick Georgi3b81b9d2011-06-30 19:55:31 +020070
Patrick Georgiad6331d2011-07-02 00:35:02 +020071 # Skip the line starting with the diff command and everything after it,
72 # up to the end of the file, assuming it is only patch data.
73 # If more than one line before the diff was empty, strip all but one.
74 /^diff --git a/ {
Patrick Georgic0ea5432012-03-07 09:30:03 +010075 blankLines = 0
Patrick Georgiad6331d2011-07-02 00:35:02 +020076 while (getline) { }
77 next
78 }
Patrick Georgi3b81b9d2011-06-30 19:55:31 +020079
Patrick Georgic0ea5432012-03-07 09:30:03 +010080 # Count blank lines outside footer comments
81 /^$/ && (footerComment == 0) {
82 blankLines++
Patrick Georgiad6331d2011-07-02 00:35:02 +020083 next
84 }
Patrick Georgi3b81b9d2011-06-30 19:55:31 +020085
Patrick Georgic0ea5432012-03-07 09:30:03 +010086 # Catch footer comment
87 /^\[[a-zA-Z0-9-]+:/ && (isFooter == 1) {
88 footerComment = 1
Patrick Georgiad6331d2011-07-02 00:35:02 +020089 }
Patrick Georgi3b81b9d2011-06-30 19:55:31 +020090
Patrick Georgic0ea5432012-03-07 09:30:03 +010091 /]$/ && (footerComment == 1) {
92 footerComment = 2
93 }
94
95 # We have a non-blank line after blank lines. Handle this.
96 (blankLines > 0) {
97 print lines
98 for (i = 0; i < blankLines; i++) {
99 print ""
100 }
101
102 lines = ""
103 blankLines = 0
104 isFooter = 1
105 footerComment = 0
106 }
107
108 # Detect that the current block is not the footer
109 (footerComment == 0) && (!/^\[?[a-zA-Z0-9-]+:/ || /^[a-zA-Z0-9-]+:\/\//) {
110 isFooter = 0
111 }
112
Patrick Georgiad6331d2011-07-02 00:35:02 +0200113 {
Patrick Georgic0ea5432012-03-07 09:30:03 +0100114 # We need this information about the current last comment line
115 if (footerComment == 2) {
116 footerComment = 0
117 }
Patrick Georgiad6331d2011-07-02 00:35:02 +0200118 if (lines != "") {
119 lines = lines "\n";
120 }
121 lines = lines $0
122 }
123
Patrick Georgic0ea5432012-03-07 09:30:03 +0100124 # Footer handling:
125 # If the last block is considered a footer, splice in the Change-Id at the
126 # right place.
127 # Look for the right place to inject Change-Id by considering
128 # CHANGE_ID_AFTER. Keys listed in it (case insensitive) come first,
Patrick Georgiad6331d2011-07-02 00:35:02 +0200129 # then Change-Id, then everything else (eg. Signed-off-by:).
Patrick Georgic0ea5432012-03-07 09:30:03 +0100130 #
131 # Otherwise just print the last block, a new line and the Change-Id as a
132 # block of its own.
Patrick Georgiad6331d2011-07-02 00:35:02 +0200133 END {
134 unprinted = 1
Patrick Georgic0ea5432012-03-07 09:30:03 +0100135 if (isFooter == 0) {
136 print lines "\n"
137 lines = ""
138 }
139 changeIdAfter = "^(" tolower("'"$CHANGE_ID_AFTER"'") "):"
140 numlines = split(lines, footer, "\n")
141 for (line = 1; line <= numlines; line++) {
142 if (unprinted && match(tolower(footer[line]), changeIdAfter) != 1) {
143 unprinted = 0
144 print "Change-Id: I'"$id"'"
Patrick Georgiad6331d2011-07-02 00:35:02 +0200145 }
Patrick Georgic0ea5432012-03-07 09:30:03 +0100146 print footer[line]
Patrick Georgiad6331d2011-07-02 00:35:02 +0200147 }
148 if (unprinted) {
149 print "Change-Id: I'"$id"'"
150 }
151 }' "$MSG" > $T && mv $T "$MSG" || rm -f $T
Patrick Georgi3b81b9d2011-06-30 19:55:31 +0200152}
153_gen_ChangeIdInput() {
154 echo "tree `git write-tree`"
Patrick Georgiad6331d2011-07-02 00:35:02 +0200155 if parent=`git rev-parse "HEAD^0" 2>/dev/null`
Patrick Georgi3b81b9d2011-06-30 19:55:31 +0200156 then
157 echo "parent $parent"
158 fi
159 echo "author `git var GIT_AUTHOR_IDENT`"
160 echo "committer `git var GIT_COMMITTER_IDENT`"
161 echo
162 printf '%s' "$clean_message"
163}
164_gen_ChangeId() {
165 _gen_ChangeIdInput |
166 git hash-object -t commit --stdin
167}
168
Zheng Baoe8183592023-03-01 12:35:14 +0800169if ! grep -qi '^[[:space:]]*Signed-off-by:.\+<.\+@.\+>' "$MSG"; then
Martin Roth0bb936d2017-08-08 20:31:46 -0600170 printf "\nError: No Signed-off-by line in the commit message.\n"
171 exit 1
172fi
Patrick Georgi3b81b9d2011-06-30 19:55:31 +0200173add_ChangeId