summaryrefslogtreecommitdiff
path: root/mailimport
blob: 7b233932ee1022780ba948a6cea2af5a8de94ee2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
#!/bin/bash
# Copyright (c) 2006 Mauro Carvalho Chehab (mchehab@infradead.org)
# This code is placed under the terms of the GNU General Public License
#
#This script is capable to mass import patches on a mbox file, test for it, allow
#comment editing and applying it.
#
#This script requires:
#1) git, since it uses gitimport to break a mbox into patches;
#2) hg mailqueue. It is easier to manage patches using mq, allowing to work with
#   the patches before applying to the tree.

#uncomment to use mq
#usemq=1

#uncomment to stop at the first error while testing a patch with --dry-run
#exitonerror=1

head=v4l/scripts/hghead.pl

edit_patch()
{
	next=$1

	if [ "`grep 'has been added to the -mm tree.  Its filename is' $next`" != "" ]; then
		newfile="$TMPDIR/next2_$$"

		echo Processing -mm file $next

		perl -e 'while (<>) { if (m/Date:/) { print $_; };
			if (m/^------------------------------------------------------/) {
				print <>; exit;
			}}' $next >$newfile;
		mv $tmp/$newfile $next;
	fi
	echo Editing $next
	$EDITOR $next
}

####################################################################
# Tries to apply a patch at the tree

apply_patch () {
	next=$1

	unset cont
	until [ "$cont" == "0" ]; do
		cont=1
		pdir=linux
		echo patch -s -t -p1 --dry-run -l -N -d $pdir -i $next
		patch -s -t -p1 --dry-run -l -N -d $pdir -i $next
		if [ "$?" != "0" ]; then
			pdir=.
			echo patch -s -t -p1 --dry-run -l -N -d $pdir -i $next
			patch -s -t -p1 --dry-run -l -N -d $pdir -i $next
		fi

		if [ "$?" != "0" ]; then
			echo "*** ERROR: Patch didn't applied well"
			if [ "$exitonerror" != "" ]; then
				$head $next
				exit
			fi
			echo "** Edit file $next"
			sleep 1
			edit_patch $next
		else
			echo "Patch applied OK against $pdir"
			cont=0
		fi
	done

	edit_patch $next

	unset cont
	until [ "$cont" == "0" ]; do
		cont=0
		$head $next >$TMP2

		if [ "`grep '^Bad:' $TMP2`" != "" ]; then
			echo "*** ERROR: Patch bad formed. Please fix."
			sleep 1
			$EDITOR $next
			cont=1
		fi
	done

	committer=`grep "Committer:" $TMP2|sed s/"#Committer: "//`
	date=`grep "Date:" $TMP2|sed s/"#Date: "//|sed s/\(.*\)//`

	if [ "$usemq" != "" ]; then
		name=`cat $next | perl -ne '
			if (s/Subject:\s+(.*)/$1/) {
				m/\s*(.*)\s*\n/;
				$_="$1";

				tr/[A-Z]/[a-z]/;
				s/[^a-z0-9]/_/g;
				s/_+$//;
				s/_+/_/g;
				s/^v4l_dvb_\d+[a-z]*_//g;

				printf "%s.patch",$_;
				exit;
			}'`

		cat $next| grep -v "^#" >$TMPDIR/$name

		echo hg -d "$date" -m "`cat $TMP2|grep -v "^#"`" qnew $name
		hg qnew -d "$date" -m "`cat $TMP2|grep -v "^#"`" $name
		make cardlist
		make whitespace
		hg qrefresh
	else
		patch -s -t -p1 -l -N -d $pdir -i $next
		if [ "$?" != "0" ]; then
			echo "*** ERROR at: patch -s -t -p1 -l -N -d $pdir -i $next"
			exit
		fi
		make cardlist
		make whitespace

		cur=`pwd`
		cd $pdir
		hg addremove `diffstat -p1 -l $next`
		if [ "$?" != "0" ]; then
			echo "*** ERROR at hg addremove"
			exit
		fi

		# Commit the changed files
		CARDLIST="`hg status -n -m|grep CARDLIST.`"
		FILES=""
		for i in `diffstat -p1 -l $next`; do
			FILES="$FILES `pwd`/$i"
		done

		if [ "$FILES" == "" ]; then
			echo "*** ERROR nothing to commit"
			cd $cur
			exit
		fi

		hg commit -d "$date" -u "$committer" -m "`cat $TMP2|grep -v "^#"`" $CARDLIST $FILES

		if [ "$?" != "0" ]; then
			echo "*** ERROR at hg commit"
			cd $cur
			exit
		fi
		cd $cur
	fi
	hg log -r -1 -v
}

