summaryrefslogtreecommitdiffstats
path: root/scripts/patchfns.in
blob: 60f855e93543de2bde1b845b3ce8cc6b57c5284b (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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
# This file contains the common functions used in all quilt script
# It is meant to be sourced by bash scripts.

#  This script is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License version 2 as
#  published by the Free Software Foundation.
#
#  See the COPYING and AUTHORS files for more details.

export TEXTDOMAIN=quilt
		

if [ -e $HOME/.quiltrc ]
then
	. $HOME/.quiltrc
fi

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 a string for use in a basic regular expression.
quote_bre()
{
	echo "$1" | @SED@ -e 's:\([][^$/.*\\]\):\\\1:g'
}

# Quote a string for use in an extended regular expression.
quote_re()
{
	echo "$1" | @SED@ -e 's:\([][?{(|)}^$/.+*\\]\):\\\1:g'
}

basename()
{
        local path="${1//\/\//}"
        path="${path%%/}"
	echo "${path##*/}"
}

dirname()
{
        local path="${1//\/\//}"
        path="${path%%/}"
	local basename="${path##*/}"
	path="${path:0:${#path}-${#basename}}"
	[ x"$path" != x"/" ] && path="${path%/}"
	echo "${path:-.}"
}

patch_file_name()
{
	local patch=$1

	if [ -e $SERIES ]
	then
		@AWK@ '/^'"$(quote_re $patch)"'(|\.patch|\.diff?)(|\.gz|\.bz2)([ \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?)(|\.gz|\.bz2)([ \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 tmpfile=$(gen_tempfile)
		@AWK@ '
		/^'"$(quote_re $patch)"'(\.patch|\.diff?)(|\.gz|\.bz2)([ \t]|$)/ \
			{ for(i=2; i<=NF; i++)
				if ($i ~ /^-p/) {
					$i="'"$level"'"
					break
				}
			  if (i > NF)
				 $i="'"$level"'"
			}
			{ print }
		' $SERIES > $tmpfile
		if cmp $SERIES $tmpfile >/dev/null 2>/dev/null
		then
			rm -f $tmpfile
		else
			mv -f $tmpfile $SERIES
		fi
	else
		return 1
	fi
}

patch_in_series()
{
	local patch=$1

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

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

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

	tmpfile=$(gen_tempfile) || return 1
	mkdir -p $(dirname $SERIES)
	if [ -n "$top" ]
	then
		@AWK@ '
				{ print }
		/^'"$(quote_re $top)"'(|\.patch|\.diff?)(|\.gz|\.bz2)([ \t]|$)/ \
				{ print "'"$patch$patch_args"'" }
		' $SERIES > $tmpfile
		status=$?
		if [ $status -ne 0 ]
		then
			rm -f $tmpfile
			return 1
		fi
	else
		echo $patch$patch_args > $tmpfile
		if [ -e $SERIES ]
		then
			cat $SERIES >> $tmpfile
		fi
	fi
	mv -f $tmpfile $SERIES
}

remove_from_series()
{
	local patch=$1

	tmpfile=$(gen_tempfile) || return 1
	@AWK@ '
	! /^'"$(quote_re $patch)"'(|\.patch|\.diff?)(|\.gz|\.bz2)([ \t]|$)/ \
				{ print }
	' $SERIES > $tmpfile
	if [ $? -eq 0 ]
	then
		mv -f $tmpfile $SERIES
	else
		rm -f $tmpfile
	   	return 1
	fi
}
save_patch_name()
{
	local patch=$1
	while [ $# -gt 0 ]
	do
		echo "${patch}.quiltsave"
		shift
	done

}
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()
{
	if [ -e $SERIES ]
	then
		@SED@ -e '/^#/d' -e 's/^[ '$'\t'']*//' -e 's/[ '$'\t''].*//' \
		      -e 's/\.gz$//' -e 's/\.bz2$//' \
		      -e 's/\.patch$//' -e 's/\.diff\?$//' $SERIES
	else
		return 1
	fi
}

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

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

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

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

applied_patches()
{
	[ -e $DB ] || return 1
	cat $DB
}

applied_before()
{
	local patch=$1

	if [ -n "$patch" ]
	then
		@AWK@ '
		$0 == "'"$patch"'"	{ exit }
					{ print }
		' $DB
	fi
}
		
patches_before()
{
	local patch=$1

	if [ -n "$patch" ]
	then
		cat_series \
		| @AWK@ '
		$0 == "'"$patch"'"	{ exit }
					{ print }
		'
	fi
}

patches_after()
{
	local patch=$1
	if [ -n "$patch" ]
	then
		cat_series \
		| @AWK@ '
		seen			{ print }
		$0 == "'"$patch"'"	{ seen=1 }
		'
	else
		cat_series
	fi
}

# 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 -n 1 \
		| @SED@ -e 's:^\.pc/::' -e 's:/\.pc$::'
	fi

	#modified_files $file -- $patches_on_top \
	#| cut -d $'\t' -f2 \
	#| cut -d ' ' -f1
}

# Create a list of files and the patches that modify them.
refresh_patches_per_file()
{
	local pc_files=$(pc_file_name $(cat_series))
	local ex_pc_files pc_file

	if [ -e .pc/patches-per-file ]
	then
		local needs_refresh
		for pc_file in pc_files
		do
			if [ .pc/patches-per-file -ot $pc_file ]
			then
				needs_refresh=1
				break
			fi
		done
		if [ -z "$needs_refresh" ]
		then
			return 0
		fi
	fi

	for pc_file in $pc_files
	do
		if [ -e $pc_file ]
		then
			ex_pc_files[${#ex_pc_files[@]}]=$pc_file
		fi
	done

	if [ ${#ex_pc_files[@]} -eq 0 ]
	then
		rm -f .pc/patches-per-file
		return 0
	fi

	@AWK@ '
	ARGIND!=saved	{ sub(/^.pc\//, "", FILENAME)
			  sub(/\/\.pc/, "", FILENAME)
			  saved=ARGIND
			}
			{ if (files[$0])
				files[$0]=files[$0] " " FILENAME
			  else
				files[$0]=FILENAME
			}
	END		{ for (file in files)
				printf "%s\t%s\n", file, files[file]
			}
	' "${ex_pc_files[@]}" > .pc/patches-per-file
}

# For a lists of patches and a list of files, compute which patches
# modify which files. Invoked as
# 	modified_files file ... [-- patch ...]
#
modified_files()
{
	if ! refresh_patches_per_file
	then
		return 1
	fi

	@AWK@ '
	BEGIN		{ no_files=1
			  no_patches=1
			  for (i=1; i<ARGC; i++) {
				if (ARGV[i]=="--")
					break
				files[ARGV[i]]=1
				no_files=0
			  }
			  for (i++; i<ARGC; i++) {
				 patches[ARGV[i]]=1
				 no_patches=0
			  }
			  split("", ARGV) # read from standard input
			}
	no_files || files[$1] {
			  if (no_patches) {
				  print
				  next
			  }
			  for (i=2; i<=NF; i++)
				if ($i in patches) {
					printf "%s\t%s", $1, $i
					for (i++; i<=NF; i++)
						if ($i in patches)
							 printf " %s", $i
					printf "\n"
				}
			}
	' "$@" < .pc/patches-per-file
}

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

remove_from_db()
{
	local patch=$1
	local tmpfile
	if tmpfile=$(gen_tempfile)
	then
		grep -v -E "^$(quote_re $patch)\$" $DB > $tmpfile
		mv -f $tmpfile $DB
		rm -f $tmpfile
		[ -s $DB ] || rm -f $DB
	fi
}

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

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

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

touched_by_patch()
{
	local strip=$1 patch=$2
	cat_file $(patch_file_name $patch) \
	|  @AWK@ '
	/^\+\+\+[ \t]/ {
		sub(/^\+\+\+[ \t]/, "")
		sub(/[ \t].*/, "")
		sub(/^\/dev\/null/, "")
		for (i=0; i<'$strip'; i++)
			sub(/^[^\/]*\//, "")
		print
	}'
}

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
		return 0
	fi
	if [ ! -e $pc_file -o \
	     $pc_file -ot $patch_file -o \
	     $pc_file -ot $SERIES ]
	then
		local tmpfile status
		if ! mkdir -p $(dirname $pc_file) || \
		   ! tmpfile=$(gen_tempfile)
		then
			return 1
		fi

		# Do not reorder files in the file list...

		if [ -e $pc_file ]
		then
			cat $pc_file >> $tmpfile
		fi
		if ! touched_by_patch $(patch_strip_level $patch) \
			$patch >> $tmpfile
		then
			return 1
		fi
		@AWK@ '	{ if (seen[$0]) next
			  seen[$0]=1
			  print
			}' $tmpfile > $pc_file
		rm $tmpfile
		return 0
	fi
}

fix_diff_header()
{
	local from=$1 to=$2
	@SED@ -e 's/^--- [^ '$'\t'']*/--- '"$(quote_bre $from)"'/' \
	      -e 's/^+++ [^ '$'\t'']*/+++ '"$(quote_bre $to)"'/'
}

diff_file()
{
	local file=$1 old_file=$2 new_file=$3
	local old_hdr new_hdr line

	if [ ! -e "$old_file" -a ! -e "$new_file" ]
	then
		return 0
	fi
	if [ $opt_strip_level -eq 0 ]
	then
		old_hdr=$file.orig
		new_hdr=$file
	else
		local dir=$(basename $PWD)
		old_hdr=$dir.orig/$file
		new_hdr=$dir/$file
	fi

	@DIFF@ -Nu $QUILT_DIFF_OPTS $old_file $new_file |
	if read line
	then
		echo "Index: $new_hdr"
		echo "==================================================================="
		(echo "$line" ; cat) \
		| fix_diff_header $old_hdr $new_hdr
	fi
}

cat_file()
{
	local filename
	
	for filename in "$@"
	do
		if [ -e "$filename" ]
		then
			case "$filename" in
			*.gz|*.tgz)
				gzip -cd "$filename" ;;
			*.bz2)
				bzip2 -cd "$filename" ;;
			*)
				cat "$filename" ;;
			esac
		fi
	done
}

cat_to_file()
{
	local filename="$1"

	if [ -z "$filename" ]
	then
		cat
	else
		case "$filename" in
		*.gz|*.tgz)
			gzip -c > "$filename" ;;
		*.bz2)
			bzip2 -c > "$filename" ;;
		*)
			cat > "$filename" ;;
		esac
	fi
}

