xref: /aosp_15_r20/external/flashrom/util/git-hooks/commit-msg (revision 0d6140be3aa665ecc836e8907834fcd3e3b018fc)
1#!/bin/sh
2#
3# Change-ID amending from Gerrit Code Review 2.14.2
4#
5# Part of Gerrit Code Review (https://www.gerritcodereview.com/)
6#
7# Copyright (C) 2009 The Android Open Source Project
8#
9# Any other changes including test_duplicate_signoffs_acks
10#
11# Copyright (C) 2017 Stefan Tauner
12#
13# 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
26unset GREP_OPTIONS
27
28CHANGE_ID_AFTER="Bug|Depends-On|Issue|Test|Feature|Fixes|Fixed|Staging-ID"
29MSG="$1"
30
31DEV_GUIDELINES_URL="https://www.flashrom.org/Development_Guidelines"
32
33# Check for, and add if missing, a unique Change-Id
34#
35add_ChangeId() {
36	clean_message=`sed -e '
37		/^diff --git .*/{
38			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
49	# *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
52	#	return
53	# fi
54
55	if test "false" = "`git config --bool --get gerrit.createChangeId`"
56	then
57		return
58	fi
59
60	# Does Change-Id: already exist? if so, exit (no change).
61	if grep -i '^Change-Id:' "$MSG" >/dev/null
62	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
74	# Get core.commentChar from git config or use default symbol
75	commentChar=`git config --get core.commentChar`
76	commentChar=${commentChar:-#}
77
78	# 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
96	# Skip lines starting with commentChar without any spaces before it.
97	/^'"$commentChar"'/ { next }
98
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.
102	/^diff --git / {
103		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		}
179	}' "$MSG" > "$T" && mv "$T" "$MSG" || rm -f "$T"
180}
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
197test_signoff() {
198	if ! grep -qi '^[[:space:]]*\+Signed-off-by:' "$MSG"; then
199		cat "$MSG"
200		printf "\nError: No Signed-off-by line in the commit message.\n"
201		printf "See: ${DEV_GUIDELINES_URL}\n"
202		exit 1
203	fi
204}
205
206# Test for duplicate signoffs/acks
207test_duplicate_signoffs_acks() {
208	test "" = "$(grep -i '^(Signed-off-by|Acked-by): ' "$MSG" |
209		 sort | uniq -c | sed -e '/^[[:space:]]*1[[:space:]]/d')" || {
210		cat "$MSG"
211		echo "Duplicate Signed-off-by or Acked-by lines." >&2
212		exit 1
213	}
214}
215
216main() {
217	test_signoff
218	test_duplicate_signoffs_acks
219	add_ChangeId
220}
221
222main
223