2 # git-mergetool--lib is a library for common merge tool functions
4 test "$TOOL_MODE" = diff
8 test "$TOOL_MODE" = merge
11 translate_merge_tool_path () {
16 if test "$MERGED" -nt "$BACKUP"
22 echo "$MERGED seems unchanged."
23 printf "Was the merge successful? [y/n] "
24 read answer || return 1
26 y*|Y*) status=0; break ;;
27 n*|N*) status=1; break ;;
33 valid_tool_config () {
34 if test -n "$(get_merge_tool_cmd "$1")"
43 setup_tool "$1" || valid_tool_config "$1"
55 mergetools="$(git --exec-path)/mergetools"
57 # Load the default definitions
58 . "$mergetools/defaults"
59 if ! test -f "$mergetools/$tool"
61 # Use a special return code for this case since we want to
62 # source "defaults" even when an explicit tool path is
63 # configured since the user can use that to override the
64 # default path in the scriptlet.
68 # Load the redefined functions
71 if merge_mode && ! can_merge
73 echo "error: '$tool' can not be used to resolve merges" >&2
75 elif diff_mode && ! can_diff
77 echo "error: '$tool' can only be used to resolve merges" >&2
83 get_merge_tool_cmd () {
84 # Prints the custom command for a merge tool
88 echo "$(git config difftool.$merge_tool.cmd ||
89 git config mergetool.$merge_tool.cmd)"
91 echo "$(git config mergetool.$merge_tool.cmd)"
95 # Entry point for running tools
97 # If GIT_PREFIX is empty then we cannot use it in tools
98 # that expect to be able to chdir() to its value.
99 GIT_PREFIX=${GIT_PREFIX:-.}
102 merge_tool_path="$(get_merge_tool_path "$1")" || exit
106 # Bring tool-specific functions into scope
114 # The configured tool is not a built-in tool.
115 test -n "$merge_tool_path" || return 1
131 # Run a either a configured or built-in diff tool
133 merge_tool_cmd="$(get_merge_tool_cmd "$1")"
134 if test -n "$merge_tool_cmd"
136 ( eval $merge_tool_cmd )
144 # Run a either a configured or built-in merge tool
146 merge_tool_cmd="$(get_merge_tool_cmd "$1")"
147 if test -n "$merge_tool_cmd"
149 trust_exit_code="$(git config --bool \
150 mergetool."$1".trustExitCode || echo false)"
151 if test "$trust_exit_code" = "false"
154 ( eval $merge_tool_cmd )
158 ( eval $merge_tool_cmd )
167 list_merge_tool_candidates () {
170 tools="tortoisemerge"
174 if test -n "$DISPLAY"
176 if test -n "$GNOME_DESKTOP_SESSION_ID"
178 tools="meld opendiff kdiff3 tkdiff xxdiff $tools"
180 tools="opendiff kdiff3 tkdiff xxdiff meld $tools"
182 tools="$tools gvimdiff diffuse ecmerge p4merge araxis bc3 codecompare"
184 case "${VISUAL:-$EDITOR}" in
186 tools="$tools vimdiff emerge"
189 tools="$tools emerge vimdiff"
195 unavailable= available= LF='
198 scriptlets="$(git --exec-path)"/mergetools
199 for i in "$scriptlets"/*
201 . "$scriptlets"/defaults
204 tool="$(basename "$i")"
205 if test "$tool" = "defaults"
208 elif merge_mode && ! can_merge
211 elif diff_mode && ! can_diff
216 merge_tool_path=$(translate_merge_tool_path "$tool")
217 if type "$merge_tool_path" >/dev/null 2>&1
219 available="$available$tool$LF"
221 unavailable="$unavailable$tool$LF"
225 cmd_name=${TOOL_MODE}tool
226 if test -n "$available"
228 echo "'git $cmd_name --tool=<tool>' may be set to one of the following:"
229 echo "$available" | sort | sed -e 's/^/ /'
231 echo "No suitable tool for 'git $cmd_name --tool=<tool>' found."
233 if test -n "$unavailable"
236 echo 'The following tools are valid, but not currently available:'
237 echo "$unavailable" | sort | sed -e 's/^/ /'
239 if test -n "$unavailable$available"
242 echo "Some of the tools listed above only work in a windowed"
243 echo "environment. If run in a terminal-only session, they will fail."
248 guess_merge_tool () {
249 list_merge_tool_candidates
250 echo >&2 "merge tool candidates: $tools"
252 # Loop over each candidate and stop when a valid merge tool is found.
255 merge_tool_path="$(translate_merge_tool_path "$i")"
256 if type "$merge_tool_path" >/dev/null 2>&1
263 echo >&2 "No known merge resolution program available."
267 get_configured_merge_tool () {
268 # Diff mode first tries diff.tool and falls back to merge.tool.
269 # Merge mode only checks merge.tool
272 merge_tool=$(git config diff.tool || git config merge.tool)
274 merge_tool=$(git config merge.tool)
276 if test -n "$merge_tool" && ! valid_tool "$merge_tool"
278 echo >&2 "git config option $TOOL_MODE.tool set to unknown tool: $merge_tool"
279 echo >&2 "Resetting to default..."
285 get_merge_tool_path () {
286 # A merge tool has been set, so verify that it's valid.
288 if ! valid_tool "$merge_tool"
290 echo >&2 "Unknown merge tool $merge_tool"
295 merge_tool_path=$(git config difftool."$merge_tool".path ||
296 git config mergetool."$merge_tool".path)
298 merge_tool_path=$(git config mergetool."$merge_tool".path)
300 if test -z "$merge_tool_path"
302 merge_tool_path="$(translate_merge_tool_path "$merge_tool")"
304 if test -z "$(get_merge_tool_cmd "$merge_tool")" &&
305 ! type "$merge_tool_path" >/dev/null 2>&1
307 echo >&2 "The $TOOL_MODE tool $merge_tool is not available as"\
311 echo "$merge_tool_path"
315 # Check if a merge tool has been configured
316 merge_tool="$(get_configured_merge_tool)"
317 # Try to guess an appropriate merge tool if no tool has been set.
318 if test -z "$merge_tool"
320 merge_tool="$(guess_merge_tool)" || exit