1 # git-mergetool--lib is a shell library for common merge tool functions
3 : ${MERGE_TOOLS_DIR=$(git --exec-path)/mergetools}
21 merge_tool_path
=$
(translate_merge_tool_path
"$1") &&
22 type "$merge_tool_path" >/dev
/null
2>&1
25 list_config_tools
() {
29 git config
--get-regexp $section'\..*\.cmd' |
30 while read -r key value
32 toolname
=${key#$section.}
33 toolname
=${toolname%.cmd}
35 printf "%s%s\n" "$line_prefix" "$toolname"
40 condition
=${1:-true} per_line_prefix=${2:-} preamble=${3:-}
45 ( cd "$MERGE_TOOLS_DIR" && ls ) |
{
48 setup_tool
"$scriptname" 2>/dev
/null
49 # We need an actual line feed here
51 $(list_tool_variants)"
53 variants
="$(echo "$variants" | sort -u)"
55 for toolname
in $variants
57 if setup_tool
"$toolname" 2>/dev
/null
&&
58 (eval "$condition" "$toolname")
60 if test -n "$preamble"
62 printf "%s\n" "$preamble"
66 printf "%s%-15s %s\n" "$per_line_prefix" "$toolname" $
(diff_mode
&& diff_cmd_help
"$toolname" || merge_cmd_help
"$toolname")
70 if test -n "$extra_content"
72 if test -n "$preamble"
74 # Note: no '\n' here since we don't want a
75 # blank line if there is no initial content.
76 printf "%s" "$preamble"
80 printf "\n%s\n" "$extra_content"
83 if test -n "$preamble" && test -n "$not_found_msg"
85 printf "%s\n" "$not_found_msg"
93 test "$TOOL_MODE" = diff
97 test "$TOOL_MODE" = merge
103 GUI_DEFAULT_KEY
="difftool.guiDefault"
105 GUI_DEFAULT_KEY
="mergetool.guiDefault"
107 GUI_DEFAULT_CONFIG_LCASE
=$
(git config
--default false
--get "$GUI_DEFAULT_KEY" |
tr 'A-Z' 'a-z')
108 if test "$GUI_DEFAULT_CONFIG_LCASE" = "auto"
110 if test -n "$DISPLAY"
117 GUI_DEFAULT
=$
(git config
--default false
--bool --get "$GUI_DEFAULT_KEY")
118 subshell_exit_status
=$?
119 if test $subshell_exit_status -ne 0
121 exit $subshell_exit_status
128 if test -z "$GIT_MERGETOOL_GUI"
130 GIT_MERGETOOL_GUI
=$
(get_gui_default
)
136 test "$GIT_MERGETOOL_GUI" = true
139 translate_merge_tool_path
() {
144 if test "$MERGED" -nt "$BACKUP"
150 echo "$MERGED seems unchanged."
151 printf "Was the merge successful [y/n]? "
152 read answer ||
return 1
162 setup_tool
"$1" && return 0
163 cmd
=$
(get_merge_tool_cmd
"$1")
168 merge_tool_cmd
=$
(get_merge_tool_cmd
"$tool")
169 test -n "$merge_tool_cmd" ||
return 1
172 ( eval $merge_tool_cmd )
176 ( eval $merge_tool_cmd )
179 list_tool_variants
() {
187 # Fallback definitions, to be overridden by tools.
212 hide_resolved_enabled
() {
216 translate_merge_tool_path
() {
220 list_tool_variants
() {
224 # Most tools' exit codes cannot be trusted, so By default we ignore
225 # their exit code and check the merged file's modification time in
226 # check_unchanged() to determine whether or not the merge was
227 # successful. The return value from run_merge_cmd, by default, is
228 # determined by check_unchanged().
230 # When a tool's exit code can be trusted then the return value from
231 # run_merge_cmd is simply the tool's exit code, and check_unchanged()
234 # The return value of exit_code_trustable() tells us whether or not we
235 # can trust the tool's exit code.
237 # User-defined and built-in tools default to false.
238 # Built-in tools advertise that their exit code is trustable by
239 # redefining exit_code_trustable() to true.
241 exit_code_trustable
() {
245 if test -f "$MERGE_TOOLS_DIR/$tool"
247 .
"$MERGE_TOOLS_DIR/$tool"
248 elif test -f "$MERGE_TOOLS_DIR/${tool%[0-9]}"
250 .
"$MERGE_TOOLS_DIR/${tool%[0-9]}"
256 # Now let the user override the default command for the tool. If
257 # they have not done so then this will return 1 which we ignore.
260 if ! list_tool_variants |
grep -q "^$tool$"
265 if merge_mode
&& ! can_merge
267 echo "error: '$tool' can not be used to resolve merges" >&2
269 elif diff_mode
&& ! can_diff
271 echo "error: '$tool' can only be used to resolve merges" >&2
277 get_merge_tool_cmd
() {
281 git config
"difftool.$merge_tool.cmd" ||
282 git config
"mergetool.$merge_tool.cmd"
284 git config
"mergetool.$merge_tool.cmd"
289 if git config
--bool "mergetool.$1.trustExitCode"
292 elif exit_code_trustable
300 initialize_merge_tool
() {
301 # Bring tool-specific functions into scope
302 setup_tool
"$1" ||
return 1
305 # Entry point for running tools
307 # If GIT_PREFIX is empty then we cannot use it in tools
308 # that expect to be able to chdir() to its value.
309 GIT_PREFIX
=${GIT_PREFIX:-.}
312 merge_tool_path
=$
(get_merge_tool_path
"$1") ||
exit
323 # Run a either a configured or built-in diff tool
328 # Run a either a configured or built-in merge tool
330 mergetool_trust_exit_code
=$
(trust_exit_code
"$1")
331 if test "$mergetool_trust_exit_code" = "true"
341 list_merge_tool_candidates
() {
344 tools
="tortoisemerge"
348 if test -n "$DISPLAY"
350 if test -n "$GNOME_DESKTOP_SESSION_ID"
352 tools
="meld opendiff kdiff3 tkdiff xxdiff $tools"
354 tools
="opendiff kdiff3 tkdiff xxdiff meld $tools"
356 tools
="$tools gvimdiff diffuse diffmerge ecmerge"
357 tools
="$tools p4merge araxis bc codecompare"
358 tools
="$tools smerge"
360 case "${VISUAL:-$EDITOR}" in
362 tools
="$tools nvimdiff vimdiff emerge"
365 tools
="$tools vimdiff nvimdiff emerge"
368 tools
="$tools emerge vimdiff nvimdiff"
374 tool_opt
="'git ${TOOL_MODE}tool --tool=<tool>'"
381 cmd_name
=${TOOL_MODE}tool
383 diff_mode
&& list_config_tools difftool
"$tab$tab"
384 list_config_tools mergetool
"$tab$tab"
387 if test -n "$config_tools"
389 extra_content
="${tab}user-defined:${LF}$config_tools"
392 show_tool_names
'mode_ok && is_available' "$tab$tab" \
393 "$tool_opt may be set to one of the following:" \
394 "No suitable tool for 'git $cmd_name --tool=<tool>' found." \
398 show_tool_names
'mode_ok && ! is_available' "$tab$tab" \
399 "${LF}The following tools are valid, but not currently available:" &&
402 if test "$any_shown" = yes
405 echo "Some of the tools listed above only work in a windowed"
406 echo "environment. If run in a terminal-only session, they will fail."
411 guess_merge_tool
() {
412 list_merge_tool_candidates
415 This message is displayed because '$TOOL_MODE.tool' is not configured.
416 See 'git ${TOOL_MODE}tool --tool-help' or 'git help config' for more details.
417 'git ${TOOL_MODE}tool' will now attempt to use one of the following tools:
421 # Loop over each candidate and stop when a valid merge tool is found.
425 is_available
"$tool" && echo "$tool" && return 0
428 echo >&2 "No known ${TOOL_MODE} tool is available."
432 get_configured_merge_tool
() {
438 keys
="diff.guitool merge.guitool diff.tool merge.tool"
440 keys
="diff.tool merge.tool"
445 keys
="merge.guitool merge.tool"
455 selected
=$
(git config
$key)
456 if test -n "$selected"
463 if test -n "$merge_tool" && ! valid_tool
"$merge_tool"
465 echo >&2 "git config option $TOOL_MODE.${gui_prefix}tool set to unknown tool: $merge_tool"
466 echo >&2 "Resetting to default..."
472 get_merge_tool_path
() {
473 # A merge tool has been set, so verify that it's valid.
475 if ! valid_tool
"$merge_tool"
477 echo >&2 "Unknown merge tool $merge_tool"
482 merge_tool_path
=$
(git config difftool.
"$merge_tool".path ||
483 git config mergetool.
"$merge_tool".path
)
485 merge_tool_path
=$
(git config mergetool.
"$merge_tool".path
)
487 if test -z "$merge_tool_path"
489 merge_tool_path
=$
(translate_merge_tool_path
"$merge_tool")
491 if test -z "$(get_merge_tool_cmd "$merge_tool")" &&
492 ! type "$merge_tool_path" >/dev
/null
2>&1
494 echo >&2 "The $TOOL_MODE tool $merge_tool is not available as"\
498 echo "$merge_tool_path"
503 # Check if a merge tool has been configured
504 merge_tool
=$
(get_configured_merge_tool
)
505 subshell_exit_status
=$?
506 if test $subshell_exit_status -gt "1"
508 exit $subshell_exit_status
510 # Try to guess an appropriate merge tool if no tool has been set.
511 if test -z "$merge_tool"
513 merge_tool
=$
(guess_merge_tool
) ||
exit
517 test "$is_guessed" = false
520 mergetool_find_win32_cmd
() {
524 # Use $executable if it exists in $PATH
525 if type -p "$executable" >/dev
/null
2>&1
527 printf '%s' "$executable"
531 # Look for executable in the typical locations
532 for directory
in $
(env |
grep -Ei '^PROGRAM(FILES(\(X86\))?|W6432)=' |
533 cut
-d '=' -f 2- |
sort -u)
535 if test -n "$directory" && test -x "$directory/$sub_directory/$executable"
537 printf '%s' "$directory/$sub_directory/$executable"
542 printf '%s' "$executable"