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