2 # git-mergetool--lib is a library for common merge tool functions
4 : ${MERGE_TOOLS_DIR=$(git --exec-path)/mergetools}
19 merge_tool_path
=$
(translate_merge_tool_path
"$1") &&
20 type "$merge_tool_path" >/dev
/null
2>&1
23 list_config_tools
() {
27 git config
--get-regexp $section'\..*\.cmd' |
28 while read -r key value
30 toolname
=${key#$section.}
31 toolname
=${toolname%.cmd}
33 printf "%s%s\n" "$line_prefix" "$toolname"
38 condition
=${1:-true} per_line_prefix=${2:-} preamble=${3:-}
43 ( cd "$MERGE_TOOLS_DIR" && ls ) |
{
46 if setup_tool
"$toolname" 2>/dev
/null
&&
47 (eval "$condition" "$toolname")
49 if test -n "$preamble"
51 printf "%s\n" "$preamble"
55 printf "%s%s\n" "$per_line_prefix" "$toolname"
59 if test -n "$extra_content"
61 if test -n "$preamble"
63 # Note: no '\n' here since we don't want a
64 # blank line if there is no initial content.
65 printf "%s" "$preamble"
69 printf "\n%s\n" "$extra_content"
72 if test -n "$preamble" && test -n "$not_found_msg"
74 printf "%s\n" "$not_found_msg"
82 test "$TOOL_MODE" = diff
86 test "$TOOL_MODE" = merge
89 translate_merge_tool_path
() {
94 if test "$MERGED" -nt "$BACKUP"
100 echo "$MERGED seems unchanged."
101 printf "Was the merge successful? [y/n] "
102 read answer ||
return 1
104 y
*|Y
*) status
=0; break ;;
105 n
*|N
*) status
=1; break ;;
112 setup_tool
"$1" && return 0
113 cmd
=$
(get_merge_tool_cmd
"$1")
120 # Fallback definitions, to be overriden by tools.
139 translate_merge_tool_path
() {
143 if ! test -f "$MERGE_TOOLS_DIR/$tool"
145 # Use a special return code for this case since we want to
146 # source "defaults" even when an explicit tool path is
147 # configured since the user can use that to override the
148 # default path in the scriptlet.
152 # Load the redefined functions
153 .
"$MERGE_TOOLS_DIR/$tool"
155 if merge_mode
&& ! can_merge
157 echo "error: '$tool' can not be used to resolve merges" >&2
159 elif diff_mode
&& ! can_diff
161 echo "error: '$tool' can only be used to resolve merges" >&2
167 get_merge_tool_cmd
() {
171 git config
"difftool.$merge_tool.cmd" ||
172 git config
"mergetool.$merge_tool.cmd"
174 git config
"mergetool.$merge_tool.cmd"
178 # Entry point for running tools
180 # If GIT_PREFIX is empty then we cannot use it in tools
181 # that expect to be able to chdir() to its value.
182 GIT_PREFIX
=${GIT_PREFIX:-.}
185 merge_tool_path
=$
(get_merge_tool_path
"$1") ||
exit
189 # Bring tool-specific functions into scope
197 # The configured tool is not a built-in tool.
198 test -n "$merge_tool_path" ||
return 1
214 # Run a either a configured or built-in diff tool
216 merge_tool_cmd
=$
(get_merge_tool_cmd
"$1")
217 if test -n "$merge_tool_cmd"
219 ( eval $merge_tool_cmd )
227 # Run a either a configured or built-in merge tool
229 merge_tool_cmd
=$
(get_merge_tool_cmd
"$1")
230 if test -n "$merge_tool_cmd"
232 trust_exit_code
=$
(git config
--bool \
233 "mergetool.$1.trustExitCode" ||
echo false
)
234 if test "$trust_exit_code" = "false"
237 ( eval $merge_tool_cmd )
241 ( eval $merge_tool_cmd )
250 list_merge_tool_candidates
() {
253 tools
="tortoisemerge"
257 if test -n "$DISPLAY"
259 if test -n "$GNOME_DESKTOP_SESSION_ID"
261 tools
="meld opendiff kdiff3 tkdiff xxdiff $tools"
263 tools
="opendiff kdiff3 tkdiff xxdiff meld $tools"
265 tools
="$tools gvimdiff diffuse ecmerge p4merge araxis bc3 codecompare"
267 case "${VISUAL:-$EDITOR}" in
269 tools
="$tools vimdiff emerge"
272 tools
="$tools emerge vimdiff"
278 tool_opt
="'git ${TOOL_MODE}tool --tool-<tool>'"
285 cmd_name
=${TOOL_MODE}tool
287 diff_mode
&& list_config_tools difftool
"$tab$tab"
288 list_config_tools mergetool
"$tab$tab"
291 if test -n "$config_tools"
293 extra_content
="${tab}user-defined:${LF}$config_tools"
296 show_tool_names
'mode_ok && is_available' "$tab$tab" \
297 "$tool_opt may be set to one of the following:" \
298 "No suitable tool for 'git $cmd_name --tool=<tool>' found." \
302 show_tool_names
'mode_ok && ! is_available' "$tab$tab" \
303 "${LF}The following tools are valid, but not currently available:" &&
306 if test "$any_shown" = yes
309 echo "Some of the tools listed above only work in a windowed"
310 echo "environment. If run in a terminal-only session, they will fail."
315 guess_merge_tool
() {
316 list_merge_tool_candidates
319 This message is displayed because '$TOOL_MODE.tool' is not configured.
320 See 'git ${TOOL_MODE}tool --tool-help' or 'git help config' for more details.
321 'git ${TOOL_MODE}tool' will now attempt to use one of the following tools:
325 # Loop over each candidate and stop when a valid merge tool is found.
328 is_available
"$tool" && echo "$tool" && return 0
331 echo >&2 "No known ${TOOL_MODE} tool is available."
335 get_configured_merge_tool
() {
336 # Diff mode first tries diff.tool and falls back to merge.tool.
337 # Merge mode only checks merge.tool
340 merge_tool
=$
(git config
diff.tool || git config merge.tool
)
342 merge_tool
=$
(git config merge.tool
)
344 if test -n "$merge_tool" && ! valid_tool
"$merge_tool"
346 echo >&2 "git config option $TOOL_MODE.tool set to unknown tool: $merge_tool"
347 echo >&2 "Resetting to default..."
353 get_merge_tool_path
() {
354 # A merge tool has been set, so verify that it's valid.
356 if ! valid_tool
"$merge_tool"
358 echo >&2 "Unknown merge tool $merge_tool"
363 merge_tool_path
=$
(git config difftool.
"$merge_tool".path ||
364 git config mergetool.
"$merge_tool".path
)
366 merge_tool_path
=$
(git config mergetool.
"$merge_tool".path
)
368 if test -z "$merge_tool_path"
370 merge_tool_path
=$
(translate_merge_tool_path
"$merge_tool")
372 if test -z "$(get_merge_tool_cmd "$merge_tool")" &&
373 ! type "$merge_tool_path" >/dev
/null
2>&1
375 echo >&2 "The $TOOL_MODE tool $merge_tool is not available as"\
379 echo "$merge_tool_path"
383 # Check if a merge tool has been configured
384 merge_tool
=$
(get_configured_merge_tool
)
385 # Try to guess an appropriate merge tool if no tool has been set.
386 if test -z "$merge_tool"
388 merge_tool
=$
(guess_merge_tool
) ||
exit