From 648dfcf31851bfaaca33e4e02b49305bd5bd6ae1 Mon Sep 17 00:00:00 2001 From: Misty Stanley-Jones Date: Fri, 6 Nov 2015 10:33:43 +1000 Subject: [PATCH] HBASE-14776 Rewrite smart-apply-patch.sh to use 'git am' or 'git apply' rather than 'patch' --- dev-support/smart-apply-patch.sh | 83 +++++++++++++++++----------------------- 1 file changed, 36 insertions(+), 47 deletions(-) diff --git a/dev-support/smart-apply-patch.sh b/dev-support/smart-apply-patch.sh index 9200e3b..53f05c1 100755 --- a/dev-support/smart-apply-patch.sh +++ b/dev-support/smart-apply-patch.sh @@ -16,15 +16,14 @@ # specific language governing permissions and limitations # under the License. -set -e +#set -e PATCH_FILE=$1 -if [ -z "$PATCH_FILE" ]; then +if [ -z "$PATCH_FILE" -o ! -f "$PATCH_FILE" ]; then echo usage: $0 patch-file exit 1 fi -PATCH=${PATCH:-patch} # allow overriding patch binary # Cleanup handler for temporary files TOCLEAN="" @@ -45,52 +44,42 @@ fi TMP=/tmp/tmp.paths.$$ TOCLEAN="$TOCLEAN $TMP" -if $PATCH -p0 -E --dry-run < $PATCH_FILE 2>&1 > $TMP; 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. +AM_WORKS=0 +APPLY_WORKS=0 -# NOTE 2014/07/17: -# Temporarily disabling below check since our jenkins boxes seems to be not defaulting to bash -# causing below checks to fail. Once it is fixed, we can revert the commit and enable this again. - -# TMP2=/tmp/tmp.paths.2.$$ -# TOCLEAN="$TOCLEAN $TMP2" -# -# grep '^patching file ' $TMP | awk '{print $3}' | grep -v /dev/null | sort | uniq > $TMP2 -# -# #first off check that all of the files do not exist -# FOUND_ANY=0 -# for CHECK_FILE in $(cat $TMP2) -# do -# if [[ -f $CHECK_FILE ]]; then -# FOUND_ANY=1 -# fi -# done -# -# if [[ "$FOUND_ANY" = "0" ]]; then -# #all of the files are new files so we have to guess where the correct place to put it is. -# -# # 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 -# 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 -# fi -# fi -elif $PATCH -p1 -E --dry-run < $PATCH_FILE 2>&1 > /dev/null; then - PLEVEL=1 -elif $PATCH -p2 -E --dry-run < $PATCH_FILE 2>&1 > /dev/null; then - PLEVEL=2 +# First, try git am if the patch has a Subject: header +grep -q 'Subject:' $PATCH_FILE +status=$? +if [ $status -eq 0 ]; then + git am $PATCH_FILE + status=$? + if [ $status -eq 0 ]; then + AM_WORKS=1 + echo "Patch applies with git am: $status" + # Auto-merge actually applies the patch, we need to pop off the new commit + git reset -q --hard HEAD^ + else + echo "Patch does not apply with git am." + fi else - echo "The patch does not appear to apply with p0 to p2"; - cleanup 1; + echo "Patch has no headers. Not trying git am." fi -echo Going to apply patch with: $PATCH -p$PLEVEL -$PATCH -p$PLEVEL -E < $PATCH_FILE - +if [ $AM_WORKS -eq 0 ]; then + # try git apply + git apply $PATCH_FILE + status=$? + if [ $status -eq 0 ]; then + APPLY_WORKS=1 + echo "Patch applies with git apply. Consider using git format-patch to create patches." + # Apply does not commit, just reset + git reset -q --hard + else + echo "Patch does not apply with git apply." + fi +fi +if [ $AM_WORKS -eq 0 -a $APPLY_WORKS -eq 0 ]; then + echo "Could not apply the patch." + cleanup 1 +fi cleanup $? -- 2.4.9 (Apple Git-60)