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