git-am: implement --reject option passed to git-apply
[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 ;;
71ee483a 203 meld)
c4b4a5af 204 touch "$BACKUP"
641dba49 205 "$merge_tool_path" "$LOCAL" "$MERGED" "$REMOTE"
ddc0c497 206 check_unchanged
c4b4a5af 207 ;;
71ee483a
MH
208 vimdiff)
209 touch "$BACKUP"
210 "$merge_tool_path" -c "wincmd l" "$LOCAL" "$MERGED" "$REMOTE"
211 check_unchanged
212 ;;
730b5b45 213 gvimdiff)
44c36d1c 214 touch "$BACKUP"
71ee483a 215 "$merge_tool_path" -c "wincmd l" -f "$LOCAL" "$MERGED" "$REMOTE"
44c36d1c
CB
216 check_unchanged
217 ;;
c4b4a5af
TT
218 xxdiff)
219 touch "$BACKUP"
220 if base_present ; then
e3fa2c76 221 "$merge_tool_path" -X --show-merged-pane \
c4b4a5af
TT
222 -R 'Accel.SaveAsMerged: "Ctrl-S"' \
223 -R 'Accel.Search: "Ctrl+F"' \
224 -R 'Accel.SearchForward: "Ctrl-G"' \
641dba49 225 --merged-file "$MERGED" "$LOCAL" "$BASE" "$REMOTE"
c4b4a5af 226 else
e3fa2c76 227 "$merge_tool_path" -X --show-merged-pane \
c4b4a5af
TT
228 -R 'Accel.SaveAsMerged: "Ctrl-S"' \
229 -R 'Accel.Search: "Ctrl+F"' \
230 -R 'Accel.SearchForward: "Ctrl-G"' \
641dba49 231 --merged-file "$MERGED" "$LOCAL" "$REMOTE"
c4b4a5af 232 fi
ddc0c497 233 check_unchanged
c4b4a5af 234 ;;
365cf979
TT
235 opendiff)
236 touch "$BACKUP"
237 if base_present; then
b3ea27e4 238 "$merge_tool_path" "$LOCAL" "$REMOTE" -ancestor "$BASE" -merge "$MERGED" | cat
365cf979 239 else
b3ea27e4 240 "$merge_tool_path" "$LOCAL" "$REMOTE" -merge "$MERGED" | cat
365cf979
TT
241 fi
242 check_unchanged
365cf979 243 ;;
ca8e6b7a
SP
244 ecmerge)
245 touch "$BACKUP"
246 if base_present; then
f4e9f786 247 "$merge_tool_path" "$BASE" "$LOCAL" "$REMOTE" --default --mode=merge3 --to="$MERGED"
ca8e6b7a 248 else
f4e9f786 249 "$merge_tool_path" "$LOCAL" "$REMOTE" --default --mode=merge2 --to="$MERGED"
ca8e6b7a
SP
250 fi
251 check_unchanged
ca8e6b7a 252 ;;
c4b4a5af
TT
253 emerge)
254 if base_present ; then
b3ea27e4 255 "$merge_tool_path" -f emerge-files-with-ancestor-command "$LOCAL" "$REMOTE" "$BASE" "$(basename "$MERGED")"
c4b4a5af 256 else
b3ea27e4 257 "$merge_tool_path" -f emerge-files-command "$LOCAL" "$REMOTE" "$(basename "$MERGED")"
c4b4a5af
TT
258 fi
259 status=$?
c4b4a5af 260 ;;
964473a0
CB
261 *)
262 if test -n "$merge_tool_cmd"; then
263 if test "$merge_tool_trust_exit_code" = "false"; then
264 touch "$BACKUP"
265 ( eval $merge_tool_cmd )
266 check_unchanged
267 else
268 ( eval $merge_tool_cmd )
269 status=$?
270 fi
271 fi
272 ;;
c4b4a5af
TT
273 esac
274 if test "$status" -ne 0; then
b3ea27e4
CB
275 echo "merge of $MERGED failed" 1>&2
276 mv -- "$BACKUP" "$MERGED"
162eba8b
CB
277
278 if test "$merge_keep_temporaries" = "false"; then
279 cleanup_temp_files
280 fi
281
b0169d84 282 return 1
c4b4a5af 283 fi
44c36d1c
CB
284
285 if test "$merge_keep_backup" = "true"; then
b3ea27e4 286 mv -- "$BACKUP" "$MERGED.orig"
44c36d1c
CB
287 else
288 rm -- "$BACKUP"
289 fi
290
b3ea27e4 291 git add -- "$MERGED"
c4b4a5af 292 cleanup_temp_files
b0169d84 293 return 0
c4b4a5af
TT
294}
295
682b451f
CB
296prompt=$(git config --bool mergetool.prompt || echo true)
297
822f7c73 298while test $# != 0
c4b4a5af
TT
299do
300 case "$1" in
301 -t|--tool*)
302 case "$#,$1" in
303 *,*=*)
304 merge_tool=`expr "z$1" : 'z-[^=]*=\(.*\)'`
305 ;;
306 1,*)
307 usage ;;
308 *)
309 merge_tool="$2"
310 shift ;;
311 esac
312 ;;
682b451f
CB
313 -y|--no-prompt)
314 prompt=false
315 ;;
316 --prompt)
317 prompt=true
318 ;;
c4b4a5af 319 --)
ce2c3ebb 320 shift
c4b4a5af
TT
321 break
322 ;;
323 -*)
324 usage
325 ;;
326 *)
327 break
328 ;;
329 esac
330 shift
331done
332
964473a0
CB
333valid_custom_tool()
334{
335 merge_tool_cmd="$(git config mergetool.$1.cmd)"
336 test -n "$merge_tool_cmd"
337}
338
e3fa2c76
SP
339valid_tool() {
340 case "$1" in
ca8e6b7a 341 kdiff3 | tkdiff | xxdiff | meld | opendiff | emerge | vimdiff | gvimdiff | ecmerge)
e3fa2c76
SP
342 ;; # happy
343 *)
964473a0
CB
344 if ! valid_custom_tool "$1"; then
345 return 1
346 fi
e3fa2c76
SP
347 ;;
348 esac
349}
350
351init_merge_tool_path() {
352 merge_tool_path=`git config mergetool.$1.path`
353 if test -z "$merge_tool_path" ; then
354 case "$1" in
355 emerge)
356 merge_tool_path=emacs
357 ;;
358 *)
359 merge_tool_path=$1
360 ;;
361 esac
362 fi
363}
364
b0169d84
CB
365prompt_after_failed_merge() {
366 while true; do
367 printf "Continue merging other unresolved paths (y/n) ? "
368 read ans
369 case "$ans" in
370
371 [yY]*)
372 return 0
373 ;;
374
375 [nN]*)
376 return 1
377 ;;
378 esac
379 done
380}
e3fa2c76 381
c4b4a5af 382if test -z "$merge_tool"; then
5be60078 383 merge_tool=`git config merge.tool`
d279fc1a 384 if test -n "$merge_tool" && ! valid_tool "$merge_tool"; then
d6678c28
TT
385 echo >&2 "git config option merge.tool set to unknown tool: $merge_tool"
386 echo >&2 "Resetting to default..."
387 unset merge_tool
e3fa2c76 388 fi
c4b4a5af
TT
389fi
390
391if test -z "$merge_tool" ; then
301ac38b
TT
392 if test -n "$DISPLAY"; then
393 merge_tool_candidates="kdiff3 tkdiff xxdiff meld gvimdiff"
394 if test -n "$GNOME_DESKTOP_SESSION_ID" ; then
395 merge_tool_candidates="meld $merge_tool_candidates"
396 fi
397 if test "$KDE_FULL_SESSION" = "true"; then
398 merge_tool_candidates="kdiff3 $merge_tool_candidates"
399 fi
400 fi
401 if echo "${VISUAL:-$EDITOR}" | grep 'emacs' > /dev/null 2>&1; then
402 merge_tool_candidates="$merge_tool_candidates emerge"
403 fi
404 if echo "${VISUAL:-$EDITOR}" | grep 'vim' > /dev/null 2>&1; then
405 merge_tool_candidates="$merge_tool_candidates vimdiff"
406 fi
407 merge_tool_candidates="$merge_tool_candidates opendiff emerge vimdiff"
408 echo "merge tool candidates: $merge_tool_candidates"
409 for i in $merge_tool_candidates; do
e3fa2c76
SP
410 init_merge_tool_path $i
411 if type "$merge_tool_path" > /dev/null 2>&1; then
301ac38b
TT
412 merge_tool=$i
413 break
414 fi
415 done
416 if test -z "$merge_tool" ; then
e3fa2c76 417 echo "No known merge resolution program available."
c4b4a5af
TT
418 exit 1
419 fi
e3fa2c76
SP
420else
421 if ! valid_tool "$merge_tool"; then
422 echo >&2 "Unknown merge_tool $merge_tool"
423 exit 1
424 fi
425
426 init_merge_tool_path "$merge_tool"
427
44c36d1c 428 merge_keep_backup="$(git config --bool merge.keepBackup || echo true)"
162eba8b 429 merge_keep_temporaries="$(git config --bool mergetool.keepTemporaries || echo false)"
44c36d1c 430
964473a0 431 if test -z "$merge_tool_cmd" && ! type "$merge_tool_path" > /dev/null 2>&1; then
e3fa2c76
SP
432 echo "The merge tool $merge_tool is not available as '$merge_tool_path'"
433 exit 1
434 fi
964473a0
CB
435
436 if ! test -z "$merge_tool_cmd"; then
437 merge_tool_trust_exit_code="$(git config --bool mergetool.$merge_tool.trustExitCode || echo false)"
438 fi
c4b4a5af
TT
439fi
440
b0169d84
CB
441last_status=0
442rollup_status=0
c4b4a5af
TT
443
444if test $# -eq 0 ; then
0eea3451
CB
445 files=`git ls-files -u | sed -e 's/^[^ ]* //' | sort -u`
446 if test -z "$files" ; then
447 echo "No files need merging"
448 exit 0
449 fi
450 echo Merging the files: "$files"
451 git ls-files -u |
452 sed -e 's/^[^ ]* //' |
453 sort -u |
454 while IFS= read i
455 do
b0169d84
CB
456 if test $last_status -ne 0; then
457 prompt_after_failed_merge < /dev/tty || exit 1
c4b4a5af 458 fi
0eea3451
CB
459 printf "\n"
460 merge_file "$i" < /dev/tty > /dev/tty
b0169d84
CB
461 last_status=$?
462 if test $last_status -ne 0; then
463 rollup_status=1
464 fi
0eea3451 465 done
c4b4a5af 466else
0eea3451 467 while test $# -gt 0; do
b0169d84
CB
468 if test $last_status -ne 0; then
469 prompt_after_failed_merge || exit 1
470 fi
0eea3451
CB
471 printf "\n"
472 merge_file "$1"
b0169d84
CB
473 last_status=$?
474 if test $last_status -ne 0; then
475 rollup_status=1
476 fi
0eea3451
CB
477 shift
478 done
c4b4a5af 479fi
b0169d84
CB
480
481exit $rollup_status