File: import.in

package info (click to toggle)
quilt 0.65-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,980 kB
  • sloc: sh: 5,898; perl: 1,256; lisp: 492; makefile: 430
file content (260 lines) | stat: -rw-r--r-- 5,229 bytes parent folder | download | duplicates (3)
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
#! @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

usage()
{
	printf $"Usage: quilt import [-p num] [-R] [-P patch] [-f] [-d {o|a|n}] patchfile ...\n"
	if [ x$1 = x-h ]
	then
		printf $"
Import external patches.  The patches will be inserted following the
current top patch, and must be pushed after import to apply them.

-p num
	Number of directory levels to strip when applying (default=1)

-R
	Apply patch in reverse.

-P patch
	Patch filename to use inside quilt. This option can only be
	used when importing a single patch.

-f	Overwrite/update existing patches.

-d {o|a|n}
	When overwriting in existing patch, keep the old (o), all (a), or
	new (n) patch header. If both patches include headers, this option
	must be specified. This option is only effective when -f is used.
"
		exit 0
	else
		exit 1
	fi
}

find_patch_file()
{
	local name="$1"

	if [ ${name:0:1} = / ]
	then
		# Patch has absolute path
		if [ -r "$name" ]
		then
			echo "$name"
			return
		fi
	else
		# Patch has a relative path
		if [ -r "$SUBDIR$name" ]
		then
			echo "$SUBDIR$name"
			return
		fi
	fi

	printf $"Patch %s does not exist\n" "$name" >&2
	return 1
}

merge_patches()
{
	local old="$1" new="$2"
	local old_desc=$(gen_tempfile) new_desc=$(gen_tempfile)

	cat_file "$old" | patch_header | strip_diffstat > $old_desc
	cat_file "$new" | patch_header | strip_diffstat > $new_desc

	if [ -z "$opt_desc" ]
	then
		[ -s $old_desc ] || opt_desc=n
		[ -z "$opt_desc" -a ! -s $new_desc ] && opt_desc=o

		if [ -z "$opt_desc" ]
		then
			local diff=$(diff -u $old_desc $new_desc \
				     | sed -e '1,2d')
			if [ -n "$diff" ]
			then
				printf $"Patch headers differ:\n" >&2
				echo "$diff" >&2
				printf \
$"Please use -d {o|a|n} to specify which patch header(s) to keep.\n" >&2
				rm -f $old_desc $new_desc
				return 1
			fi
		fi
	fi

	[ "$opt_desc" = n ] || cat "$old_desc"
	[ "$opt_desc" = a ] && echo '---'
	if [ "$opt_desc" = o ]
	then
		cat_file "$new" | patch_body
	else
		cat_file "$new"
	fi
	rm -f $old_desc $new_desc
}

die()
{
	local status=$1
	[ "$merged_patch_file" != "$patch_file" ] && rm -f "$merged_patch_file"
	exit $status
}

options=`getopt -o P:d:fp:Rh -- "$@"`

if [ $? -ne 0 ]
then
	usage
fi

eval set -- "$options"

while true
do
	case "$1" in
	-P)
		opt_patch=${2#$QUILT_PATCHES/}
		shift 2 ;;
	-p)
		opt_strip=$2
		shift 2 ;;
	-R)
		opt_reverse=1
		shift ;;
	-d)
		case "$2" in
			o|n|a) opt_desc=$2 ;;
			*) usage ;;
		esac
		shift 2 ;;
	-f)
		opt_force=1
		shift ;;
	-h)
		usage -h ;;
	--)
		shift
		break ;;
	esac
done

if [ $# -gt 1 -a -n "$opt_patch" ]
then
	printf $"Option \`-P' can only be used when importing a single patch\n" >&2
	exit 1
fi

[ -n "$opt_strip" ] && patch_args="-p$opt_strip"
if [ -n "$opt_reverse" ]
then
	if [ -n "$patch_args" ]
	then
		patch_args="$patch_args -R"
	else
		patch_args="-R"
	fi
fi

before=$(patch_after "$(top_patch)")
for orig_patch_file in "$@"
do
	if [ -n "$opt_patch" ]
	then
		patch=$opt_patch
	else
		patch=${orig_patch_file##*/}
	fi

	check_potential_patchname "$patch"

	patch_file=$(find_patch_file "$orig_patch_file") || exit 1
	merged_patch_file="$patch_file"

	if is_applied $patch
	then
		printf $"Patch %s is applied\n" "$(print_patch $patch)" >&2
		exit 1
	fi

	dest=$(patch_file_name $patch)
	if patch_in_series "$patch"
	then
		if [ "$patch_file" = "$dest" ]
		then
			printf $"Patch %s already exists in series.\n" \
			       "$(print_patch $patch)" >&2
			exit 1
		fi
		if [ -z "$opt_force" ]
		then
			printf $"Patch %s exists. Replace with -f.\n" \
			       "$(print_patch $patch)" >&2
			exit 1
		fi
		if [ "$opt_desc" != n ]
		then
			merged_patch_file=$(gen_tempfile)
			merge_patches "$dest" "$patch_file" \
				      > $merged_patch_file \
				|| die 1
		fi
		printf $"Replacing patch %s with new version\n" \
		       "$(print_patch $patch)" >&2
	elif [ -e "$dest" ]
	then
		printf $"Importing patch %s\n" "$(print_patch $patch)"
	else
		printf $"Importing patch %s (stored as %s)\n" \
		       "$orig_patch_file" \
		       "$(print_patch $patch)"

		mkdir -p "${dest%/*}"
	fi

	if [ "$merged_patch_file" != "$dest" ] && \
	   (( [ "$merged_patch_file" != "$patch_file" ] && \
	      ! cat_to_new_file "$dest" < "$merged_patch_file" ) || \
	    ( [ "$merged_patch_file" = "$patch_file" ] && \
	      ! cp "$merged_patch_file" "$dest" ))
	then
		printf $"Failed to import patch %s\n" "$(print_patch $patch)" >&2
		die 1
	fi

	[ "$merged_patch_file" != "$patch_file" ] && rm -f "$merged_patch_file"

	if ! patch_in_series $patch &&
	   ! insert_in_series $patch "$patch_args" "$before"
	then
		printf $"Failed to insert patch %s into file series\n" \
		       "$(print_patch $patch)" >&2
		exit 1
	fi

	rm -rf $QUILT_PC/$patch
done
### Local Variables:
### mode: shell-script
### End:
# vim:filetype=sh