3 # This program resolves merge conflicts in git
5 # Copyright (c) 2006 Theodore Y. Ts'o
6 # Copyright (c) 2009-2016 David Aguilar
8 # This file is licensed under the GPL v2, or a later version
9 # at the discretion of Junio C Hamano.
12 USAGE
='[--tool=tool] [--tool-help] [-y|--no-prompt|--prompt] [-O<orderfile>] [file to merge] ...'
20 # Returns true if the mode reflects a symlink
34 test -n "$remote_mode"
41 mergetool_tmpdir_init
() {
42 if test "$(git config --bool mergetool.writeToTemp)" != true
47 if MERGETOOL_TMPDIR
=$
(mktemp
-d -t "git-mergetool-XXXXXX" 2>/dev
/null
)
51 die
"error: mktemp is needed when 'mergetool.writeToTemp' is true"
54 cleanup_temp_files
() {
55 if test "$1" = --save-backup
57 rm -rf -- "$MERGED.orig"
58 test -e "$BACKUP" && mv -- "$BACKUP" "$MERGED.orig"
59 rm -f -- "$LOCAL" "$REMOTE" "$BASE"
61 rm -f -- "$LOCAL" "$REMOTE" "$BASE" "$BACKUP"
63 if test "$MERGETOOL_TMPDIR" != "."
65 rmdir "$MERGETOOL_TMPDIR"
74 printf " {%s}: " "$branch"
78 elif is_symlink
"$mode"
80 echo "a symbolic link -> '$(cat "$file")'"
81 elif is_submodule
"$mode"
83 echo "submodule commit $file"
92 resolve_symlink_merge
() {
95 printf "Use (l)ocal or (r)emote, or (a)bort? "
99 git checkout-index
-f --stage=2 -- "$MERGED"
101 cleanup_temp_files
--save-backup
105 git checkout-index
-f --stage=3 -- "$MERGED"
107 cleanup_temp_files
--save-backup
117 resolve_deleted_merge
() {
122 printf "Use (m)odified or (d)eleted file, or (a)bort? "
124 printf "Use (c)reated or (d)eleted file, or (a)bort? "
130 if test "$merge_keep_backup" = "true"
132 cleanup_temp_files
--save-backup
139 git
rm -- "$MERGED" > /dev
/null
144 if test "$merge_keep_temporaries" = "false"
154 resolve_submodule_merge
() {
157 printf "Use (l)ocal or (r)emote, or (a)bort? "
163 if test -n "$(git ls-tree HEAD -- "$MERGED")"
165 # Local isn't present, but it's a subdirectory
166 git ls-tree
--full-name -r HEAD
-- "$MERGED" |
167 git update-index
--index-info ||
exit $?
169 test -e "$MERGED" && mv -- "$MERGED" "$BACKUP"
170 git update-index
--force-remove "$MERGED"
171 cleanup_temp_files
--save-backup
173 elif is_submodule
"$local_mode"
175 stage_submodule
"$MERGED" "$local_sha1"
177 git checkout-index
-f --stage=2 -- "$MERGED"
185 if test -n "$(git ls-tree MERGE_HEAD -- "$MERGED")"
187 # Remote isn't present, but it's a subdirectory
188 git ls-tree
--full-name -r MERGE_HEAD
-- "$MERGED" |
189 git update-index
--index-info ||
exit $?
191 test -e "$MERGED" && mv -- "$MERGED" "$BACKUP"
192 git update-index
--force-remove "$MERGED"
194 elif is_submodule
"$remote_mode"
196 ! is_submodule
"$local_mode" &&
198 mv -- "$MERGED" "$BACKUP"
199 stage_submodule
"$MERGED" "$remote_sha1"
201 test -e "$MERGED" && mv -- "$MERGED" "$BACKUP"
202 git checkout-index
-f --stage=3 -- "$MERGED"
205 cleanup_temp_files
--save-backup
219 die
"fatal: unable to create directory for module at $path"
220 # Find $path relative to work tree
221 work_tree_root
=$
(cd_to_toplevel
&& pwd)
222 work_rel_path
=$
(cd "$path" &&
223 GIT_WORK_TREE
="${work_tree_root}" git rev-parse
--show-prefix
225 test -n "$work_rel_path" ||
226 die
"fatal: unable to get path of module $path relative to work tree"
227 git update-index
--add --replace --cacheinfo 160000 "$submodule_sha1" "${work_rel_path%/}" || die
230 checkout_staged_file
() {
232 "$(git checkout-index --temp --stage="$1" "$2" 2>/dev/null)" \
235 if test $?
-eq 0 && test -n "$tmpfile"
237 mv -- "$(git rev-parse --show-cdup)$tmpfile" "$3"
246 f
=$
(git ls-files
-u -- "$MERGED")
249 if test ! -f "$MERGED"
251 echo "$MERGED: file not found"
253 echo "$MERGED: file does not need merging"
258 if BASE
=$
(expr "$MERGED" : '\(.*\)\.[^/]*$')
260 ext
=$
(expr "$MERGED" : '.*\(\.[^/]*\)$')
266 mergetool_tmpdir_init
268 if test "$MERGETOOL_TMPDIR" != "."
270 # If we're using a temporary directory then write to the
271 # top-level of that directory.
275 BACKUP
="$MERGETOOL_TMPDIR/${BASE}_BACKUP_$$$ext"
276 LOCAL
="$MERGETOOL_TMPDIR/${BASE}_LOCAL_$$$ext"
277 REMOTE
="$MERGETOOL_TMPDIR/${BASE}_REMOTE_$$$ext"
278 BASE
="$MERGETOOL_TMPDIR/${BASE}_BASE_$$$ext"
280 base_mode
=$
(git ls-files
-u -- "$MERGED" |
awk '{if ($3==1) print $1;}')
281 local_mode
=$
(git ls-files
-u -- "$MERGED" |
awk '{if ($3==2) print $1;}')
282 remote_mode
=$
(git ls-files
-u -- "$MERGED" |
awk '{if ($3==3) print $1;}')
284 if is_submodule
"$local_mode" || is_submodule
"$remote_mode"
286 echo "Submodule merge conflict for '$MERGED':"
287 local_sha1
=$
(git ls-files
-u -- "$MERGED" |
awk '{if ($3==2) print $2;}')
288 remote_sha1
=$
(git ls-files
-u -- "$MERGED" |
awk '{if ($3==3) print $2;}')
289 describe_file
"$local_mode" "local" "$local_sha1"
290 describe_file
"$remote_mode" "remote" "$remote_sha1"
291 resolve_submodule_merge
297 mv -- "$MERGED" "$BACKUP"
298 cp -- "$BACKUP" "$MERGED"
300 # Create a parent directory to handle delete/delete conflicts
301 # where the base's directory no longer exists.
302 mkdir
-p "$(dirname "$MERGED")"
304 checkout_staged_file
1 "$MERGED" "$BASE"
305 checkout_staged_file
2 "$MERGED" "$LOCAL"
306 checkout_staged_file
3 "$MERGED" "$REMOTE"
308 if test -z "$local_mode" ||
test -z "$remote_mode"
310 echo "Deleted merge conflict for '$MERGED':"
311 describe_file
"$local_mode" "local" "$LOCAL"
312 describe_file
"$remote_mode" "remote" "$REMOTE"
313 resolve_deleted_merge
315 rmdir -p "$(dirname "$MERGED")" 2>/dev
/null
319 if is_symlink
"$local_mode" || is_symlink
"$remote_mode"
321 echo "Symbolic link merge conflict for '$MERGED':"
322 describe_file
"$local_mode" "local" "$LOCAL"
323 describe_file
"$remote_mode" "remote" "$REMOTE"
324 resolve_symlink_merge
328 echo "Normal merge conflict for '$MERGED':"
329 describe_file
"$local_mode" "local" "$LOCAL"
330 describe_file
"$remote_mode" "remote" "$REMOTE"
331 if test "$guessed_merge_tool" = true ||
test "$prompt" = true
333 printf "Hit return to start merge resolution tool (%s): " "$merge_tool"
344 if ! run_merge_tool
"$merge_tool" "$present"
346 echo "merge of $MERGED failed" 1>&2
347 mv -- "$BACKUP" "$MERGED"
349 if test "$merge_keep_temporaries" = "false"
357 if test "$merge_keep_backup" = "true"
359 mv -- "$BACKUP" "$MERGED.orig"
369 prompt_after_failed_merge
() {
372 printf "Continue merging other unresolved paths [y/n]? "
385 print_noop_and_exit
() {
386 echo "No files need merging"
391 prompt
=$
(git config
--bool mergetool.prompt
)
392 guessed_merge_tool
=false
399 TOOL_MODE
=${1#--tool-help=}
408 merge_tool
=$
(expr "z$1" : 'z-[^=]*=\(.*\)')
443 if test -z "$merge_tool"
445 # Check if a merge tool has been configured
446 merge_tool
=$
(get_configured_merge_tool
)
447 # Try to guess an appropriate merge tool if no tool has been set.
448 if test -z "$merge_tool"
450 merge_tool
=$
(guess_merge_tool
) ||
exit
451 guessed_merge_tool
=true
454 merge_keep_backup
="$(git config --bool mergetool.keepBackup || echo true)"
455 merge_keep_temporaries
="$(git config --bool mergetool.keepTemporaries || echo false)"
457 prefix
=$
(git rev-parse
--show-prefix) ||
exit 1
460 if test -n "$orderfile"
463 git rev-parse
--prefix "$prefix" -- "$orderfile" |
468 if test $# -eq 0 && test -e "$GIT_DIR/MERGE_RR"
470 set -- $
(git rerere remaining
)
477 # rev-parse provides the -- needed for 'set'
478 eval "set $(git rev-parse --sq --prefix "$prefix" -- "$@
")"
481 files
=$
(git
-c core.quotePath
=false \
482 diff --name-only --diff-filter=U \
483 ${orderfile:+"-O$orderfile"} -- "$@")
491 printf "%s\n" "$files"
501 test $# -ne 1 && prompt_after_failed_merge ||
exit 1