changeset 1700:e2f91e0acbb8

hgmerge: add cleanup functions This patch adds functions cleanup, success and failure. The last two of these should be used instead of exit. Current code was changed to use them. It also moves $HGTMP to the top of the file (it's used in the cleanup function), changes the comment and removes now unneeded trap in the diff+patch merge.
author Radoslaw Szkodzinski <astralstorm@gorzow.mm.pl>
date Mon, 06 Feb 2006 17:32:06 -0600
parents 83e8cd97b9f9
children 4ba8fe499df2
files hgmerge
diffstat 1 files changed, 46 insertions(+), 30 deletions(-) [+]
line wrap: on
line diff
--- a/hgmerge	Mon Feb 06 17:32:00 2006 -0600
+++ b/hgmerge	Mon Feb 06 17:32:06 2006 -0600
@@ -37,20 +37,45 @@
 type $KDIFF3    >/dev/null 2>&1 || KDIFF3=
 type $TKDIFF    >/dev/null 2>&1 || TKDIFF=
 
+# temporary directory for diff+patch merge
+HGTMP="${TMPDIR-/tmp}/hgmerge.$RANDOM.$RANDOM.$RANDOM.$$"
+
+# put all your required cleanup here
+cleanup() {
+    rm -f "$LOCAL.orig"
+    rm -rf "$HGTMP"
+}
+
+# functions concerning program exit
+success() {
+    cleanup
+    exit 0
+}
+
+failure() {
+    echo "merge failed" 1>&2
+    cp "$LOCAL.orig" "$LOCAL"
+    cleanup
+    exit 1
+}
+
+# Clean up when interrupted
+trap "failure" 1 2 3 6 15 # HUP INT QUIT ABRT TERM
+
 # Back up our file
 cp "$LOCAL" "$LOCAL.orig"
 
 # Attempt to do a non-interactive merge
 if [ -n "$MERGE" ]; then
-    $MERGE "$LOCAL" "$BASE" "$OTHER" 2> /dev/null && exit 0
+    $MERGE "$LOCAL" "$BASE" "$OTHER" 2> /dev/null && success
     cp "$LOCAL.orig" "$LOCAL"
 elif [ -n "$DIFF3" ]; then
     echo $DIFF3 -m "$LOCAL.orig" "$BASE" "$OTHER"
-    $DIFF3 -m "$LOCAL.orig" "$BASE" "$OTHER" > "$LOCAL" && exit 0
+    $DIFF3 -m "$LOCAL.orig" "$BASE" "$OTHER" > "$LOCAL" && success
     if [ $? -eq 2 ]; then
         echo "$DIFF3 failed! Exiting." 1>&2
         cp "$LOCAL.orig" "$LOCAL"
-        exit 1
+        failure
     fi
     cp "$LOCAL.orig" "$LOCAL"
 fi
@@ -65,12 +90,12 @@
     if ! "$FILEMERGE" -left "$OTHER" -right "$LOCAL" -ancestor "$BASE" -merge "$LOCAL"
     then
         echo "FileMerge failed to launch"
-        exit 1
+        failure
     fi
     if ! test "$LOCAL" -ef "$LOCAL.link"
     then
         rm "$LOCAL.orig" "$LOCAL.link"
-        exit 0
+        success
     else
         rm "$LOCAL.link"
         echo "$LOCAL is unchanged. Was the merge successful?"
@@ -79,26 +104,26 @@
             if test "$answer" == "yes"
             then
                 rm "$LOCAL.orig"
-                exit 0
+                success
             else
-                exit 1
+                failure
             fi
         done
-        exit 1
+        failure
     fi
 fi
 
 if [ -n "$DISPLAY" ]; then
     # try using kdiff3, which is fairly nice
     if [ -n "$KDIFF3" ]; then
-	$KDIFF3 --auto "$BASE" "$LOCAL" "$OTHER" -o "$LOCAL" || exit 1
-	exit 0
+	$KDIFF3 --auto "$BASE" "$LOCAL" "$OTHER" -o "$LOCAL" || failure
+	success
     fi
 
     # try using tkdiff, which is a bit less sophisticated
     if [ -n "$TKDIFF" ]; then
-	$TKDIFF "$LOCAL" "$OTHER" -a "$BASE" -o "$LOCAL" || exit 1
-	exit 0
+	$TKDIFF "$LOCAL" "$OTHER" -a "$BASE" -o "$LOCAL" || failure
+	success
     fi
 fi
 
@@ -106,7 +131,7 @@
 if [ -n "$MERGE" ]; then
     echo "conflicts detected in $LOCAL"
     $MERGE "$LOCAL" "$BASE" "$OTHER" 2>/dev/null || $EDITOR "$LOCAL"
-    exit 0
+    success
 fi
 
 if [ -n "$DIFF3" ]; then
@@ -117,38 +142,29 @@
                 $EDITOR "$LOCAL" ;;
             2)  echo "$DIFF3 failed! Exiting." 1>&2
                 cp "$LOCAL.orig" "$LOCAL"
-                exit 1 ;;
+                failure ;;
         esac
-        exit 0
+        success
     }
 fi
 
-HGTMP=""
-cleanup_exit() {
-    rm -rf "$HGTMP"
-}
-
 # attempt to manually merge with diff and patch
 if [ -n "$DIFF" -a -n "$PATCH" ]; then
-    # Remove temporary files even if we get interrupted
-    trap "cleanup_exit" 0 # normal exit
-    trap "exit 1" 1 2 3 6 15 # HUP INT QUIT ABRT TERM
 
-    HGTMP="${TMPDIR-/tmp}/hgmerge.$RANDOM.$RANDOM.$RANDOM.$$"
     (umask 077 && mkdir "$HGTMP") || {
-	echo "Could not create temporary directory! Exiting." 1>&2
-	exit 1
+	echo "Could not create temporary directory $HGTMP" 1>&2
+	failure
     }
 
     $DIFF -u "$BASE" "$OTHER" > "$HGTMP/diff" || :
     if $PATCH "$LOCAL" < "$HGTMP/diff"; then
-	exit 0
+	success
     else
 	# If rejects are empty after using the editor, merge was ok
-	$EDITOR "$LOCAL" "$LOCAL.rej" && test -s "$LOCAL.rej" || exit 0
+	$EDITOR "$LOCAL" "$LOCAL.rej" && test -s "$LOCAL.rej" || success
     fi
-    exit 1
+    failure
 fi
 
 echo "hgmerge: unable to find merge, tkdiff, kdiff3, or diff+patch!"
-exit 1
+failure