forked from dustinkirkland/bikeshed
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapply-patch
executable file
·58 lines (53 loc) · 1.58 KB
/
apply-patch
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
#!/bin/sh
#
# apply-patch - apply the patch specified on the argument line,
# automatically detecting the patch level
#
# Copyright (C) 2011 Dustin Kirkland <[email protected]>
#
# Authors:
# Dustin Kirkland <[email protected]>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# apply-patch [PATCHFILE|URL]
P=
DIFF="$1"
# If $1 is a URL, grab it!
case "$1" in
http://*|https://*|ftp://*)
DIFF=$(mktemp -t apply-patch-XXXXXXXX)
trap "rm -f $DIFF 2>/dev/null || true" EXIT HUP INT QUIT TERM
if which wget >/dev/null 2>&1; then
if ! wget -q -O"$DIFF" "$1"; then
echo "ERROR: Unable to retrieve [$1]" 1>&2
exit 1
fi
else
echo "ERROR: Please install wget to use a URL with this tool" 1>&2
exit 1
fi
;;
esac
# Try patch levels 0 - 16
for i in $(seq 0 16); do
if patch -p$i --dry-run -i "$DIFF" 1>/dev/null 2>&1; then
P=$i
break
fi
done
if [ -n "$P" ]; then
exec patch -p$P -i "$DIFF"
else
echo "ERROR: Unable to determine suitable patch strip level" 1>&2
exit 1
fi