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