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 trust_exit_code=$(git config --bool \
129 "mergetool.$1.trustExitCode" || echo false)
130 if test "$trust_exit_code" = "false"
133 ( eval $merge_tool_cmd )
136 ( eval $merge_tool_cmd )
144 # Fallback definitions, to be overridden by tools.
161 translate_merge_tool_path () {
165 if ! test -f "$MERGE_TOOLS_DIR/$tool"
171 # Load the redefined functions
172 . "$MERGE_TOOLS_DIR/$tool"
173 # Now let the user override the default command for the tool. If
174 # they have not done so then this will return 1 which we ignore.
177 if merge_mode && ! can_merge
179 echo "error: '$tool' can not be used to resolve merges" >&2
181 elif diff_mode && ! can_diff
183 echo "error: '$tool' can only be used to resolve merges" >&2
189 get_merge_tool_cmd () {
193 git config "difftool.$merge_tool.cmd" ||
194 git config "mergetool.$merge_tool.cmd"
196 git config "mergetool.$merge_tool.cmd"
200 # Entry point for running tools
202 # If GIT_PREFIX is empty then we cannot use it in tools
203 # that expect to be able to chdir() to its value.
204 GIT_PREFIX=${GIT_PREFIX:-.}
207 merge_tool_path=$(get_merge_tool_path "$1") || exit
210 # Bring tool-specific functions into scope
211 setup_tool "$1" || return 1
221 # Run a either a configured or built-in diff tool
226 # Run a either a configured or built-in merge tool
231 list_merge_tool_candidates () {
234 tools="tortoisemerge"
238 if test -n "$DISPLAY"
240 if test -n "$GNOME_DESKTOP_SESSION_ID"
242 tools="meld opendiff kdiff3 tkdiff xxdiff $tools"
244 tools="opendiff kdiff3 tkdiff xxdiff meld $tools"
246 tools="$tools gvimdiff diffuse diffmerge ecmerge"
247 tools="$tools p4merge araxis bc codecompare"
249 case "${VISUAL:-$EDITOR}" in
251 tools="$tools vimdiff emerge"
254 tools="$tools emerge vimdiff"
260 tool_opt="'git ${TOOL_MODE}tool --tool=<tool>'"
267 cmd_name=${TOOL_MODE}tool
269 diff_mode && list_config_tools difftool "$tab$tab"
270 list_config_tools mergetool "$tab$tab"
273 if test -n "$config_tools"
275 extra_content="${tab}user-defined:${LF}$config_tools"
278 show_tool_names 'mode_ok && is_available' "$tab$tab" \
279 "$tool_opt may be set to one of the following:" \
280 "No suitable tool for 'git $cmd_name --tool=<tool>' found." \
284 show_tool_names 'mode_ok && ! is_available' "$tab$tab" \
285 "${LF}The following tools are valid, but not currently available:" &&
288 if test "$any_shown" = yes
291 echo "Some of the tools listed above only work in a windowed"
292 echo "environment. If run in a terminal-only session, they will fail."
297 guess_merge_tool () {
298 list_merge_tool_candidates
301 This message is displayed because '$TOOL_MODE.tool' is not configured.
302 See 'git ${TOOL_MODE}tool --tool-help' or 'git help config' for more details.
303 'git ${TOOL_MODE}tool' will now attempt to use one of the following tools:
307 # Loop over each candidate and stop when a valid merge tool is found.
311 is_available "$tool" && echo "$tool" && return 0
314 echo >&2 "No known ${TOOL_MODE} tool is available."
318 get_configured_merge_tool () {
319 # Diff mode first tries diff.tool and falls back to merge.tool.
320 # Merge mode only checks merge.tool
323 merge_tool=$(git config diff.tool || git config merge.tool)
325 merge_tool=$(git config merge.tool)
327 if test -n "$merge_tool" && ! valid_tool "$merge_tool"
329 echo >&2 "git config option $TOOL_MODE.tool set to unknown tool: $merge_tool"
330 echo >&2 "Resetting to default..."
336 get_merge_tool_path () {
337 # A merge tool has been set, so verify that it's valid.
339 if ! valid_tool "$merge_tool"
341 echo >&2 "Unknown merge tool $merge_tool"
346 merge_tool_path=$(git config difftool."$merge_tool".path ||
347 git config mergetool."$merge_tool".path)
349 merge_tool_path=$(git config mergetool."$merge_tool".path)
351 if test -z "$merge_tool_path"
353 merge_tool_path=$(translate_merge_tool_path "$merge_tool")
355 if test -z "$(get_merge_tool_cmd "$merge_tool")" &&
356 ! type "$merge_tool_path" >/dev/null 2>&1
358 echo >&2 "The $TOOL_MODE tool $merge_tool is not available as"\
362 echo "$merge_tool_path"
366 # Check if a merge tool has been configured
367 merge_tool=$(get_configured_merge_tool)
368 # Try to guess an appropriate merge tool if no tool has been set.
369 if test -z "$merge_tool"
371 merge_tool=$(guess_merge_tool) || exit