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