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"
64 # Load the redefined functions
67 if merge_mode
&& ! can_merge
69 echo "error: '$tool' can not be used to resolve merges" >&2
71 elif diff_mode
&& ! can_diff
73 echo "error: '$tool' can only be used to resolve merges" >&2
79 get_merge_tool_cmd
() {
80 # Prints the custom command for a merge tool
84 echo "$(git config difftool.$merge_tool.cmd ||
85 git config mergetool.$merge_tool.cmd)"
87 echo "$(git config mergetool.$merge_tool.cmd)"
91 # Entry point for running tools
93 # If GIT_PREFIX is empty then we cannot use it in tools
94 # that expect to be able to chdir() to its value.
95 GIT_PREFIX
=${GIT_PREFIX:-.}
98 merge_tool_path
="$(get_merge_tool_path "$1")" ||
exit
102 # Bring tool-specific functions into scope
114 # Run a either a configured or built-in diff tool
116 merge_tool_cmd
="$(get_merge_tool_cmd "$1")"
117 if test -n "$merge_tool_cmd"
119 ( eval $merge_tool_cmd )
127 # Run a either a configured or built-in merge tool
129 merge_tool_cmd
="$(get_merge_tool_cmd "$1")"
130 if test -n "$merge_tool_cmd"
132 trust_exit_code
="$(git config --bool \
133 mergetool."$1".trustExitCode || echo false)"
134 if test "$trust_exit_code" = "false"
137 ( eval $merge_tool_cmd )
141 ( eval $merge_tool_cmd )
150 list_merge_tool_candidates
() {
153 tools
="tortoisemerge"
157 if test -n "$DISPLAY"
159 if test -n "$GNOME_DESKTOP_SESSION_ID"
161 tools
="meld opendiff kdiff3 tkdiff xxdiff $tools"
163 tools
="opendiff kdiff3 tkdiff xxdiff meld $tools"
165 tools
="$tools gvimdiff diffuse ecmerge p4merge araxis bc3 codecompare"
167 case "${VISUAL:-$EDITOR}" in
169 tools
="$tools vimdiff emerge"
172 tools
="$tools emerge vimdiff"
178 unavailable
= available
= LF
='
181 scriptlets
="$(git --exec-path)"/mergetools
182 for i
in "$scriptlets"/*
184 .
"$scriptlets"/defaults
187 tool
="$(basename "$i")"
188 if test "$tool" = "defaults"
191 elif merge_mode
&& ! can_merge
194 elif diff_mode
&& ! can_diff
199 merge_tool_path
=$
(translate_merge_tool_path
"$tool")
200 if type "$merge_tool_path" >/dev
/null
2>&1
202 available
="$available$tool$LF"
204 unavailable
="$unavailable$tool$LF"
208 cmd_name
=${TOOL_MODE}tool
209 if test -n "$available"
211 echo "'git $cmd_name --tool=<tool>' may be set to one of the following:"
212 echo "$available" |
sort |
sed -e 's/^/ /'
214 echo "No suitable tool for 'git $cmd_name --tool=<tool>' found."
216 if test -n "$unavailable"
219 echo 'The following tools are valid, but not currently available:'
220 echo "$unavailable" |
sort |
sed -e 's/^/ /'
222 if test -n "$unavailable$available"
225 echo "Some of the tools listed above only work in a windowed"
226 echo "environment. If run in a terminal-only session, they will fail."
231 guess_merge_tool
() {
232 list_merge_tool_candidates
233 echo >&2 "merge tool candidates: $tools"
235 # Loop over each candidate and stop when a valid merge tool is found.
238 merge_tool_path
="$(translate_merge_tool_path "$i")"
239 if type "$merge_tool_path" >/dev
/null
2>&1
246 echo >&2 "No known merge resolution program available."
250 get_configured_merge_tool
() {
251 # Diff mode first tries diff.tool and falls back to merge.tool.
252 # Merge mode only checks merge.tool
255 merge_tool
=$
(git config
diff.tool || git config merge.tool
)
257 merge_tool
=$
(git config merge.tool
)
259 if test -n "$merge_tool" && ! valid_tool
"$merge_tool"
261 echo >&2 "git config option $TOOL_MODE.tool set to unknown tool: $merge_tool"
262 echo >&2 "Resetting to default..."
268 get_merge_tool_path
() {
269 # A merge tool has been set, so verify that it's valid.
271 if ! valid_tool
"$merge_tool"
273 echo >&2 "Unknown merge tool $merge_tool"
278 merge_tool_path
=$
(git config difftool.
"$merge_tool".path ||
279 git config mergetool.
"$merge_tool".path
)
281 merge_tool_path
=$
(git config mergetool.
"$merge_tool".path
)
283 if test -z "$merge_tool_path"
285 merge_tool_path
="$(translate_merge_tool_path "$merge_tool")"
287 if test -z "$(get_merge_tool_cmd "$merge_tool")" &&
288 ! type "$merge_tool_path" >/dev
/null
2>&1
290 echo >&2 "The $TOOL_MODE tool $merge_tool is not available as"\
294 echo "$merge_tool_path"
298 # Check if a merge tool has been configured
299 merge_tool
="$(get_configured_merge_tool)"
300 # Try to guess an appropriate merge tool if no tool has been set.
301 if test -z "$merge_tool"
303 merge_tool
="$(guess_merge_tool)" ||
exit