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
91 translate_merge_tool_path
() {
96 if test "$MERGED" -nt "$BACKUP"
102 echo "$MERGED seems unchanged."
103 printf "Was the merge successful [y/n]? "
104 read answer ||
return 1
114 setup_tool
"$1" && return 0
115 cmd
=$
(get_merge_tool_cmd
"$1")
120 merge_tool_cmd
=$
(get_merge_tool_cmd
"$tool")
121 test -n "$merge_tool_cmd" ||
return 1
124 ( eval $merge_tool_cmd )
128 ( eval $merge_tool_cmd )
135 # Fallback definitions, to be overridden by tools.
152 translate_merge_tool_path
() {
156 # Most tools' exit codes cannot be trusted, so By default we ignore
157 # their exit code and check the merged file's modification time in
158 # check_unchanged() to determine whether or not the merge was
159 # successful. The return value from run_merge_cmd, by default, is
160 # determined by check_unchanged().
162 # When a tool's exit code can be trusted then the return value from
163 # run_merge_cmd is simply the tool's exit code, and check_unchanged()
166 # The return value of exit_code_trustable() tells us whether or not we
167 # can trust the tool's exit code.
169 # User-defined and built-in tools default to false.
170 # Built-in tools advertise that their exit code is trustable by
171 # redefining exit_code_trustable() to true.
173 exit_code_trustable
() {
178 if ! test -f "$MERGE_TOOLS_DIR/$tool"
184 # Load the redefined functions
185 .
"$MERGE_TOOLS_DIR/$tool"
186 # Now let the user override the default command for the tool. If
187 # they have not done so then this will return 1 which we ignore.
190 if merge_mode
&& ! can_merge
192 echo "error: '$tool' can not be used to resolve merges" >&2
194 elif diff_mode
&& ! can_diff
196 echo "error: '$tool' can only be used to resolve merges" >&2
202 get_merge_tool_cmd
() {
206 git config
"difftool.$merge_tool.cmd" ||
207 git config
"mergetool.$merge_tool.cmd"
209 git config
"mergetool.$merge_tool.cmd"
214 if git config
--bool "mergetool.$1.trustExitCode"
217 elif exit_code_trustable
226 # Entry point for running tools
228 # If GIT_PREFIX is empty then we cannot use it in tools
229 # that expect to be able to chdir() to its value.
230 GIT_PREFIX
=${GIT_PREFIX:-.}
233 merge_tool_path
=$
(get_merge_tool_path
"$1") ||
exit
236 # Bring tool-specific functions into scope
237 setup_tool
"$1" ||
return 1
247 # Run a either a configured or built-in diff tool
252 # Run a either a configured or built-in merge tool
254 mergetool_trust_exit_code
=$
(trust_exit_code
"$1")
255 if test "$mergetool_trust_exit_code" = "true"
265 list_merge_tool_candidates
() {
268 tools
="tortoisemerge"
272 if test -n "$DISPLAY"
274 if test -n "$GNOME_DESKTOP_SESSION_ID"
276 tools
="meld opendiff kdiff3 tkdiff xxdiff $tools"
278 tools
="opendiff kdiff3 tkdiff xxdiff meld $tools"
280 tools
="$tools gvimdiff diffuse diffmerge ecmerge"
281 tools
="$tools p4merge araxis bc codecompare"
283 case "${VISUAL:-$EDITOR}" in
285 tools
="$tools vimdiff emerge"
288 tools
="$tools emerge vimdiff"
294 tool_opt
="'git ${TOOL_MODE}tool --tool=<tool>'"
301 cmd_name
=${TOOL_MODE}tool
303 diff_mode
&& list_config_tools difftool
"$tab$tab"
304 list_config_tools mergetool
"$tab$tab"
307 if test -n "$config_tools"
309 extra_content
="${tab}user-defined:${LF}$config_tools"
312 show_tool_names
'mode_ok && is_available' "$tab$tab" \
313 "$tool_opt may be set to one of the following:" \
314 "No suitable tool for 'git $cmd_name --tool=<tool>' found." \
318 show_tool_names
'mode_ok && ! is_available' "$tab$tab" \
319 "${LF}The following tools are valid, but not currently available:" &&
322 if test "$any_shown" = yes
325 echo "Some of the tools listed above only work in a windowed"
326 echo "environment. If run in a terminal-only session, they will fail."
331 guess_merge_tool
() {
332 list_merge_tool_candidates
335 This message is displayed because '$TOOL_MODE.tool' is not configured.
336 See 'git ${TOOL_MODE}tool --tool-help' or 'git help config' for more details.
337 'git ${TOOL_MODE}tool' will now attempt to use one of the following tools:
341 # Loop over each candidate and stop when a valid merge tool is found.
345 is_available
"$tool" && echo "$tool" && return 0
348 echo >&2 "No known ${TOOL_MODE} tool is available."
352 get_configured_merge_tool
() {
353 # Diff mode first tries diff.tool and falls back to merge.tool.
354 # Merge mode only checks merge.tool
357 merge_tool
=$
(git config
diff.tool || git config merge.tool
)
359 merge_tool
=$
(git config merge.tool
)
361 if test -n "$merge_tool" && ! valid_tool
"$merge_tool"
363 echo >&2 "git config option $TOOL_MODE.tool set to unknown tool: $merge_tool"
364 echo >&2 "Resetting to default..."
370 get_merge_tool_path
() {
371 # A merge tool has been set, so verify that it's valid.
373 if ! valid_tool
"$merge_tool"
375 echo >&2 "Unknown merge tool $merge_tool"
380 merge_tool_path
=$
(git config difftool.
"$merge_tool".path ||
381 git config mergetool.
"$merge_tool".path
)
383 merge_tool_path
=$
(git config mergetool.
"$merge_tool".path
)
385 if test -z "$merge_tool_path"
387 merge_tool_path
=$
(translate_merge_tool_path
"$merge_tool")
389 if test -z "$(get_merge_tool_cmd "$merge_tool")" &&
390 ! type "$merge_tool_path" >/dev
/null
2>&1
392 echo >&2 "The $TOOL_MODE tool $merge_tool is not available as"\
396 echo "$merge_tool_path"
400 # Check if a merge tool has been configured
401 merge_tool
=$
(get_configured_merge_tool
)
402 # Try to guess an appropriate merge tool if no tool has been set.
403 if test -z "$merge_tool"
405 merge_tool
=$
(guess_merge_tool
) ||
exit
410 mergetool_find_win32_cmd
() {
414 # Use $executable if it exists in $PATH
415 if type -p "$executable" >/dev
/null
2>&1
417 printf '%s' "$executable"
421 # Look for executable in the typical locations
422 for directory
in $
(env |
grep -Ei '^PROGRAM(FILES(\(X86\))?|W6432)=' |
423 cut
-d '=' -f 2- |
sort -u)
425 if test -n "$directory" && test -x "$directory/$sub_directory/$executable"
427 printf '%s' "$directory/$sub_directory/$executable"
432 printf '%s' "$executable"