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
243 echo >&2 "merge tool candidates: $tools"
245 # Loop over each candidate and stop when a valid merge tool is found.
248 merge_tool_path
=$
(translate_merge_tool_path
"$i")
249 if type "$merge_tool_path" >/dev
/null
2>&1
256 echo >&2 "No known merge resolution program available."
260 get_configured_merge_tool
() {
261 # Diff mode first tries diff.tool and falls back to merge.tool.
262 # Merge mode only checks merge.tool
265 merge_tool
=$
(git config
diff.tool || git config merge.tool
)
267 merge_tool
=$
(git config merge.tool
)
269 if test -n "$merge_tool" && ! valid_tool
"$merge_tool"
271 echo >&2 "git config option $TOOL_MODE.tool set to unknown tool: $merge_tool"
272 echo >&2 "Resetting to default..."
278 get_merge_tool_path
() {
279 # A merge tool has been set, so verify that it's valid.
281 if ! valid_tool
"$merge_tool"
283 echo >&2 "Unknown merge tool $merge_tool"
288 merge_tool_path
=$
(git config difftool.
"$merge_tool".path ||
289 git config mergetool.
"$merge_tool".path
)
291 merge_tool_path
=$
(git config mergetool.
"$merge_tool".path
)
293 if test -z "$merge_tool_path"
295 merge_tool_path
=$
(translate_merge_tool_path
"$merge_tool")
297 if test -z "$(get_merge_tool_cmd "$merge_tool")" &&
298 ! type "$merge_tool_path" >/dev
/null
2>&1
300 echo >&2 "The $TOOL_MODE tool $merge_tool is not available as"\
304 echo "$merge_tool_path"
308 # Check if a merge tool has been configured
309 merge_tool
=$
(get_configured_merge_tool
)
310 # Try to guess an appropriate merge tool if no tool has been set.
311 if test -z "$merge_tool"
313 merge_tool
=$
(guess_merge_tool
) ||
exit