2 # git-mergetool--lib is a library for common merge tool functions
3 MERGE_TOOLS_DIR=$(git --exec-path)/mergetools
6 test "$TOOL_MODE" = diff
10 test "$TOOL_MODE" = merge
13 translate_merge_tool_path () {
18 if test "$MERGED" -nt "$BACKUP"
24 echo "$MERGED seems unchanged."
25 printf "Was the merge successful? [y/n] "
26 read answer || return 1
28 y*|Y*) status=0; break ;;
29 n*|N*) status=1; break ;;
36 setup_tool "$1" && return 0
37 cmd=$(get_merge_tool_cmd "$1")
44 # Fallback definitions, to be overriden by tools.
63 translate_merge_tool_path () {
67 if ! test -f "$MERGE_TOOLS_DIR/$tool"
69 # Use a special return code for this case since we want to
70 # source "defaults" even when an explicit tool path is
71 # configured since the user can use that to override the
72 # default path in the scriptlet.
76 # Load the redefined functions
77 . "$MERGE_TOOLS_DIR/$tool"
79 if merge_mode && ! can_merge
81 echo "error: '$tool' can not be used to resolve merges" >&2
83 elif diff_mode && ! can_diff
85 echo "error: '$tool' can only be used to resolve merges" >&2
91 get_merge_tool_cmd () {
95 git config "difftool.$merge_tool.cmd" ||
96 git config "mergetool.$merge_tool.cmd"
98 git config "mergetool.$merge_tool.cmd"
102 # Entry point for running tools
104 # If GIT_PREFIX is empty then we cannot use it in tools
105 # that expect to be able to chdir() to its value.
106 GIT_PREFIX=${GIT_PREFIX:-.}
109 merge_tool_path=$(get_merge_tool_path "$1") || exit
113 # Bring tool-specific functions into scope
121 # The configured tool is not a built-in tool.
122 test -n "$merge_tool_path" || return 1
138 # Run a either a configured or built-in diff tool
140 merge_tool_cmd=$(get_merge_tool_cmd "$1")
141 if test -n "$merge_tool_cmd"
143 ( eval $merge_tool_cmd )
151 # Run a either a configured or built-in merge tool
153 merge_tool_cmd=$(get_merge_tool_cmd "$1")
154 if test -n "$merge_tool_cmd"
156 trust_exit_code=$(git config --bool \
157 "mergetool.$1.trustExitCode" || echo false)
158 if test "$trust_exit_code" = "false"
161 ( eval $merge_tool_cmd )
165 ( eval $merge_tool_cmd )
174 list_merge_tool_candidates () {
177 tools="tortoisemerge"
181 if test -n "$DISPLAY"
183 if test -n "$GNOME_DESKTOP_SESSION_ID"
185 tools="meld opendiff kdiff3 tkdiff xxdiff $tools"
187 tools="opendiff kdiff3 tkdiff xxdiff meld $tools"
189 tools="$tools gvimdiff diffuse ecmerge p4merge araxis bc3 codecompare"
191 case "${VISUAL:-$EDITOR}" in
193 tools="$tools vimdiff emerge"
196 tools="$tools emerge vimdiff"
202 unavailable= available= LF='
204 for i in "$MERGE_TOOLS_DIR"/*
206 tool=$(basename "$i")
207 setup_tool "$tool" 2>/dev/null || continue
209 merge_tool_path=$(translate_merge_tool_path "$tool")
210 if type "$merge_tool_path" >/dev/null 2>&1
212 available="$available$tool$LF"
214 unavailable="$unavailable$tool$LF"
218 cmd_name=${TOOL_MODE}tool
219 if test -n "$available"
221 echo "'git $cmd_name --tool=<tool>' may be set to one of the following:"
222 echo "$available" | sort | sed -e 's/^/ /'
224 echo "No suitable tool for 'git $cmd_name --tool=<tool>' found."
226 if test -n "$unavailable"
229 echo 'The following tools are valid, but not currently available:'
230 echo "$unavailable" | sort | sed -e 's/^/ /'
232 if test -n "$unavailable$available"
235 echo "Some of the tools listed above only work in a windowed"
236 echo "environment. If run in a terminal-only session, they will fail."
241 guess_merge_tool () {
242 list_merge_tool_candidates
245 This message is displayed because '$TOOL_MODE.tool' is not configured.
246 See 'git ${TOOL_MODE}tool --tool-help' or 'git help config' for more details.
247 'git ${TOOL_MODE}tool' will now attempt to use one of the following tools:
251 # Loop over each candidate and stop when a valid merge tool is found.
254 merge_tool_path=$(translate_merge_tool_path "$i")
255 if type "$merge_tool_path" >/dev/null 2>&1
262 echo >&2 "No known merge resolution program available."
266 get_configured_merge_tool () {
267 # Diff mode first tries diff.tool and falls back to merge.tool.
268 # Merge mode only checks merge.tool
271 merge_tool=$(git config diff.tool || git config merge.tool)
273 merge_tool=$(git config merge.tool)
275 if test -n "$merge_tool" && ! valid_tool "$merge_tool"
277 echo >&2 "git config option $TOOL_MODE.tool set to unknown tool: $merge_tool"
278 echo >&2 "Resetting to default..."
284 get_merge_tool_path () {
285 # A merge tool has been set, so verify that it's valid.
287 if ! valid_tool "$merge_tool"
289 echo >&2 "Unknown merge tool $merge_tool"
294 merge_tool_path=$(git config difftool."$merge_tool".path ||
295 git config mergetool."$merge_tool".path)
297 merge_tool_path=$(git config mergetool."$merge_tool".path)
299 if test -z "$merge_tool_path"
301 merge_tool_path=$(translate_merge_tool_path "$merge_tool")
303 if test -z "$(get_merge_tool_cmd "$merge_tool")" &&
304 ! type "$merge_tool_path" >/dev/null 2>&1
306 echo >&2 "The $TOOL_MODE tool $merge_tool is not available as"\
310 echo "$merge_tool_path"
314 # Check if a merge tool has been configured
315 merge_tool=$(get_configured_merge_tool)
316 # Try to guess an appropriate merge tool if no tool has been set.
317 if test -z "$merge_tool"
319 merge_tool=$(guess_merge_tool) || exit