HBASE-16530 Reduce DBE code duplication
[hbase.git] / dev-support / smart-apply-patch.sh
blob9200e3ba921c406644e8e6c4e7cdab50924f9141
1 #!/usr/bin/env bash
2 # Licensed to the Apache Software Foundation (ASF) under one
3 # or more contributor license agreements. See the NOTICE file
4 # distributed with this work for additional information
5 # regarding copyright ownership. The ASF licenses this file
6 # to you under the Apache License, Version 2.0 (the
7 # "License"); you may not use this file except in compliance
8 # with the License. You may obtain a copy of the License at
10 # http://www.apache.org/licenses/LICENSE-2.0
12 # Unless required by applicable law or agreed to in writing,
13 # software distributed under the License is distributed on an
14 # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 # KIND, either express or implied. See the License for the
16 # specific language governing permissions and limitations
17 # under the License.
19 set -e
21 PATCH_FILE=$1
22 if [ -z "$PATCH_FILE" ]; then
23 echo usage: $0 patch-file
24 exit 1
27 PATCH=${PATCH:-patch} # allow overriding patch binary
29 # Cleanup handler for temporary files
30 TOCLEAN=""
31 cleanup() {
32 rm $TOCLEAN
33 exit $1
35 trap "cleanup 1" HUP INT QUIT TERM
37 # Allow passing "-" for stdin patches
38 if [ "$PATCH_FILE" == "-" ]; then
39 PATCH_FILE=/tmp/tmp.in.$$
40 cat /dev/fd/0 > $PATCH_FILE
41 TOCLEAN="$TOCLEAN $PATCH_FILE"
44 # Come up with a list of changed files into $TMP
45 TMP=/tmp/tmp.paths.$$
46 TOCLEAN="$TOCLEAN $TMP"
48 if $PATCH -p0 -E --dry-run < $PATCH_FILE 2>&1 > $TMP; then
49 PLEVEL=0
50 #if the patch applied at P0 there is the possability that all we are doing
51 # is adding new files and they would apply anywhere. So try to guess the
52 # correct place to put those files.
54 # NOTE 2014/07/17:
55 # Temporarily disabling below check since our jenkins boxes seems to be not defaulting to bash
56 # causing below checks to fail. Once it is fixed, we can revert the commit and enable this again.
58 # TMP2=/tmp/tmp.paths.2.$$
59 # TOCLEAN="$TOCLEAN $TMP2"
61 # grep '^patching file ' $TMP | awk '{print $3}' | grep -v /dev/null | sort | uniq > $TMP2
63 # #first off check that all of the files do not exist
64 # FOUND_ANY=0
65 # for CHECK_FILE in $(cat $TMP2)
66 # do
67 # if [[ -f $CHECK_FILE ]]; then
68 # FOUND_ANY=1
69 # fi
70 # done
72 # if [[ "$FOUND_ANY" = "0" ]]; then
73 # #all of the files are new files so we have to guess where the correct place to put it is.
75 # # if all of the lines start with a/ or b/, then this is a git patch that
76 # # was generated without --no-prefix
77 # if ! grep -qv '^a/\|^b/' $TMP2 ; then
78 # echo Looks like this is a git patch. Stripping a/ and b/ prefixes
79 # echo and incrementing PLEVEL
80 # PLEVEL=$[$PLEVEL + 1]
81 # sed -i -e 's,^[ab]/,,' $TMP2
82 # fi
83 # fi
84 elif $PATCH -p1 -E --dry-run < $PATCH_FILE 2>&1 > /dev/null; then
85 PLEVEL=1
86 elif $PATCH -p2 -E --dry-run < $PATCH_FILE 2>&1 > /dev/null; then
87 PLEVEL=2
88 else
89 echo "The patch does not appear to apply with p0 to p2";
90 cleanup 1;
93 echo Going to apply patch with: $PATCH -p$PLEVEL
94 $PATCH -p$PLEVEL -E < $PATCH_FILE
96 cleanup $?