2 # git-mergetool--lib is a library for common merge tool functions
3 MERGE_TOOLS_DIR
=$
(git
--exec-path)/mergetools
18 merge_tool_path
=$
(translate_merge_tool_path
"$1") &&
19 type "$merge_tool_path" >/dev
/null
2>&1
22 list_config_tools
() {
26 git config
--get-regexp $section'\..*\.cmd' |
27 while read -r key value
29 toolname
=${key#$section.}
30 toolname
=${toolname%.cmd}
32 printf "%s%s\n" "$line_prefix" "$toolname"
37 condition
=${1:-true} per_line_prefix=${2:-} preamble=${3:-}
42 ( cd "$MERGE_TOOLS_DIR" && ls ) |
{
45 if setup_tool
"$toolname" 2>/dev
/null
&&
46 (eval "$condition" "$toolname")
48 if test -n "$preamble"
50 printf "%s\n" "$preamble"
54 printf "%s%s\n" "$per_line_prefix" "$toolname"
58 if test -n "$extra_content"
60 if test -n "$preamble"
62 # Note: no '\n' here since we don't want a
63 # blank line if there is no initial content.
64 printf "%s" "$preamble"
68 printf "\n%s\n" "$extra_content"
71 if test -n "$preamble" && test -n "$not_found_msg"
73 printf "%s\n" "$not_found_msg"
81 test "$TOOL_MODE" = diff
85 test "$TOOL_MODE" = merge
88 translate_merge_tool_path
() {
93 if test "$MERGED" -nt "$BACKUP"
99 echo "$MERGED seems unchanged."
100 printf "Was the merge successful? [y/n] "
101 read answer ||
return 1
103 y
*|Y
*) status
=0; break ;;
104 n
*|N
*) status
=1; break ;;
111 setup_tool
"$1" && return 0
112 cmd
=$
(get_merge_tool_cmd
"$1")
119 # Fallback definitions, to be overriden by tools.
138 translate_merge_tool_path
() {
142 if ! test -f "$MERGE_TOOLS_DIR/$tool"
144 # Use a special return code for this case since we want to
145 # source "defaults" even when an explicit tool path is
146 # configured since the user can use that to override the
147 # default path in the scriptlet.
151 # Load the redefined functions
152 .
"$MERGE_TOOLS_DIR/$tool"
154 if merge_mode
&& ! can_merge
156 echo "error: '$tool' can not be used to resolve merges" >&2
158 elif diff_mode
&& ! can_diff
160 echo "error: '$tool' can only be used to resolve merges" >&2
166 get_merge_tool_cmd
() {
170 git config
"difftool.$merge_tool.cmd" ||
171 git config
"mergetool.$merge_tool.cmd"
173 git config
"mergetool.$merge_tool.cmd"
177 # Entry point for running tools
179 # If GIT_PREFIX is empty then we cannot use it in tools
180 # that expect to be able to chdir() to its value.
181 GIT_PREFIX
=${GIT_PREFIX:-.}
184 merge_tool_path
=$
(get_merge_tool_path
"$1") ||
exit
188 # Bring tool-specific functions into scope
196 # The configured tool is not a built-in tool.
197 test -n "$merge_tool_path" ||
return 1
213 # Run a either a configured or built-in diff tool
215 merge_tool_cmd
=$
(get_merge_tool_cmd
"$1")
216 if test -n "$merge_tool_cmd"
218 ( eval $merge_tool_cmd )
226 # Run a either a configured or built-in merge tool
228 merge_tool_cmd
=$
(get_merge_tool_cmd
"$1")
229 if test -n "$merge_tool_cmd"
231 trust_exit_code
=$
(git config
--bool \
232 "mergetool.$1.trustExitCode" ||
echo false
)
233 if test "$trust_exit_code" = "false"
236 ( eval $merge_tool_cmd )
240 ( eval $merge_tool_cmd )
249 list_merge_tool_candidates
() {
252 tools
="tortoisemerge"
256 if test -n "$DISPLAY"
258 if test -n "$GNOME_DESKTOP_SESSION_ID"
260 tools
="meld opendiff kdiff3 tkdiff xxdiff $tools"
262 tools
="opendiff kdiff3 tkdiff xxdiff meld $tools"
264 tools
="$tools gvimdiff diffuse ecmerge p4merge araxis bc3 codecompare"
266 case "${VISUAL:-$EDITOR}" in
268 tools
="$tools vimdiff emerge"
271 tools
="$tools emerge vimdiff"
277 tool_opt
="'git ${TOOL_MODE}tool --tool-<tool>'"
284 cmd_name
=${TOOL_MODE}tool
286 diff_mode
&& list_config_tools difftool
"$tab$tab"
287 list_config_tools mergetool
"$tab$tab"
290 if test -n "$config_tools"
292 extra_content
="${tab}user-defined:${LF}$config_tools"
295 show_tool_names
'mode_ok && is_available' "$tab$tab" \
296 "$tool_opt may be set to one of the following:" \
297 "No suitable tool for 'git $cmd_name --tool=<tool>' found." \
301 show_tool_names
'mode_ok && ! is_available' "$tab$tab" \
302 "${LF}The following tools are valid, but not currently available:" &&
305 if test "$any_shown" = yes
308 echo "Some of the tools listed above only work in a windowed"
309 echo "environment. If run in a terminal-only session, they will fail."
314 guess_merge_tool
() {
315 list_merge_tool_candidates
318 This message is displayed because '$TOOL_MODE.tool' is not configured.
319 See 'git ${TOOL_MODE}tool --tool-help' or 'git help config' for more details.
320 'git ${TOOL_MODE}tool' will now attempt to use one of the following tools:
324 # Loop over each candidate and stop when a valid merge tool is found.
327 is_available
"$tool" && echo "$tool" && return 0
330 echo >&2 "No known ${TOOL_MODE} tool is available."
334 get_configured_merge_tool
() {
335 # Diff mode first tries diff.tool and falls back to merge.tool.
336 # Merge mode only checks merge.tool
339 merge_tool
=$
(git config
diff.tool || git config merge.tool
)
341 merge_tool
=$
(git config merge.tool
)
343 if test -n "$merge_tool" && ! valid_tool
"$merge_tool"
345 echo >&2 "git config option $TOOL_MODE.tool set to unknown tool: $merge_tool"
346 echo >&2 "Resetting to default..."
352 get_merge_tool_path
() {
353 # A merge tool has been set, so verify that it's valid.
355 if ! valid_tool
"$merge_tool"
357 echo >&2 "Unknown merge tool $merge_tool"
362 merge_tool_path
=$
(git config difftool.
"$merge_tool".path ||
363 git config mergetool.
"$merge_tool".path
)
365 merge_tool_path
=$
(git config mergetool.
"$merge_tool".path
)
367 if test -z "$merge_tool_path"
369 merge_tool_path
=$
(translate_merge_tool_path
"$merge_tool")
371 if test -z "$(get_merge_tool_cmd "$merge_tool")" &&
372 ! type "$merge_tool_path" >/dev
/null
2>&1
374 echo >&2 "The $TOOL_MODE tool $merge_tool is not available as"\
378 echo "$merge_tool_path"
382 # Check if a merge tool has been configured
383 merge_tool
=$
(get_configured_merge_tool
)
384 # Try to guess an appropriate merge tool if no tool has been set.
385 if test -z "$merge_tool"
387 merge_tool
=$
(guess_merge_tool
) ||
exit