summaryrefslogtreecommitdiffstats
path: root/lib/patchfns.in
blob: 63fae456da40a2f61ba999fa3f724bacde11687c (plain) (blame)
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
debug()
{
	#echo "$@" >&2
	true
}

if [ -n "$PATCHSCRIPTS" ]
then
	P=$PATCHSCRIPTS/
else
	unset P
fi

if [ -e .pc/series ]
then
	SERIES=.pc/series
elif [ -e series ]
then
	SERIES=series
else
	SERIES=${P}patches/series
fi

DB=".pc/applied-patches"

quote_re()
{
	echo "$1" | sed -e 's:\([/.+*\[\\]\):\\\1:g'
}

patch_file_name()
{
	local patch=$1

	if [ -e $SERIES ]
	then
		awk '/^'"$(quote_re $patch)"'(|\.patch|\.diff?)([ \t]|$)/ \
			{ printf "'"$P"'patches/%s\n", $1; exit }' \
		    $SERIES
	fi
}

# The -pN option and possibly others that should be passed to patch.
patch_args()
{
	local patch=$1

	if [ -e $SERIES ]
	then
		awk '/^'"$(quote_re $patch)"'(|\.patch|\.diff?)([ \t]|$)/ \
			{ if (NF >= 2)
				for (i=2; i <= NF; i++)
					print $i ;
			  else print "-p1" ;
			  exit }' \
		    $SERIES
	fi
}

patch_strip_level()
{
	local patch=$1 i
	for i in $(patch_args $patch)
	do
		case $i in
		-p)
			echo $2
			return ;;
		-p*)
			echo ${i:2}
			return ;;
		esac
	done
	echo "1"
}

change_db_strip_level()
{
	local level=$1 patch=$2

	if [ x$level != x-p1 ]
	then
		level="$level"
	else
		level=""
	fi

	if [ -e $SERIES ]
	then
		local tmp=$(mktemp /tmp/patch-scripts.XXXXXX)
		awk '/^'"$(quote_re $patch)"'(\.patch|\.diff?)([ \t]|$)/ \
			{for(i=2; i<=NF; i++)
				if ($i ~ /^-p/)
					{$i="'"$level"'" ; break}
			 if (i > NF)
				 $i="'"$level"'"}
			{print}' \
		    $SERIES > $tmp
		if cmp $SERIES $tmp >/dev/null 2>/dev/null
		then
			rm -f $tmp
		else
			mv -f $tmp $SERIES
		fi
	else
		return 1
	fi
}

patch_in_series()
{
	local patch=$1

	if ! [ -e $SERIES ]
	then
		return 1
	else
		grep -q '^'"$(quote_re $patch)"'\(\|\.patch\|.diff\?\)\([ \t]\|$\)' $SERIES
	fi
}

# Insert new patch after topmost patch
insert_in_series()
{
	local patch=$1 patch_args=$2
	local top=$(top_patch) tmp

	if [ -n "$patch_args" ]
	then
		patch_args=" $patch_args"
	fi

	tmp=$(mktemp /tmp/patch-scripts.XXXXXX) || return 1
	if [ -n "$top" ]
	then
		awk '		{ print }
		     /^'"$(quote_re $top)"'(|\.patch|\.diff?)([ \t]|$)/ \
		     		{ print "'"$patch$patch_args"'" }' \
		    < $SERIES \
		    > $tmp \
		    || return 1
	else
		echo $patch$patch_args > $tmp
		if [ -e $SERIES ]
		then
			cat $SERIES >> $tmp
		fi
	fi
	mkdir -p $(dirname $SERIES)
	mv -f $tmp $SERIES
}

