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] [-g|--gui|--no-gui] [-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
() {
231 tmpfile
="$(git checkout-index --temp --stage="$1" "$2" 2>/dev/null)" &&
232 tmpfile
=${tmpfile%%' '*}
234 if test $?
-eq 0 && test -n "$tmpfile"
236 mv -- "$(git rev-parse --show-cdup)$tmpfile" "$3"
243 git merge-file
--ours -q -p "$LOCAL" "$BASE" "$REMOTE" >"$LCONFL"
244 git merge-file
--theirs -q -p "$LOCAL" "$BASE" "$REMOTE" >"$RCONFL"
245 mv -- "$LCONFL" "$LOCAL"
246 mv -- "$RCONFL" "$REMOTE"
252 f
=$
(git ls-files
-u -- "$MERGED")
255 if test ! -f "$MERGED"
257 echo "$MERGED: file not found"
259 echo "$MERGED: file does not need merging"
264 # extract file extension from the last path component
265 case "${MERGED##*/}" in
268 BASE
=${MERGED%"$ext"}
275 initialize_merge_tool
"$merge_tool" ||
return
277 mergetool_tmpdir_init
279 if test "$MERGETOOL_TMPDIR" != "."
281 # If we're using a temporary directory then write to the
282 # top-level of that directory.
286 BACKUP
="$MERGETOOL_TMPDIR/${BASE}_BACKUP_$$$ext"
287 LOCAL
="$MERGETOOL_TMPDIR/${BASE}_LOCAL_$$$ext"
288 LCONFL
="$MERGETOOL_TMPDIR/${BASE}_LOCAL_LCONFL_$$$ext"
289 REMOTE
="$MERGETOOL_TMPDIR/${BASE}_REMOTE_$$$ext"
290 RCONFL
="$MERGETOOL_TMPDIR/${BASE}_REMOTE_RCONFL_$$$ext"
291 BASE
="$MERGETOOL_TMPDIR/${BASE}_BASE_$$$ext"
293 base_mode
= local_mode
= remote_mode
=
295 # here, $IFS is just a LF
298 mode
=${line%% *} # 1st word
299 sha1
=${line#"$mode "}
300 sha1
=${sha1%% *} # 2nd word
301 case "${line#$mode $sha1 }" in # remainder
306 local_mode
=$mode local_sha1
=$sha1
309 remote_mode
=$mode remote_sha1
=$sha1
314 if is_submodule
"$local_mode" || is_submodule
"$remote_mode"
316 echo "Submodule merge conflict for '$MERGED':"
317 describe_file
"$local_mode" "local" "$local_sha1"
318 describe_file
"$remote_mode" "remote" "$remote_sha1"
319 resolve_submodule_merge
325 mv -- "$MERGED" "$BACKUP"
326 cp -- "$BACKUP" "$MERGED"
328 # Create a parent directory to handle delete/delete conflicts
329 # where the base's directory no longer exists.
330 mkdir
-p "$(dirname "$MERGED")"
332 checkout_staged_file
1 "$MERGED" "$BASE"
333 checkout_staged_file
2 "$MERGED" "$LOCAL"
334 checkout_staged_file
3 "$MERGED" "$REMOTE"
336 # hideResolved preferences hierarchy.
337 global_config
="mergetool.hideResolved"
338 tool_config
="mergetool.${merge_tool}.hideResolved"
340 if enabled
=$
(git config
--type=bool
"$tool_config")
342 # The user has a specific preference for a specific tool and no
343 # other preferences should override that.
345 elif enabled
=$
(git config
--type=bool
"$global_config")
347 # The user has a general preference for all tools.
349 # 'true' means the user likes the feature so we should use it
350 # where possible but tool authors can still override.
352 # 'false' means the user doesn't like the feature so we should
353 # not use it anywhere.
354 if test "$enabled" = true
&& hide_resolved_enabled
361 # The user does not have a preference. Default to disabled.
365 if test "$enabled" = true
370 if test -z "$local_mode" ||
test -z "$remote_mode"
372 echo "Deleted merge conflict for '$MERGED':"
373 describe_file
"$local_mode" "local" "$LOCAL"
374 describe_file
"$remote_mode" "remote" "$REMOTE"
375 resolve_deleted_merge
377 rmdir -p "$(dirname "$MERGED")" 2>/dev
/null
381 if is_symlink
"$local_mode" || is_symlink
"$remote_mode"
383 echo "Symbolic link merge conflict for '$MERGED':"
384 describe_file
"$local_mode" "local" "$LOCAL"
385 describe_file
"$remote_mode" "remote" "$REMOTE"
386 resolve_symlink_merge
390 echo "Normal merge conflict for '$MERGED':"
391 describe_file
"$local_mode" "local" "$LOCAL"
392 describe_file
"$remote_mode" "remote" "$REMOTE"
393 if test "$guessed_merge_tool" = true ||
test "$prompt" = true
395 printf "Hit return to start merge resolution tool (%s): " "$merge_tool"
406 if ! run_merge_tool
"$merge_tool" "$present"
408 echo "merge of $MERGED failed" 1>&2
409 mv -- "$BACKUP" "$MERGED"
411 if test "$merge_keep_temporaries" = "false"
419 if test "$merge_keep_backup" = "true"
421 mv -- "$BACKUP" "$MERGED.orig"
431 prompt_after_failed_merge
() {
434 printf "Continue merging other unresolved paths [y/n]? "
447 print_noop_and_exit
() {
448 echo "No files need merging"
453 prompt
=$
(git config
--bool mergetool.prompt
)
454 GIT_MERGETOOL_GUI
=false
455 guessed_merge_tool
=false
462 TOOL_MODE
=${1#--tool-help=}
481 GIT_MERGETOOL_GUI
=false
484 GIT_MERGETOOL_GUI
=true
512 if test -z "$merge_tool"
514 if ! merge_tool
=$
(get_merge_tool
)
516 guessed_merge_tool
=true
519 merge_keep_backup
="$(git config --bool mergetool.keepBackup || echo true)"
520 merge_keep_temporaries
="$(git config --bool mergetool.keepTemporaries || echo false)"
522 prefix
=$
(git rev-parse
--show-prefix) ||
exit 1
525 if test -n "$orderfile"
528 git rev-parse
--prefix "$prefix" -- "$orderfile" |
533 if test $# -eq 0 && test -e "$GIT_DIR/MERGE_RR"
535 set -- $
(git rerere remaining
)
542 # rev-parse provides the -- needed for 'set'
543 eval "set $(git rev-parse --sq --prefix "$prefix" -- "$@
")"
546 files
=$
(git
-c core.quotePath
=false \
547 diff --name-only --diff-filter=U \
548 ${orderfile:+"-O$orderfile"} -- "$@")
556 printf "%s\n" "$files"
566 test $# -ne 1 && prompt_after_failed_merge ||
exit 1