-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen-patchwork.sh
executable file
·101 lines (87 loc) · 2.06 KB
/
gen-patchwork.sh
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
#!/usr/bin/env bash
set -euo pipefail
. patchfile
function show_usage() {
cat <<EOF
Usage: $(basename "$0") [-b|--baseline] [<basedir>]"
Create a new patchwork under <basedir>, based on the patchfile in the current
working directory. <basedir> is assumed to be the current working directory by
default.
If -b or --baseline is specified, don't apply the patch in PATCHNAME, only
apply the patches in BASEPATCHES. This is useful for generating a patch.
EOF
}
cwd="$(pwd)"
baseline=
basedir="$(pwd)"
if [ "$#" -gt 0 ] ; then
while [ "$#" -gt 0 ]; do
case "$1" in
-h | --help )
show_usage
exit 0
;;
-b | --baseline )
baseline=1
shift
;;
* )
if [ "$#" -gt 1 ] ; then
show_usage
exit 1
fi
basedir=$1
break
;;
esac
done
fi
set +u
if [ -n "${BASEPATCHES}" ] ; then
set -u
for ((i=0; i<${#BASEPATCHES[@]}; i++));
do
BASEPATCHES[$i]=$(echo "${BASEPATCHES[$i]}" | perl -pe "s/$/.patch/")
done
else
set -u
fi
if [ -n "${PATCHNAME}" ] ; then
PATCHNAME="${PATCHNAME}.patch"
fi
basename="$(echo "${HTTPS_REMOTE}" | perl -pe 's/.*[\/:](.*?).git/\1/')"
if [ -z "$basename" ]; then
echo "Couldn't resolve remote basename :-/"
exit 1
fi
patchworkdir="${basedir}/${basename}"
if [ -d "${patchworkdir}" ]; then
read -p "Overwrite ${patchworkdir}? " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]] ; then
rm -rf "${patchworkdir}"
else
echo "As you wish, nothing left for me to do here."
exit 0
fi
fi
mkdir "${patchworkdir}"
git clone "${HTTPS_REMOTE}" "${patchworkdir}"
(cd "${patchworkdir}" && git checkout "tags/${BASETAG}" -b "${BASETAG}")
rm -rf "${patchworkdir}/.git" # Just so no one tries to push this to master.
cd "${basedir}"
set +u
if [ -n "${BASEPATCHES}" ] ; then
set -u
for ((i=0; i<${#BASEPATCHES[@]}; i++));
do
patch="${BASEPATCHES[$i]}"
echo "Applying ${patch}.."
patch -p1 < "${cwd}/${patch}"
done
fi
set -u
if [ -z "${baseline}" ] && [ -n "${PATCHNAME}" ] ; then
echo "Applying ${PATCHNAME}.."
patch -p1 < "${cwd}/${PATCHNAME}"
fi