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

usage()
{
	echo "Usage: quilt import [-f] [-p num] [-n patch] [patchfile]"
	if [ x$1 = x-h ]
	then
		cat <<EOF

Import an external patch. If a patch file name is specified,
the patch will be stored in this relative path in the patches/
directory. Else, if an input file name is given this name is
used as the patch name.

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

-n patch
	File name relative to patches/ to use.

-f	Overwite/update existing patches.

EOF
		exit 0
	else
		exit 1
	fi
}

options=`getopt -o fn:p:h -- "$@"`

if [ $? -ne 0 ]
then
        usage
fi

eval set -- "$options"

while true
do
        case "$1" in
        -n)
		opt_patch=$(echo "$2" |
			    sed -e 's:^'"$P"'patches/::' -e 's:^\.pc/::')
		shift 2 ;;
	-p)
		opt_strip=$2
		shift 2 ;;
	-f)
		opt_force=1
		shift ;;
	-h)
		usage -h ;;
        --)
                shift
		break ;;
        esac
done

if [ $# -eq 1 ]
then
	input_file=$1
elif [ $# -gt 1 ]
then
        usage
fi

[ -n "$opt_strip" ] && patch_args="-p$opt_strip"

if [ -n "$opt_patch" ]
then
	patch=$(stripit "$opt_patch")
	patch_file="${P}patches/$opt_patch"
else
	patch="$(stripit "$input_file")"
	if [ -n "$patch" ]
	then
		opt_patch="$patch.patch"
	else
		echo "Please use -n to specify a patch file name."
		exit 1
	fi
	patch_file="${P}patches/$opt_patch"
fi

if echo "$patch_file" | grep -q -e $'[ \t]'
then
	echo "Patch file name \"$patch_file\" contains whitespace."
	exit 1
fi

if is_applied $patch
then
	echo "Patch $patch is applied."
	exit 1
fi

case "$input_file" in
'')
	tmpfile=$(@MKTEMP@ /tmp/patch-scripts.XXXXXX)
	if ! cat > $tmpfile
	then
		echo "Cannot read from standard input."
		rm -f $tmpfile
		exit 1
	fi
	input_file=$tmpfile ;;
*.gz)
	tmpfile=$(@MKTEMP@ /tmp/patch-scripts.XXXXXX)
	if ! gzip -cd "$input_file" > $tmpfile
	then
		echo "Cannot decompress file $input_file"
		rm -f $tmpfile
		exit 1
	fi
	input_file=$tmpfile ;;
*.bz2)
	tmpfile=$(@MKTEMP@ /tmp/patch-scripts.XXXXXX)
	if ! bzip2 -cd "$input_file" > $tmpfile
	then
		echo "Cannot decompress file $input_file"
		rm -f $tmpfile
		exit
	fi
	input_file=$tmpfile ;;
*)
	if ! [ -r "$input_file" ]
	then
		echo "Cannot read from file $input_file"
		exit 1
	fi
esac

if [ -e "$patch_file" ]
then
	if [ -z "$opt_force" ]
	then
		echo "Patch $patch exists. Replace with -f."
		exit 1
	fi

	if grep -q '^%patch$' "$patch_file" &&
	   ! grep -q '^%patch$' "$input_file"
	then
		echo "Updating %patch section of patch $patch"
		if ! @LIB@/parse-patch -u patch $patch_file < "$input_file"
		then
			echo "Failed to update %patch section of patch $patch"
			exit 1
		fi
	else
		echo "Replacing patch $patch with new version"
		if ! cat "$input_file" > "$patch_file"
		then
			echo "Failed to replace patch $patch"
			exit 1
		fi
	fi
else
	echo "Importing patch $patch (stored as $patch_file)"
	if ! grep -q '^%patch$' "$input_file"
	then
		echo "%patch" >> "$patch_file"
	fi
	if ! cat "$input_file" >> "$patch_file"
	then
		echo "Failed to import patch $patch"
		exit 1
	fi
fi

rm -rf .pc/$patch

if ! patch_in_series $patch &&
   ! insert_in_series $opt_patch "$patch_args"
then
	echo "Failed to insert $patch in file series."
fi

if [ -n "$tmpfile" ]
then
	rm -f $tmpfile
fi