]>
Commit | Line | Data |
---|---|---|
c4b4a5af TT |
1 | #!/bin/sh |
2 | # | |
3 | # This program resolves merge conflicts in git | |
4 | # | |
5 | # Copyright (c) 2006 Theodore Y. Ts'o | |
6 | # | |
7 | # This file is licensed under the GPL v2, or a later version | |
8 | # at the discretion of Junio C Hammano. | |
9 | # | |
10 | ||
11 | USAGE='[--tool=tool] [file to merge] ...' | |
12 | SUBDIRECTORY_OK=Yes | |
13 | . git-sh-setup | |
14 | require_work_tree | |
15 | ||
16 | # Returns true if the mode reflects a symlink | |
17 | function is_symlink () { | |
18 | test "$1" = 120000 | |
19 | } | |
20 | ||
21 | function local_present () { | |
22 | test -n "$local_mode" | |
23 | } | |
24 | ||
25 | function remote_present () { | |
26 | test -n "$remote_mode" | |
27 | } | |
28 | ||
29 | function base_present () { | |
30 | test -n "$base_mode" | |
31 | } | |
32 | ||
33 | cleanup_temp_files () { | |
34 | if test "$1" = --save-backup ; then | |
35 | mv -- "$BACKUP" "$path.orig" | |
36 | rm -f -- "$LOCAL" "$REMOTE" "$BASE" | |
37 | else | |
38 | rm -f -- "$LOCAL" "$REMOTE" "$BASE" "$BACKUP" | |
39 | fi | |
40 | } | |
41 | ||
42 | function describe_file () { | |
43 | mode="$1" | |
44 | branch="$2" | |
45 | file="$3" | |
46 | ||
20fa04ea | 47 | printf " " |
c4b4a5af | 48 | if test -z "$mode"; then |
20fa04ea | 49 | printf "'%s' was deleted" "$path" |
c4b4a5af | 50 | elif is_symlink "$mode" ; then |
20fa04ea | 51 | printf "'%s' is a symlink containing '%s'" "$path" "$file" |
c4b4a5af TT |
52 | else |
53 | if base_present; then | |
20fa04ea | 54 | printf "'%s' was created" "$path" |
c4b4a5af | 55 | else |
20fa04ea | 56 | printf "'%s' was modified" "$path" |
c4b4a5af TT |
57 | fi |
58 | fi | |
59 | echo " in the $branch branch" | |
60 | } | |
61 | ||
62 | ||
63 | resolve_symlink_merge () { | |
64 | while /bin/true; do | |
20fa04ea | 65 | printf "Use (r)emote or (l)ocal, or (a)bort? " |
c4b4a5af TT |
66 | read ans |
67 | case "$ans" in | |
68 | [lL]*) | |
69 | git-checkout-index -f --stage=2 -- "$path" | |
70 | git-add -- "$path" | |
71 | cleanup_temp_files --save-backup | |
72 | return | |
73 | ;; | |
74 | [rR]*) | |
75 | git-checkout-index -f --stage=3 -- "$path" | |
76 | git-add -- "$path" | |
77 | cleanup_temp_files --save-backup | |
78 | return | |
79 | ;; | |
80 | [qQ]*) | |
81 | exit 1 | |
82 | ;; | |
83 | esac | |
84 | done | |
85 | } | |
86 | ||
87 | resolve_deleted_merge () { | |
88 | while /bin/true; do | |
20fa04ea | 89 | printf "Use (m)odified or (d)eleted file, or (a)bort? " |
c4b4a5af TT |
90 | read ans |
91 | case "$ans" in | |
92 | [mM]*) | |
93 | git-add -- "$path" | |
94 | cleanup_temp_files --save-backup | |
95 | return | |
96 | ;; | |
97 | [dD]*) | |
98 | git-rm -- "$path" | |
99 | cleanup_temp_files | |
100 | return | |
101 | ;; | |
102 | [qQ]*) | |
103 | exit 1 | |
104 | ;; | |
105 | esac | |
106 | done | |
107 | } | |
108 | ||
109 | merge_file () { | |
110 | path="$1" | |
111 | ||
112 | if test ! -f "$path" ; then | |
113 | echo "$path: file not found" | |
114 | exit 1 | |
115 | fi | |
116 | ||
117 | f=`git-ls-files -u -- "$path"` | |
118 | if test -z "$f" ; then | |
119 | echo "$path: file does not need merging" | |
120 | exit 1 | |
121 | fi | |
122 | ||
123 | BACKUP="$path.BACKUP.$$" | |
124 | LOCAL="$path.LOCAL.$$" | |
125 | REMOTE="$path.REMOTE.$$" | |
126 | BASE="$path.BASE.$$" | |
127 | ||
128 | mv -- "$path" "$BACKUP" | |
129 | cp -- "$BACKUP" "$path" | |
130 | ||
131 | base_mode=`git ls-files -u -- "$path" | awk '{if ($3==1) print $1;}'` | |
132 | local_mode=`git ls-files -u -- "$path" | awk '{if ($3==2) print $1;}'` | |
133 | remote_mode=`git ls-files -u -- "$path" | awk '{if ($3==3) print $1;}'` | |
134 | ||
135 | base_present && git cat-file blob ":1:$path" > "$BASE" 2>/dev/null | |
136 | local_present && git cat-file blob ":2:$path" > "$LOCAL" 2>/dev/null | |
137 | remote_present && git cat-file blob ":3:$path" > "$REMOTE" 2>/dev/null | |
138 | ||
139 | if test -z "$local_mode" -o -z "$remote_mode"; then | |
140 | echo "Deleted merge conflict for $path:" | |
141 | describe_file "$local_mode" "local" "$LOCAL" | |
142 | describe_file "$remote_mode" "remote" "$REMOTE" | |
143 | resolve_deleted_merge | |
144 | return | |
145 | fi | |
146 | ||
147 | if is_symlink "$local_mode" || is_symlink "$remote_mode"; then | |
148 | echo "Symlink merge conflict for $path:" | |
149 | describe_file "$local_mode" "local" "$LOCAL" | |
150 | describe_file "$remote_mode" "remote" "$REMOTE" | |
151 | resolve_symlink_merge | |
152 | return | |
153 | fi | |
154 | ||
155 | echo "Normal merge conflict for $path:" | |
156 | describe_file "$local_mode" "local" "$LOCAL" | |
157 | describe_file "$remote_mode" "remote" "$REMOTE" | |
20fa04ea | 158 | printf "Hit return to start merge resolution tool (%s): " "$merge_tool" |
c4b4a5af TT |
159 | read ans |
160 | ||
161 | case "$merge_tool" in | |
162 | kdiff3) | |
163 | if base_present ; then | |
164 | (kdiff3 --auto --L1 "$path (Base)" -L2 "$path (Local)" --L3 "$path (Remote)" \ | |
165 | -o "$path" -- "$BASE" "$LOCAL" "$REMOTE" > /dev/null 2>&1) | |
166 | else | |
167 | (kdiff3 --auto -L1 "$path (Local)" --L2 "$path (Remote)" \ | |
168 | -o "$path" -- "$LOCAL" "$REMOTE" > /dev/null 2>&1) | |
169 | fi | |
170 | status=$? | |
171 | if test "$status" -eq 0; then | |
172 | rm "$BACKUP" | |
173 | fi | |
174 | ;; | |
175 | tkdiff) | |
176 | if base_present ; then | |
177 | tkdiff -a "$BASE" -o "$path" -- "$LOCAL" "$REMOTE" | |
178 | else | |
179 | tkdiff -o "$path" -- "$LOCAL" "$REMOTE" | |
180 | fi | |
181 | status=$? | |
182 | if test "$status" -eq 0; then | |
183 | mv -- "$BACKUP" "$path.orig" | |
184 | fi | |
185 | ;; | |
9cec6539 | 186 | meld|vimdiff) |
c4b4a5af | 187 | touch "$BACKUP" |
9cec6539 | 188 | $merge_tool -- "$LOCAL" "$path" "$REMOTE" |
c4b4a5af TT |
189 | if test "$path" -nt "$BACKUP" ; then |
190 | status=0; | |
191 | else | |
192 | while true; do | |
193 | echo "$path seems unchanged." | |
20fa04ea | 194 | printf "Was the merge successful? [y/n] " |
c4b4a5af TT |
195 | read answer < /dev/tty |
196 | case "$answer" in | |
197 | y*|Y*) status=0; break ;; | |
198 | n*|N*) status=1; break ;; | |
199 | esac | |
200 | done | |
201 | fi | |
202 | if test "$status" -eq 0; then | |
203 | mv -- "$BACKUP" "$path.orig" | |
204 | fi | |
205 | ;; | |
206 | xxdiff) | |
207 | touch "$BACKUP" | |
208 | if base_present ; then | |
209 | xxdiff -X --show-merged-pane \ | |
210 | -R 'Accel.SaveAsMerged: "Ctrl-S"' \ | |
211 | -R 'Accel.Search: "Ctrl+F"' \ | |
212 | -R 'Accel.SearchForward: "Ctrl-G"' \ | |
213 | --merged-file "$path" -- "$LOCAL" "$BASE" "$REMOTE" | |
214 | else | |
215 | xxdiff -X --show-merged-pane \ | |
216 | -R 'Accel.SaveAsMerged: "Ctrl-S"' \ | |
217 | -R 'Accel.Search: "Ctrl+F"' \ | |
218 | -R 'Accel.SearchForward: "Ctrl-G"' \ | |
219 | --merged-file "$path" -- "$LOCAL" "$REMOTE" | |
220 | fi | |
221 | if test "$path" -nt "$BACKUP" ; then | |
222 | status=0; | |
223 | else | |
224 | while true; do | |
225 | echo "$path seems unchanged." | |
20fa04ea | 226 | printf "Was the merge successful? [y/n] " |
c4b4a5af TT |
227 | read answer < /dev/tty |
228 | case "$answer" in | |
229 | y*|Y*) status=0; break ;; | |
230 | n*|N*) status=1; break ;; | |
231 | esac | |
232 | done | |
233 | fi | |
234 | if test "$status" -eq 0; then | |
235 | mv -- "$BACKUP" "$path.orig" | |
236 | fi | |
237 | ;; | |
238 | emerge) | |
239 | if base_present ; then | |
240 | emacs -f emerge-files-with-ancestor-command "$LOCAL" "$REMOTE" "$BASE" "$path" | |
241 | else | |
242 | emacs -f emerge-files-command "$LOCAL" "$REMOTE" "$path" | |
243 | fi | |
244 | status=$? | |
245 | if test "$status" -eq 0; then | |
246 | mv -- "$BACKUP" "$path.orig" | |
247 | fi | |
248 | ;; | |
249 | esac | |
250 | if test "$status" -ne 0; then | |
251 | echo "merge of $path failed" 1>&2 | |
252 | mv -- "$BACKUP" "$path" | |
253 | exit 1 | |
254 | fi | |
255 | git add -- "$path" | |
256 | cleanup_temp_files | |
257 | } | |
258 | ||
259 | while case $# in 0) break ;; esac | |
260 | do | |
261 | case "$1" in | |
262 | -t|--tool*) | |
263 | case "$#,$1" in | |
264 | *,*=*) | |
265 | merge_tool=`expr "z$1" : 'z-[^=]*=\(.*\)'` | |
266 | ;; | |
267 | 1,*) | |
268 | usage ;; | |
269 | *) | |
270 | merge_tool="$2" | |
271 | shift ;; | |
272 | esac | |
273 | ;; | |
274 | --) | |
275 | break | |
276 | ;; | |
277 | -*) | |
278 | usage | |
279 | ;; | |
280 | *) | |
281 | break | |
282 | ;; | |
283 | esac | |
284 | shift | |
285 | done | |
286 | ||
287 | if test -z "$merge_tool"; then | |
288 | merge_tool=`git-config merge.tool` | |
d6678c28 TT |
289 | case "$merge_tool" in |
290 | kdiff3 | tkdiff | xxdiff | meld | emerge | vimdiff) | |
291 | ;; # happy | |
292 | *) | |
293 | echo >&2 "git config option merge.tool set to unknown tool: $merge_tool" | |
294 | echo >&2 "Resetting to default..." | |
295 | unset merge_tool | |
296 | ;; | |
297 | esac | |
c4b4a5af TT |
298 | fi |
299 | ||
300 | if test -z "$merge_tool" ; then | |
301 | if type kdiff3 >/dev/null 2>&1 && test -n "$DISPLAY"; then | |
302 | merge_tool="kdiff3"; | |
303 | elif type tkdiff >/dev/null 2>&1 && test -n "$DISPLAY"; then | |
304 | merge_tool=tkdiff | |
305 | elif type xxdiff >/dev/null 2>&1 && test -n "$DISPLAY"; then | |
306 | merge_tool=xxdiff | |
307 | elif type meld >/dev/null 2>&1 && test -n "$DISPLAY"; then | |
308 | merge_tool=meld | |
309 | elif type emacs >/dev/null 2>&1; then | |
310 | merge_tool=emerge | |
9cec6539 JB |
311 | elif type vimdiff >/dev/null 2>&1; then |
312 | merge_tool=vimdiff | |
c4b4a5af TT |
313 | else |
314 | echo "No available merge resolution programs available." | |
315 | exit 1 | |
316 | fi | |
317 | fi | |
318 | ||
319 | case "$merge_tool" in | |
9cec6539 | 320 | kdiff3|tkdiff|meld|xxdiff|vimdiff) |
c4b4a5af TT |
321 | if ! type "$merge_tool" > /dev/null 2>&1; then |
322 | echo "The merge tool $merge_tool is not available" | |
323 | exit 1 | |
324 | fi | |
325 | ;; | |
326 | emerge) | |
327 | if ! type "emacs" > /dev/null 2>&1; then | |
328 | echo "Emacs is not available" | |
329 | exit 1 | |
330 | fi | |
331 | ;; | |
332 | *) | |
333 | echo "Unknown merge tool: $merge_tool" | |
334 | exit 1 | |
335 | ;; | |
336 | esac | |
337 | ||
338 | if test $# -eq 0 ; then | |
339 | files=`git ls-files -u | sed -e 's/^[^ ]* //' | sort -u` | |
340 | if test -z "$files" ; then | |
341 | echo "No files need merging" | |
342 | exit 0 | |
343 | fi | |
344 | echo Merging the files: $files | |
345 | git ls-files -u | sed -e 's/^[^ ]* //' | sort -u | while read i | |
346 | do | |
20fa04ea | 347 | printf "\n" |
c4b4a5af TT |
348 | merge_file "$i" < /dev/tty > /dev/tty |
349 | done | |
350 | else | |
351 | while test $# -gt 0; do | |
20fa04ea | 352 | printf "\n" |
c4b4a5af TT |
353 | merge_file "$1" |
354 | shift | |
355 | done | |
356 | fi | |
357 | exit 0 |