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
22 if [ -z "$PATCH_FILE" ]; then
23 echo usage
: $0 patch-file
27 PATCH
=${PATCH:-patch} # allow overriding patch binary
29 # Cleanup handler for temporary files
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
46 TOCLEAN
="$TOCLEAN $TMP"
48 if $PATCH -p0 -E --dry-run < $PATCH_FILE 2>&1 > $TMP; then
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.
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
65 # for CHECK_FILE in $(cat $TMP2)
67 # if [[ -f $CHECK_FILE ]]; then
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
84 elif $PATCH -p1 -E --dry-run < $PATCH_FILE 2>&1 > /dev
/null
; then
86 elif $PATCH -p2 -E --dry-run < $PATCH_FILE 2>&1 > /dev
/null
; then
89 echo "The patch does not appear to apply with p0 to p2";
93 echo Going to apply
patch with
: $PATCH -p$PLEVEL
94 $PATCH -p$PLEVEL -E < $PATCH_FILE