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%s\n" "$per_line_prefix" "$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.
169 translate_merge_tool_path () {
173 list_tool_variants () {
177 # Most tools' exit codes cannot be trusted, so By default we ignore
178 # their exit code and check the merged file's modification time in
179 # check_unchanged() to determine whether or not the merge was
180 # successful. The return value from run_merge_cmd, by default, is
181 # determined by check_unchanged().
183 # When a tool's exit code can be trusted then the return value from
184 # run_merge_cmd is simply the tool's exit code, and check_unchanged()
187 # The return value of exit_code_trustable() tells us whether or not we
188 # can trust the tool's exit code.
190 # User-defined and built-in tools default to false.
191 # Built-in tools advertise that their exit code is trustable by
192 # redefining exit_code_trustable() to true.
194 exit_code_trustable () {
198 if test -f "$MERGE_TOOLS_DIR/$tool"
200 . "$MERGE_TOOLS_DIR/$tool"
201 elif test -f "$MERGE_TOOLS_DIR/${tool%[0-9]}"
203 . "$MERGE_TOOLS_DIR/${tool%[0-9]}"
209 # Now let the user override the default command for the tool. If
210 # they have not done so then this will return 1 which we ignore.
213 if ! list_tool_variants | grep -q "^$tool$"
218 if merge_mode && ! can_merge
220 echo "error: '$tool' can not be used to resolve merges" >&2
222 elif diff_mode && ! can_diff
224 echo "error: '$tool' can only be used to resolve merges" >&2
230 get_merge_tool_cmd () {
234 git config "difftool.$merge_tool.cmd" ||
235 git config "mergetool.$merge_tool.cmd"
237 git config "mergetool.$merge_tool.cmd"
242 if git config --bool "mergetool.$1.trustExitCode"
245 elif exit_code_trustable
254 # Entry point for running tools
256 # If GIT_PREFIX is empty then we cannot use it in tools
257 # that expect to be able to chdir() to its value.
258 GIT_PREFIX=${GIT_PREFIX:-.}
261 merge_tool_path=$(get_merge_tool_path "$1") || exit
264 # Bring tool-specific functions into scope
265 setup_tool "$1" || return 1
275 # Run a either a configured or built-in diff tool
280 # Run a either a configured or built-in merge tool
282 mergetool_trust_exit_code=$(trust_exit_code "$1")
283 if test "$mergetool_trust_exit_code" = "true"
293 list_merge_tool_candidates () {
296 tools="tortoisemerge"
300 if test -n "$DISPLAY"
302 if test -n "$GNOME_DESKTOP_SESSION_ID"
304 tools="meld opendiff kdiff3 tkdiff xxdiff $tools"
306 tools="opendiff kdiff3 tkdiff xxdiff meld $tools"
308 tools="$tools gvimdiff diffuse diffmerge ecmerge"
309 tools="$tools p4merge araxis bc codecompare"
310 tools="$tools smerge"
312 case "${VISUAL:-$EDITOR}" in
314 tools="$tools nvimdiff vimdiff emerge"
317 tools="$tools vimdiff nvimdiff emerge"
320 tools="$tools emerge vimdiff nvimdiff"
326 tool_opt="'git ${TOOL_MODE}tool --tool=<tool>'"
333 cmd_name=${TOOL_MODE}tool
335 diff_mode && list_config_tools difftool "$tab$tab"
336 list_config_tools mergetool "$tab$tab"
339 if test -n "$config_tools"
341 extra_content="${tab}user-defined:${LF}$config_tools"
344 show_tool_names 'mode_ok && is_available' "$tab$tab" \
345 "$tool_opt may be set to one of the following:" \
346 "No suitable tool for 'git $cmd_name --tool=<tool>' found." \
350 show_tool_names 'mode_ok && ! is_available' "$tab$tab" \
351 "${LF}The following tools are valid, but not currently available:" &&
354 if test "$any_shown" = yes
357 echo "Some of the tools listed above only work in a windowed"
358 echo "environment. If run in a terminal-only session, they will fail."
363 guess_merge_tool () {
364 list_merge_tool_candidates
367 This message is displayed because '$TOOL_MODE.tool' is not configured.
368 See 'git ${TOOL_MODE}tool --tool-help' or 'git help config' for more details.
369 'git ${TOOL_MODE}tool' will now attempt to use one of the following tools:
373 # Loop over each candidate and stop when a valid merge tool is found.
377 is_available "$tool" && echo "$tool" && return 0
380 echo >&2 "No known ${TOOL_MODE} tool is available."
384 get_configured_merge_tool () {
390 keys="diff.guitool merge.guitool diff.tool merge.tool"
392 keys="diff.tool merge.tool"
397 keys="merge.guitool merge.tool"
407 selected=$(git config $key)
408 if test -n "$selected"
415 if test -n "$merge_tool" && ! valid_tool "$merge_tool"
417 echo >&2 "git config option $TOOL_MODE.${gui_prefix}tool set to unknown tool: $merge_tool"
418 echo >&2 "Resetting to default..."
424 get_merge_tool_path () {
425 # A merge tool has been set, so verify that it's valid.
427 if ! valid_tool "$merge_tool"
429 echo >&2 "Unknown merge tool $merge_tool"
434 merge_tool_path=$(git config difftool."$merge_tool".path ||
435 git config mergetool."$merge_tool".path)
437 merge_tool_path=$(git config mergetool."$merge_tool".path)
439 if test -z "$merge_tool_path"
441 merge_tool_path=$(translate_merge_tool_path "$merge_tool")
443 if test -z "$(get_merge_tool_cmd "$merge_tool")" &&
444 ! type "$merge_tool_path" >/dev/null 2>&1
446 echo >&2 "The $TOOL_MODE tool $merge_tool is not available as"\
450 echo "$merge_tool_path"
455 # Check if a merge tool has been configured
456 merge_tool=$(get_configured_merge_tool)
457 # Try to guess an appropriate merge tool if no tool has been set.
458 if test -z "$merge_tool"
460 merge_tool=$(guess_merge_tool) || exit
464 test "$is_guessed" = false
467 mergetool_find_win32_cmd () {
471 # Use $executable if it exists in $PATH
472 if type -p "$executable" >/dev/null 2>&1
474 printf '%s' "$executable"
478 # Look for executable in the typical locations
479 for directory in $(env | grep -Ei '^PROGRAM(FILES(\(X86\))?|W6432)=' |
480 cut -d '=' -f 2- | sort -u)
482 if test -n "$directory" && test -x "$directory/$sub_directory/$executable"
484 printf '%s' "$directory/$sub_directory/$executable"
489 printf '%s' "$executable"