summaryrefslogtreecommitdiffstats
path: root/quilt/push.in
blob: 757cc2bbb324f831cee91322d1ffd03d0acdafb0 (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
#! @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 @SCRIPTS@/patchfns ]
	then
		echo "Cannot read library @SCRIPTS@/patchfns" >&2
		exit 1
	fi
	. @SCRIPTS@/patchfns
fi

usage()
{
	printf $"Usage: quilt push [-afqv] [--leave-rejects] [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.

-f	Force apply, even if the patch has rejects.

-q	Quiet operation.

-v	Verbose operation.

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

--interactive
	Allow the patch utility to ask how to deal with conflicts. If
	this option is not given, the option -f will be passed to the 
	patch program.
"
		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
}

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

	if ! [ -s $patch_file ]
	then
		printf $"Patch %s appears to be empty\n" "$patch_file"
		return 0
	fi
	
	if [ "${patch_file:(-3)}" = ".gz" ]
	then
		gzip -cd $patch_file \
		| @PATCH@ $QUILT_PATCH_OPTS $(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 $(patch_args $patch) \
			--backup --prefix="$QUILT_PC/$patch/" \
			$no_reject_files \
			-E $silent $force_apply 2>&1
	else
		@PATCH@ $QUILT_PATCH_OPTS $(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

	@LIB@/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 file status

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

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

	apply_patch $patch \
	| cleanup_patch_output
	status=${PIPESTATUS[0]}
	trap "" SIGINT

	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
		fi

		if [ "$(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
		printf $"Patch %s does not apply (enforce with -f)\n" \
		       "$(print_patch $patch)"
		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 [ $patch = "$stop_at_patch" ]
			then
				break
			fi
		done
		if [ -n "$stop_at_patch" -a "$patch" != "$stop_at_patch" ]
		then
			printf $"Patch %s not found in file series\n" \
			       "$stop_at_patch" >&2
			return 1
		fi
	fi
}

options=`getopt -o fqvah --long leave-rejects,interactive -- "$@"`

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 ;;
	--interactive)
		opt_interactive=1
		shift ;;
        --)
                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
		if ! stop_at_patch=$(find_patch $1)
		then
			printf $"Patch %s is not in series\n" "$1" >&2
			exit 1
		fi
	fi
else
	[ -z "$opt_all" ] && number=1
fi

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

if [ -n "$stop_at_patch" ]
then
	if is_applied $stop_at_patch
	then
		printf $"Patch %s is already applied\n" \
		       "$(print_patch $stop_at_patch)" >&2
		exit 1
	fi
fi

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

if ! patches=$(list_patches) 2>&1
then
	exit 1
elif [ -z "$patches" ]
then
	if [ -z "$top" ]
	then
		printf $"No patches applied\n" >&2
	else
		printf $"File series fully applied, ends at patch %s\n" \
		       "$(print_patch $top)" >&2
	fi
	exit 2
fi

create_db
for patch in $patches
do
	if ! add_patch $patch
	then
		exit 1
	fi
	[ -z "$opt_quiet" ] && echo
done

printf $"Now at patch %s\n" "$(print_patch $patch)"
### Local Variables:
### mode: shell-script
### End:
# vim:filetype=sh