patch_description()
{
	local patch_file=$1

	if [ -e "$patch_file" -o -z "$patch_file" ]
	then
		@AWK@ '
		$1 == "---"	{ exit }
               /^Index:[ \t]|^diff[ \t]|^==*$/ \
                               { eat = eat $0
				  next }
		eat		{ print eat
				  eat="" }
				{ print }
		' $patch_file
	fi
}

in_array()
{
	local a=$1
	while [ $# -gt 1 ]
	do
		shift
		[ "$a" = "$1" ] && return 0
	done
	return 1
}

gen_tempfile()
{
	# This is a substitute for the mktemp executable.
	internal_mktemp()
	{
		local try n
		if [ x"$1" = x"-d" ]
		then
			for ((n=0 ; $n<100 ; n++))
			do
				try=${2%XXXXXX}$RANDOM
				mkdir -m 700 $try 2>/dev/null \
				&& break
			done
		else
			local user_mask=$(umask)
			umask 077
			set -o noclobber
			for ((n=0 ; $n<100 ; n++))
			do
				try=${1%XXXXXX}$RANDOM
				echo -n "" 2> /dev/null > $try \
				&& break
			done
			set +o noclobber
			umask $user_mask
		fi
		if [ $n -lt 100 ]
		then
			echo $try
		else
			return 1
		fi
	}

	local dir
	if [ x"$1" = x"-d" ]
	then
		dir=-d
		shift
	fi
	@MKTEMP@ $dir ${1:-${TMPDIR:-/tmp}/quilt}.XXXXXX
}
### Local Variables:
### mode: shell-script
### End:
# vim:filetype=sh