This file is indexed.

/usr/share/quilt/scripts/patchfns is in quilt 0.63-8.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

   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
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
# 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
export TEXTDOMAINDIR=/usr/share/locale

: ${LC_CTYPE:=$LANG}
: ${LC_MESSAGES:=$LANG}
ORIGINAL_LANG=$LANG
export LANG=POSIX

export QUILT_PATCHES QUILT_PC SUBDIR SERIES DB
DB_VERSION=2

: ${QUILT_PC:=.pc}

# Support compatibility layer
if [ -d $QUILT_DIR/compat ]
then
	export PATH="$QUILT_DIR/compat:$PATH"
fi

unset CDPATH
shopt -s dotglob

if [ -e "$QUILTRC" ]
then
	source "$QUILTRC"
fi

# Add default arguments for this command
if [ -n "$QUILT_COMMAND" ]; then
	args="QUILT_$(echo $QUILT_COMMAND | tr a-z A-Z)_ARGS"
	eval set -- ${!args} \"\$@\"
	unset args
fi

. $QUILT_DIR/scripts/utilfns

# ========================================================

declare -a exit_handlers

add_exit_handler()
{
	exit_handlers[${#exit_handlers[@]}]=$1
}

remove_exit_handler()
{
	declare -a handlers
	local h
	for h in "${exit_handlers[@]}"
	do
		[ "$h" = "$1" ] && continue
		handlers[${#handlers[@]}]=$h
	done
	exit_handlers=( "${handlers[@]}" )
}

run_exit_handlers()
{
	local h
	for h in "${exit_handlers[@]}"
	do
		eval $h
	done
}

trap run_exit_handlers EXIT

# ========================================================

# Join multiple stings using the given separator.
array_join()
{
	local sep=$1 str=$2
	shift 2

	printf %s "$str"

	for str in "$@"
	do
		printf %s%s "$sep" "$str"
	done
}

# 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'
}

# Quote a string for use in a globing pattern.
quote_glob()
{
	echo "$1" | sed -e 's:\([][*?\\]\):\\\1:g'
}

patch_file_name()
{
	echo "$QUILT_PATCHES/$1"
}

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

	if [ -e $SERIES ]
	then
		awk '
			{sub(/(^|[ \t]+)#.*/, "") }
		$1 == "'"$patch"'" \
			{ p_printed=0
			  for (i=2; i<=NF; i++) {
				print $i
				if ($i ~ /^-p/)
					p_printed=1
			  }
			  if (!p_printed)
				print "-p1" ;
			  exit
			}
		' $SERIES
	fi
}

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

# Also remove -R if present.
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 '
		function remove_arg(nr, j) {
			for (j=nr; j<NF; j++)
				$j=$(j+1)
			NF--
		}
		function insert_arg(nr, value, j) {
			for (j=NF; j>=nr; j--)
				$(j+1)=$j
			$nr=value
		}

		/^'"$(quote_re $patch)"'([ \t]|$)/ \
			{ p_printed=0
			  for (i=2; i<=NF && $i !~ /^#/; i++) {
				if ($i ~ /^-p/) {
					if ("'"$level"'" == "")
						remove_arg(i--)
					else
						$i="'"$level"'"
					p_printed=1
					continue
				}
				if ($i == "-R") {
					remove_arg(i--)
					continue
				}
			  }
			  if (!p_printed && "'"$level"'" != "")
				insert_arg(2, "'"$level"'")
			}
			{ print }
		' $SERIES > $tmpfile
		if ! cmp $SERIES $tmpfile >/dev/null 2>/dev/null
		then
			cat $tmpfile > $SERIES
		fi
		rm -f $tmpfile
	else
		return 1
	fi
}

patch_in_series()
{
	local patch=$1

	if [ -e $SERIES ]
	then
		grep -q "^$(quote_bre $patch)\([ \t]\|$\)" $SERIES
	else
		return 1
	fi
}

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

	if [ -e "$SERIES" -a ! -f "$SERIES" ]; then
		printf $"%s is not a regular file\n" "$SERIES" >&2
		return 1
	fi

	before=${3-$(patch_after "$(top_patch)")}

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

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

remove_from_series()
{
	local patch=$1

	tmpfile=$(gen_tempfile) || return 1
	awk '
	! /^'"$(quote_re $patch)"'([ \t]|$)/ \
				{ print }
	' $SERIES > $tmpfile
	if [ $? -ne 0 ]
	then
		rm -f $tmpfile
		return 1
	fi
	cat $tmpfile > $SERIES
	rm -f $tmpfile
}

rename_in_series()
{
	local from=$1 to=$2

	tmpfile=$(gen_tempfile) || return 1
	awk '
	/^'"$(quote_re $from)"'([ \t]|$)/ \
		{ sub(/'"$(quote_re $from)"'/, "'"${to//\"/\\\"}"'")
		  good=1 }
		{ print }
	END	{ exit(! good) }
	' $SERIES > $tmpfile
	if [ $? -ne 0 ]
	then
		rm -f $tmpfile
		return 1
	fi
	cat $tmpfile > $SERIES
	rm -f $tmpfile
}

rename_in_db()
{
	local from=$1 to=$2
	local tmpfile
	tmpfile=$(gen_tempfile) || return 1
	awk '
	/^'"$(quote_re $from)"'$/ \
		{ sub(/'"$(quote_re $from)"'/, "'"${to//\"/\\\"}"'")
		  good=1 }
		{ print }
	END	{ exit(! good) }
	' $DB > $tmpfile
	if [ $? -eq 0 ]
	then
		cat $tmpfile > $DB
		rm -f $tmpfile
	else
		rm -f $tmpfile
		return 1
	fi
}

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

cat_series()
{
	if [ -e $SERIES ]
	then
		sed -e '/^#/d' -e 's/^[ '$'\t'']*//' \
		    -e 's/[ '$'\t''].*//' -e '/^$/d' $SERIES
	else
		if [ "$opt_verbose" ]; then
			echo $"No series file found\n" >&2
		fi
		return 1
	fi
}

top_patch()
{
	local patch=$(tail -n 1 $DB 2>/dev/null)
	[ -n "$patch" ] && echo "$patch"
}

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

is_applied()
{
	local patch=$1
	[ -e $DB ] || return 1
	grep -q "^$(quote_bre $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 == 1		{ print }
		$0 == "'"$patch"'"	{ seen=1 }
		'
	else
		cat_series
	fi
}

patch_after()
{
	local patch="$1"
	patch=$(patches_after "$patch" | head -n 1)
	[ -n "$patch" ] && echo "$patch"
}

# 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 == 1		{ 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
		for patch in $patches_on_top
		do
			if file_in_patch "$file" $patch
			then
				echo $patch
				break
			fi
		done
	fi
}

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

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

find_first_patch()
{
	local patch=$(cat_series | head -n 1)
	if [ -z "$patch" ]
	then
		if [ -e "$SERIES" ]
		then
			printf $"No patches in series\n" >&2
		else
			printf $"No series file found\n" >&2
		fi
		return 1
	fi

	echo "$patch"
}

find_last_patch()
{
	local patch=$(cat_series | tail -n 1)
	if [ -z "$patch" ]
	then
		if [ -e "$SERIES" ]
		then
			printf $"No patches in series\n" >&2
		else
			printf $"No series file found\n" >&2
		fi
		return 1
	fi

	echo "$patch"
}

find_top_patch()
{
	if ! top_patch
	then
		if find_first_patch > /dev/null
		then
			printf $"No patches applied\n" >&2
		fi
		return 1
	fi
}

find_patch()
{
	local name="$1"
	if [ -e "$SERIES" ]
	then
		if ! [ -f "$SERIES" ]
		then
			printf $"%s is not a regular file\n" "$SERIES" >&2
			return 1
		fi

		local patch=${1#$SUBDIR_DOWN$QUILT_PATCHES/}
		local bre=$(quote_bre "$patch")
		set -- $(sed -e "/^$bre\(\|\.patch\|\.diff\?\)\(\|\.gz\|\.bz2\|\.xz\|\.lzma\)\([ "$'\t'"]\|$\)/!d" \
			       -e 's/[ '$'\t''].*//' $SERIES)
		if [ $# -eq 1 ]
		then
			echo $1
			return 0
		else
			# We may have an exact match, which overrides
			# extension expansion
			while [ $# -gt 0 ]
			do
				if [ $1 = "$patch" ]
				then
					echo $1
					return 0
				fi
				shift
			done
		fi
	fi
	# Finding the first patch will error when the series is empty
	if [ -n "$(find_first_patch)" ]
	then
		printf $"Patch %s is not in series\n" "$name" >&2
	fi
	return 1
}

find_patch_in_series()
{
	local name="$1"

	if [ -n "$name" ]
	then
		find_patch "$name"
	else
		find_top_patch
	fi
}

find_applied_patch()
{
	local name="$1"

	if [ -n "$name" ]
	then
		local patch

		patch=$(find_patch "$name") || return 1
		if ! is_applied "$patch"
		then
			printf $"Patch %s is not applied\n" \
			       "$(print_patch $patch)" >&2
			return 1
		fi
		echo "$patch"
	else
		find_top_patch
	fi
}

find_unapplied_patch()
{
	local name="$1"

	if [ -n "$name" ]
	then
		local patch

		patch=$(find_patch "$name") || return 1
		if is_applied "$patch"
		then
			printf $"Patch %s is currently applied\n" \
				"$(print_patch $patch)" >&2
			return 2
		fi
		echo "$patch"
	else
		local start

		if start=$(top_patch)
		then
			patch_after "$start"
		else
			find_first_patch || return 2
		fi
		if [ $? -ne 0 ]
		then
			printf $"File series fully applied, ends at patch %s\n" \
				"$(print_patch $start)" >&2
			return 2
		fi
	fi
}

file_in_patch()
{
	local file=$1 patch=$2
	[ -f "$QUILT_PC/$patch/$file" ]
}

files_in_patch()
{
	local patch="$1"
	local path="$QUILT_PC/$patch"

	if [ -d "$path" ]
	then
		find "$path" -type f \
			       -a ! -path "$(quote_glob "$path")/.timestamp" |
		sed -e "s/$(quote_bre "$path")\///"
	fi
}

filenames_in_patch()
{
	local patch=$1
	local patch_file=$(patch_file_name $patch)
	if [ -e "$patch_file" ]
	then
		local strip=$(patch_strip_level $patch)
		[ "$strip" = ab ] && strip=1
		awk '
		($1 == "+++" || $1 == "---" || $1 == "***") && \
		$3 != "----" && $3 != "****" \
			{ sub(/\t.*/, "")
			  sub(/^... /, "")
			  for (n=0 ; n<'"$strip"'; n++)
			      sub(/^([^\/]+\/)/, "")
			  print $0 }' $patch_file
	fi
}

files_in_patch_ordered()
{
	local patch=$1

	(   files_in_patch $patch | sort
	    echo "-"
	    filenames_in_patch $patch
	) | awk '
	$1 == "-" { out=1 ; next }
	!out	{ files[$0]=1
		  new_files[++n]=$0 }
	out	{ if ($0 in files && !($0 in printed)) {
		    print $0
		    printed[$0]=1
		  }
		}
	END	{
		  for (i=1; i<=n; i++)
		    if (!(new_files[i] in printed))
		      print new_files[i]
		}
	'
}

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

	: ${opt_strip_level:=1}
	if [ $opt_strip_level = ab ]
	then
		old_hdr=a/$file
		new_hdr=b/$file
	elif [ $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
	index=$new_hdr

	if [ -s "$old_file" ]
	then
		[ -n "$QUILT_NO_DIFF_TIMESTAMPS" ] \
		|| old_date=$'\t'$(date +'%Y-%m-%d %H:%M:%S.%N %z' \
					-r "$old_file")
	else
		old_file=/dev/null
		old_hdr=/dev/null
		[ -n "$QUILT_NO_DIFF_TIMESTAMPS" ] \
		|| old_date=$'\t'"1970-01-01 00:00:00.000000000 +0000"
	fi
	if [ -e "$new_file" ]
	then
		[ -n "$QUILT_NO_DIFF_TIMESTAMPS" ] \
		|| new_date=$'\t'$(date +'%Y-%m-%d %H:%M:%S.%N %z' \
					-r "$new_file")
	else
		[ $opt_strip_level = 0 ] \
		&& old_hdr=$new_hdr
		new_file=/dev/null
		new_hdr=/dev/null
		[ -n "$QUILT_NO_DIFF_TIMESTAMPS" ] \
		|| new_date=$'\t'"1970-01-01 00:00:00.000000000 +0000"
	fi

	diff $QUILT_DIFF_OPTS \
	     --label "$old_hdr$old_date" --label "$new_hdr$new_date" \
	     "$old_file" "$new_file" \
	| if read line
	then
		if [ -z "$QUILT_NO_DIFF_INDEX" ]
		then
			echo "Index: $index"
			echo "==================================================================="
		fi
		echo "$line"
		cat
	fi

	# Test the return value of diff, and propagate the error retcode if any
	if [ ${PIPESTATUS[0]} == 2 ]
	then
		return 1
	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" ;;
			*.xz)
				xz -cd "$filename" ;;
			*.lzma)
				lzma -cd "$filename" ;;
			*)
				cat "$filename" ;;
			esac
		fi
	done
}

cat_to_new_file()
{
	local filename="$1"

	[ -e "$filename" ] && rm -f "$filename"

	case "$filename" in
	*.gz)
		gzip -c ;;
	*.bz2)
		bzip2 -c ;;
	*.xz)
		xz -c ;;
	*.lzma)
		lzma -c ;;
	*)
		cat ;;
	esac \
	> "$filename"
}

patch_header()
{
	awk '
	/^(---|\*\*\*|Index:)[ \t][^ \t]|^diff -/ \
		{ exit }
		{ print }
	'
}

patch_body()
{
	awk '
	!body && /^(---|\*\*\*|Index:)[ \t][^ \t]|^diff -/ \
		{ body=1 }
	body	{ print }
	'
}

strip_diffstat()
{
	awk '
	/#? .* \| / \
		{ eat = eat $0 "\n"
		  next }
	/^#? .* files? changed(, .* insertions?\(\+\))?(, .* deletions?\(-\))?/ \
		{ eat = ""
		  next }
		{ print eat $0
		  eat = "" }
	'
}

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

first_modified_by()
{
	local file=$1 patch
	local -a patches
	if [ $# -eq 0 ]
	then
		patches=( $(applied_patches) )
	else
		shift
		patches=( "$@" )
	fi
	for patch in ${patches[@]}
	do
		if [ -f "$QUILT_PC/$patch/$file" ]
		then
			echo $patch
			return 0
		fi
	done
	return 1
}

apply_patch_temporarily()
{
	local workdir=$1 patch=$2 patch_file patch_args

	patch_file=$(patch_file_name "$patch")
	patch_args=$(patch_args "$patch")

	shift 2

	local prefix=$QUILT_PC/$patch/
	[ ${prefix:0:1} == / ] || prefix=$PWD/$prefix
	if ! ( cd $workdir && \
	       $QUILT_DIR/scripts/backup-files -B $prefix -r -k -s "${@:--}" )
	then
		printf $"Failed to copy files to temporary directory\n" >&2
		return 1
	fi

	if [ -s $patch_file ]
	then
		if ! cat_file $patch_file \
		     | patch -d $workdir $QUILT_PATCH_OPTS $patch_args \
			     --no-backup-if-mismatch -f \
			     >/dev/null 2>/dev/null
		then
			# Generating a relative diff for a subset of files in
			# the patch will fail. Also, if a patch was force
			# applied, we know that it won't apply cleanly. In
			# all other cases, print a warning.

			if [ ! -e $QUILT_PC/$patch~refresh -a $# -eq 0 ]
			then
				printf $"Failed to patch temporary files\n" >&2
				return 1
			fi
		fi
	fi
}

next_filename()
{
	local patch=$1 base num
	base=$(echo "$patch" \
	       | sed -r -e 's:(\.gz|\.bz2|\.xz|\.lzma)$::' -e 's:(\.diff?|\.patch)$::')
	num=$(echo "$base" | sed -nre 's:.*-([0-9]+)$:\1:'p)
	[ -n "$num" ] || num=1
	echo "${base%-$num}-$((num+1))${patch#$base}"
}

create_db()
{
	if ! [ -e $QUILT_PC ]
	then
		mkdir -p $QUILT_PC
		echo $DB_VERSION > $QUILT_PC/.version
	fi
	if ! [ -e $QUILT_PC/.quilt_patches ]
	then
		echo "$QUILT_PATCHES" > $QUILT_PC/.quilt_patches
	fi
	if ! [ -e $QUILT_PC/.quilt_series ]
	then
		echo "$QUILT_SERIES" > $QUILT_PC/.quilt_series
	fi
}

version_check()
{
	[ -e $QUILT_PC ] || return 0

	if [ -e $QUILT_PC/.version ]
	then
		version="$(< $QUILT_PC/.version)"
		if [ "$version" -gt $DB_VERSION ]
		then
			printf $"The quilt meta-data in this tree has version %s, but this version of quilt can only handle meta-data formats up to and including version %s. Please pop all the patches using the version of quilt used to push them before downgrading.\n" "$version" "$DB_VERSION" >&2
			exit 1
		elif [ "$version" = $DB_VERSION ]
		then
			return 0
		fi
	fi
	return 1
}

# We don't want patch with spaces in their name
check_potential_patchname()
{
	local patch="$1"
	if echo "$patch" | grep -q ' '
	then
		printf $"Patch name '%s' invalid: cannot contain spaces.\n" "$patch" >&2
		exit 1
	fi

	if [ "$patch" = "$QUILT_SERIES" ]
	then
		printf $"No patch can be named '%s' as this would conflict with the\nseries file used internally by quilt.\n" "$QUILT_SERIES" >&2
		exit 1
	fi
}

print_patch()
{
	echo "${QUILT_PATCHES_PREFIX:+$SUBDIR_DOWN$QUILT_PATCHES/}$1"
}

# Generate a format suitable to print patch names with printf
patch_format()
{
	local prefix=${QUILT_PATCHES_PREFIX:+$SUBDIR_DOWN$QUILT_PATCHES/}

	echo -n "${prefix/\%/%%}%s"
}

setup_colors()
{
	local C=diff_hdr=32:diff_add=36:diff_mod=35:diff_rem=35:diff_hunk=33:diff_ctx=35:diff_cctx=33:patch_offs=33:patch_fuzz=35:patch_fail=31:series_app=32:series_top=33:series_una=00:clear=00
	[ -n "$QUILT_COLORS" ] && C="$C:$QUILT_COLORS"

	C=${C//=/=\'$'\e'[}
	C=color_${C//:/m\'; color_}m\'
	eval $C
}

quilt_command()
{
	local command=$1
	shift

	QUILT_COMMAND="" bash $BASH_OPTS -c "${SUBDIR:+cd $SUBDIR;} . $QUILT_DIR/$command" "quilt $command" "$@"
}

declare pager_fifo pager_fifo_dir pager_pid

wait_for_pager()
{
	exec >&-
	wait $pager_pid
	rm $pager_fifo 2>/dev/null
	rmdir $pager_fifo_dir 2>/dev/null
}

wait_for_pager_signal()
{
	remove_exit_handler wait_for_pager
	wait_for_pager
	trap - INT HUP TERM QUIT
}

# Spawn pager process and redirect the rest of our output to it
setup_pager()
{
	test -t 1 || return 0

	# QUILT_PAGER = QUILT_PAGER | GIT_PAGER | PAGER | less -R
	# NOTE: QUILT_PAGER='' is significant
	QUILT_PAGER=${QUILT_PAGER-${GIT_PAGER-${PAGER-less -R}}}

	[ -z "$QUILT_PAGER" -o "$QUILT_PAGER" = "cat" ]  && return 0

	export LESS="${LESS:-FRSX}"

	# NOTE: with "exec > >($pager)" there is no way to get the pid of the
	# pager so we can't wait for it to complete.  Otherwise we wouldn't
	# need temporary files here.  Alternatively, in recent versions of
	# bash, a coprocess could be used instead.

	pager_fifo_dir="$(gen_tempfile -d)"
	pager_fifo="$pager_fifo_dir/0"
	mkfifo -m 600 "$pager_fifo"
	$QUILT_PAGER < "$pager_fifo" &
	pager_pid=$!
	exec > "$pager_fifo"

	trap wait_for_pager_signal INT HUP TERM QUIT
	add_exit_handler wait_for_pager
}

#
# If the working directory does not contain a $QUILT_PATCHES directory,
# quilt searches for its base directory up the directory tree. If no
# $QUILT_PATCHES directory exists, the quilt operations that create
# patches will create $QUILT_PATCHES in the current working directory.
#
# When quilt is invoked from a directory below the base directory, it
# changes into the base directory, and sets $SUBDIR to the relative
# path from the base directory to the directory in which it was
# invoked. (e.g., if quilt is invoked in /usr/src/linux/drivers/net
# and the base direcory is /usr/src/linux, $SUBDIR is set to
# drivers/net/.

unset SUBDIR SUBDIR_DOWN
if ! [ -d "$QUILT_PC" -o -d "${QUILT_PATCHES:-patches}" ]
then
    basedir=$PWD
    while [ -n "$basedir" ]
    do
	basedir=${basedir%/*}
	down=$down../
	if [ -d "$basedir/$QUILT_PC" -o -d "$basedir/${QUILT_PATCHES:-patches}" ]
	then
	    SUBDIR="${PWD#$basedir/}/"
	    SUBDIR_DOWN=$down
	    if ! cd $basedir/
	    then
		echo "Cannot change into parent directory $basedir/" >&2
		exit 1
	    fi
	    break
	fi
    done
    unset basedir down
fi

if [ -r $QUILT_PC/.quilt_patches ]
then
	: ${QUILT_PATCHES:=$(< $QUILT_PC/.quilt_patches)}
else
	: ${QUILT_PATCHES:=patches}
fi
if [ -r $QUILT_PC/.quilt_series ]
then
	: ${QUILT_SERIES:=$(< $QUILT_PC/.quilt_series)}
else
	: ${QUILT_SERIES:=series}
fi

if [ "${QUILT_SERIES:0:1}" = / ]
then
	SERIES=$QUILT_SERIES
elif [ -f $QUILT_PC/$QUILT_SERIES ]
then
	SERIES=$QUILT_PC/$QUILT_SERIES
elif [ -f $QUILT_SERIES ]
then
	SERIES=$QUILT_SERIES
else
	SERIES=$QUILT_PATCHES/$QUILT_SERIES
fi

DB="$QUILT_PC/applied-patches"

if [ -z "$skip_version_check" ] && ! version_check
then
	printf $"The working tree was created by an older version of quilt. Please run 'quilt upgrade'.\n" >&2
	exit 1
fi
### Local Variables:
### mode: shell-script
### End:
# vim:filetype=sh