File: push.in

package info (click to toggle)
quilt 0.63-8
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 1,868 kB
  • ctags: 153
  • sloc: sh: 5,792; perl: 1,236; lisp: 492; makefile: 431
file content (448 lines) | stat: -rw-r--r-- 9,083 bytes parent folder | download | duplicates (2)
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
#! @BASH@

#  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.

# Read in library functions
if [ "$(type -t patch_file_name)" != function ]
then
	if ! [ -r $QUILT_DIR/scripts/patchfns ]
	then
		echo "Cannot read library $QUILT_DIR/scripts/patchfns" >&2
		exit 1
	fi
	. $QUILT_DIR/scripts/patchfns
fi

setup_colors

usage()
{
	printf $"Usage: quilt push [-afqv] [--merge[=merge|diff3]] [--leave-rejects] [--color[=always|auto|never]] [num|patch]\n"
	if [ x$1 = x-h ]
	then
		printf $"
Apply patch(es) from the series file.  Without options, the next patch
in the series file is applied.  When a number is specified, apply the
specified number of patches.  When a patch name is specified, apply
all patches up to and including the specified patch.  Patch names may
include the patches/ prefix, which means that filename completion can
be used. The mtime of all touched files will be exactly the same to
prevent time skews.

-a	Apply all patches in the series file.

-q	Quiet operation.

-f	Force apply, even if the patch has rejects. Unless in quiet mode,
	apply the patch interactively: the patch utility may ask questions.

-v	Verbose operation.

--fuzz=N
	Set the maximum fuzz factor (default: 2).

-m, --merge[=merge|diff3]
	Merge the patch file into the original files (see patch(1)).

--leave-rejects
	Leave around the reject files patch produced, even if the patch
	is not actually applied.

--color[=always|auto|never]
	Use syntax coloring (auto activates it only if the output is a tty).
"
		exit 0
	else
		exit 1
	fi
}

interrupt()
{
	local patch=$1

	rollback_patch "$patch"
	printf $"Interrupted by user; patch %s was not applied.\n" \
	       "$(print_patch "$patch")" >&2
	exit 1
}

colorize()
{
	if [ -n "$opt_color" ]; then
		awk '
		{ if (/FAILED|hunks? ignored|can'\''t find file|file .* already exists|NOT MERGED/)
		    print "'$color_patch_fail'" $0 "'$color_clear'"
		  else if (/already applied$/)
		    print "'$color_patch_fuzz'" $0 "'$color_clear'"
		  else if (/^Hunk/) {
		    sub(/^Hunk .* with fuzz [0-9]*/,
			"'$color_patch_fuzz'&'$color_clear'")
		    sub(/offset -?[0-9]* lines?/,
			"'$color_patch_offs'&'$color_clear'")
		    print
		  } else
		    print
		}'
	else
		cat
	fi
}

push_patch_args()
{
	local patch=$1

	if [ -z "$opt_reverse" ]
	then
		patch_args "$patch"
	else
		set -- $(patch_args "$patch")
		if [ "${*#-R}" != "$*" ]
		then
			echo "${*#-R}"
		else
			echo "$*" -R
		fi
	fi
}

apply_patch()
{
	local patch=$1 patch_file=$2
	local output

	[ -s "$patch_file" ] || return 0

	set -- patch $QUILT_PATCH_OPTS $(push_patch_args "$patch") \
		     --backup --prefix="$QUILT_PC/$patch/" \
		     $no_reject_files $more_patch_args

	if [ "${patch_file:(-3)}" = ".gz" ]
	then
		gzip -cd "$patch_file" | "$@" 2>&1
	elif [ "${patch_file:(-4)}" = ".bz2" ]
	then
		bzip2 -cd "$patch_file" | "$@" 2>&1
	elif [ "${patch_file:(-3)}" = ".xz" ]
	then
		xz -cd "$patch_file" | "$@" 2>&1
	elif [ "${patch_file:(-5)}" = ".lzma" ]
	then
		lzma -cd "$patch_file" | "$@" 2>&1
	else
		"$@" -i "$patch_file" 2>&1
	fi
}