####################################################################
# Proccess a new patch

proccess_patch ()
{
	i="$_"

	echo $i
	if [ "`diffstat -p1 -l "$i"`" == "" ]; then
		echo "*** ERROR nothing to commit"
		cd $cur
		exit
	fi

	cat $i| git-mailinfo $DIR/msg $DIR/patch >$DIR/author
	cat $DIR/msg|grep -vi ^CC: >$DIR/msg2

	cat $DIR/author|perl -ne "if (m/Author[:]\\s*(.*)\\n/) { \$auth=\$1; } else \
		{ if (m/Email[:]\\s*(.*)\\n/) { print \"From: \$auth \<\$1\>\n\"; } else \
		{ if (m/Subject[:]\\s*(.*)\\n/) { \
		\$sub=\$1; \
		\$sub=~ s;^(media|dvb|v4l|video|drivers|[-_/: \\t])*(.*);\$2;; \
		print \"Subject: \$sub\\n\"; \
		} else  \
		{ print; } } \
		}" >$DIR/author2

	out=$DIR/patch.diff

	cat $DIR/author2

	sob="Signed-off-by: $CHANGE_LOG_NAME <$CHANGE_LOG_EMAIL_ADDRESS>"

	if [ "$(perl -ne 'if (m/$sob/) { print $_; }' $DIR/msg2)" == "" ]; then
		echo $sob >>$DIR/msg2
	fi

	echo "cat $DIR/author2 $DIR/msg2 $DIR/patch >$out"
	$CHECKPATCH -q --notree $i|perl -ne '{ print "# $_"; }' >$out
	cat $DIR/author2 $DIR/msg2 $DIR/patch >>$out

	apply_patch $out
}

####################################################################
# Main

if [ "$1" == "" ]; then
	echo "Usage: $0 <mbox>"
	exit
fi

if [ "$TMPDIR" == "" ]; then
	TMPDIR=/tmp
fi

if [ "$EDITOR" == "" ]; then
	EDITOR="nano -w"
fi

if [ "$CHECKPATCH" == "" ]; then
	CHECKPATCH="/lib/modules/`uname -r`/build/scripts/checkpatch.pl"
fi

NAME="$1"

if [ ! -r $NAME ]; then
	sudo chmod -R og+r $NAME
fi

DIR="$TMPDIR/mailimport$$"
mkdir $DIR
if [ "$?" != "0" ]; then
	echo "*** Error at mkdir $DIR"
	exit;
fi
trap "rm -rf $DIR" EXIT

if [ -d "$NAME" ]; then
	TMP2="$DIR/patchheader"

	if [ -e "$NAME/series" ]; then
		echo "Processing quilt tree $NAME"
		for i in `cat "$NAME/series"|grep -v "^#"`; do
			echo "$NAME/$i"
			proccess_patch "$NAME/$i"
		done
	else
		echo "Processing patches from tree $NAME"
		for i in $NAME/*; do
			if [ ! -r $i ]; then
				sudo chmod og+r $i
			fi

			echo "$i"
			proccess_patch "$i"
		done
	fi
else
	TMP2=$DIR/patchheader

	grep -v -f - $NAME >$DIR/tmpbox <<EOF
^Content-Type:
^--Boundary-
^Content-Disposition: inline
^Content-Transfer-Encoding: 8bit
EOF

	echo git-mailsplit -b $DIR/tmpbox $DIR
	echo -n "Number of patches at file: "
	git-mailsplit -b $DIR/tmpbox $DIR
	echo

	for i in $DIR/0*; do
		echo $i
		proccess_patch $i
	done
fi