blob: 2eef7521561161e0f606e10d2d33ffe1fadf7849 [file] [log] [blame]
Patrick Georgi3b81b9d2011-06-30 19:55:31 +02001#!/bin/sh
Patrick Georgi3b81b9d2011-06-30 19:55:31 +02002#
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
20CHANGE_ID_AFTER="Bug|Issue"
21MSG="$1"
22
23# Check for, and add if missing, a unique Change-Id
24#
25add_ChangeId() {
26 clean_message=`sed -e '
27 /^diff --git a\/.*/{
28 s///
29 q
30 }
31 /^Signed-off-by:/d
32 /^#/d
33 ' "$MSG" | git stripspace`
34 if test -z "$clean_message"
35 then
36 return
37 fi
38
Patrick Georgiad6331d2011-07-02 00:35:02 +020039 # Does Change-Id: already exist? if so, exit (no change).
Zheng Baoa5de9412012-08-27 16:58:39 +080040 if grep -i '^Change-Id: I[0-9a-f]\{40\}$' "$MSG" >/dev/null
Patrick Georgi3b81b9d2011-06-30 19:55:31 +020041 then
42 return
43 fi
44
45 id=`_gen_ChangeId`
Patrick Georgiad6331d2011-07-02 00:35:02 +020046 T="$MSG.tmp.$$"
47 AWK=awk
48 if [ -x /usr/xpg4/bin/awk ]; then
49 # Solaris AWK is just too broken
50 AWK=/usr/xpg4/bin/awk
51 fi
Patrick Georgic0ea5432012-03-07 09:30:03 +010052
53 # How this works:
54 # - parse the commit message as (textLine+ blankLine*)*
55 # - assume textLine+ to be a footer until proven otherwise
56 # - exception: the first block is not footer (as it is the title)
57 # - read textLine+ into a variable
58 # - then count blankLines
59 # - once the next textLine appears, print textLine+ blankLine* as these
60 # aren't footer
61 # - in END, the last textLine+ block is available for footer parsing
Patrick Georgiad6331d2011-07-02 00:35:02 +020062 $AWK '
Patrick Georgic0ea5432012-03-07 09:30:03 +010063 BEGIN {
64 # while we start with the assumption that textLine+
65 # is a footer, the first block is not.
66 isFooter = 0
67 footerComment = 0
68 blankLines = 0
69 }
70
Patrick Georgiad6331d2011-07-02 00:35:02 +020071 # Skip lines starting with "#" without any spaces before it.
72 /^#/ { next }
Patrick Georgi3b81b9d2011-06-30 19:55:31 +020073
Patrick Georgiad6331d2011-07-02 00:35:02 +020074 # Skip the line starting with the diff command and everything after it,
75 # up to the end of the file, assuming it is only patch data.
76 # If more than one line before the diff was empty, strip all but one.
77 /^diff --git a/ {
Patrick Georgic0ea5432012-03-07 09:30:03 +010078 blankLines = 0
Patrick Georgiad6331d2011-07-02 00:35:02 +020079 while (getline) { }
80 next
81 }
Patrick Georgi3b81b9d2011-06-30 19:55:31 +020082
Patrick Georgic0ea5432012-03-07 09:30:03 +010083 # Count blank lines outside footer comments
84 /^$/ && (footerComment == 0) {
85 blankLines++
Patrick Georgiad6331d2011-07-02 00:35:02 +020086 next
87 }
Patrick Georgi3b81b9d2011-06-30 19:55:31 +020088
Patrick Georgic0ea5432012-03-07 09:30:03 +010089 # Catch footer comment
90 /^\[[a-zA-Z0-9-]+:/ && (isFooter == 1) {
91 footerComment = 1
Patrick Georgiad6331d2011-07-02 00:35:02 +020092 }
Patrick Georgi3b81b9d2011-06-30 19:55:31 +020093
Patrick Georgic0ea5432012-03-07 09:30:03 +010094 /]$/ && (footerComment == 1) {
95 footerComment = 2
96 }
97
98 # We have a non-blank line after blank lines. Handle this.
99 (blankLines > 0) {
100 print lines
101 for (i = 0; i < blankLines; i++) {
102 print ""
103 }
104
105 lines = ""
106 blankLines = 0
107 isFooter = 1
108 footerComment = 0
109 }
110
111 # Detect that the current block is not the footer
112 (footerComment == 0) && (!/^\[?[a-zA-Z0-9-]+:/ || /^[a-zA-Z0-9-]+:\/\//) {
113 isFooter = 0
114 }
115
Patrick Georgiad6331d2011-07-02 00:35:02 +0200116 {
Patrick Georgic0ea5432012-03-07 09:30:03 +0100117 # We need this information about the current last comment line
118 if (footerComment == 2) {
119 footerComment = 0
120 }
Patrick Georgiad6331d2011-07-02 00:35:02 +0200121 if (lines != "") {
122 lines = lines "\n";
123 }
124 lines = lines $0
125 }
126
Patrick Georgic0ea5432012-03-07 09:30:03 +0100127 # Footer handling:
128 # If the last block is considered a footer, splice in the Change-Id at the
129 # right place.
130 # Look for the right place to inject Change-Id by considering
131 # CHANGE_ID_AFTER. Keys listed in it (case insensitive) come first,
Patrick Georgiad6331d2011-07-02 00:35:02 +0200132 # then Change-Id, then everything else (eg. Signed-off-by:).
Patrick Georgic0ea5432012-03-07 09:30:03 +0100133 #
134 # Otherwise just print the last block, a new line and the Change-Id as a
135 # block of its own.
Patrick Georgiad6331d2011-07-02 00:35:02 +0200136 END {
137 unprinted = 1
Patrick Georgic0ea5432012-03-07 09:30:03 +0100138 if (isFooter == 0) {
139 print lines "\n"
140 lines = ""
141 }
142 changeIdAfter = "^(" tolower("'"$CHANGE_ID_AFTER"'") "):"
143 numlines = split(lines, footer, "\n")
144 for (line = 1; line <= numlines; line++) {
145 if (unprinted && match(tolower(footer[line]), changeIdAfter) != 1) {
146 unprinted = 0
147 print "Change-Id: I'"$id"'"
Patrick Georgiad6331d2011-07-02 00:35:02 +0200148 }
Patrick Georgic0ea5432012-03-07 09:30:03 +0100149 print footer[line]
Patrick Georgiad6331d2011-07-02 00:35:02 +0200150 }
151 if (unprinted) {
152 print "Change-Id: I'"$id"'"
153 }
154 }' "$MSG" > $T && mv $T "$MSG" || rm -f $T
Patrick Georgi3b81b9d2011-06-30 19:55:31 +0200155}
156_gen_ChangeIdInput() {
157 echo "tree `git write-tree`"
Patrick Georgiad6331d2011-07-02 00:35:02 +0200158 if parent=`git rev-parse "HEAD^0" 2>/dev/null`
Patrick Georgi3b81b9d2011-06-30 19:55:31 +0200159 then
160 echo "parent $parent"
161 fi
162 echo "author `git var GIT_AUTHOR_IDENT`"
163 echo "committer `git var GIT_COMMITTER_IDENT`"
164 echo
165 printf '%s' "$clean_message"
166}
167_gen_ChangeId() {
168 _gen_ChangeIdInput |
169 git hash-object -t commit --stdin
170}
171
Martin Roth0bb936d2017-08-08 20:31:46 -0600172if ! grep -qi '^[[:space:]]*\+Signed-off-by:' "$MSG"; then
173 printf "\nError: No Signed-off-by line in the commit message.\n"
174 exit 1
175fi
Patrick Georgi3b81b9d2011-06-30 19:55:31 +0200176add_ChangeId