mergetool: Add prompt to continue after failing to merge a file
[git/git.git] / git-mergetool.sh
CommitLineData
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
2571ac67 8# at the discretion of Junio C Hamano.
c4b4a5af
TT
9#
10
682b451f 11USAGE='[--tool=tool] [-y|--no-prompt|--prompt] [file to merge] ...'
c4b4a5af 12SUBDIRECTORY_OK=Yes
8f321a39 13OPTIONS_SPEC=
c4b4a5af
TT
14. git-sh-setup
15require_work_tree
769f3986 16prefix=$(git rev-parse --show-prefix)
c4b4a5af
TT
17
18# Returns true if the mode reflects a symlink
262c981e 19is_symlink () {
c4b4a5af
TT
20 test "$1" = 120000
21}
22
262c981e 23local_present () {
c4b4a5af
TT
24 test -n "$local_mode"
25}
26
262c981e 27remote_present () {
c4b4a5af
TT
28 test -n "$remote_mode"
29}
30
262c981e 31base_present () {
c4b4a5af
TT
32 test -n "$base_mode"
33}
34
35cleanup_temp_files () {
36 if test "$1" = --save-backup ; then
b3ea27e4 37 mv -- "$BACKUP" "$MERGED.orig"
c4b4a5af
TT
38 rm -f -- "$LOCAL" "$REMOTE" "$BASE"
39 else
40 rm -f -- "$LOCAL" "$REMOTE" "$BASE" "$BACKUP"
41 fi
42}
43
262c981e 44describe_file () {
c4b4a5af
TT
45 mode="$1"
46 branch="$2"
47 file="$3"
48
27090aa1 49 printf " {%s}: " "$branch"
c4b4a5af 50 if test -z "$mode"; then
27090aa1 51 echo "deleted"
c4b4a5af 52 elif is_symlink "$mode" ; then
27090aa1 53 echo "a symbolic link -> '$(cat "$file")'"
c4b4a5af
TT
54 else
55 if base_present; then
27090aa1 56 echo "modified"
c4b4a5af 57 else
27090aa1 58 echo "created"
c4b4a5af
TT
59 fi
60 fi
c4b4a5af
TT
61}
62
63
64resolve_symlink_merge () {
d1dc6959 65 while true; do
27090aa1 66 printf "Use (l)ocal or (r)emote, or (a)bort? "
c4b4a5af
TT
67 read ans
68 case "$ans" in
69 [lL]*)
b3ea27e4
CB
70 git checkout-index -f --stage=2 -- "$MERGED"
71 git add -- "$MERGED"
c4b4a5af 72 cleanup_temp_files --save-backup
b0169d84 73 return 0
c4b4a5af 74 ;;
5a174f1a 75 [rR]*)
b3ea27e4
CB
76 git checkout-index -f --stage=3 -- "$MERGED"
77 git add -- "$MERGED"
c4b4a5af 78 cleanup_temp_files --save-backup
b0169d84 79 return 0
c4b4a5af 80 ;;
5a174f1a 81 [aA]*)
b0169d84 82 return 1
c4b4a5af
TT
83 ;;
84 esac
85 done
86}
87
88resolve_deleted_merge () {
d1dc6959 89 while true; do
27090aa1
TT
90 if base_present; then
91 printf "Use (m)odified or (d)eleted file, or (a)bort? "
92 else
93 printf "Use (c)reated or (d)eleted file, or (a)bort? "
94 fi
c4b4a5af
TT
95 read ans
96 case "$ans" in
27090aa1 97 [mMcC]*)
b3ea27e4 98 git add -- "$MERGED"
c4b4a5af 99 cleanup_temp_files --save-backup
b0169d84 100 return 0
c4b4a5af 101 ;;
5a174f1a 102 [dD]*)
b3ea27e4 103 git rm -- "$MERGED" > /dev/null
c4b4a5af 104 cleanup_temp_files
b0169d84 105 return 0
c4b4a5af 106 ;;
5a174f1a 107 [aA]*)
b0169d84 108 return 1
c4b4a5af
TT
109 ;;
110 esac
111 done
112}
113
ddc0c497 114check_unchanged () {
b3ea27e4 115 if test "$MERGED" -nt "$BACKUP" ; then
ddc0c497
TT
116 status=0;
117 else
118 while true; do
b3ea27e4 119 echo "$MERGED seems unchanged."
ddc0c497
TT
120 printf "Was the merge successful? [y/n] "
121 read answer < /dev/tty
122 case "$answer" in
123 y*|Y*) status=0; break ;;
124 n*|N*) status=1; break ;;
125 esac
126 done
127 fi
128}
129
c4b4a5af 130merge_file () {
b3ea27e4 131 MERGED="$1"
c4b4a5af 132
b3ea27e4 133 f=`git ls-files -u -- "$MERGED"`
c4b4a5af 134 if test -z "$f" ; then
b3ea27e4
CB
135 if test ! -f "$MERGED" ; then
136 echo "$MERGED: file not found"
ce5b6d75 137 else
b3ea27e4 138 echo "$MERGED: file does not need merging"
ce5b6d75 139 fi
b0169d84 140 return 1
c4b4a5af
TT
141 fi
142
b3ea27e4 143 ext="$$$(expr "$MERGED" : '.*\(\.[^/]*\)$')"
641dba49
PH
144 BACKUP="./$MERGED.BACKUP.$ext"
145 LOCAL="./$MERGED.LOCAL.$ext"
146 REMOTE="./$MERGED.REMOTE.$ext"
147 BASE="./$MERGED.BASE.$ext"
c4b4a5af 148
b3ea27e4
CB
149 mv -- "$MERGED" "$BACKUP"
150 cp -- "$BACKUP" "$MERGED"
c4b4a5af 151
b3ea27e4
CB
152 base_mode=`git ls-files -u -- "$MERGED" | awk '{if ($3==1) print $1;}'`
153 local_mode=`git ls-files -u -- "$MERGED" | awk '{if ($3==2) print $1;}'`
154 remote_mode=`git ls-files -u -- "$MERGED" | awk '{if ($3==3) print $1;}'`
c4b4a5af 155
b3ea27e4
CB
156 base_present && git cat-file blob ":1:$prefix$MERGED" >"$BASE" 2>/dev/null
157 local_present && git cat-file blob ":2:$prefix$MERGED" >"$LOCAL" 2>/dev/null
158 remote_present && git cat-file blob ":3:$prefix$MERGED" >"$REMOTE" 2>/dev/null
c4b4a5af
TT
159
160 if test -z "$local_mode" -o -z "$remote_mode"; then
b3ea27e4 161 echo "Deleted merge conflict for '$MERGED':"
c4b4a5af
TT
162 describe_file "$local_mode" "local" "$LOCAL"
163 describe_file "$remote_mode" "remote" "$REMOTE"
164 resolve_deleted_merge
165 return
166 fi
167
168 if is_symlink "$local_mode" || is_symlink "$remote_mode"; then
b3ea27e4 169 echo "Symbolic link merge conflict for '$MERGED':"
c4b4a5af
TT
170 describe_file "$local_mode" "local" "$LOCAL"
171 describe_file "$remote_mode" "remote" "$REMOTE"
172 resolve_symlink_merge
173 return
174 fi
175
b3ea27e4 176 echo "Normal merge conflict for '$MERGED':"
c4b4a5af
TT
177 describe_file "$local_mode" "local" "$LOCAL"
178 describe_file "$remote_mode" "remote" "$REMOTE"
682b451f
CB
179 if "$prompt" = true; then
180 printf "Hit return to start merge resolution tool (%s): " "$merge_tool"
181 read ans
182 fi
c4b4a5af
TT
183
184 case "$merge_tool" in
185 kdiff3)
186 if base_present ; then
b3ea27e4 187 ("$merge_tool_path" --auto --L1 "$MERGED (Base)" --L2 "$MERGED (Local)" --L3 "$MERGED (Remote)" \
641dba49 188 -o "$MERGED" "$BASE" "$LOCAL" "$REMOTE" > /dev/null 2>&1)
c4b4a5af 189 else
b3ea27e4 190 ("$merge_tool_path" --auto --L1 "$MERGED (Local)" --L2 "$MERGED (Remote)" \
641dba49 191 -o "$MERGED" "$LOCAL" "$REMOTE" > /dev/null 2>&1)
c4b4a5af
TT
192 fi
193 status=$?
c4b4a5af
TT
194 ;;
195 tkdiff)
196 if base_present ; then
641dba49 197 "$merge_tool_path" -a "$BASE" -o "$MERGED" "$LOCAL" "$REMOTE"
c4b4a5af 198 else
641dba49 199 "$merge_tool_path" -o "$MERGED" "$LOCAL" "$REMOTE"
c4b4a5af
TT
200 fi
201 status=$?
c4b4a5af 202 ;;
9cec6539 203 meld|vimdiff)
c4b4a5af 204 touch "$BACKUP"
641dba49 205 "$merge_tool_path" "$LOCAL" "$MERGED" "$REMOTE"
ddc0c497 206 check_unchanged
c4b4a5af 207 ;;
730b5b45 208 gvimdiff)
44c36d1c 209 touch "$BACKUP"
641dba49 210 "$merge_tool_path" -f "$LOCAL" "$MERGED" "$REMOTE"
44c36d1c
CB
211 check_unchanged
212 ;;
c4b4a5af
TT
213 xxdiff)
214 touch "$BACKUP"
215 if base_present ; then
e3fa2c76 216 "$merge_tool_path" -X --show-merged-pane \
c4b4a5af
TT
217 -R 'Accel.SaveAsMerged: "Ctrl-S"' \
218 -R 'Accel.Search: "Ctrl+F"' \
219 -R 'Accel.SearchForward: "Ctrl-G"' \
641dba49 220 --merged-file "$MERGED" "$LOCAL" "$BASE" "$REMOTE"
c4b4a5af 221 else
e3fa2c76 222 "$merge_tool_path" -X --show-merged-pane \
c4b4a5af
TT
223 -R 'Accel.SaveAsMerged: "Ctrl-S"' \
224 -R 'Accel.Search: "Ctrl+F"' \
225 -R 'Accel.SearchForward: "Ctrl-G"' \
641dba49 226 --merged-file "$MERGED" "$LOCAL" "$REMOTE"
c4b4a5af 227 fi
ddc0c497 228 check_unchanged
c4b4a5af 229 ;;
365cf979
TT
230 opendiff)
231 touch "$BACKUP"
232 if base_present; then
b3ea27e4 233 "$merge_tool_path" "$LOCAL" "$REMOTE" -ancestor "$BASE" -merge "$MERGED" | cat
365cf979 234 else
b3ea27e4 235 "$merge_tool_path" "$LOCAL" "$REMOTE" -merge "$MERGED" | cat
365cf979
TT
236 fi
237 check_unchanged
365cf979 238 ;;
ca8e6b7a
SP
239 ecmerge)
240 touch "$BACKUP"
241 if base_present; then
f4e9f786 242 "$merge_tool_path" "$BASE" "$LOCAL" "$REMOTE" --default --mode=merge3 --to="$MERGED"
ca8e6b7a 243 else
f4e9f786 244 "$merge_tool_path" "$LOCAL" "$REMOTE" --default --mode=merge2 --to="$MERGED"
ca8e6b7a
SP
245 fi
246 check_unchanged
ca8e6b7a 247 ;;
c4b4a5af
TT
248 emerge)
249 if base_present ; then
b3ea27e4 250 "$merge_tool_path" -f emerge-files-with-ancestor-command "$LOCAL" "$REMOTE" "$BASE" "$(basename "$MERGED")"
c4b4a5af 251 else
b3ea27e4 252 "$merge_tool_path" -f emerge-files-command "$LOCAL" "$REMOTE" "$(basename "$MERGED")"
c4b4a5af
TT
253 fi
254 status=$?
c4b4a5af 255 ;;
964473a0
CB
256 *)
257 if test -n "$merge_tool_cmd"; then
258 if test "$merge_tool_trust_exit_code" = "false"; then
259 touch "$BACKUP"
260 ( eval $merge_tool_cmd )
261 check_unchanged
262 else
263 ( eval $merge_tool_cmd )
264 status=$?
265 fi
266 fi
267 ;;
c4b4a5af
TT
268 esac
269 if test "$status" -ne 0; then
b3ea27e4
CB
270 echo "merge of $MERGED failed" 1>&2
271 mv -- "$BACKUP" "$MERGED"
b0169d84 272 return 1
c4b4a5af 273 fi
44c36d1c
CB
274
275 if test "$merge_keep_backup" = "true"; then
b3ea27e4 276 mv -- "$BACKUP" "$MERGED.orig"
44c36d1c
CB
277 else
278 rm -- "$BACKUP"
279 fi
280
b3ea27e4 281 git add -- "$MERGED"
c4b4a5af 282 cleanup_temp_files
b0169d84 283 return 0
c4b4a5af
TT
284}
285
682b451f
CB
286prompt=$(git config --bool mergetool.prompt || echo true)
287
822f7c73 288while test $# != 0
c4b4a5af
TT
289do
290 case "$1" in
291 -t|--tool*)
292 case "$#,$1" in
293 *,*=*)
294 merge_tool=`expr "z$1" : 'z-[^=]*=\(.*\)'`
295 ;;
296 1,*)
297 usage ;;
298 *)
299 merge_tool="$2"
300 shift ;;
301 esac
302 ;;
682b451f
CB
303 -y|--no-prompt)
304 prompt=false
305 ;;
306 --prompt)
307 prompt=true
308 ;;
c4b4a5af
TT
309 --)
310 break
311 ;;
312 -*)
313 usage
314 ;;
315 *)
316 break
317 ;;
318 esac
319 shift
320done
321
964473a0
CB
322valid_custom_tool()
323{
324 merge_tool_cmd="$(git config mergetool.$1.cmd)"
325 test -n "$merge_tool_cmd"
326}
327
e3fa2c76
SP
328valid_tool() {
329 case "$1" in
ca8e6b7a 330 kdiff3 | tkdiff | xxdiff | meld | opendiff | emerge | vimdiff | gvimdiff | ecmerge)
e3fa2c76
SP
331 ;; # happy
332 *)
964473a0
CB
333 if ! valid_custom_tool "$1"; then
334 return 1
335 fi
e3fa2c76
SP
336 ;;
337 esac
338}
339
340init_merge_tool_path() {
341 merge_tool_path=`git config mergetool.$1.path`
342 if test -z "$merge_tool_path" ; then
343 case "$1" in
344 emerge)
345 merge_tool_path=emacs
346 ;;
347 *)
348 merge_tool_path=$1
349 ;;
350 esac
351 fi
352}
353
b0169d84
CB
354prompt_after_failed_merge() {
355 while true; do
356 printf "Continue merging other unresolved paths (y/n) ? "
357 read ans
358 case "$ans" in
359
360 [yY]*)
361 return 0
362 ;;
363
364 [nN]*)
365 return 1
366 ;;
367 esac
368 done
369}
e3fa2c76 370
c4b4a5af 371if test -z "$merge_tool"; then
5be60078 372 merge_tool=`git config merge.tool`
d279fc1a 373 if test -n "$merge_tool" && ! valid_tool "$merge_tool"; then
d6678c28
TT
374 echo >&2 "git config option merge.tool set to unknown tool: $merge_tool"
375 echo >&2 "Resetting to default..."
376 unset merge_tool
e3fa2c76 377 fi
c4b4a5af
TT
378fi
379
380if test -z "$merge_tool" ; then
301ac38b
TT
381 if test -n "$DISPLAY"; then
382 merge_tool_candidates="kdiff3 tkdiff xxdiff meld gvimdiff"
383 if test -n "$GNOME_DESKTOP_SESSION_ID" ; then
384 merge_tool_candidates="meld $merge_tool_candidates"
385 fi
386 if test "$KDE_FULL_SESSION" = "true"; then
387 merge_tool_candidates="kdiff3 $merge_tool_candidates"
388 fi
389 fi
390 if echo "${VISUAL:-$EDITOR}" | grep 'emacs' > /dev/null 2>&1; then
391 merge_tool_candidates="$merge_tool_candidates emerge"
392 fi
393 if echo "${VISUAL:-$EDITOR}" | grep 'vim' > /dev/null 2>&1; then
394 merge_tool_candidates="$merge_tool_candidates vimdiff"
395 fi
396 merge_tool_candidates="$merge_tool_candidates opendiff emerge vimdiff"
397 echo "merge tool candidates: $merge_tool_candidates"
398 for i in $merge_tool_candidates; do
e3fa2c76
SP
399 init_merge_tool_path $i
400 if type "$merge_tool_path" > /dev/null 2>&1; then
301ac38b
TT
401 merge_tool=$i
402 break
403 fi
404 done
405 if test -z "$merge_tool" ; then
e3fa2c76 406 echo "No known merge resolution program available."
c4b4a5af
TT
407 exit 1
408 fi
e3fa2c76
SP
409else
410 if ! valid_tool "$merge_tool"; then
411 echo >&2 "Unknown merge_tool $merge_tool"
412 exit 1
413 fi
414
415 init_merge_tool_path "$merge_tool"
416
44c36d1c
CB
417 merge_keep_backup="$(git config --bool merge.keepBackup || echo true)"
418
964473a0 419 if test -z "$merge_tool_cmd" && ! type "$merge_tool_path" > /dev/null 2>&1; then
e3fa2c76
SP
420 echo "The merge tool $merge_tool is not available as '$merge_tool_path'"
421 exit 1
422 fi
964473a0
CB
423
424 if ! test -z "$merge_tool_cmd"; then
425 merge_tool_trust_exit_code="$(git config --bool mergetool.$merge_tool.trustExitCode || echo false)"
426 fi
c4b4a5af
TT
427fi
428
b0169d84
CB
429last_status=0
430rollup_status=0
c4b4a5af
TT
431
432if test $# -eq 0 ; then
0eea3451
CB
433 files=`git ls-files -u | sed -e 's/^[^ ]* //' | sort -u`
434 if test -z "$files" ; then
435 echo "No files need merging"
436 exit 0
437 fi
438 echo Merging the files: "$files"
439 git ls-files -u |
440 sed -e 's/^[^ ]* //' |
441 sort -u |
442 while IFS= read i
443 do
b0169d84
CB
444 if test $last_status -ne 0; then
445 prompt_after_failed_merge < /dev/tty || exit 1
446 fi
0eea3451
CB
447 printf "\n"
448 merge_file "$i" < /dev/tty > /dev/tty
b0169d84
CB
449 last_status=$?
450 if test $last_status -ne 0; then
451 rollup_status=1
452 fi
0eea3451 453 done
c4b4a5af 454else
0eea3451 455 while test $# -gt 0; do
b0169d84
CB
456 if test $last_status -ne 0; then
457 prompt_after_failed_merge || exit 1
458 fi
0eea3451
CB
459 printf "\n"
460 merge_file "$1"
b0169d84
CB
461 last_status=$?
462 if test $last_status -ne 0; then
463 rollup_status=1
464 fi
0eea3451
CB
465 shift
466 done
c4b4a5af 467fi
b0169d84
CB
468
469exit $rollup_status