~ubuntu-branches/ubuntu/maverick/quilt/maverick

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
#! @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] [--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.

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

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

--color[=always|auto|never]
	Use syntax coloring.
"
		exit 0
	else
		exit 1
	fi
}

interrupt()
{
	rollback_patch $1
	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/)
		    print "'$color_patch_fail'" $0 "'$color_clear'"
		  else if (/is already applied$/)
		    print "'$color_patch_applied'" $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

	if [ "${patch_file:(-3)}" = ".gz" ]
	then
		gzip -cd $patch_file \
		| patch $QUILT_PATCH_OPTS $(push_patch_args $patch) \
			--backup --prefix="$QUILT_PC/$patch/" \
			$no_reject_files \
			-E $silent $force_apply 2>&1
	elif [ "${patch_file:(-4)}" = ".bz2" ]
	then
		bzip2 -cd $patch_file \
		| patch $QUILT_PATCH_OPTS $(push_patch_args $patch) \
			--backup --prefix="$QUILT_PC/$patch/" \
			$no_reject_files \
			-E $silent $force_apply 2>&1
	else
		patch $QUILT_PATCH_OPTS $(push_patch_args $patch) \
		      --backup --prefix="$QUILT_PC/$patch/" \
		      $no_reject_files \
		      -E $silent $force_apply -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
			touch $QUILT_PC/$patch/.timestamp
		else
			mkdir "$QUILT_PC/$patch"
		fi

		if ! [ -e $patch_file ]
		then
			printf $"Patch %s does not exist; applied empty patch\n" \
			       "$(print_patch $patch)"
		elif [ -z "$(shopt -s nullglob ; echo "$QUILT_PC/$patch/"*)" ]
		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
}

options=`getopt -o fqvah --long 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 ;;
	--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 $?

[ -n "$opt_quiet" ] && silent=-s
[ -z "$opt_verbose" ] && silent_unless_verbose=-s
[ -z "$opt_force" -o -n "$opt_quiet" ] && force_apply=-f
[ -n "$opt_force" ] && opt_leave_rejects=1

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)
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