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