rollback_patch()
{
	local patch=$1

	$QUILT_DIR/scripts/backup-files $silent_unless_verbose -r -B "$QUILT_PC/$patch/" -
}

cleanup_patch_output()
{
	if [ -z "$opt_leave_rejects" ]
	then
		if [ -n "$opt_quiet" ]; then
			# In this case, patch does not allow us to find out
			# which file contains the rejects; it only tells us
			# which reject file is used. We use a single temporary
			# reject file, so this does not help us.

			awk '
			{ gsub(/ -- saving rejects to (file )?.*/, "") }
			{ print }
			'
		else
			awk '
			/^patching file / { filename = substr($0, 15) }
			{ gsub(/ -- saving rejects to (file )?.*/,
			       " -- rejects in file " filename) }
			{ print }
			'
		fi
	else
		cat
	fi
}

add_patch()
{
	local patch=$1
	local patch_file=$(patch_file_name "$patch")
	local file status tmp

	printf $"Applying patch %s\n" "$(print_patch "$patch")"
	trap "interrupt $patch" SIGINT

	no_reject_files=
	if [ -z "$opt_leave_rejects" ]; then
		tmp=$(gen_tempfile)
		no_reject_files="-r $tmp"
	fi

	if [ ! -e "$patch_file" -a -z "$opt_force" ]
	then
		printf $"Patch %s does not exist\n" \
		       "$(print_patch $patch)" >&2
		return 1
	fi

	apply_patch "$patch" "$patch_file"
	status=$?
	trap "" SIGINT

	[ -n "$tmp" ] && rm -f $tmp

	if [ $status -eq 0 -o -n "$opt_force" ]
	then
		add_to_db "$patch"
		if [ $status -eq 0 ]
		then
			rm -f "$QUILT_PC/$patch~refresh"
		else
			touch "$QUILT_PC/$patch~refresh"
		fi

		if ! [ -e "$QUILT_PC/$patch" ]
		then
			mkdir "$QUILT_PC/$patch"
		fi
		touch "$QUILT_PC/$patch/.timestamp"

		# Store the list of files to process
		NONEMPTY_FILES=$(gen_tempfile)
		trap "rm -f \"$NONEMPTY_FILES\"" EXIT
		find "$QUILT_PC/$patch" -type f \
		      -a ! -path "$QUILT_PC/$patch/.timestamp" -size +0 -print0 > "$NONEMPTY_FILES"

		if [ -s "$NONEMPTY_FILES" ]; then
			xargs -0 touch -c -r "$QUILT_PC/$patch/.timestamp" < "$NONEMPTY_FILES"
		fi

		rm -f $NONEMPTY_FILES

		if ! [ -e "$patch_file" ]
		then
			printf $"Patch %s does not exist; applied empty patch\n" \
			       "$(print_patch "$patch")"
		elif [ "$(shopt -s nullglob ; echo "$QUILT_PC/$patch/"*)" = "$QUILT_PC/$patch/.timestamp" ]
		then
			printf $"Patch %s appears to be empty; applied\n" \
			       "$(print_patch "$patch")"
		elif [ $status -ne 0 ]
		then
			printf $"Applied patch %s (forced; needs refresh)\n" \
			       "$(print_patch "$patch")"
		fi
	else
		rollback_patch "$patch"
		tmp=$(gen_tempfile)
		no_reject_files="-r $tmp"
		opt_reverse=1
		if apply_patch "$patch" "$patch_file" > /dev/null 2> /dev/null
		then
			printf $"Patch %s can be reverse-applied\n" \
			       "$(print_patch "$patch")"
		else
			printf $"Patch %s does not apply (enforce with -f)\n" \
			       "$(print_patch "$patch")"
		fi
		rollback_patch "$patch"
		rm -f $tmp
		status=1
	fi
	trap - SIGINT
	return $status
}

