comparison hgmerge @ 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
comparison
equal deleted inserted replaced
1699:83e8cd97b9f9 1700:e2f91e0acbb8
35 35
36 type $FILEMERGE >/dev/null 2>&1 || FILEMERGE= 36 type $FILEMERGE >/dev/null 2>&1 || FILEMERGE=
37 type $KDIFF3 >/dev/null 2>&1 || KDIFF3= 37 type $KDIFF3 >/dev/null 2>&1 || KDIFF3=
38 type $TKDIFF >/dev/null 2>&1 || TKDIFF= 38 type $TKDIFF >/dev/null 2>&1 || TKDIFF=
39 39
40 # temporary directory for diff+patch merge
41 HGTMP="${TMPDIR-/tmp}/hgmerge.$RANDOM.$RANDOM.$RANDOM.$$"
42
43 # put all your required cleanup here
44 cleanup() {
45 rm -f "$LOCAL.orig"
46 rm -rf "$HGTMP"
47 }
48
49 # functions concerning program exit
50 success() {
51 cleanup
52 exit 0
53 }
54
55 failure() {
56 echo "merge failed" 1>&2
57 cp "$LOCAL.orig" "$LOCAL"
58 cleanup
59 exit 1
60 }
61
62 # Clean up when interrupted
63 trap "failure" 1 2 3 6 15 # HUP INT QUIT ABRT TERM
64
40 # Back up our file 65 # Back up our file
41 cp "$LOCAL" "$LOCAL.orig" 66 cp "$LOCAL" "$LOCAL.orig"
42 67
43 # Attempt to do a non-interactive merge 68 # Attempt to do a non-interactive merge
44 if [ -n "$MERGE" ]; then 69 if [ -n "$MERGE" ]; then
45 $MERGE "$LOCAL" "$BASE" "$OTHER" 2> /dev/null && exit 0 70 $MERGE "$LOCAL" "$BASE" "$OTHER" 2> /dev/null && success
46 cp "$LOCAL.orig" "$LOCAL" 71 cp "$LOCAL.orig" "$LOCAL"
47 elif [ -n "$DIFF3" ]; then 72 elif [ -n "$DIFF3" ]; then
48 echo $DIFF3 -m "$LOCAL.orig" "$BASE" "$OTHER" 73 echo $DIFF3 -m "$LOCAL.orig" "$BASE" "$OTHER"
49 $DIFF3 -m "$LOCAL.orig" "$BASE" "$OTHER" > "$LOCAL" && exit 0 74 $DIFF3 -m "$LOCAL.orig" "$BASE" "$OTHER" > "$LOCAL" && success
50 if [ $? -eq 2 ]; then 75 if [ $? -eq 2 ]; then
51 echo "$DIFF3 failed! Exiting." 1>&2 76 echo "$DIFF3 failed! Exiting." 1>&2
52 cp "$LOCAL.orig" "$LOCAL" 77 cp "$LOCAL.orig" "$LOCAL"
53 exit 1 78 failure
54 fi 79 fi
55 cp "$LOCAL.orig" "$LOCAL" 80 cp "$LOCAL.orig" "$LOCAL"
56 fi 81 fi
57 82
58 # on MacOS X try FileMerge.app, shipped with Apple's developer tools 83 # on MacOS X try FileMerge.app, shipped with Apple's developer tools
63 ln "$LOCAL" "$LOCAL.link" 88 ln "$LOCAL" "$LOCAL.link"
64 # filemerge prefers the right by default 89 # filemerge prefers the right by default
65 if ! "$FILEMERGE" -left "$OTHER" -right "$LOCAL" -ancestor "$BASE" -merge "$LOCAL" 90 if ! "$FILEMERGE" -left "$OTHER" -right "$LOCAL" -ancestor "$BASE" -merge "$LOCAL"
66 then 91 then
67 echo "FileMerge failed to launch" 92 echo "FileMerge failed to launch"
68 exit 1 93 failure
69 fi 94 fi
70 if ! test "$LOCAL" -ef "$LOCAL.link" 95 if ! test "$LOCAL" -ef "$LOCAL.link"
71 then 96 then
72 rm "$LOCAL.orig" "$LOCAL.link" 97 rm "$LOCAL.orig" "$LOCAL.link"
73 exit 0 98 success
74 else 99 else
75 rm "$LOCAL.link" 100 rm "$LOCAL.link"
76 echo "$LOCAL is unchanged. Was the merge successful?" 101 echo "$LOCAL is unchanged. Was the merge successful?"
77 select answer in yes no 102 select answer in yes no
78 do 103 do
79 if test "$answer" == "yes" 104 if test "$answer" == "yes"
80 then 105 then
81 rm "$LOCAL.orig" 106 rm "$LOCAL.orig"
82 exit 0 107 success
83 else 108 else
84 exit 1 109 failure
85 fi 110 fi
86 done 111 done
87 exit 1 112 failure
88 fi 113 fi
89 fi 114 fi
90 115
91 if [ -n "$DISPLAY" ]; then 116 if [ -n "$DISPLAY" ]; then
92 # try using kdiff3, which is fairly nice 117 # try using kdiff3, which is fairly nice
93 if [ -n "$KDIFF3" ]; then 118 if [ -n "$KDIFF3" ]; then
94 $KDIFF3 --auto "$BASE" "$LOCAL" "$OTHER" -o "$LOCAL" || exit 1 119 $KDIFF3 --auto "$BASE" "$LOCAL" "$OTHER" -o "$LOCAL" || failure
95 exit 0 120 success
96 fi 121 fi
97 122
98 # try using tkdiff, which is a bit less sophisticated 123 # try using tkdiff, which is a bit less sophisticated
99 if [ -n "$TKDIFF" ]; then 124 if [ -n "$TKDIFF" ]; then
100 $TKDIFF "$LOCAL" "$OTHER" -a "$BASE" -o "$LOCAL" || exit 1 125 $TKDIFF "$LOCAL" "$OTHER" -a "$BASE" -o "$LOCAL" || failure
101 exit 0 126 success
102 fi 127 fi
103 fi 128 fi
104 129
105 # Attempt to do a merge with $EDITOR 130 # Attempt to do a merge with $EDITOR
106 if [ -n "$MERGE" ]; then 131 if [ -n "$MERGE" ]; then
107 echo "conflicts detected in $LOCAL" 132 echo "conflicts detected in $LOCAL"
108 $MERGE "$LOCAL" "$BASE" "$OTHER" 2>/dev/null || $EDITOR "$LOCAL" 133 $MERGE "$LOCAL" "$BASE" "$OTHER" 2>/dev/null || $EDITOR "$LOCAL"
109 exit 0 134 success
110 fi 135 fi
111 136
112 if [ -n "$DIFF3" ]; then 137 if [ -n "$DIFF3" ]; then
113 echo "conflicts detected in $LOCAL" 138 echo "conflicts detected in $LOCAL"
114 $DIFF3 -m "$LOCAL.orig" "$BASE" "$OTHER" > "$LOCAL" || { 139 $DIFF3 -m "$LOCAL.orig" "$BASE" "$OTHER" > "$LOCAL" || {
115 case $? in 140 case $? in
116 1) 141 1)
117 $EDITOR "$LOCAL" ;; 142 $EDITOR "$LOCAL" ;;
118 2) echo "$DIFF3 failed! Exiting." 1>&2 143 2) echo "$DIFF3 failed! Exiting." 1>&2
119 cp "$LOCAL.orig" "$LOCAL" 144 cp "$LOCAL.orig" "$LOCAL"
120 exit 1 ;; 145 failure ;;
121 esac 146 esac
122 exit 0 147 success
123 } 148 }
124 fi 149 fi
125 150
126 HGTMP=""
127 cleanup_exit() {
128 rm -rf "$HGTMP"
129 }
130
131 # attempt to manually merge with diff and patch 151 # attempt to manually merge with diff and patch
132 if [ -n "$DIFF" -a -n "$PATCH" ]; then 152 if [ -n "$DIFF" -a -n "$PATCH" ]; then
133 # Remove temporary files even if we get interrupted
134 trap "cleanup_exit" 0 # normal exit
135 trap "exit 1" 1 2 3 6 15 # HUP INT QUIT ABRT TERM
136 153
137 HGTMP="${TMPDIR-/tmp}/hgmerge.$RANDOM.$RANDOM.$RANDOM.$$"
138 (umask 077 && mkdir "$HGTMP") || { 154 (umask 077 && mkdir "$HGTMP") || {
139 echo "Could not create temporary directory! Exiting." 1>&2 155 echo "Could not create temporary directory $HGTMP" 1>&2
140 exit 1 156 failure
141 } 157 }
142 158
143 $DIFF -u "$BASE" "$OTHER" > "$HGTMP/diff" || : 159 $DIFF -u "$BASE" "$OTHER" > "$HGTMP/diff" || :
144 if $PATCH "$LOCAL" < "$HGTMP/diff"; then 160 if $PATCH "$LOCAL" < "$HGTMP/diff"; then
145 exit 0 161 success
146 else 162 else
147 # If rejects are empty after using the editor, merge was ok 163 # If rejects are empty after using the editor, merge was ok
148 $EDITOR "$LOCAL" "$LOCAL.rej" && test -s "$LOCAL.rej" || exit 0 164 $EDITOR "$LOCAL" "$LOCAL.rej" && test -s "$LOCAL.rej" || success
149 fi 165 fi
150 exit 1 166 failure
151 fi 167 fi
152 168
153 echo "hgmerge: unable to find merge, tkdiff, kdiff3, or diff+patch!" 169 echo "hgmerge: unable to find merge, tkdiff, kdiff3, or diff+patch!"
154 exit 1 170 failure