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 variants="$variants$(list_tool_variants)\n"
51 variants="$(echo "$variants" | sort | uniq)"
53 for toolname in $variants
55 if setup_tool "$toolname" 2>/dev/null &&
56 (eval "$condition" "$toolname")
58 if test -n "$preamble"
60 printf "%s\n" "$preamble"
64 printf "%s%s\n" "$per_line_prefix" "$toolname"
68 if test -n "$extra_content"
70 if test -n "$preamble"
72 # Note: no '\n' here since we don't want a
73 # blank line if there is no initial content.
74 printf "%s" "$preamble"
78 printf "\n%s\n" "$extra_content"
81 if test -n "$preamble" && test -n "$not_found_msg"
83 printf "%s\n" "$not_found_msg"
91 test "$TOOL_MODE" = diff
95 test "$TOOL_MODE" = merge
99 test "$GIT_MERGETOOL_GUI" = true
102 translate_merge_tool_path () {
107 if test "$MERGED" -nt "$BACKUP"
113 echo "$MERGED seems unchanged."
114 printf "Was the merge successful [y/n]? "
115 read answer || return 1
125 setup_tool "$1" && return 0
126 cmd=$(get_merge_tool_cmd "$1")
131 merge_tool_cmd=$(get_merge_tool_cmd "$tool")
132 test -n "$merge_tool_cmd" || return 1
135 ( eval $merge_tool_cmd )
139 ( eval $merge_tool_cmd )
146 # Fallback definitions, to be overridden by tools.
163 translate_merge_tool_path () {
167 list_tool_variants () {
171 # Most tools' exit codes cannot be trusted, so By default we ignore
172 # their exit code and check the merged file's modification time in
173 # check_unchanged() to determine whether or not the merge was
174 # successful. The return value from run_merge_cmd, by default, is
175 # determined by check_unchanged().
177 # When a tool's exit code can be trusted then the return value from
178 # run_merge_cmd is simply the tool's exit code, and check_unchanged()
181 # The return value of exit_code_trustable() tells us whether or not we
182 # can trust the tool's exit code.
184 # User-defined and built-in tools default to false.
185 # Built-in tools advertise that their exit code is trustable by
186 # redefining exit_code_trustable() to true.
188 exit_code_trustable () {
192 if test -f "$MERGE_TOOLS_DIR/$tool"
194 . "$MERGE_TOOLS_DIR/$tool"
195 elif test -f "$MERGE_TOOLS_DIR/${tool%[0-9]}"
197 . "$MERGE_TOOLS_DIR/${tool%[0-9]}"
203 # Now let the user override the default command for the tool. If
204 # they have not done so then this will return 1 which we ignore.
207 if ! list_tool_variants | grep -q "^$tool$"
212 if merge_mode && ! can_merge
214 echo "error: '$tool' can not be used to resolve merges" >&2
216 elif diff_mode && ! can_diff
218 echo "error: '$tool' can only be used to resolve merges" >&2
224 get_merge_tool_cmd () {
228 git config "difftool.$merge_tool.cmd" ||
229 git config "mergetool.$merge_tool.cmd"
231 git config "mergetool.$merge_tool.cmd"
236 if git config --bool "mergetool.$1.trustExitCode"
239 elif exit_code_trustable
248 # Entry point for running tools
250 # If GIT_PREFIX is empty then we cannot use it in tools
251 # that expect to be able to chdir() to its value.
252 GIT_PREFIX=${GIT_PREFIX:-.}
255 merge_tool_path=$(get_merge_tool_path "$1") || exit
258 # Bring tool-specific functions into scope
259 setup_tool "$1" || return 1
269 # Run a either a configured or built-in diff tool
274 # Run a either a configured or built-in merge tool
276 mergetool_trust_exit_code=$(trust_exit_code "$1")
277 if test "$mergetool_trust_exit_code" = "true"
287 list_merge_tool_candidates () {
290 tools="tortoisemerge"
294 if test -n "$DISPLAY"
296 if test -n "$GNOME_DESKTOP_SESSION_ID"
298 tools="meld opendiff kdiff3 tkdiff xxdiff $tools"
300 tools="opendiff kdiff3 tkdiff xxdiff meld $tools"
302 tools="$tools gvimdiff diffuse diffmerge ecmerge"
303 tools="$tools p4merge araxis bc codecompare"
304 tools="$tools smerge"
306 case "${VISUAL:-$EDITOR}" in
308 tools="$tools nvimdiff vimdiff emerge"
311 tools="$tools vimdiff nvimdiff emerge"
314 tools="$tools emerge vimdiff nvimdiff"
320 tool_opt="'git ${TOOL_MODE}tool --tool=<tool>'"
327 cmd_name=${TOOL_MODE}tool
329 diff_mode && list_config_tools difftool "$tab$tab"
330 list_config_tools mergetool "$tab$tab"
333 if test -n "$config_tools"
335 extra_content="${tab}user-defined:${LF}$config_tools"
338 show_tool_names 'mode_ok && is_available' "$tab$tab" \
339 "$tool_opt may be set to one of the following:" \
340 "No suitable tool for 'git $cmd_name --tool=<tool>' found." \
344 show_tool_names 'mode_ok && ! is_available' "$tab$tab" \
345 "${LF}The following tools are valid, but not currently available:" &&
348 if test "$any_shown" = yes
351 echo "Some of the tools listed above only work in a windowed"
352 echo "environment. If run in a terminal-only session, they will fail."
357 guess_merge_tool () {
358 list_merge_tool_candidates
361 This message is displayed because '$TOOL_MODE.tool' is not configured.
362 See 'git ${TOOL_MODE}tool --tool-help' or 'git help config' for more details.
363 'git ${TOOL_MODE}tool' will now attempt to use one of the following tools:
367 # Loop over each candidate and stop when a valid merge tool is found.
371 is_available "$tool" && echo "$tool" && return 0
374 echo >&2 "No known ${TOOL_MODE} tool is available."
378 get_configured_merge_tool () {
384 keys="diff.guitool merge.guitool diff.tool merge.tool"
386 keys="diff.tool merge.tool"
391 keys="merge.guitool merge.tool"
401 selected=$(git config $key)
402 if test -n "$selected"
409 if test -n "$merge_tool" && ! valid_tool "$merge_tool"
411 echo >&2 "git config option $TOOL_MODE.${gui_prefix}tool set to unknown tool: $merge_tool"
412 echo >&2 "Resetting to default..."
418 get_merge_tool_path () {
419 # A merge tool has been set, so verify that it's valid.
421 if ! valid_tool "$merge_tool"
423 echo >&2 "Unknown merge tool $merge_tool"
428 merge_tool_path=$(git config difftool."$merge_tool".path ||
429 git config mergetool."$merge_tool".path)
431 merge_tool_path=$(git config mergetool."$merge_tool".path)
433 if test -z "$merge_tool_path"
435 merge_tool_path=$(translate_merge_tool_path "$merge_tool")
437 if test -z "$(get_merge_tool_cmd "$merge_tool")" &&
438 ! type "$merge_tool_path" >/dev/null 2>&1
440 echo >&2 "The $TOOL_MODE tool $merge_tool is not available as"\
444 echo "$merge_tool_path"
449 # Check if a merge tool has been configured
450 merge_tool=$(get_configured_merge_tool)
451 # Try to guess an appropriate merge tool if no tool has been set.
452 if test -z "$merge_tool"
454 merge_tool=$(guess_merge_tool) || exit
458 test "$is_guessed" = false
461 mergetool_find_win32_cmd () {
465 # Use $executable if it exists in $PATH
466 if type -p "$executable" >/dev/null 2>&1
468 printf '%s' "$executable"
472 # Look for executable in the typical locations
473 for directory in $(env | grep -Ei '^PROGRAM(FILES(\(X86\))?|W6432)=' |
474 cut -d '=' -f 2- | sort -u)
476 if test -n "$directory" && test -x "$directory/$sub_directory/$executable"
478 printf '%s' "$directory/$sub_directory/$executable"
483 printf '%s' "$executable"