pc_file_name()
{
	while [ $# -gt 0 ]
	do
		echo ".pc/$1/.pc"
		shift
	done

}

backup_file_name()
{
	local patch=$1
	while [ $# -gt 1 ]
	do
		echo ".pc/$patch/$2"
		shift
	done
}

__cat_series()
{
	local series=$1
	if [ -e $series ]
	then
		sed -e '/^#/d' -e 's/^[ \t]*//' -e 's/[ \t].*//' \
		    -e 's/\.patch$//' -e 's/\.diff\?//' $series
	else
		return 1
	fi
}

cat_series()
{
	__cat_series $SERIES
}

top_patch()
{
	[ -e $DB ] && tail -1 $DB
}

die()
{
	echo "error: $*"
	exit 1
}

is_numeric()
{
	echo $1 | grep -q -e '^[0-9]*$'
}

is_applied_last()
{
	local patch=$1
	[ "$(top_patch)" == $patch ]
}

is_applied()
{
	local patch=$1
	[ -e $DB ] || return 1
	grep -q "^$patch\$" $DB
}

patches_before()
{
	local patch=$1
	cat_series \
	| awk '$0 == "'"$patch"'"	{ exit }
					{ print }'
}

patches_after()
{
	local patch=$1
	cat_series \
	| awk 'seen			{ print }
	       $0 == "'"$patch"'"	{ seen=1 }'
}

# List all patches that have been applied on top of patch $1
patches_on_top_of()
{
	local patch=$1
	[ -e $DB ] || return
	awk '$0 == "'"$patch"'"		{ seen=1 ; next }
	     seen			{ print }' $DB
}

# Print the name of the patch that modified the file $2 next after
# patch $1, or print nothing if patch $1 is on top.
next_patch_for_file()
{
	local patch=$1 file=$2
	local patches_on_top=$(patches_on_top_of $patch)

	if [ -n "$patches_on_top" ]
	then
		grep -l -e "^$(quote_re $file)\$" \
			$(pc_file_name $patches_on_top) \
		| head -1 \
		| sed -e 's:^\.pc/::' -e 's:/\.pc$::'
	fi
}

can_apply()
{
	local silent
	if [ "x$1" == "x-s" ]
	then
		silent=-s
		shift
	fi
	local patch=$1
	patch -p1 --dry-run -f $silent -i $(patch_file_name $patch) < /dev/null
}

can_remove()
{
	local silent
	if [ "x$1" == "x-s" ]
	then
		silent=-s
		shift
	fi
	local patch=$1
	patch -R -p1 --dry-run -f $silent -i $(patch_file_name $patch) < /dev/null
}

add_to_db()
{
	echo $1 >> $DB
}

remove_from_db()
{
	local patch=$1
	local tmpfile
	tmpfile=$(mktemp /tmp/patch-scripts.XXXXXX)
	grep -v "^$patch\$" $DB > $tmpfile
	mv -f $tmpfile $DB
	[ -s $DB ] || rm -f $DB
}

stripit()
{
	if [ -n "$1" ]
	then
		echo $1 |
		sed -e 's/^\(\.\/\)*//' \
		    -e 's/^'"$P"'patches\///' -e 's/^\.pc\///' \
		    -e 's/\.patch$//' -e 's/\.diff\?$//'
	fi
}

file_in_patch()
{
	local file=$1 patch=$2
	files_in_patch $patch | grep -q "^$file\$"
}

files_in_patch()
{
	local pc_file=$(pc_file_name $1)
	if [ -e "$pc_file" ]
	then
		cat $pc_file
	fi
}

install_file_in_patch()
{
	local file=$1 patch=$2 bup_file=$(backup_file_name $patch $file)
	
	if ! echo $file  @LIB@/backup-file -s -B .pc/$patch/ || \
	   ! echo $file >> $(pc_file_name $patch)
	then
		return 1
	fi
}

refresh_file_list()
{
	local patch=$1
	local pc_file=$(pc_file_name $patch)
	local patch_file=$(patch_file_name $patch)
	
	if ! [ -e "$patch_file" ]
	then
		echo "Patch file $patch_file does not exist"
		return 1
	fi
	if [ ! -e $pc_file -o $pc_file -ot $patch_file ]
	then
		mkdir -p $(dirname $pc_file)
		echo "touched-by-patch $(patch_strip_level $patch) $patch_file > $pc_file" >&2
		@LIB@/touched-by-patch -p$(patch_strip_level $patch) \
			$patch_file > $pc_file
		if [ $? -ne 0 ]
		then
			return 1
		fi
		return 2
	fi
}

directory_names()
{
	echo "$@" \
	| awk 'BEGIN	{RS=" "}
			{sub(/\/?[^\/]*$/, "") ; printf "[%s]\n", $0}
			{if (last != $0)
				print last
			 last = $0}
		END	{print last}'
}

need_file_there()
{
	if [ ! -e $1 ]
	then
		echo "File $1 does not exist"
		exit 1
	fi
}

patch_description()
{
	local patch_file=$1

	if [ -e "$patch_file" ]
	then
		awk	'/^--- /	{exit}
			 diff		{print diff ; diff=""}
			 /^diff /	{diff=$0 ; next}
					{print}' \
			$patch_file
	fi
}