1#!/bin/bash
2#
3# NAME:
4#         gen-patch.sh - extract linuxized patches from the ACPICA git
5#                        repository
6#
7# SYNOPSIS:
8#         gen-patch.sh [-f from] [-i index] [-l link] [-m maintainer] [-n hash] [-u] [commit]
9#
10# DESCRIPTION:
11#         Extract linuxized patches from the git repository.
12#         Options:
13#          -i: Specify patch index.
14#          -l: Specify a URL prefix that can be used to link the commit.
15#          -m: Specify maintainer name <email> for Signed-off-by field.
16#          -n: Specify number of digits from commit hash to form a name.
17#          -u: Specify whether the commit is in an upstream repository.
18#          commit: GIT commit (default to HEAD).
19#
20
21usage() {
22	echo "Usage: `basename $0` [-f from] [-i index] [-l link] [-m maintainer] [-n hash] [-u] <commit>"
23	echo "Where:"
24	echo "     -i: Specify patch index (default to 0)."
25	echo "     -l: Specify a URL prefix that can be used to link the commit."
26	echo "     -m: Specify maintainer name <email> for Signed-off-by field."
27	echo "     -n: Specify number of digits from commit hash to form a name"
28	echo "         (default to 8)."
29	echo "     -u: Specify whether the commit is in an upstream repository."
30	echo " commit: GIT commit (default to HEAD)."
31	exit 1
32}
33
34COMMITTER="`git config user.name` <`git config user.email`>"
35INDEX=0
36UPSTREAM=no
37HASH=8
38
39while getopts "i:l:m:n:u" opt
40do
41	case $opt in
42	i) INDEX=$OPTARG;;
43	l) LINK=$OPTARG;;
44	n) HASH=$OPTARG;;
45	m) MAINTAINER=$OPTARG;;
46	u) UPSTREAM=yes
47	   if [ "x${LINK}" = "x" ]; then
48		LINK=https://github.com/acpica/acpica/commit/
49	   fi;;
50	?) echo "Invalid argument $opt"
51	   usage;;
52	esac
53done
54shift $(($OPTIND - 1))
55
56COMMIT=$1
57if [ "x${COMMIT}" = "x" ]; then
58	COMMIT=HEAD
59fi
60
61after=`git log -1 ${COMMIT} --format=%H`
62after_name=`git log -1 ${COMMIT} --format=%H | cut -c 1-${HASH}`
63before=`git log -1 ${COMMIT}^1 --format=%H`
64before_name=`git log -1 ${COMMIT}^1 --format=%H | cut -c 1-${HASH}`
65
66SCRIPT=`(cd \`dirname $0\`; pwd)`
67. $SCRIPT/libacpica.sh
68
69GP_acpica_repo=$CURDIR/acpica.repo
70GP_linux_before=$CURDIR/linux.before_name
71GP_linux_after=$CURDIR/linux.after_name
72GP_acpica_patch=$CURDIR/acpica-$after_name.patch
73GP_linux_patch=$CURDIR/linux-$after_name.patch
74
75echo "[gen-patch.sh] Extracting GIT ($SRCDIR)..."
76# Cleanup
77rm -rf $GP_linux_before
78rm -rf $GP_linux_after
79rm -rf $GP_acpica_repo
80git clone $SRCDIR $GP_acpica_repo > /dev/null || exit 2
81
82# Preset environments: LINK
83# Arg 1: commit ID
84generate_refs()
85{
86	if [ "x${LINK}" != "x" ]; then
87		echo "Link: ${LINK}$1"
88	fi
89}
90
91# Preset environments: AUTHOR, MAINTAINER, COMMITTER
92# Arg 1: commit ID
93generate_sobs()
94{
95	split_desc $1 1
96	echo "Signed-off-by: ${AUTHOR}"
97	if [ "x${MAINTAINER}" != "x" ]; then
98		echo "Signed-off-by: ${MAINTAINER}"
99	fi
100	echo "Signed-off-by: ${COMMITTER}"
101}
102
103# Preset environments: UPSTREAM, INDEX, COMMITTER
104# Arg 1: commit ID
105generate_acpica_desc()
106{
107	AUTHOR_NAME=`git log -1 $1 --format="%aN"`
108	AUTHOR_EMAIL=`git log -1 $1 --format="%aE"`
109	if [ "x${AUTHOR_NAME}" = "xRobert Moore" ]; then
110		AUTHOR_NAME="Bob Moore"
111	fi
112	if [ "x${AUTHOR_EMAIL}" = "xRobert.Moore@intel.com" ]; then
113		AUTHOR_EMAIL="robert.moore@intel.com"
114	fi
115	AUTHOR="${AUTHOR_NAME} <${AUTHOR_EMAIL}>"
116	FORMAT="From %H Mon Sep 17 00:00:00 2001%nFrom: $COMMITTER%nDate: %aD%nFrom: $AUTHOR%nSubject: [PATCH $INDEX] ACPICA: %s%n%n%b"
117	if [ "x$UPSTREAM" = "xyes" ]; then
118		FORMAT="From %H Mon Sep 17 00:00:00 2001%nFrom: $COMMITTER%nDate: %aD%nFrom: $AUTHOR%nSubject: [PATCH $INDEX] ACPICA: %s%n%nACPICA commit %H%n%n%b"
119	fi
120	GIT_LOG_FORMAT=`echo $FORMAT`
121	eval "git log -1 $1 --format=\"$GIT_LOG_FORMAT\""
122}
123
124# Arg 1: patch description file
125# Arg 2: 1=dump SOB block, 0=dump other text
126split_desc()
127{
128	tac $1 | DOSOB=$2 awk '
129	BEGIN { SOB=1 }
130	{
131		if (SOB==1) {
132			if (match($0, /^Signed-off-by:.*/)) {
133				if (ENVIRON["DOSOB"]==1)
134					print $0
135			} else if (match($0, /^Fixed-by:.*/)) {
136				if (ENVIRON["DOSOB"]==1)
137					print $0
138			} else if (match($0, /^Original-by:.*/)) {
139				if (ENVIRON["DOSOB"]==1)
140					print $0
141			} else if (match($0, /^Acked-by:.*/)) {
142				if (ENVIRON["DOSOB"]==1)
143					print $0
144			} else if (match($0, /^Reviewed-by:.*/)) {
145				if (ENVIRON["DOSOB"]==1)
146					print $0
147			} else if (match($0, /^Reported-by:.*/)) {
148				if (ENVIRON["DOSOB"]==1)
149					print $0
150			} else if (match($0, /^Tested-by:.*/)) {
151				if (ENVIRON["DOSOB"]==1)
152					print $0
153			} else if (match($0, /^Reported-and-tested-by:.*/)) {
154				if (ENVIRON["DOSOB"]==1)
155					print $0
156			} else if (match($0, /^Link:.*/)) {
157				if (ENVIRON["DOSOB"]==1)
158					print $0
159			} else if (match($0, /^Reference:.*/)) {
160				if (ENVIRON["DOSOB"]==1)
161					print $0
162			} else if (match($0, /^$/)) {
163			} else {
164				SOB=0
165				if (ENVIRON["DOSOB"]==0)
166					print $0
167			}
168		} else {
169			if (ENVIRON["DOSOB"]==0)
170				print $0
171		}
172	}
173	' | tac
174}
175
176# Preset environments: LINK, AUTHOR, MAINTAINER, COMMITTER
177# Arg 1: commit ID
178# Arg 2: patch description file
179generate_linux_desc()
180{
181	split_desc $2 0
182	echo ""
183	generate_refs $1
184	generate_sobs $2 | awk '
185	{
186		if (printed[$0]==0) {
187			print $0
188			printed[$0]=1;
189		}
190	}
191	'
192}
193
194echo "[gen-patch.sh] Creating ACPICA repository ($after_name)..."
195(
196	cd $GP_acpica_repo
197	git reset $after --hard >/dev/null 2>&1
198)
199
200echo "[gen-patch.sh] Creating ACPICA patch (acpica-$after_name.patch)..."
201(
202	cd $GP_acpica_repo
203	git format-patch -1 --stdout >> $GP_acpica_patch
204)
205
206echo "[gen-patch.sh] Creating Linux repository ($after_name)..."
207(
208	cd $GP_acpica_repo/generate/linux
209	if [ ! -f ./gen-repo.sh ]; then
210		cp $SRCDIR/generate/linux/gen-repo.sh ./
211	fi
212	./gen-repo.sh -c -n $HASH $after
213)
214mv -f $GP_acpica_repo/generate/linux/linux-$after_name $GP_linux_after
215
216echo "[gen-patch.sh] Creating ACPICA repository ($before_name)..."
217(
218	cd $GP_acpica_repo
219	git reset $before --hard >/dev/null 2>&1
220)
221
222echo "[gen-patch.sh] Creating Linux repository ($before_name)..."
223(
224	cd $GP_acpica_repo/generate/linux
225	if [ ! -f ./gen-repo.sh ]; then
226		cp $SRCDIR/generate/linux/gen-repo.sh ./
227	fi
228	./gen-repo.sh -c -n $HASH $before
229)
230mv -f $GP_acpica_repo/generate/linux/linux-$before_name $GP_linux_before
231
232(
233	echo "[gen-patch.sh] Creating Linux patch (linux-$after_name.patch)..."
234	cd $CURDIR
235	tmpdiff=`mktemp -u`
236	tmpdesc=`mktemp -u`
237	diff -Nurp linux.before_name linux.after_name >> $tmpdiff
238
239	if [ $? -ne 0 ]; then
240		generate_acpica_desc $after > $tmpdesc
241		generate_linux_desc $after_name $tmpdesc > $GP_linux_patch
242		$ACPISRC -ldqy $GP_linux_patch $GP_linux_patch > /dev/null
243		echo "---" >> $GP_linux_patch
244		diffstat $tmpdiff >> $GP_linux_patch
245		echo >> $GP_linux_patch
246		cat $tmpdiff >> $GP_linux_patch
247	else
248		echo "Warning: Linux version is empty, skipping $after_name..."
249	fi
250	rm -f $tmpdiff
251	rm -f $tmpdesc
252)
253
254# Cleanup temporary directories
255rm -rf $GP_linux_before
256rm -rf $GP_linux_after
257rm -rf $GP_acpica_repo
258