-
Notifications
You must be signed in to change notification settings - Fork 224
/
Copy pathapply-patch
executable file
·63 lines (48 loc) · 1.12 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
59
60
61
62
63
#!/usr/bin/env bash
function usage {
cat <<EOF
Apply the changes for directory SRC for commit COMMIT to directory DST.
The script assumes it is run from the root of the repository.
Usage:
$0 <COMMIT> <SRC> <DST>
Examples:
$0 HEAD~ docs/sources/k6/next docs/sources/k6/v0.47.x
EOF
}
TMPDIR=$(mktemp -d)
if [[ $# -ne 3 ]]; then
usage
exit 1
fi
case "${@}" in
--help | -h)
usage
exit 0
;;
*)
esac
COMMIT="$1"
SRC="$2"
DST="$3"
PATCH="${TMPDIR}/diff.patch"
# path_components returns the number of leading path components to strip from traditional diff paths.
function path_components {
local p="$1"
local -i n=2
if [[ "${p}" =~ "/$" ]]; then
n=1
fi
while [[ "${p}" =~ "/" ]]; do
(( n++ ))
p="${p%/*}"
done
echo "${n}"
}
GIT_ROOT="$(git rev-parse --show-toplevel)"
if [[ "$(pwd)" != "${GIT_ROOT}" ]]; then
echo "ERROR: Script has not been run from the repository root ('${GIT_ROOT}')."
echo "Please move to the root directory and re-run the script."
exit 1
fi
git diff "${COMMIT}" -- "${SRC}" > "${PATCH}"
git apply -p "$(path_components "${SRC}")" --directory "${DST}" "${PATCH}"