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