##// END OF EJS Templates
hgmerge: add and use ask_if_merged function...
Radoslaw Szkodzinski -
r1771:e22bbca2 default
parent child Browse files
Show More
@@ -1,168 +1,171 b''
1 1 #!/bin/sh
2 2 #
3 3 # hgmerge - default merge helper for Mercurial
4 4 #
5 5 # This tries to find a way to do three-way merge on the current system.
6 6 # The result ought to end up in $1.
7 7
8 8 set -e # bail out quickly on failure
9 9
10 10 LOCAL="$1"
11 11 BASE="$2"
12 12 OTHER="$3"
13 13
14 14 if [ -z "$EDITOR" ]; then
15 15 EDITOR="vi"
16 16 fi
17 17
18 18 # find decent versions of our utilities, insisting on the GNU versions where we
19 19 # need to
20 20 MERGE="merge"
21 21 DIFF3="gdiff3"
22 22 DIFF="gdiff"
23 23 PATCH="gpatch"
24 24
25 25 type "$MERGE" >/dev/null 2>&1 || MERGE=
26 26 type "$DIFF3" >/dev/null 2>&1 || DIFF3="diff3"
27 27 $DIFF3 --version >/dev/null 2>&1 || DIFF3=
28 28 type "$DIFF" >/dev/null 2>&1 || DIFF="diff"
29 29 type "$DIFF" >/dev/null 2>&1 || DIFF=
30 30 type "$PATCH" >/dev/null 2>&1 || PATCH="patch"
31 31 type "$PATCH" >/dev/null 2>&1 || PATCH=
32 32
33 33 # find optional visual utilities
34 34 FILEMERGE="/Developer/Applications/Utilities/FileMerge.app/Contents/MacOS/FileMerge"
35 35 KDIFF3="kdiff3"
36 36 TKDIFF="tkdiff"
37 37
38 38 type "$FILEMERGE" >/dev/null 2>&1 || FILEMERGE=
39 39 type "$KDIFF3" >/dev/null 2>&1 || KDIFF3=
40 40 type "$TKDIFF" >/dev/null 2>&1 || TKDIFF=
41 41
42 42 # random part of names
43 43 RAND="$RANDOM$RANDOM"
44 44
45 45 # temporary directory for diff+patch merge
46 46 HGTMP="${TMPDIR-'/tmp'}/hgmerge.$RAND"
47 47
48 48 # backup file
49 49 BACKUP="$LOCAL.orig.$RAND"
50 50
51 51 # file used to test for file change
52 52 CHGTEST="$LOCAL.chg.$RAND"
53 53
54 54 # put all your required cleanup here
55 55 cleanup() {
56 56 rm -f "$BACKUP" "$CHGTEST"
57 57 rm -rf "$HGTMP"
58 58 }
59 59
60 60 # functions concerning program exit
61 61 success() {
62 62 cleanup
63 63 exit 0
64 64 }
65 65
66 66 failure() {
67 67 echo "merge failed" 1>&2
68 68 mv "$BACKUP" "$LOCAL"
69 69 cleanup
70 70 exit 1
71 71 }
72 72
73 # Ask if the merge was successful
74 ask_if_merged() {
75 while 1; do
76 echo "$LOCAL seems unchanged. Was the merge successful? [y/n]"
77 read answer
78 case answer in
79 y*|Y*) success;;
80 n*|N*) failure;;
81 esac
82 done
83 }
84
73 85 # Clean up when interrupted
74 86 trap "failure" 1 2 3 6 15 # HUP INT QUIT ABRT TERM
75 87
76 88 # Back up our file (and try hard to keep the mtime unchanged)
77 89 mv "$LOCAL" "$BACKUP"
78 90 cp "$BACKUP" "$LOCAL"
79 91
80 92 # Attempt to do a non-interactive merge
81 93 if [ -n "$MERGE" -o -n "$DIFF3" ]; then
82 94 if [ -n "$MERGE" ]; then
83 95 $MERGE "$LOCAL" "$BASE" "$OTHER" 2> /dev/null && success
84 96 elif [ -n "$DIFF3" ]; then
85 97 $DIFF3 -m "$BACKUP" "$BASE" "$OTHER" > "$LOCAL" && success
86 98 fi
87 99 if [ $? -gt 1 ]; then
88 100 echo "automatic merge failed! Exiting." 1>&2
89 101 failure
90 102 fi
91 103 fi
92 104 cp "$BACKUP" "$LOCAL"
93 105
94 106 # on MacOS X try FileMerge.app, shipped with Apple's developer tools
95 107 if [ -n "$FILEMERGE" ]; then
96 108 cp "$BACKUP" "$LOCAL"
97 109 cp "$BACKUP" "$CHGTEST"
98 110 # filemerge prefers the right by default
99 111 $FILEMERGE -left "$OTHER" -right "$LOCAL" -ancestor "$BASE" -merge "$LOCAL"
100 112 [ $? -ne 0 ] && echo "FileMerge failed to launch" && failure
101 if test "$LOCAL" -nt "$CHGTEST"
102 then
103 success
104 else
105 echo "$LOCAL seems unchanged. Was the merge successful?"
106 select answer in yes no
107 do
108 test "$answer" == "yes" && success || failure
109 done
110 fi
113 test "$LOCAL" -nt "$CHGTEST" && success || ask_if_merged
111 114 failure
112 115 fi
113 116
114 117 if [ -n "$DISPLAY" ]; then
115 118 # try using kdiff3, which is fairly nice
116 119 if [ -n "$KDIFF3" ]; then
117 120 $KDIFF3 --auto "$BASE" "$LOCAL" "$OTHER" -o "$LOCAL" || failure
118 121 success
119 122 fi
120 123
121 124 # try using tkdiff, which is a bit less sophisticated
122 125 if [ -n "$TKDIFF" ]; then
123 126 $TKDIFF "$LOCAL" "$OTHER" -a "$BASE" -o "$LOCAL" || failure
124 127 success
125 128 fi
126 129 fi
127 130
128 131 # Attempt to do a merge with $EDITOR
129 132 if [ -n "$MERGE" ]; then
130 133 echo "conflicts detected in $LOCAL"
131 134 $MERGE "$LOCAL" "$BASE" "$OTHER" 2>/dev/null || $EDITOR "$LOCAL"
132 135 success
133 136 fi
134 137
135 138 if [ -n "$DIFF3" ]; then
136 139 echo "conflicts detected in $LOCAL"
137 140 $DIFF3 -m "$BACKUP" "$BASE" "$OTHER" > "$LOCAL" || {
138 141 case $? in
139 142 1)
140 143 $EDITOR "$LOCAL" ;;
141 144 2) echo "$DIFF3 failed! Exiting." 1>&2
142 145 cp "$BACKUP" "$LOCAL"
143 146 failure ;;
144 147 esac
145 148 success
146 149 }
147 150 fi
148 151
149 152 # attempt to manually merge with diff and patch
150 153 if [ -n "$DIFF" -a -n "$PATCH" ]; then
151 154
152 155 (umask 077 && mkdir "$HGTMP") || {
153 156 echo "Could not create temporary directory $HGTMP" 1>&2
154 157 failure
155 158 }
156 159
157 160 $DIFF -u "$BASE" "$OTHER" > "$HGTMP/diff" || :
158 161 if $PATCH "$LOCAL" < "$HGTMP/diff"; then
159 162 success
160 163 else
161 164 # If rejects are empty after using the editor, merge was ok
162 165 $EDITOR "$LOCAL" "$LOCAL.rej" && test -s "$LOCAL.rej" || success
163 166 fi
164 167 failure
165 168 fi
166 169
167 170 echo "hgmerge: unable to find merge, tkdiff, kdiff3, or diff+patch!"
168 171 failure
General Comments 0
You need to be logged in to leave comments. Login now