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 ;;
35 valid_tool_config
() {
36 if test -n "$(get_merge_tool_cmd "$1")"
45 setup_tool
"$1" || valid_tool_config
"$1"
51 # Fallback definitions, to be overriden by tools.
70 translate_merge_tool_path
() {
74 if ! test -f "$MERGE_TOOLS_DIR/$tool"
76 # Use a special return code for this case since we want to
77 # source "defaults" even when an explicit tool path is
78 # configured since the user can use that to override the
79 # default path in the scriptlet.
83 # Load the redefined functions
84 .
"$MERGE_TOOLS_DIR/$tool"
86 if merge_mode
&& ! can_merge
88 echo "error: '$tool' can not be used to resolve merges" >&2
90 elif diff_mode
&& ! can_diff
92 echo "error: '$tool' can only be used to resolve merges" >&2
98 get_merge_tool_cmd
() {
99 # Prints the custom command for a merge tool
103 echo "$(git config difftool.$merge_tool.cmd ||
104 git config mergetool.$merge_tool.cmd)"
106 echo "$(git config mergetool.$merge_tool.cmd)"
110 # Entry point for running tools
112 # If GIT_PREFIX is empty then we cannot use it in tools
113 # that expect to be able to chdir() to its value.
114 GIT_PREFIX
=${GIT_PREFIX:-.}
117 merge_tool_path
="$(get_merge_tool_path "$1")" ||
exit
121 # Bring tool-specific functions into scope
129 # The configured tool is not a built-in tool.
130 test -n "$merge_tool_path" ||
return 1
146 # Run a either a configured or built-in diff tool
148 merge_tool_cmd
="$(get_merge_tool_cmd "$1")"
149 if test -n "$merge_tool_cmd"
151 ( eval $merge_tool_cmd )
159 # Run a either a configured or built-in merge tool
161 merge_tool_cmd
="$(get_merge_tool_cmd "$1")"
162 if test -n "$merge_tool_cmd"
164 trust_exit_code
="$(git config --bool \
165 mergetool."$1".trustExitCode || echo false)"
166 if test "$trust_exit_code" = "false"
169 ( eval $merge_tool_cmd )
173 ( eval $merge_tool_cmd )
182 list_merge_tool_candidates
() {
185 tools
="tortoisemerge"
189 if test -n "$DISPLAY"
191 if test -n "$GNOME_DESKTOP_SESSION_ID"
193 tools
="meld opendiff kdiff3 tkdiff xxdiff $tools"
195 tools
="opendiff kdiff3 tkdiff xxdiff meld $tools"
197 tools
="$tools gvimdiff diffuse ecmerge p4merge araxis bc3 codecompare"
199 case "${VISUAL:-$EDITOR}" in
201 tools
="$tools vimdiff emerge"
204 tools
="$tools emerge vimdiff"
210 unavailable
= available
= LF
='
212 for i
in "$MERGE_TOOLS_DIR"/*
214 tool
=$
(basename "$i")
215 setup_tool
"$tool" 2>/dev
/null ||
continue
217 merge_tool_path
=$
(translate_merge_tool_path
"$tool")
218 if type "$merge_tool_path" >/dev
/null
2>&1
220 available
="$available$tool$LF"
222 unavailable
="$unavailable$tool$LF"
226 cmd_name
=${TOOL_MODE}tool
227 if test -n "$available"
229 echo "'git $cmd_name --tool=<tool>' may be set to one of the following:"
230 echo "$available" |
sort |
sed -e 's/^/ /'
232 echo "No suitable tool for 'git $cmd_name --tool=<tool>' found."
234 if test -n "$unavailable"
237 echo 'The following tools are valid, but not currently available:'
238 echo "$unavailable" |
sort |
sed -e 's/^/ /'
240 if test -n "$unavailable$available"
243 echo "Some of the tools listed above only work in a windowed"
244 echo "environment. If run in a terminal-only session, they will fail."
249 guess_merge_tool
() {
250 list_merge_tool_candidates
251 echo >&2 "merge tool candidates: $tools"
253 # Loop over each candidate and stop when a valid merge tool is found.
256 merge_tool_path
="$(translate_merge_tool_path "$i")"
257 if type "$merge_tool_path" >/dev
/null
2>&1
264 echo >&2 "No known merge resolution program available."
268 get_configured_merge_tool
() {
269 # Diff mode first tries diff.tool and falls back to merge.tool.
270 # Merge mode only checks merge.tool
273 merge_tool
=$
(git config
diff.tool || git config merge.tool
)
275 merge_tool
=$
(git config merge.tool
)
277 if test -n "$merge_tool" && ! valid_tool
"$merge_tool"
279 echo >&2 "git config option $TOOL_MODE.tool set to unknown tool: $merge_tool"
280 echo >&2 "Resetting to default..."
286 get_merge_tool_path
() {
287 # A merge tool has been set, so verify that it's valid.
289 if ! valid_tool
"$merge_tool"
291 echo >&2 "Unknown merge tool $merge_tool"
296 merge_tool_path
=$
(git config difftool.
"$merge_tool".path ||
297 git config mergetool.
"$merge_tool".path
)
299 merge_tool_path
=$
(git config mergetool.
"$merge_tool".path
)
301 if test -z "$merge_tool_path"
303 merge_tool_path
="$(translate_merge_tool_path "$merge_tool")"
305 if test -z "$(get_merge_tool_cmd "$merge_tool")" &&
306 ! type "$merge_tool_path" >/dev
/null
2>&1
308 echo >&2 "The $TOOL_MODE tool $merge_tool is not available as"\
312 echo "$merge_tool_path"
316 # Check if a merge tool has been configured
317 merge_tool
="$(get_configured_merge_tool)"
318 # Try to guess an appropriate merge tool if no tool has been set.
319 if test -z "$merge_tool"
321 merge_tool
="$(guess_merge_tool)" ||
exit