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 if setup_tool
"$toolname" 2>/dev
/null
&&
49 (eval "$condition" "$toolname")
51 if test -n "$preamble"
53 printf "%s\n" "$preamble"
57 printf "%s%s\n" "$per_line_prefix" "$toolname"
61 if test -n "$extra_content"
63 if test -n "$preamble"
65 # Note: no '\n' here since we don't want a
66 # blank line if there is no initial content.
67 printf "%s" "$preamble"
71 printf "\n%s\n" "$extra_content"
74 if test -n "$preamble" && test -n "$not_found_msg"
76 printf "%s\n" "$not_found_msg"
84 test "$TOOL_MODE" = diff
88 test "$TOOL_MODE" = merge
92 test "$GIT_MERGETOOL_GUI" = true
95 translate_merge_tool_path
() {
100 if test "$MERGED" -nt "$BACKUP"
106 echo "$MERGED seems unchanged."
107 printf "Was the merge successful [y/n]? "
108 read answer ||
return 1
118 setup_tool
"$1" && return 0
119 cmd
=$
(get_merge_tool_cmd
"$1")
124 merge_tool_cmd
=$
(get_merge_tool_cmd
"$tool")
125 test -n "$merge_tool_cmd" ||
return 1
128 ( eval $merge_tool_cmd )
132 ( eval $merge_tool_cmd )
139 # Fallback definitions, to be overridden by tools.
156 translate_merge_tool_path
() {
160 # Most tools' exit codes cannot be trusted, so By default we ignore
161 # their exit code and check the merged file's modification time in
162 # check_unchanged() to determine whether or not the merge was
163 # successful. The return value from run_merge_cmd, by default, is
164 # determined by check_unchanged().
166 # When a tool's exit code can be trusted then the return value from
167 # run_merge_cmd is simply the tool's exit code, and check_unchanged()
170 # The return value of exit_code_trustable() tells us whether or not we
171 # can trust the tool's exit code.
173 # User-defined and built-in tools default to false.
174 # Built-in tools advertise that their exit code is trustable by
175 # redefining exit_code_trustable() to true.
177 exit_code_trustable
() {
182 if ! test -f "$MERGE_TOOLS_DIR/$tool"
188 # Load the redefined functions
189 .
"$MERGE_TOOLS_DIR/$tool"
190 # Now let the user override the default command for the tool. If
191 # they have not done so then this will return 1 which we ignore.
194 if merge_mode
&& ! can_merge
196 echo "error: '$tool' can not be used to resolve merges" >&2
198 elif diff_mode
&& ! can_diff
200 echo "error: '$tool' can only be used to resolve merges" >&2
206 get_merge_tool_cmd
() {
210 git config
"difftool.$merge_tool.cmd" ||
211 git config
"mergetool.$merge_tool.cmd"
213 git config
"mergetool.$merge_tool.cmd"
218 if git config
--bool "mergetool.$1.trustExitCode"
221 elif exit_code_trustable
230 # Entry point for running tools
232 # If GIT_PREFIX is empty then we cannot use it in tools
233 # that expect to be able to chdir() to its value.
234 GIT_PREFIX
=${GIT_PREFIX:-.}
237 merge_tool_path
=$
(get_merge_tool_path
"$1") ||
exit
240 # Bring tool-specific functions into scope
241 setup_tool
"$1" ||
return 1
251 # Run a either a configured or built-in diff tool
256 # Run a either a configured or built-in merge tool
258 mergetool_trust_exit_code
=$
(trust_exit_code
"$1")
259 if test "$mergetool_trust_exit_code" = "true"
269 list_merge_tool_candidates
() {
272 tools
="tortoisemerge"
276 if test -n "$DISPLAY"
278 if test -n "$GNOME_DESKTOP_SESSION_ID"
280 tools
="meld opendiff kdiff3 tkdiff xxdiff $tools"
282 tools
="opendiff kdiff3 tkdiff xxdiff meld $tools"
284 tools
="$tools gvimdiff diffuse diffmerge ecmerge"
285 tools
="$tools p4merge araxis bc codecompare"
287 case "${VISUAL:-$EDITOR}" in
289 tools
="$tools vimdiff emerge"
292 tools
="$tools emerge vimdiff"
298 tool_opt
="'git ${TOOL_MODE}tool --tool=<tool>'"
305 cmd_name
=${TOOL_MODE}tool
307 diff_mode
&& list_config_tools difftool
"$tab$tab"
308 list_config_tools mergetool
"$tab$tab"
311 if test -n "$config_tools"
313 extra_content
="${tab}user-defined:${LF}$config_tools"
316 show_tool_names
'mode_ok && is_available' "$tab$tab" \
317 "$tool_opt may be set to one of the following:" \
318 "No suitable tool for 'git $cmd_name --tool=<tool>' found." \
322 show_tool_names
'mode_ok && ! is_available' "$tab$tab" \
323 "${LF}The following tools are valid, but not currently available:" &&
326 if test "$any_shown" = yes
329 echo "Some of the tools listed above only work in a windowed"
330 echo "environment. If run in a terminal-only session, they will fail."
335 guess_merge_tool
() {
336 list_merge_tool_candidates
339 This message is displayed because '$TOOL_MODE.tool' is not configured.
340 See 'git ${TOOL_MODE}tool --tool-help' or 'git help config' for more details.
341 'git ${TOOL_MODE}tool' will now attempt to use one of the following tools:
345 # Loop over each candidate and stop when a valid merge tool is found.
349 is_available
"$tool" && echo "$tool" && return 0
352 echo >&2 "No known ${TOOL_MODE} tool is available."
356 get_configured_merge_tool
() {
362 # Diff mode first tries diff.(gui)tool and falls back to merge.(gui)tool.
363 # Merge mode only checks merge.(gui)tool
366 merge_tool
=$
(git config
diff.
${gui_prefix}tool || git config merge.
${gui_prefix}tool
)
368 merge_tool
=$
(git config merge.
${gui_prefix}tool
)
370 if test -n "$merge_tool" && ! valid_tool
"$merge_tool"
372 echo >&2 "git config option $TOOL_MODE.${gui_prefix}tool set to unknown tool: $merge_tool"
373 echo >&2 "Resetting to default..."
379 get_merge_tool_path
() {
380 # A merge tool has been set, so verify that it's valid.
382 if ! valid_tool
"$merge_tool"
384 echo >&2 "Unknown merge tool $merge_tool"
389 merge_tool_path
=$
(git config difftool.
"$merge_tool".path ||
390 git config mergetool.
"$merge_tool".path
)
392 merge_tool_path
=$
(git config mergetool.
"$merge_tool".path
)
394 if test -z "$merge_tool_path"
396 merge_tool_path
=$
(translate_merge_tool_path
"$merge_tool")
398 if test -z "$(get_merge_tool_cmd "$merge_tool")" &&
399 ! type "$merge_tool_path" >/dev
/null
2>&1
401 echo >&2 "The $TOOL_MODE tool $merge_tool is not available as"\
405 echo "$merge_tool_path"
410 # Check if a merge tool has been configured
411 merge_tool
=$
(get_configured_merge_tool
)
412 # Try to guess an appropriate merge tool if no tool has been set.
413 if test -z "$merge_tool"
415 merge_tool
=$
(guess_merge_tool
) ||
exit
419 test "$is_guessed" = false
422 mergetool_find_win32_cmd
() {
426 # Use $executable if it exists in $PATH
427 if type -p "$executable" >/dev
/null
2>&1
429 printf '%s' "$executable"
433 # Look for executable in the typical locations
434 for directory
in $
(env |
grep -Ei '^PROGRAM(FILES(\(X86\))?|W6432)=' |
435 cut
-d '=' -f 2- |
sort -u)
437 if test -n "$directory" && test -x "$directory/$sub_directory/$executable"
439 printf '%s' "$directory/$sub_directory/$executable"
444 printf '%s' "$executable"