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
23 condition
=${1:-true} per_line_prefix=${2:-} preamble=${3:-}
27 ( cd "$MERGE_TOOLS_DIR" && ls ) |
{
30 if setup_tool
"$toolname" 2>/dev
/null
&&
31 (eval "$condition" "$toolname")
33 if test -n "$preamble"
35 printf "%s\n" "$preamble"
39 printf "%s%s\n" "$per_line_prefix" "$toolname"
43 if test -n "$preamble" && test -n "$not_found_msg"
45 printf "%s\n" "$not_found_msg"
53 test "$TOOL_MODE" = diff
57 test "$TOOL_MODE" = merge
60 translate_merge_tool_path
() {
65 if test "$MERGED" -nt "$BACKUP"
71 echo "$MERGED seems unchanged."
72 printf "Was the merge successful? [y/n] "
73 read answer ||
return 1
75 y
*|Y
*) status
=0; break ;;
76 n
*|N
*) status
=1; break ;;
83 setup_tool
"$1" && return 0
84 cmd
=$
(get_merge_tool_cmd
"$1")
91 # Fallback definitions, to be overriden by tools.
110 translate_merge_tool_path
() {
114 if ! test -f "$MERGE_TOOLS_DIR/$tool"
116 # Use a special return code for this case since we want to
117 # source "defaults" even when an explicit tool path is
118 # configured since the user can use that to override the
119 # default path in the scriptlet.
123 # Load the redefined functions
124 .
"$MERGE_TOOLS_DIR/$tool"
126 if merge_mode
&& ! can_merge
128 echo "error: '$tool' can not be used to resolve merges" >&2
130 elif diff_mode
&& ! can_diff
132 echo "error: '$tool' can only be used to resolve merges" >&2
138 get_merge_tool_cmd
() {
142 git config
"difftool.$merge_tool.cmd" ||
143 git config
"mergetool.$merge_tool.cmd"
145 git config
"mergetool.$merge_tool.cmd"
149 # Entry point for running tools
151 # If GIT_PREFIX is empty then we cannot use it in tools
152 # that expect to be able to chdir() to its value.
153 GIT_PREFIX
=${GIT_PREFIX:-.}
156 merge_tool_path
=$
(get_merge_tool_path
"$1") ||
exit
160 # Bring tool-specific functions into scope
168 # The configured tool is not a built-in tool.
169 test -n "$merge_tool_path" ||
return 1
185 # Run a either a configured or built-in diff tool
187 merge_tool_cmd
=$
(get_merge_tool_cmd
"$1")
188 if test -n "$merge_tool_cmd"
190 ( eval $merge_tool_cmd )
198 # Run a either a configured or built-in merge tool
200 merge_tool_cmd
=$
(get_merge_tool_cmd
"$1")
201 if test -n "$merge_tool_cmd"
203 trust_exit_code
=$
(git config
--bool \
204 "mergetool.$1.trustExitCode" ||
echo false
)
205 if test "$trust_exit_code" = "false"
208 ( eval $merge_tool_cmd )
212 ( eval $merge_tool_cmd )
221 list_merge_tool_candidates
() {
224 tools
="tortoisemerge"
228 if test -n "$DISPLAY"
230 if test -n "$GNOME_DESKTOP_SESSION_ID"
232 tools
="meld opendiff kdiff3 tkdiff xxdiff $tools"
234 tools
="opendiff kdiff3 tkdiff xxdiff meld $tools"
236 tools
="$tools gvimdiff diffuse ecmerge p4merge araxis bc3 codecompare"
238 case "${VISUAL:-$EDITOR}" in
240 tools
="$tools vimdiff emerge"
243 tools
="$tools emerge vimdiff"
249 tool_opt
="'git ${TOOL_MODE}tool --tool-<tool>'"
256 cmd_name
=${TOOL_MODE}tool
257 show_tool_names
'mode_ok && is_available' "$tab$tab" \
258 "$tool_opt may be set to one of the following:" \
259 "No suitable tool for 'git $cmd_name --tool=<tool>' found." &&
262 show_tool_names
'mode_ok && ! is_available' "$tab$tab" \
263 "${LF}The following tools are valid, but not currently available:" &&
266 if test "$any_shown" = yes
269 echo "Some of the tools listed above only work in a windowed"
270 echo "environment. If run in a terminal-only session, they will fail."
275 guess_merge_tool
() {
276 list_merge_tool_candidates
279 This message is displayed because '$TOOL_MODE.tool' is not configured.
280 See 'git ${TOOL_MODE}tool --tool-help' or 'git help config' for more details.
281 'git ${TOOL_MODE}tool' will now attempt to use one of the following tools:
285 # Loop over each candidate and stop when a valid merge tool is found.
288 is_available
"$tool" && echo "$tool" && return 0
291 echo >&2 "No known ${TOOL_MODE} tool is available."
295 get_configured_merge_tool
() {
296 # Diff mode first tries diff.tool and falls back to merge.tool.
297 # Merge mode only checks merge.tool
300 merge_tool
=$
(git config
diff.tool || git config merge.tool
)
302 merge_tool
=$
(git config merge.tool
)
304 if test -n "$merge_tool" && ! valid_tool
"$merge_tool"
306 echo >&2 "git config option $TOOL_MODE.tool set to unknown tool: $merge_tool"
307 echo >&2 "Resetting to default..."
313 get_merge_tool_path
() {
314 # A merge tool has been set, so verify that it's valid.
316 if ! valid_tool
"$merge_tool"
318 echo >&2 "Unknown merge tool $merge_tool"
323 merge_tool_path
=$
(git config difftool.
"$merge_tool".path ||
324 git config mergetool.
"$merge_tool".path
)
326 merge_tool_path
=$
(git config mergetool.
"$merge_tool".path
)
328 if test -z "$merge_tool_path"
330 merge_tool_path
=$
(translate_merge_tool_path
"$merge_tool")
332 if test -z "$(get_merge_tool_cmd "$merge_tool")" &&
333 ! type "$merge_tool_path" >/dev
/null
2>&1
335 echo >&2 "The $TOOL_MODE tool $merge_tool is not available as"\
339 echo "$merge_tool_path"
343 # Check if a merge tool has been configured
344 merge_tool
=$
(get_configured_merge_tool
)
345 # Try to guess an appropriate merge tool if no tool has been set.
346 if test -z "$merge_tool"
348 merge_tool
=$
(guess_merge_tool
) ||
exit