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