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] ...'
17 # Returns true if the mode reflects a symlink
27 test -n "$remote_mode"
34 cleanup_temp_files
() {
35 if test "$1" = --save-backup ; then
36 mv -- "$BACKUP" "$MERGED.orig"
37 rm -f -- "$LOCAL" "$REMOTE" "$BASE"
39 rm -f -- "$LOCAL" "$REMOTE" "$BASE" "$BACKUP"
48 printf " {%s}: " "$branch"
49 if test -z "$mode"; then
51 elif is_symlink
"$mode" ; then
52 echo "a symbolic link -> '$(cat "$file")'"
63 resolve_symlink_merge
() {
65 printf "Use (l)ocal or (r)emote, or (a)bort? "
69 git checkout-index
-f --stage=2 -- "$MERGED"
71 cleanup_temp_files
--save-backup
75 git checkout-index
-f --stage=3 -- "$MERGED"
77 cleanup_temp_files
--save-backup
87 resolve_deleted_merge
() {
90 printf "Use (m)odified or (d)eleted file, or (a)bort? "
92 printf "Use (c)reated or (d)eleted file, or (a)bort? "
98 cleanup_temp_files
--save-backup
102 git
rm -- "$MERGED" > /dev
/null
114 if test "$MERGED" -nt "$BACKUP" ; then
118 echo "$MERGED seems unchanged."
119 printf "Was the merge successful? [y/n] "
120 read answer
< /dev
/tty
122 y
*|Y
*) status
=0; break ;;
123 n
*|N
*) status
=1; break ;;
129 checkout_staged_file
() {
130 tmpfile
=$
(expr "$(git checkout-index --temp --stage="$1" "$2")" : '\([^ ]*\) ')
132 if test $?
-eq 0 -a -n "$tmpfile" ; then
133 mv -- "$(git rev-parse --show-cdup)$tmpfile" "$3"
140 f
=`git ls-files -u -- "$MERGED"`
141 if test -z "$f" ; then
142 if test ! -f "$MERGED" ; then
143 echo "$MERGED: file not found"
145 echo "$MERGED: file does not need merging"
150 ext
="$$$(expr "$MERGED" : '.*\(\.[^/]*\)$')"
151 BACKUP
="./$MERGED.BACKUP.$ext"
152 LOCAL
="./$MERGED.LOCAL.$ext"
153 REMOTE
="./$MERGED.REMOTE.$ext"
154 BASE
="./$MERGED.BASE.$ext"
156 mv -- "$MERGED" "$BACKUP"
157 cp -- "$BACKUP" "$MERGED"
159 base_mode
=`git ls-files -u -- "$MERGED" | awk '{if ($3==1) print $1;}'`
160 local_mode
=`git ls-files -u -- "$MERGED" | awk '{if ($3==2) print $1;}'`
161 remote_mode
=`git ls-files -u -- "$MERGED" | awk '{if ($3==3) print $1;}'`
163 base_present
&& checkout_staged_file
1 "$MERGED" "$BASE"
164 local_present
&& checkout_staged_file
2 "$MERGED" "$LOCAL"
165 remote_present
&& checkout_staged_file
3 "$MERGED" "$REMOTE"
167 if test -z "$local_mode" -o -z "$remote_mode"; then
168 echo "Deleted merge conflict for '$MERGED':"
169 describe_file
"$local_mode" "local" "$LOCAL"
170 describe_file
"$remote_mode" "remote" "$REMOTE"
171 resolve_deleted_merge
175 if is_symlink
"$local_mode" || is_symlink
"$remote_mode"; then
176 echo "Symbolic link merge conflict for '$MERGED':"
177 describe_file
"$local_mode" "local" "$LOCAL"
178 describe_file
"$remote_mode" "remote" "$REMOTE"
179 resolve_symlink_merge
183 echo "Normal merge conflict for '$MERGED':"
184 describe_file
"$local_mode" "local" "$LOCAL"
185 describe_file
"$remote_mode" "remote" "$REMOTE"
186 if "$prompt" = true
; then
187 printf "Hit return to start merge resolution tool (%s): " "$merge_tool"
191 case "$merge_tool" in
193 if base_present
; then
194 ("$merge_tool_path" --auto --L1 "$MERGED (Base)" --L2 "$MERGED (Local)" --L3 "$MERGED (Remote)" \
195 -o "$MERGED" "$BASE" "$LOCAL" "$REMOTE" > /dev
/null
2>&1)
197 ("$merge_tool_path" --auto --L1 "$MERGED (Local)" --L2 "$MERGED (Remote)" \
198 -o "$MERGED" "$LOCAL" "$REMOTE" > /dev
/null
2>&1)
203 if base_present
; then
204 "$merge_tool_path" -a "$BASE" -o "$MERGED" "$LOCAL" "$REMOTE"
206 "$merge_tool_path" -o "$MERGED" "$LOCAL" "$REMOTE"
212 "$merge_tool_path" "$LOCAL" "$MERGED" "$REMOTE"
217 "$merge_tool_path" -d -c "wincmd l" "$LOCAL" "$MERGED" "$REMOTE"
222 "$merge_tool_path" -d -c "wincmd l" -f "$LOCAL" "$MERGED" "$REMOTE"
227 if base_present
; then
228 "$merge_tool_path" -X --show-merged-pane \
229 -R 'Accel.SaveAsMerged: "Ctrl-S"' \
230 -R 'Accel.Search: "Ctrl+F"' \
231 -R 'Accel.SearchForward: "Ctrl-G"' \
232 --merged-file "$MERGED" "$LOCAL" "$BASE" "$REMOTE"
234 "$merge_tool_path" -X --show-merged-pane \
235 -R 'Accel.SaveAsMerged: "Ctrl-S"' \
236 -R 'Accel.Search: "Ctrl+F"' \
237 -R 'Accel.SearchForward: "Ctrl-G"' \
238 --merged-file "$MERGED" "$LOCAL" "$REMOTE"
244 if base_present
; then
245 "$merge_tool_path" "$LOCAL" "$REMOTE" -ancestor "$BASE" -merge "$MERGED" |
cat
247 "$merge_tool_path" "$LOCAL" "$REMOTE" -merge "$MERGED" |
cat
253 if base_present
; then
254 "$merge_tool_path" "$BASE" "$LOCAL" "$REMOTE" --default --mode=merge3
--to="$MERGED"
256 "$merge_tool_path" "$LOCAL" "$REMOTE" --default --mode=merge2
--to="$MERGED"
261 if base_present
; then
262 "$merge_tool_path" -f emerge-files-with-ancestor-command
"$LOCAL" "$REMOTE" "$BASE" "$(basename "$MERGED")"
264 "$merge_tool_path" -f emerge-files-command
"$LOCAL" "$REMOTE" "$(basename "$MERGED")"
269 if base_present
; then
271 "$merge_tool_path" -base:"$BASE" -mine:"$LOCAL" -theirs:"$REMOTE" -merged:"$MERGED"
274 echo "TortoiseMerge cannot be used without a base" 1>&2
279 if test -n "$merge_tool_cmd"; then
280 if test "$merge_tool_trust_exit_code" = "false"; then
282 ( eval $merge_tool_cmd )
285 ( eval $merge_tool_cmd )
291 if test "$status" -ne 0; then
292 echo "merge of $MERGED failed" 1>&2
293 mv -- "$BACKUP" "$MERGED"
295 if test "$merge_keep_temporaries" = "false"; then
302 if test "$merge_keep_backup" = "true"; then
303 mv -- "$BACKUP" "$MERGED.orig"
313 prompt
=$
(git config
--bool mergetool.prompt ||
echo true
)
321 merge_tool
=`expr "z$1" : 'z-[^=]*=\(.*\)'`
352 merge_tool_cmd
="$(git config mergetool.$1.cmd)"
353 test -n "$merge_tool_cmd"
358 kdiff3 | tkdiff | xxdiff | meld | opendiff | emerge | vimdiff | gvimdiff | ecmerge | tortoisemerge
)
361 if ! valid_custom_tool
"$1"; then
368 init_merge_tool_path
() {
369 merge_tool_path
=`git config mergetool.$1.path`
370 if test -z "$merge_tool_path" ; then
379 merge_tool_path
=emacs
388 prompt_after_failed_merge
() {
390 printf "Continue merging other unresolved paths (y/n) ? "
405 if test -z "$merge_tool"; then
406 merge_tool
=`git config merge.tool`
407 if test -n "$merge_tool" && ! valid_tool
"$merge_tool"; then
408 echo >&2 "git config option merge.tool set to unknown tool: $merge_tool"
409 echo >&2 "Resetting to default..."
414 if test -z "$merge_tool" ; then
415 if test -n "$DISPLAY"; then
416 if test -n "$GNOME_DESKTOP_SESSION_ID" ; then
417 merge_tool_candidates
="meld kdiff3 tkdiff xxdiff tortoisemerge gvimdiff diffuse"
419 merge_tool_candidates
="kdiff3 tkdiff xxdiff meld tortoisemerge gvimdiff diffuse"
422 if echo "${VISUAL:-$EDITOR}" |
grep 'emacs' > /dev
/null
2>&1; then
423 merge_tool_candidates
="$merge_tool_candidates emerge opendiff vimdiff"
424 elif echo "${VISUAL:-$EDITOR}" |
grep 'vim' > /dev
/null
2>&1; then
425 merge_tool_candidates
="$merge_tool_candidates vimdiff opendiff emerge"
427 merge_tool_candidates
="$merge_tool_candidates opendiff emerge vimdiff"
429 echo "merge tool candidates: $merge_tool_candidates"
430 for i
in $merge_tool_candidates; do
431 init_merge_tool_path
$i
432 if type "$merge_tool_path" > /dev
/null
2>&1; then
437 if test -z "$merge_tool" ; then
438 echo "No known merge resolution program available."
442 if ! valid_tool
"$merge_tool"; then
443 echo >&2 "Unknown merge_tool $merge_tool"
447 init_merge_tool_path
"$merge_tool"
449 merge_keep_backup
="$(git config --bool merge.keepBackup || echo true)"
450 merge_keep_temporaries
="$(git config --bool mergetool.keepTemporaries || echo false)"
452 if test -z "$merge_tool_cmd" && ! type "$merge_tool_path" > /dev
/null
2>&1; then
453 echo "The merge tool $merge_tool is not available as '$merge_tool_path'"
457 if ! test -z "$merge_tool_cmd"; then
458 merge_tool_trust_exit_code
="$(git config --bool mergetool.$merge_tool.trustExitCode || echo false)"
465 if test $# -eq 0 ; then
466 files
=`git ls-files -u | sed -e 's/^[^ ]* //' | sort -u`
467 if test -z "$files" ; then
468 echo "No files need merging"
471 echo Merging the files
: "$files"
473 sed -e 's/^[^ ]* //' |
477 if test $last_status -ne 0; then
478 prompt_after_failed_merge
< /dev
/tty ||
exit 1
481 merge_file
"$i" < /dev
/tty
> /dev
/tty
483 if test $last_status -ne 0; then
488 while test $# -gt 0; do
489 if test $last_status -ne 0; then
490 prompt_after_failed_merge ||
exit 1
495 if test $last_status -ne 0; then