list_patches()
{
	local top=$(top_patch) n=0 patch
	if [ -n "$top" ]
	then
		patches_after "$top"
	else
		cat_series
	fi \
	| if [ -n "$opt_all" ]
	then
		cat
	else
		while read patch
		do
			if [ -n "$number" ]
			then
				if [ $n -eq $number ]
				then
					break
				fi
				n=$[$n+1]
			fi
			echo "$patch"
			if [ -z "$number" -a "$patch" = "$stop_at_patch" ]
			then
				break
			fi
		done
	fi
}

check_duplicate_patches()
{
	local IFS=$'\n'
	local -a duplicates
	local patch

	duplicates=($((applied_patches ; printf $'%s\n' "${patches[@]}") \
		      | awk '{ if (lines[$0]++ == 1) print }'))
	[ ${#duplicates[@]} -ge 1 ] || return 0

	for patch in "${duplicates[@]}"
	do
		printf $"Patch %s is already applied; check your series file\n" \
		       "$(print_patch "$patch")"
	done
	return 1
}

options=`getopt -o fqvam::h --long fuzz:,merge::,leave-rejects,color:: -- "$@"`

if [ $? -ne 0 ]
then
	usage
fi

eval set -- "$options"

while true
do
	case "$1" in
	-f)
		opt_force=1
		shift ;;
	-q)
		opt_quiet=1
		shift ;;
	-v)
		opt_verbose=1
		shift ;;
	-a)
		opt_all=1
		shift ;;
	-h)
		usage -h
		;;
	--fuzz)
		opt_fuzz=$2
		shift 2 ;;
	-m | --merge)
		case "$2" in
		"" | merge)
			opt_merge=1
			opt_merge_arg= ;;
		diff3)
			opt_merge=1
			opt_merge_arg="=diff3" ;;
		*)
			usage ;;
		esac
		shift 2 ;;
	--leave-rejects)
		opt_leave_rejects=1
		shift ;;
	--color)
		case "$2" in
		"" | always)
			opt_color=1 ;;
		auto | tty)
			opt_color=
			[ -t 1 ] && opt_color=1 ;;
		never)
			opt_color= ;;
		*)
			usage ;;
		esac
		shift 2 ;;
	--)
		shift
		break ;;
	esac
done

if [ $# -gt 1 -o \( -n "$opt_all" -a $# -ne 0 \) ]
then
	usage
fi

if [ $# -eq 1 ]
then
	if is_numeric "$1"
	then
		number=$1
	else
		stop_at_patch=$1
	fi
else
	[ -z "$opt_all" ] && number=1
fi

stop_at_patch=$(find_unapplied_patch "$stop_at_patch") || exit

[ -z "$opt_verbose" ] && silent_unless_verbose=-s
[ -n "$opt_force" ] && opt_leave_rejects=1

more_patch_args=
[ -n "$opt_quiet" ] && more_patch_args="$more_patch_args -s"
[ -z "$opt_force" -o -n "$opt_quiet" ] &&
	more_patch_args="$more_patch_args -f"
if [ -n "$opt_merge" ]
then
	more_patch_args="$more_patch_args --merge$opt_merge_arg"
fi
[ -n "$opt_fuzz" ] && more_patch_args="$more_patch_args -F$opt_fuzz"

top=$(top_patch)
if [ -n "$top" -a -e "$QUILT_PC/$top~refresh" ]
then
	printf $"The topmost patch %s needs to be refreshed first.\n" \
	       "$(print_patch "$top")"
	exit 1
fi

patches=$(list_patches)

# In theory, these patches can't be already applied. However in the case
# of a generated or manually tweaked series file, this could happen and
# cause havoc, so play it safe and check.
check_duplicate_patches || exit 1

create_db
for patch in $patches
do
	if ! add_patch "$patch"
	then
		exit 1
	fi
	[ -n "$opt_quiet" ] || echo
done \
| cleanup_patch_output \
| colorize

if [ ${PIPESTATUS[0]} -eq 0 ]; then
	set -- $patches
	printf $"Now at patch %s\n" "$(print_patch ${!#})"
else
	exit 1
fi
### Local Variables:
### mode: shell-script
### End:
# vim:filetype=sh