3 # This program resolves merge conflicts in git
5 # Copyright (c) 2006 Theodore Y. Ts'o
7 # This file is licensed under the GPL v2, or a later version
8 # at the discretion of Junio C Hamano.
11 USAGE
='[--tool=tool] [-y|--no-prompt|--prompt] [file to merge] ...'
16 prefix
=$
(git rev-parse
--show-prefix)
18 # Returns true if the mode reflects a symlink
28 test -n "$remote_mode"
35 cleanup_temp_files
() {
36 if test "$1" = --save-backup ; then
37 mv -- "$BACKUP" "$MERGED.orig"
38 rm -f -- "$LOCAL" "$REMOTE" "$BASE"
40 rm -f -- "$LOCAL" "$REMOTE" "$BASE" "$BACKUP"
49 printf " {%s}: " "$branch"
50 if test -z "$mode"; then
52 elif is_symlink
"$mode" ; then
53 echo "a symbolic link -> '$(cat "$file")'"
64 resolve_symlink_merge
() {
66 printf "Use (l)ocal or (r)emote, or (a)bort? "
70 git checkout-index
-f --stage=2 -- "$MERGED"
72 cleanup_temp_files
--save-backup
76 git checkout-index
-f --stage=3 -- "$MERGED"
78 cleanup_temp_files
--save-backup
88 resolve_deleted_merge
() {
91 printf "Use (m)odified or (d)eleted file, or (a)bort? "
93 printf "Use (c)reated or (d)eleted file, or (a)bort? "
99 cleanup_temp_files
--save-backup
103 git
rm -- "$MERGED" > /dev
/null
115 if test "$MERGED" -nt "$BACKUP" ; then
119 echo "$MERGED seems unchanged."
120 printf "Was the merge successful? [y/n] "
121 read answer
< /dev
/tty
123 y
*|Y
*) status
=0; break ;;
124 n
*|N
*) status
=1; break ;;
130 checkout_staged_file
() {
131 tmpfile
=$
(expr "$(git checkout-index --temp --stage="$1" "$2")" : '\([^ ]*\) ')
133 if test $?
-eq 0 -a -n "$tmpfile" ; then
134 mv -- "$tmpfile" "$3"
141 f
=`git ls-files -u -- "$MERGED"`
142 if test -z "$f" ; then
143 if test ! -f "$MERGED" ; then
144 echo "$MERGED: file not found"
146 echo "$MERGED: file does not need merging"
151 ext
="$$$(expr "$MERGED" : '.*\(\.[^/]*\)$')"
152 BACKUP
="./$MERGED.BACKUP.$ext"
153 LOCAL
="./$MERGED.LOCAL.$ext"
154 REMOTE
="./$MERGED.REMOTE.$ext"
155 BASE
="./$MERGED.BASE.$ext"
157 mv -- "$MERGED" "$BACKUP"
158 cp -- "$BACKUP" "$MERGED"
160 base_mode
=`git ls-files -u -- "$MERGED" | awk '{if ($3==1) print $1;}'`
161 local_mode
=`git ls-files -u -- "$MERGED" | awk '{if ($3==2) print $1;}'`
162 remote_mode
=`git ls-files -u -- "$MERGED" | awk '{if ($3==3) print $1;}'`
164 base_present
&& checkout_staged_file
1 "$prefix$MERGED" "$BASE"
165 local_present
&& checkout_staged_file
2 "$prefix$MERGED" "$LOCAL"
166 remote_present
&& checkout_staged_file
3 "$prefix$MERGED" "$REMOTE"
168 if test -z "$local_mode" -o -z "$remote_mode"; then
169 echo "Deleted merge conflict for '$MERGED':"
170 describe_file
"$local_mode" "local" "$LOCAL"
171 describe_file
"$remote_mode" "remote" "$REMOTE"
172 resolve_deleted_merge
176 if is_symlink
"$local_mode" || is_symlink
"$remote_mode"; then
177 echo "Symbolic link merge conflict for '$MERGED':"
178 describe_file
"$local_mode" "local" "$LOCAL"
179 describe_file
"$remote_mode" "remote" "$REMOTE"
180 resolve_symlink_merge
184 echo "Normal merge conflict for '$MERGED':"
185 describe_file
"$local_mode" "local" "$LOCAL"
186 describe_file
"$remote_mode" "remote" "$REMOTE"
187 if "$prompt" = true
; then
188 printf "Hit return to start merge resolution tool (%s): " "$merge_tool"
192 case "$merge_tool" in
194 if base_present
; then
195 ("$merge_tool_path" --auto --L1 "$MERGED (Base)" --L2 "$MERGED (Local)" --L3 "$MERGED (Remote)" \
196 -o "$MERGED" "$BASE" "$LOCAL" "$REMOTE" > /dev
/null
2>&1)
198 ("$merge_tool_path" --auto --L1 "$MERGED (Local)" --L2 "$MERGED (Remote)" \
199 -o "$MERGED" "$LOCAL" "$REMOTE" > /dev
/null
2>&1)
204 if base_present
; then
205 "$merge_tool_path" -a "$BASE" -o "$MERGED" "$LOCAL" "$REMOTE"
207 "$merge_tool_path" -o "$MERGED" "$LOCAL" "$REMOTE"
213 "$merge_tool_path" "$LOCAL" "$MERGED" "$REMOTE"
218 "$merge_tool_path" -c "wincmd l" "$LOCAL" "$MERGED" "$REMOTE"
223 "$merge_tool_path" -c "wincmd l" -f "$LOCAL" "$MERGED" "$REMOTE"
228 if base_present
; then
229 "$merge_tool_path" -X --show-merged-pane \
230 -R 'Accel.SaveAsMerged: "Ctrl-S"' \
231 -R 'Accel.Search: "Ctrl+F"' \
232 -R 'Accel.SearchForward: "Ctrl-G"' \
233 --merged-file "$MERGED" "$LOCAL" "$BASE" "$REMOTE"
235 "$merge_tool_path" -X --show-merged-pane \
236 -R 'Accel.SaveAsMerged: "Ctrl-S"' \
237 -R 'Accel.Search: "Ctrl+F"' \
238 -R 'Accel.SearchForward: "Ctrl-G"' \
239 --merged-file "$MERGED" "$LOCAL" "$REMOTE"
245 if base_present
; then
246 "$merge_tool_path" "$LOCAL" "$REMOTE" -ancestor "$BASE" -merge "$MERGED" |
cat
248 "$merge_tool_path" "$LOCAL" "$REMOTE" -merge "$MERGED" |
cat
254 if base_present
; then
255 "$merge_tool_path" "$BASE" "$LOCAL" "$REMOTE" --default --mode=merge3
--to="$MERGED"
257 "$merge_tool_path" "$LOCAL" "$REMOTE" --default --mode=merge2
--to="$MERGED"
262 if base_present
; then
263 "$merge_tool_path" -f emerge-files-with-ancestor-command
"$LOCAL" "$REMOTE" "$BASE" "$(basename "$MERGED")"
265 "$merge_tool_path" -f emerge-files-command
"$LOCAL" "$REMOTE" "$(basename "$MERGED")"
270 if test -n "$merge_tool_cmd"; then
271 if test "$merge_tool_trust_exit_code" = "false"; then
273 ( eval $merge_tool_cmd )
276 ( eval $merge_tool_cmd )
282 if test "$status" -ne 0; then
283 echo "merge of $MERGED failed" 1>&2
284 mv -- "$BACKUP" "$MERGED"
286 if test "$merge_keep_temporaries" = "false"; then
293 if test "$merge_keep_backup" = "true"; then
294 mv -- "$BACKUP" "$MERGED.orig"
304 prompt
=$
(git config
--bool mergetool.prompt ||
echo true
)
312 merge_tool
=`expr "z$1" : 'z-[^=]*=\(.*\)'`
343 merge_tool_cmd
="$(git config mergetool.$1.cmd)"
344 test -n "$merge_tool_cmd"
349 kdiff3 | tkdiff | xxdiff | meld | opendiff | emerge | vimdiff | gvimdiff | ecmerge
)
352 if ! valid_custom_tool
"$1"; then
359 init_merge_tool_path
() {
360 merge_tool_path
=`git config mergetool.$1.path`
361 if test -z "$merge_tool_path" ; then
364 merge_tool_path
=emacs
373 prompt_after_failed_merge
() {
375 printf "Continue merging other unresolved paths (y/n) ? "
390 if test -z "$merge_tool"; then
391 merge_tool
=`git config merge.tool`
392 if test -n "$merge_tool" && ! valid_tool
"$merge_tool"; then
393 echo >&2 "git config option merge.tool set to unknown tool: $merge_tool"
394 echo >&2 "Resetting to default..."
399 if test -z "$merge_tool" ; then
400 if test -n "$DISPLAY"; then
401 merge_tool_candidates
="kdiff3 tkdiff xxdiff meld gvimdiff"
402 if test -n "$GNOME_DESKTOP_SESSION_ID" ; then
403 merge_tool_candidates
="meld $merge_tool_candidates"
405 if test "$KDE_FULL_SESSION" = "true"; then
406 merge_tool_candidates
="kdiff3 $merge_tool_candidates"
409 if echo "${VISUAL:-$EDITOR}" |
grep 'emacs' > /dev
/null
2>&1; then
410 merge_tool_candidates
="$merge_tool_candidates emerge"
412 if echo "${VISUAL:-$EDITOR}" |
grep 'vim' > /dev
/null
2>&1; then
413 merge_tool_candidates
="$merge_tool_candidates vimdiff"
415 merge_tool_candidates
="$merge_tool_candidates opendiff emerge vimdiff"
416 echo "merge tool candidates: $merge_tool_candidates"
417 for i
in $merge_tool_candidates; do
418 init_merge_tool_path
$i
419 if type "$merge_tool_path" > /dev
/null
2>&1; then
424 if test -z "$merge_tool" ; then
425 echo "No known merge resolution program available."
429 if ! valid_tool
"$merge_tool"; then
430 echo >&2 "Unknown merge_tool $merge_tool"
434 init_merge_tool_path
"$merge_tool"
436 merge_keep_backup
="$(git config --bool merge.keepBackup || echo true)"
437 merge_keep_temporaries
="$(git config --bool mergetool.keepTemporaries || echo false)"
439 if test -z "$merge_tool_cmd" && ! type "$merge_tool_path" > /dev
/null
2>&1; then
440 echo "The merge tool $merge_tool is not available as '$merge_tool_path'"
444 if ! test -z "$merge_tool_cmd"; then
445 merge_tool_trust_exit_code
="$(git config --bool mergetool.$merge_tool.trustExitCode || echo false)"
452 if test $# -eq 0 ; then
453 files
=`git ls-files -u | sed -e 's/^[^ ]* //' | sort -u`
454 if test -z "$files" ; then
455 echo "No files need merging"
458 echo Merging the files
: "$files"
460 sed -e 's/^[^ ]* //' |
464 if test $last_status -ne 0; then
465 prompt_after_failed_merge
< /dev
/tty ||
exit 1
468 merge_file
"$i" < /dev
/tty
> /dev
/tty
470 if test $last_status -ne 0; then
475 while test $# -gt 0; do
476 if test $last_status -ne 0; then
477 prompt_after_failed_merge ||
exit 1
482 if test $last_status -ne 0; then