github.com/status-im/status-go@v1.1.0/_assets/patches/patcher (about) 1 #!/usr/bin/env bash 2 3 # Default behaviour: 4 # Reverts all patches in patch dir, notes down the ones 5 # which were previously applied. Applies all from the beginning 6 # and reports about previously unapplied patches. If there's 7 # an error, reverts the last one and stops. 8 # 9 # Usage: ./patcher -b <base_path> -r -v 10 # -b: <base_path> is the target location relative to which patches will be applied 11 # -p: <patch_path> is where to take the patches from (default is geth) 12 # -r: reverts all and exit if this flag is present 13 # -c: reverts all to see what's applied, applies all previously applied back again, 14 # reports unapplied patches in this branch by comparing with "develop" including 15 # uncommitted ones and exits (with 1 if there are any) 16 # -v: verbose error reporting about failed patch 17 # 18 # If -b is not present, default path is as below ($basepath). 19 20 dir=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd ) 21 # Patches path is geth unless specified. 22 patches=("$dir"/geth/*.patch) 23 24 # Use this branch as a reference for comparing patches 25 # in current branch (-c option). 26 baseBranch="develop" 27 28 # Base path is vendor/github.com/ethereum/go-ethereum 29 # unless specified. 30 basepath="vendor/github.com/ethereum/go-ethereum" 31 32 gitApply() { 33 f=$1 34 basepath=$2 35 verbose=$3 36 37 if [ $verbose -eq 1 ]; then 38 if [ $basepath == "." ]; then 39 git apply "$f" 40 else 41 git apply "$f" --directory="$basepath" 42 fi 43 else 44 if [ $basepath == "." ]; then 45 git apply "$f" > /dev/null 2>&1 46 else 47 git apply "$f" --directory="$basepath" > /dev/null 2>&1 48 fi 49 fi 50 } 51 52 gitApplyReverse() { 53 f=$1 54 basepath=$2 55 56 if [ $basepath == "." ]; then 57 git apply "$f" -R > /dev/null 2>&1 58 else 59 git apply "$f" --directory="$basepath" -R > /dev/null 2>&1 60 fi 61 } 62 63 verbose=0 64 revert=0 65 compare=0 66 while getopts b:p:rcv opt; do 67 case $opt in 68 b) 69 basepath=$OPTARG 70 ;; 71 p) 72 patches=("$dir"/$OPTARG/*.patch) 73 ;; 74 r) 75 revert=1 76 ;; 77 c) 78 compare=1 79 ;; 80 v) 81 verbose=1 82 ;; 83 \?) 84 echo "Invalid flag: -$OPTARG" >&2 85 exit 86 ;; 87 esac 88 done 89 90 if [ $revert -eq 1 ]; then 91 # Reverts in reverse order and exits. 92 for ((i=${#patches[@]}-1; i>=0; i--)); do 93 gitApplyReverse "${patches[$i]}" "$basepath" 0 94 done 95 echo "Reverted all." 96 exit 97 fi 98 if [ $compare -eq 1 ]; then 99 applied=() 100 unapplied=() 101 # Finds applied patches using reverse order and 102 # notes them down. 103 for ((i=${#patches[@]}-1; i>=0; i--)); do 104 f=${patches[$i]} 105 gitApplyReverse "$f" "$basepath" 106 if [ $? -ne 0 ]; then 107 unapplied+=("$f") 108 else 109 applied+=("$f") 110 fi 111 done 112 # Applies reverted patches back again. 113 for ((i=${#applied[@]}-1; i>=0; i--)); do 114 f=${applied[$i]} 115 gitApply "$f" "$basepath" 0 116 done 117 # Sorts out new patches' paths by comparing with base branch. 118 fromBaseBranch=($(git diff $baseBranch --stat | grep "\\.patch" | 119 while IFS=" " read -r -a line; do 120 path="$(pwd)/${line[0]}" 121 echo "$path" 122 done 123 )) 124 # Also does the same with uncommitted. 125 uncommitted=($(git status -u --porcelain | grep "\\.patch" | 126 while IFS=" " read -r -a line; do 127 length=${#line[@]} 128 path="$(pwd)/${line[$((length - 1))]}" 129 echo "$path" 130 done 131 )) 132 newPatches=( "${fromBaseBranch[@]}" "${uncommitted[@]}" ) 133 # Checks new patches and exits with 1 if there are unapplied. 134 hasUnapplied=0 135 for newPatch in "${newPatches[@]}"; do 136 for unapp in "${unapplied[@]}"; do 137 if [ "$unapp" == "$newPatch" ]; then 138 echo "Recently added/changed but not applied: $unapp" 139 hasUnapplied=1 140 break 141 fi 142 done 143 done 144 exit $hasUnapplied 145 fi 146 147 applied=() 148 149 echo -en "\\n" 150 echo "Previously applied:" 151 echo "===================" 152 # Reverts every patch in reverse order to see 153 # which was previously applied. 154 for ((i=${#patches[@]}-1; i>=0; i--)); do 155 f=${patches[$i]} 156 gitApplyReverse "$f" "$basepath" 157 if [ $? -eq 0 ]; then 158 applied+=("$f") 159 echo "$f" 160 fi 161 done 162 echo "===================" 163 echo -en "\\n" 164 165 # Applies every patch from the beginning. 166 for ((i=0; i<${#patches[@]}; i++)); do 167 f=${patches[$i]} 168 # If not applied, report it new. 169 has=0 170 for patch in "${applied[@]}"; do 171 if [ "$patch" == "$f" ]; then 172 has=1 173 break 174 fi 175 done 176 if [ $has -eq 0 ]; then 177 echo "Applying new: $f" 178 echo -en "\\n" 179 fi 180 gitApply "$f" "$basepath" $verbose 181 if [ $? -ne 0 ]; then 182 echo "Failed and reverting: $f" 183 gitApplyReverse "$f" "$basepath" 184 echo -en "\\n" 185 exit 1 186 fi 187 done 188 189 echo -en "\\n" 190 echo "Done."