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] [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 ;;
133 f
=`git ls-files -u -- "$MERGED"`
134 if test -z "$f" ; then
135 if test ! -f "$MERGED" ; then
136 echo "$MERGED: file not found"
138 echo "$MERGED: file does not need merging"
143 ext
="$$$(expr "$MERGED" : '.*\(\.[^/]*\)$')"
144 BACKUP
="$MERGED.BACKUP.$ext"
145 LOCAL
="$MERGED.LOCAL.$ext"
146 REMOTE
="$MERGED.REMOTE.$ext"
147 BASE
="$MERGED.BASE.$ext"
149 mv -- "$MERGED" "$BACKUP"
150 cp -- "$BACKUP" "$MERGED"
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;}'`
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
160 if test -z "$local_mode" -o -z "$remote_mode"; then
161 echo "Deleted merge conflict for '$MERGED':"
162 describe_file
"$local_mode" "local" "$LOCAL"
163 describe_file
"$remote_mode" "remote" "$REMOTE"
164 resolve_deleted_merge
168 if is_symlink
"$local_mode" || is_symlink
"$remote_mode"; then
169 echo "Symbolic link merge conflict for '$MERGED':"
170 describe_file
"$local_mode" "local" "$LOCAL"
171 describe_file
"$remote_mode" "remote" "$REMOTE"
172 resolve_symlink_merge
176 echo "Normal merge conflict for '$MERGED':"
177 describe_file
"$local_mode" "local" "$LOCAL"
178 describe_file
"$remote_mode" "remote" "$REMOTE"
179 printf "Hit return to start merge resolution tool (%s): " "$merge_tool"
182 case "$merge_tool" in
184 if base_present
; then
185 ("$merge_tool_path" --auto --L1 "$MERGED (Base)" --L2 "$MERGED (Local)" --L3 "$MERGED (Remote)" \
186 -o "$MERGED" -- "$BASE" "$LOCAL" "$REMOTE" > /dev
/null
2>&1)
188 ("$merge_tool_path" --auto --L1 "$MERGED (Local)" --L2 "$MERGED (Remote)" \
189 -o "$MERGED" -- "$LOCAL" "$REMOTE" > /dev
/null
2>&1)
194 if base_present
; then
195 "$merge_tool_path" -a "$BASE" -o "$MERGED" -- "$LOCAL" "$REMOTE"
197 "$merge_tool_path" -o "$MERGED" -- "$LOCAL" "$REMOTE"
203 "$merge_tool_path" -- "$LOCAL" "$MERGED" "$REMOTE"
208 "$merge_tool_path" -f -- "$LOCAL" "$MERGED" "$REMOTE"
213 if base_present
; then
214 "$merge_tool_path" -X --show-merged-pane \
215 -R 'Accel.SaveAsMerged: "Ctrl-S"' \
216 -R 'Accel.Search: "Ctrl+F"' \
217 -R 'Accel.SearchForward: "Ctrl-G"' \
218 --merged-file "$MERGED" -- "$LOCAL" "$BASE" "$REMOTE"
220 "$merge_tool_path" -X --show-merged-pane \
221 -R 'Accel.SaveAsMerged: "Ctrl-S"' \
222 -R 'Accel.Search: "Ctrl+F"' \
223 -R 'Accel.SearchForward: "Ctrl-G"' \
224 --merged-file "$MERGED" -- "$LOCAL" "$REMOTE"
230 if base_present
; then
231 "$merge_tool_path" "$LOCAL" "$REMOTE" -ancestor "$BASE" -merge "$MERGED" |
cat
233 "$merge_tool_path" "$LOCAL" "$REMOTE" -merge "$MERGED" |
cat
239 if base_present
; then
240 "$merge_tool_path" "$BASE" "$LOCAL" "$REMOTE" --default --mode=merge3
--to="$MERGED"
242 "$merge_tool_path" "$LOCAL" "$REMOTE" --default --mode=merge2
--to="$MERGED"
247 if base_present
; then
248 "$merge_tool_path" -f emerge-files-with-ancestor-command
"$LOCAL" "$REMOTE" "$BASE" "$(basename "$MERGED")"
250 "$merge_tool_path" -f emerge-files-command
"$LOCAL" "$REMOTE" "$(basename "$MERGED")"
255 if test -n "$merge_tool_cmd"; then
256 if test "$merge_tool_trust_exit_code" = "false"; then
258 ( eval $merge_tool_cmd )
261 ( eval $merge_tool_cmd )
267 if test "$status" -ne 0; then
268 echo "merge of $MERGED failed" 1>&2
269 mv -- "$BACKUP" "$MERGED"
273 if test "$merge_keep_backup" = "true"; then
274 mv -- "$BACKUP" "$MERGED.orig"
289 merge_tool
=`expr "z$1" : 'z-[^=]*=\(.*\)'`
313 merge_tool_cmd
="$(git config mergetool.$1.cmd)"
314 test -n "$merge_tool_cmd"
319 kdiff3 | tkdiff | xxdiff | meld | opendiff | emerge | vimdiff | gvimdiff | ecmerge
)
322 if ! valid_custom_tool
"$1"; then
329 init_merge_tool_path
() {
330 merge_tool_path
=`git config mergetool.$1.path`
331 if test -z "$merge_tool_path" ; then
334 merge_tool_path
=emacs
344 if test -z "$merge_tool"; then
345 merge_tool
=`git config merge.tool`
346 if test -n "$merge_tool" && ! valid_tool
"$merge_tool"; then
347 echo >&2 "git config option merge.tool set to unknown tool: $merge_tool"
348 echo >&2 "Resetting to default..."
353 if test -z "$merge_tool" ; then
354 if test -n "$DISPLAY"; then
355 merge_tool_candidates
="kdiff3 tkdiff xxdiff meld gvimdiff"
356 if test -n "$GNOME_DESKTOP_SESSION_ID" ; then
357 merge_tool_candidates
="meld $merge_tool_candidates"
359 if test "$KDE_FULL_SESSION" = "true"; then
360 merge_tool_candidates
="kdiff3 $merge_tool_candidates"
363 if echo "${VISUAL:-$EDITOR}" |
grep 'emacs' > /dev
/null
2>&1; then
364 merge_tool_candidates
="$merge_tool_candidates emerge"
366 if echo "${VISUAL:-$EDITOR}" |
grep 'vim' > /dev
/null
2>&1; then
367 merge_tool_candidates
="$merge_tool_candidates vimdiff"
369 merge_tool_candidates
="$merge_tool_candidates opendiff emerge vimdiff"
370 echo "merge tool candidates: $merge_tool_candidates"
371 for i
in $merge_tool_candidates; do
372 init_merge_tool_path
$i
373 if type "$merge_tool_path" > /dev
/null
2>&1; then
378 if test -z "$merge_tool" ; then
379 echo "No known merge resolution program available."
383 if ! valid_tool
"$merge_tool"; then
384 echo >&2 "Unknown merge_tool $merge_tool"
388 init_merge_tool_path
"$merge_tool"
390 merge_keep_backup
="$(git config --bool merge.keepBackup || echo true)"
392 if test -z "$merge_tool_cmd" && ! type "$merge_tool_path" > /dev
/null
2>&1; then
393 echo "The merge tool $merge_tool is not available as '$merge_tool_path'"
397 if ! test -z "$merge_tool_cmd"; then
398 merge_tool_trust_exit_code
="$(git config --bool mergetool.$merge_tool.trustExitCode || echo false)"
403 if test $# -eq 0 ; then
404 files
=`git ls-files -u | sed -e 's/^[^ ]* //' | sort -u`
405 if test -z "$files" ; then
406 echo "No files need merging"
409 echo Merging the files
: "$files"
411 sed -e 's/^[^ ]* //' |
416 merge_file
"$i" < /dev
/tty
> /dev
/tty
419 while test $# -gt 0; do