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
101 test "$GIT_MERGETOOL_GUI" = true
104 translate_merge_tool_path
() {
109 if test "$MERGED" -nt "$BACKUP"
115 echo "$MERGED seems unchanged."
116 printf "Was the merge successful [y/n]? "
117 read answer ||
return 1
127 setup_tool
"$1" && return 0
128 cmd
=$
(get_merge_tool_cmd
"$1")
133 merge_tool_cmd
=$
(get_merge_tool_cmd
"$tool")
134 test -n "$merge_tool_cmd" ||
return 1
137 ( eval $merge_tool_cmd )
141 ( eval $merge_tool_cmd )
144 list_tool_variants
() {
152 # Fallback definitions, to be overridden by tools.
177 hide_resolved_enabled
() {
181 translate_merge_tool_path
() {
185 list_tool_variants
() {
189 # Most tools' exit codes cannot be trusted, so By default we ignore
190 # their exit code and check the merged file's modification time in
191 # check_unchanged() to determine whether or not the merge was
192 # successful. The return value from run_merge_cmd, by default, is
193 # determined by check_unchanged().
195 # When a tool's exit code can be trusted then the return value from
196 # run_merge_cmd is simply the tool's exit code, and check_unchanged()
199 # The return value of exit_code_trustable() tells us whether or not we
200 # can trust the tool's exit code.
202 # User-defined and built-in tools default to false.
203 # Built-in tools advertise that their exit code is trustable by
204 # redefining exit_code_trustable() to true.
206 exit_code_trustable
() {
210 if test -f "$MERGE_TOOLS_DIR/$tool"
212 .
"$MERGE_TOOLS_DIR/$tool"
213 elif test -f "$MERGE_TOOLS_DIR/${tool%[0-9]}"
215 .
"$MERGE_TOOLS_DIR/${tool%[0-9]}"
221 # Now let the user override the default command for the tool. If
222 # they have not done so then this will return 1 which we ignore.
225 if ! list_tool_variants |
grep -q "^$tool$"
230 if merge_mode
&& ! can_merge
232 echo "error: '$tool' can not be used to resolve merges" >&2
234 elif diff_mode
&& ! can_diff
236 echo "error: '$tool' can only be used to resolve merges" >&2
242 get_merge_tool_cmd
() {
246 git config
"difftool.$merge_tool.cmd" ||
247 git config
"mergetool.$merge_tool.cmd"
249 git config
"mergetool.$merge_tool.cmd"
254 if git config
--bool "mergetool.$1.trustExitCode"
257 elif exit_code_trustable
265 initialize_merge_tool
() {
266 # Bring tool-specific functions into scope
267 setup_tool
"$1" ||
return 1
270 # Entry point for running tools
272 # If GIT_PREFIX is empty then we cannot use it in tools
273 # that expect to be able to chdir() to its value.
274 GIT_PREFIX
=${GIT_PREFIX:-.}
277 merge_tool_path
=$
(get_merge_tool_path
"$1") ||
exit
288 # Run a either a configured or built-in diff tool
293 # Run a either a configured or built-in merge tool
295 mergetool_trust_exit_code
=$
(trust_exit_code
"$1")
296 if test "$mergetool_trust_exit_code" = "true"
306 list_merge_tool_candidates
() {
309 tools
="tortoisemerge"
313 if test -n "$DISPLAY"
315 if test -n "$GNOME_DESKTOP_SESSION_ID"
317 tools
="meld opendiff kdiff3 tkdiff xxdiff $tools"
319 tools
="opendiff kdiff3 tkdiff xxdiff meld $tools"
321 tools
="$tools gvimdiff diffuse diffmerge ecmerge"
322 tools
="$tools p4merge araxis bc codecompare"
323 tools
="$tools smerge"
325 case "${VISUAL:-$EDITOR}" in
327 tools
="$tools nvimdiff vimdiff emerge"
330 tools
="$tools vimdiff nvimdiff emerge"
333 tools
="$tools emerge vimdiff nvimdiff"
339 tool_opt
="'git ${TOOL_MODE}tool --tool=<tool>'"
346 cmd_name
=${TOOL_MODE}tool
348 diff_mode
&& list_config_tools difftool
"$tab$tab"
349 list_config_tools mergetool
"$tab$tab"
352 if test -n "$config_tools"
354 extra_content
="${tab}user-defined:${LF}$config_tools"
357 show_tool_names
'mode_ok && is_available' "$tab$tab" \
358 "$tool_opt may be set to one of the following:" \
359 "No suitable tool for 'git $cmd_name --tool=<tool>' found." \
363 show_tool_names
'mode_ok && ! is_available' "$tab$tab" \
364 "${LF}The following tools are valid, but not currently available:" &&
367 if test "$any_shown" = yes
370 echo "Some of the tools listed above only work in a windowed"
371 echo "environment. If run in a terminal-only session, they will fail."
376 guess_merge_tool
() {
377 list_merge_tool_candidates
380 This message is displayed because '$TOOL_MODE.tool' is not configured.
381 See 'git ${TOOL_MODE}tool --tool-help' or 'git help config' for more details.
382 'git ${TOOL_MODE}tool' will now attempt to use one of the following tools:
386 # Loop over each candidate and stop when a valid merge tool is found.
390 is_available
"$tool" && echo "$tool" && return 0
393 echo >&2 "No known ${TOOL_MODE} tool is available."
397 get_configured_merge_tool
() {
403 keys
="diff.guitool merge.guitool diff.tool merge.tool"
405 keys
="diff.tool merge.tool"
410 keys
="merge.guitool merge.tool"
420 selected
=$
(git config
$key)
421 if test -n "$selected"
428 if test -n "$merge_tool" && ! valid_tool
"$merge_tool"
430 echo >&2 "git config option $TOOL_MODE.${gui_prefix}tool set to unknown tool: $merge_tool"
431 echo >&2 "Resetting to default..."
437 get_merge_tool_path
() {
438 # A merge tool has been set, so verify that it's valid.
440 if ! valid_tool
"$merge_tool"
442 echo >&2 "Unknown merge tool $merge_tool"
447 merge_tool_path
=$
(git config difftool.
"$merge_tool".path ||
448 git config mergetool.
"$merge_tool".path
)
450 merge_tool_path
=$
(git config mergetool.
"$merge_tool".path
)
452 if test -z "$merge_tool_path"
454 merge_tool_path
=$
(translate_merge_tool_path
"$merge_tool")
456 if test -z "$(get_merge_tool_cmd "$merge_tool")" &&
457 ! type "$merge_tool_path" >/dev
/null
2>&1
459 echo >&2 "The $TOOL_MODE tool $merge_tool is not available as"\
463 echo "$merge_tool_path"
468 # Check if a merge tool has been configured
469 merge_tool
=$
(get_configured_merge_tool
)
470 # Try to guess an appropriate merge tool if no tool has been set.
471 if test -z "$merge_tool"
473 merge_tool
=$
(guess_merge_tool
) ||
exit
477 test "$is_guessed" = false
480 mergetool_find_win32_cmd
() {
484 # Use $executable if it exists in $PATH
485 if type -p "$executable" >/dev
/null
2>&1
487 printf '%s' "$executable"
491 # Look for executable in the typical locations
492 for directory
in $
(env |
grep -Ei '^PROGRAM(FILES(\(X86\))?|W6432)=' |
493 cut
-d '=' -f 2- |
sort -u)
495 if test -n "$directory" && test -x "$directory/$sub_directory/$executable"
497 printf '%s' "$directory/$sub_directory/$executable"
502 printf '%s' "$executable"