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")
118 merge_tool_cmd
=$
(get_merge_tool_cmd
"$tool")
119 test -n "$merge_tool_cmd" ||
return 1
122 ( eval $merge_tool_cmd )
128 trust_exit_code
=$
(git config
--bool \
129 "mergetool.$1.trustExitCode" ||
echo false
)
130 if test "$trust_exit_code" = "false"
133 ( eval $merge_tool_cmd )
137 ( eval $merge_tool_cmd )
147 # Fallback definitions, to be overridden by tools.
166 translate_merge_tool_path
() {
170 if ! test -f "$MERGE_TOOLS_DIR/$tool"
176 # Load the redefined functions
177 .
"$MERGE_TOOLS_DIR/$tool"
178 # Now let the user override the default command for the tool. If
179 # they have not done so then this will return 1 which we ignore.
182 if merge_mode
&& ! can_merge
184 echo "error: '$tool' can not be used to resolve merges" >&2
186 elif diff_mode
&& ! can_diff
188 echo "error: '$tool' can only be used to resolve merges" >&2
194 get_merge_tool_cmd
() {
198 git config
"difftool.$merge_tool.cmd" ||
199 git config
"mergetool.$merge_tool.cmd"
201 git config
"mergetool.$merge_tool.cmd"
205 # Entry point for running tools
207 # If GIT_PREFIX is empty then we cannot use it in tools
208 # that expect to be able to chdir() to its value.
209 GIT_PREFIX
=${GIT_PREFIX:-.}
212 merge_tool_path
=$
(get_merge_tool_path
"$1") ||
exit
216 # Bring tool-specific functions into scope
217 setup_tool
"$1" ||
return 1
228 # Run a either a configured or built-in diff tool
233 # Run a either a configured or built-in merge tool
238 list_merge_tool_candidates
() {
241 tools
="tortoisemerge"
245 if test -n "$DISPLAY"
247 if test -n "$GNOME_DESKTOP_SESSION_ID"
249 tools
="meld opendiff kdiff3 tkdiff xxdiff $tools"
251 tools
="opendiff kdiff3 tkdiff xxdiff meld $tools"
253 tools
="$tools gvimdiff diffuse ecmerge p4merge araxis bc3 codecompare"
255 case "${VISUAL:-$EDITOR}" in
257 tools
="$tools vimdiff emerge"
260 tools
="$tools emerge vimdiff"
266 tool_opt
="'git ${TOOL_MODE}tool --tool-<tool>'"
273 cmd_name
=${TOOL_MODE}tool
275 diff_mode
&& list_config_tools difftool
"$tab$tab"
276 list_config_tools mergetool
"$tab$tab"
279 if test -n "$config_tools"
281 extra_content
="${tab}user-defined:${LF}$config_tools"
284 show_tool_names
'mode_ok && is_available' "$tab$tab" \
285 "$tool_opt may be set to one of the following:" \
286 "No suitable tool for 'git $cmd_name --tool=<tool>' found." \
290 show_tool_names
'mode_ok && ! is_available' "$tab$tab" \
291 "${LF}The following tools are valid, but not currently available:" &&
294 if test "$any_shown" = yes
297 echo "Some of the tools listed above only work in a windowed"
298 echo "environment. If run in a terminal-only session, they will fail."
303 guess_merge_tool
() {
304 list_merge_tool_candidates
307 This message is displayed because '$TOOL_MODE.tool' is not configured.
308 See 'git ${TOOL_MODE}tool --tool-help' or 'git help config' for more details.
309 'git ${TOOL_MODE}tool' will now attempt to use one of the following tools:
313 # Loop over each candidate and stop when a valid merge tool is found.
316 is_available
"$tool" && echo "$tool" && return 0
319 echo >&2 "No known ${TOOL_MODE} tool is available."
323 get_configured_merge_tool
() {
324 # Diff mode first tries diff.tool and falls back to merge.tool.
325 # Merge mode only checks merge.tool
328 merge_tool
=$
(git config
diff.tool || git config merge.tool
)
330 merge_tool
=$
(git config merge.tool
)
332 if test -n "$merge_tool" && ! valid_tool
"$merge_tool"
334 echo >&2 "git config option $TOOL_MODE.tool set to unknown tool: $merge_tool"
335 echo >&2 "Resetting to default..."
341 get_merge_tool_path
() {
342 # A merge tool has been set, so verify that it's valid.
344 if ! valid_tool
"$merge_tool"
346 echo >&2 "Unknown merge tool $merge_tool"
351 merge_tool_path
=$
(git config difftool.
"$merge_tool".path ||
352 git config mergetool.
"$merge_tool".path
)
354 merge_tool_path
=$
(git config mergetool.
"$merge_tool".path
)
356 if test -z "$merge_tool_path"
358 merge_tool_path
=$
(translate_merge_tool_path
"$merge_tool")
360 if test -z "$(get_merge_tool_cmd "$merge_tool")" &&
361 ! type "$merge_tool_path" >/dev
/null
2>&1
363 echo >&2 "The $TOOL_MODE tool $merge_tool is not available as"\
367 echo "$merge_tool_path"
371 # Check if a merge tool has been configured
372 merge_tool
=$
(get_configured_merge_tool
)
373 # Try to guess an appropriate merge tool if no tool has been set.
374 if test -z "$merge_tool"
376 merge_tool
=$
(guess_merge_tool
) ||
exit