diff --git testutils/ptest2/src/main/resources/smart-apply-patch.sh testutils/ptest2/src/main/resources/smart-apply-patch.sh index 5a133d5..2a66b35 100644 --- testutils/ptest2/src/main/resources/smart-apply-patch.sh +++ testutils/ptest2/src/main/resources/smart-apply-patch.sh @@ -20,12 +20,10 @@ if [ -z "$PATCH_FILE" ]; then exit 1 fi -PATCH=${PATCH:-patch} # allow overriding patch binary - # Cleanup handler for temporary files TOCLEAN="" cleanup() { - rm $TOCLEAN + [ "x$TOCLEAN" != "x" ] && rm $TOCLEAN exit $1 } trap "cleanup 1" HUP INT QUIT TERM @@ -37,29 +35,21 @@ if [ "$PATCH_FILE" == "-" ]; then TOCLEAN="$TOCLEAN $PATCH_FILE" fi -# Come up with a list of changed files into $TMP -TMP=/tmp/tmp.paths.$$ -TOCLEAN="$TOCLEAN $TMP" - -if $PATCH -p0 -E --dry-run < $PATCH_FILE 2>&1 > $TMP; then +if git apply -p0 --check $PATCH_FILE 2>&1 > /dev/null; then PLEVEL=0 - #if the patch applied at P0 there is the possability that all we are doing - # is adding new files and they would apply anywhere. So try to guess the - # correct place to put those files. - - TMP2=/tmp/tmp.paths.2.$$ - TOCLEAN="$TOCLEAN $TMP2" + CHANGED_FILES=/tmp/tmp.paths.2.$$ + TOCLEAN="$TOCLEAN $CHANGED_FILES" - egrep '^patching file |^checking file ' $TMP | awk '{print $3}' | grep -v /dev/null | sort | uniq > $TMP2 + git apply -p0 --stat $PATCH_FILE | head -1 | awk '{print $1}' > $CHANGED_FILES - if [ ! -s $TMP2 ]; then + if [ ! -s $CHANGED_FILES ]; then echo "Error: Patch dryrun couldn't detect changes the patch would make. Exiting." cleanup 1 fi #first off check that all of the files do not exist FOUND_ANY=0 - for CHECK_FILE in $(cat $TMP2) + for CHECK_FILE in $(cat $CHANGED_FILES) do if [[ -f $CHECK_FILE ]]; then FOUND_ANY=1 @@ -71,21 +61,18 @@ if $PATCH -p0 -E --dry-run < $PATCH_FILE 2>&1 > $TMP; then # if all of the lines start with a/ or b/, then this is a git patch that # was generated without --no-prefix - if ! grep -qv '^a/\|^b/' $TMP2 ; then + if ! grep -qv '^a/\|^b/' $CHANGED_FILES ; then echo Looks like this is a git patch. Stripping a/ and b/ prefixes echo and incrementing PLEVEL PLEVEL=$[$PLEVEL + 1] - sed -i -e 's,^[ab]/,,' $TMP2 + sed -i -e 's,^[ab]/,,' $CHANGED_FILES fi fi -elif $PATCH -p1 -E --dry-run < $PATCH_FILE 2>&1 > /dev/null; then +elif git apply -p1 --check $PATCH_FILE 2>&1 > /dev/null; then PLEVEL=1 -elif $PATCH -p2 -E --dry-run < $PATCH_FILE 2>&1 > /dev/null; then +elif git apply -p2 --check $PATCH_FILE 2>&1 > /dev/null; then PLEVEL=2 -elif git apply -p0 --check $PATCH_FILE 2>&1 > /dev/null; then - git apply -p0 $PATCH_FILE - cleanup $? else echo "The patch does not appear to apply with p0, p1, or p2"; cleanup 1; @@ -96,7 +83,7 @@ if [[ -n $DRY_RUN ]]; then cleanup 0; fi -echo Going to apply patch with: $PATCH -p$PLEVEL -$PATCH -p$PLEVEL -E < $PATCH_FILE +echo Going to apply patch with: git apply -p$PLEVEL +git apply -p$PLEVEL $PATCH_FILE cleanup $? \ No newline at end of file