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