ghmerge: restore to original commit HEAD of target on error/abort
authorDr. David von Oheimb <David.von.Oheimb@siemens.com>
Mon, 22 Nov 2021 12:37:50 +0000 (13:37 +0100)
committerTomas Mraz <tomas@openssl.org>
Mon, 22 Nov 2021 13:54:52 +0000 (14:54 +0100)
Also add an empty line before the git log output for readability.

Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from https://github.com/openssl/tools/pull/97)

review-tools/ghmerge

index 2bd8ee966a69d701f1bce29dd0759b2e58d20ec7..1371bfba7dca4026f55529176db861aa9ba08db5 100755 (executable)
@@ -178,6 +178,10 @@ function cleanup {
         echo "Hint: maybe --cherry-pick was not given a suitable <n> parameter."
         git cherry-pick --abort 2>/dev/null || true
     fi
+    if [ "$ORIG_TARGET_HEAD" != "" ]; then
+        echo Restoring original commit HEAD of $TARGET
+        git reset --hard "$ORIG_TARGET_HEAD"
+    fi
     if [ "$TARGET" != "$ORIG_REF" ] || [ "$WORK_USED" != "" ]; then
         echo Returning to previous branch $ORIG_REF
         git checkout -q $ORIG_REF
@@ -186,7 +190,7 @@ function cleanup {
         git branch -qD $WORK_USED
     fi
     if [ "$STASH_OUT" != "No local changes to save" ]; then
-        git stash pop -q # restore original state, pruning any leftover commits added locally
+        git stash pop -q # restore original state of $ORIG_REF, pruning any leftover commits added locally
     fi
 }
 trap 'cleanup' EXIT
@@ -231,18 +235,23 @@ if [ "$INTERACTIVE" == "yes" ] ; then
     addrev $ADDREVOPTS --prnum=$PRNUM $TEAM $REMOTE/$TARGET..
 fi
 
+echo
 echo Log since $REMOTE/$TARGET
 git log $REMOTE/$TARGET..
 
 git checkout $TARGET
 if [ "$INTERACTIVE" != "yes" ] ; then
     echo -n "Press Enter to non-interactively merge --squash $BRANCH to $REMOTE/$TARGET: "; read foo
+    ORIG_TARGET_HEAD=`git show -s --format="%H"`
     git merge --ff-only --no-commit --squash $WORK
     AUTHOR=`git show --no-patch --pretty="format:%an <%ae>" $WORK`
     git commit --author="$AUTHOR"
     addrev $ADDREVOPTS --prnum=$PRNUM $TEAM $REMOTE/${TARGET}..
 else
-    # echo -n "Press Enter to merge to $REMOTE/$TARGET: "; read foo
+    # echo -n "Press Enter to merge to $TARGET: "; read foo
+    echo
+    echo "Merging to $TARGET"
+    ORIG_TARGET_HEAD=`git show -s --format="%H"`
     git merge --ff-only $WORK
 fi
 
@@ -265,4 +274,5 @@ done
 
 if [ "$x" = "y" -o "$x" = "yes" ] ; then
     git push -v $REMOTE $TARGET
+    ORIG_TARGET_HEAD=
 fi