2018-02-08 12:07:50 +00:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
|
|
|
# Default behaviour:
|
|
|
|
# Reverts all patches in patch dir, notes down the ones
|
|
|
|
# which were previously applied. Applies all from the beginning
|
|
|
|
# and reports about previously unapplied patches. If there's
|
|
|
|
# an error, reverts the last one and stops.
|
|
|
|
#
|
2018-02-18 16:14:27 +00:00
|
|
|
# Usage: ./patcher -b <base_path> -r -v
|
|
|
|
# -b: <base_path> is the target location relative to which patches will be applied
|
|
|
|
# -p: <patch_path> is where to take the patches from (default is geth)
|
2018-02-08 12:07:50 +00:00
|
|
|
# -r: reverts all and exit if this flag is present
|
2018-02-14 17:44:27 +00:00
|
|
|
# -c: reverts all to see what's applied, applies all previously applied back again,
|
|
|
|
# reports unapplied patches in this branch by comparing with "develop" including
|
|
|
|
# uncommitted ones and exits (with 1 if there are any)
|
2018-02-08 12:07:50 +00:00
|
|
|
# -v: verbose error reporting about failed patch
|
|
|
|
#
|
2018-02-18 16:14:27 +00:00
|
|
|
# If -b is not present, default path is as below ($basepath).
|
2018-02-08 12:07:50 +00:00
|
|
|
|
2018-02-14 17:44:27 +00:00
|
|
|
dir=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
|
2018-02-18 16:14:27 +00:00
|
|
|
# Patches path is geth unless specified.
|
2018-02-14 17:44:27 +00:00
|
|
|
patches=("$dir"/geth/*.patch)
|
|
|
|
|
|
|
|
# Use this branch as a reference for comparing patches
|
|
|
|
# in current branch (-c option).
|
|
|
|
baseBranch="develop"
|
2018-02-08 12:07:50 +00:00
|
|
|
|
|
|
|
# Base path is vendor/github.com/ethereum/go-ethereum
|
|
|
|
# unless specified.
|
|
|
|
basepath="vendor/github.com/ethereum/go-ethereum"
|
2018-02-18 16:14:27 +00:00
|
|
|
|
|
|
|
gitApply() {
|
|
|
|
f=$1
|
|
|
|
basepath=$2
|
|
|
|
verbose=$3
|
|
|
|
|
|
|
|
if [ $verbose -eq 1 ]; then
|
|
|
|
if [ $basepath == "." ]; then
|
|
|
|
git apply "$f"
|
|
|
|
else
|
|
|
|
git apply "$f" --directory="$basepath"
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
if [ $basepath == "." ]; then
|
|
|
|
git apply "$f" > /dev/null 2>&1
|
|
|
|
else
|
|
|
|
git apply "$f" --directory="$basepath" > /dev/null 2>&1
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
gitApplyReverse() {
|
|
|
|
f=$1
|
|
|
|
basepath=$2
|
|
|
|
|
|
|
|
if [ $basepath == "." ]; then
|
|
|
|
git apply "$f" -R > /dev/null 2>&1
|
|
|
|
else
|
|
|
|
git apply "$f" --directory="$basepath" -R > /dev/null 2>&1
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2018-02-08 12:07:50 +00:00
|
|
|
verbose=0
|
2018-02-18 16:14:27 +00:00
|
|
|
revert=0
|
|
|
|
compare=0
|
|
|
|
while getopts b:p:rcv opt; do
|
2018-02-08 12:07:50 +00:00
|
|
|
case $opt in
|
2018-02-18 16:14:27 +00:00
|
|
|
b)
|
2018-02-08 12:07:50 +00:00
|
|
|
basepath=$OPTARG
|
|
|
|
;;
|
2018-02-18 16:14:27 +00:00
|
|
|
p)
|
|
|
|
patches=("$dir"/$OPTARG/*.patch)
|
|
|
|
;;
|
2018-02-08 12:07:50 +00:00
|
|
|
r)
|
2018-02-18 16:14:27 +00:00
|
|
|
revert=1
|
2018-02-08 12:07:50 +00:00
|
|
|
;;
|
2018-02-14 17:44:27 +00:00
|
|
|
c)
|
2018-02-18 16:14:27 +00:00
|
|
|
compare=1
|
2018-02-14 17:44:27 +00:00
|
|
|
;;
|
2018-02-08 12:07:50 +00:00
|
|
|
v)
|
|
|
|
verbose=1
|
|
|
|
;;
|
|
|
|
\?)
|
|
|
|
echo "Invalid flag: -$OPTARG" >&2
|
|
|
|
exit
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
2018-02-18 16:14:27 +00:00
|
|
|
if [ $revert -eq 1 ]; then
|
|
|
|
# Reverts in reverse order and exits.
|
|
|
|
for ((i=${#patches[@]}-1; i>=0; i--)); do
|
|
|
|
gitApplyReverse "${patches[$i]}" "$basepath" 0
|
|
|
|
done
|
|
|
|
echo "Reverted all."
|
|
|
|
exit
|
|
|
|
fi
|
|
|
|
if [ $compare -eq 1 ]; then
|
|
|
|
applied=()
|
|
|
|
unapplied=()
|
|
|
|
# Finds applied patches using reverse order and
|
|
|
|
# notes them down.
|
|
|
|
for ((i=${#patches[@]}-1; i>=0; i--)); do
|
|
|
|
f=${patches[$i]}
|
|
|
|
gitApplyReverse "$f" "$basepath"
|
|
|
|
if [ $? -eq 1 ]; then
|
|
|
|
unapplied+=("$f")
|
|
|
|
else
|
|
|
|
applied+=("$f")
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
# Applies reverted patches back again.
|
|
|
|
for ((i=${#applied[@]}-1; i>=0; i--)); do
|
|
|
|
f=${applied[$i]}
|
|
|
|
gitApply "$f" "$basepath" 0
|
|
|
|
done
|
|
|
|
# Sorts out new patches' paths by comparing with base branch.
|
|
|
|
fromBaseBranch=($(git diff $baseBranch --stat | grep "\\.patch" |
|
|
|
|
while IFS=" " read -r -a line; do
|
|
|
|
path="$(pwd)/${line[0]}"
|
|
|
|
echo "$path"
|
|
|
|
done
|
|
|
|
))
|
|
|
|
# Also does the same with uncommitted.
|
|
|
|
uncommitted=($(git status -u --porcelain | grep "\\.patch" |
|
|
|
|
while IFS=" " read -r -a line; do
|
|
|
|
length=${#line[@]}
|
|
|
|
path="$(pwd)/${line[$((length - 1))]}"
|
|
|
|
echo "$path"
|
|
|
|
done
|
|
|
|
))
|
|
|
|
newPatches=( "${fromBaseBranch[@]}" "${uncommitted[@]}" )
|
|
|
|
# Checks new patches and exits with 1 if there are unapplied.
|
|
|
|
hasUnapplied=0
|
|
|
|
for newPatch in "${newPatches[@]}"; do
|
|
|
|
for unapp in "${unapplied[@]}"; do
|
|
|
|
if [ "$unapp" == "$newPatch" ]; then
|
|
|
|
echo "Recently added/changed but not applied: $unapp"
|
|
|
|
hasUnapplied=1
|
|
|
|
break
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
done
|
|
|
|
exit $hasUnapplied
|
|
|
|
fi
|
|
|
|
|
2018-02-08 12:07:50 +00:00
|
|
|
applied=()
|
|
|
|
|
|
|
|
echo -en "\\n"
|
|
|
|
echo "Previously applied:"
|
|
|
|
echo "==================="
|
|
|
|
# Reverts every patch in reverse order to see
|
|
|
|
# which was previously applied.
|
|
|
|
for ((i=${#patches[@]}-1; i>=0; i--)); do
|
|
|
|
f=${patches[$i]}
|
2018-02-18 16:14:27 +00:00
|
|
|
gitApplyReverse "$f" "$basepath"
|
2018-02-08 12:07:50 +00:00
|
|
|
if [ $? -eq 0 ]; then
|
|
|
|
applied+=("$f")
|
|
|
|
echo "$f"
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
echo "==================="
|
|
|
|
echo -en "\\n"
|
|
|
|
|
|
|
|
# Applies every patch from the beginning.
|
|
|
|
for ((i=0; i<${#patches[@]}; i++)); do
|
|
|
|
f=${patches[$i]}
|
|
|
|
# If not applied, report it new.
|
|
|
|
has=0
|
|
|
|
for patch in "${applied[@]}"; do
|
|
|
|
if [ "$patch" == "$f" ]; then
|
|
|
|
has=1
|
|
|
|
break
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
if [ $has -eq 0 ]; then
|
|
|
|
echo "Applying new: $f"
|
|
|
|
echo -en "\\n"
|
|
|
|
fi
|
2018-02-18 16:14:27 +00:00
|
|
|
gitApply "$f" "$basepath" $verbose
|
2018-02-08 12:07:50 +00:00
|
|
|
if [ $? -eq 1 ]; then
|
|
|
|
echo "Failed and reverting: $f"
|
2018-02-18 16:14:27 +00:00
|
|
|
gitApplyReverse "$f" "$basepath"
|
2018-02-08 12:07:50 +00:00
|
|
|
echo -en "\\n"
|
|
|
|
exit
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
|
|
|
echo -en "\\n"
|
|
|
|
echo "Done."
|