]> git.ipfire.org Git - thirdparty/git.git/blame - git-mergetool--lib.sh
path.c: don't call the match function without value in trie_find()
[thirdparty/git.git] / git-mergetool--lib.sh
CommitLineData
11d62145 1# git-mergetool--lib is a shell library for common merge tool functions
f35ec546
DA
2
3: ${MERGE_TOOLS_DIR=$(git --exec-path)/mergetools}
073678b8 4
719518f5
DA
5IFS='
6'
7
17a1f1c5
DA
8mode_ok () {
9 if diff_mode
10 then
11 can_diff
12 elif merge_mode
13 then
14 can_merge
15 else
16 false
17 fi
18}
19
20is_available () {
21 merge_tool_path=$(translate_merge_tool_path "$1") &&
22 type "$merge_tool_path" >/dev/null 2>&1
23}
24
665682c9
JK
25list_config_tools () {
26 section=$1
27 line_prefix=${2:-}
28
29 git config --get-regexp $section'\..*\.cmd' |
30 while read -r key value
31 do
32 toolname=${key#$section.}
33 toolname=${toolname%.cmd}
34
35 printf "%s%s\n" "$line_prefix" "$toolname"
36 done
37}
38
17a1f1c5
DA
39show_tool_names () {
40 condition=${1:-true} per_line_prefix=${2:-} preamble=${3:-}
41 not_found_msg=${4:-}
665682c9 42 extra_content=${5:-}
17a1f1c5
DA
43
44 shown_any=
45 ( cd "$MERGE_TOOLS_DIR" && ls ) | {
46 while read toolname
47 do
48 if setup_tool "$toolname" 2>/dev/null &&
49 (eval "$condition" "$toolname")
50 then
51 if test -n "$preamble"
52 then
53 printf "%s\n" "$preamble"
54 preamble=
55 fi
56 shown_any=yes
57 printf "%s%s\n" "$per_line_prefix" "$toolname"
58 fi
59 done
60
665682c9
JK
61 if test -n "$extra_content"
62 then
63 if test -n "$preamble"
64 then
65 # Note: no '\n' here since we don't want a
66 # blank line if there is no initial content.
67 printf "%s" "$preamble"
68 preamble=
69 fi
70 shown_any=yes
71 printf "\n%s\n" "$extra_content"
72 fi
73
17a1f1c5
DA
74 if test -n "$preamble" && test -n "$not_found_msg"
75 then
76 printf "%s\n" "$not_found_msg"
77 fi
78
79 test -n "$shown_any"
80 }
81}
82
884630b2 83diff_mode () {
21d0ba7e
DA
84 test "$TOOL_MODE" = diff
85}
86
884630b2 87merge_mode () {
21d0ba7e
DA
88 test "$TOOL_MODE" = merge
89}
90
884630b2
DL
91gui_mode () {
92 test "$GIT_MERGETOOL_GUI" = true
93}
94
21d0ba7e 95translate_merge_tool_path () {
bc7a96a8 96 echo "$1"
21d0ba7e
DA
97}
98
99check_unchanged () {
240dc3e8
DA
100 if test "$MERGED" -nt "$BACKUP"
101 then
1b6a5343 102 return 0
21d0ba7e 103 else
240dc3e8
DA
104 while true
105 do
21d0ba7e 106 echo "$MERGED seems unchanged."
cce076e3 107 printf "Was the merge successful [y/n]? "
e622f41d 108 read answer || return 1
21d0ba7e 109 case "$answer" in
1b6a5343
DA
110 y*|Y*) return 0 ;;
111 n*|N*) return 1 ;;
21d0ba7e
DA
112 esac
113 done
114 fi
115}
116
117valid_tool () {
80ff2b68
DA
118 setup_tool "$1" && return 0
119 cmd=$(get_merge_tool_cmd "$1")
120 test -n "$cmd"
bc7a96a8
DA
121}
122
d2512fc9
JK
123setup_user_tool () {
124 merge_tool_cmd=$(get_merge_tool_cmd "$tool")
125 test -n "$merge_tool_cmd" || return 1
126
127 diff_cmd () {
128 ( eval $merge_tool_cmd )
d2512fc9
JK
129 }
130
131 merge_cmd () {
7c10605d 132 ( eval $merge_tool_cmd )
d2512fc9
JK
133 }
134}
135
bc7a96a8 136setup_tool () {
073678b8
DA
137 tool="$1"
138
98e023de 139 # Fallback definitions, to be overridden by tools.
073678b8
DA
140 can_merge () {
141 return 0
142 }
143
144 can_diff () {
145 return 0
146 }
147
148 diff_cmd () {
1b6a5343 149 return 1
073678b8
DA
150 }
151
152 merge_cmd () {
1b6a5343 153 return 1
073678b8 154 }
bc7a96a8 155
073678b8
DA
156 translate_merge_tool_path () {
157 echo "$1"
158 }
159
7c10605d
DA
160 # Most tools' exit codes cannot be trusted, so By default we ignore
161 # their exit code and check the merged file's modification time in
162 # check_unchanged() to determine whether or not the merge was
163 # successful. The return value from run_merge_cmd, by default, is
164 # determined by check_unchanged().
165 #
166 # When a tool's exit code can be trusted then the return value from
167 # run_merge_cmd is simply the tool's exit code, and check_unchanged()
168 # is not called.
169 #
170 # The return value of exit_code_trustable() tells us whether or not we
171 # can trust the tool's exit code.
172 #
173 # User-defined and built-in tools default to false.
174 # Built-in tools advertise that their exit code is trustable by
175 # redefining exit_code_trustable() to true.
176
177 exit_code_trustable () {
178 false
179 }
180
181
073678b8 182 if ! test -f "$MERGE_TOOLS_DIR/$tool"
bc7a96a8 183 then
d2512fc9
JK
184 setup_user_tool
185 return $?
bc7a96a8
DA
186 fi
187
188 # Load the redefined functions
073678b8 189 . "$MERGE_TOOLS_DIR/$tool"
d2512fc9
JK
190 # Now let the user override the default command for the tool. If
191 # they have not done so then this will return 1 which we ignore.
192 setup_user_tool
bc7a96a8
DA
193
194 if merge_mode && ! can_merge
195 then
196 echo "error: '$tool' can not be used to resolve merges" >&2
62957bea 197 return 1
bc7a96a8
DA
198 elif diff_mode && ! can_diff
199 then
200 echo "error: '$tool' can only be used to resolve merges" >&2
62957bea 201 return 1
bc7a96a8
DA
202 fi
203 return 0
21d0ba7e
DA
204}
205
206get_merge_tool_cmd () {
bc7a96a8 207 merge_tool="$1"
240dc3e8
DA
208 if diff_mode
209 then
80ff2b68
DA
210 git config "difftool.$merge_tool.cmd" ||
211 git config "mergetool.$merge_tool.cmd"
47d65924 212 else
80ff2b68 213 git config "mergetool.$merge_tool.cmd"
47d65924 214 fi
21d0ba7e
DA
215}
216
7c10605d
DA
217trust_exit_code () {
218 if git config --bool "mergetool.$1.trustExitCode"
219 then
220 :; # OK
221 elif exit_code_trustable
222 then
223 echo true
224 else
225 echo false
226 fi
227}
228
229
bc7a96a8 230# Entry point for running tools
21d0ba7e 231run_merge_tool () {
f9ad901f
DA
232 # If GIT_PREFIX is empty then we cannot use it in tools
233 # that expect to be able to chdir() to its value.
234 GIT_PREFIX=${GIT_PREFIX:-.}
235 export GIT_PREFIX
236
80ff2b68 237 merge_tool_path=$(get_merge_tool_path "$1") || exit
21d0ba7e 238 base_present="$2"
21d0ba7e 239
bc7a96a8 240 # Bring tool-specific functions into scope
d2512fc9 241 setup_tool "$1" || return 1
bc7a96a8
DA
242
243 if merge_mode
244 then
a427ef7a 245 run_merge_cmd "$1"
bc7a96a8 246 else
a427ef7a 247 run_diff_cmd "$1"
bc7a96a8 248 fi
21d0ba7e
DA
249}
250
a427ef7a
DA
251# Run a either a configured or built-in diff tool
252run_diff_cmd () {
d2512fc9 253 diff_cmd "$1"
a427ef7a
DA
254}
255
256# Run a either a configured or built-in merge tool
257run_merge_cmd () {
7c10605d
DA
258 mergetool_trust_exit_code=$(trust_exit_code "$1")
259 if test "$mergetool_trust_exit_code" = "true"
260 then
261 merge_cmd "$1"
262 else
263 touch "$BACKUP"
264 merge_cmd "$1"
265 check_unchanged
266 fi
a427ef7a
DA
267}
268
109859e2 269list_merge_tool_candidates () {
240dc3e8
DA
270 if merge_mode
271 then
21d0ba7e
DA
272 tools="tortoisemerge"
273 else
274 tools="kompare"
275 fi
240dc3e8
DA
276 if test -n "$DISPLAY"
277 then
278 if test -n "$GNOME_DESKTOP_SESSION_ID"
279 then
21d0ba7e
DA
280 tools="meld opendiff kdiff3 tkdiff xxdiff $tools"
281 else
282 tools="opendiff kdiff3 tkdiff xxdiff meld $tools"
283 fi
c5f424fd 284 tools="$tools gvimdiff diffuse diffmerge ecmerge"
f13f9b0e 285 tools="$tools p4merge araxis bc codecompare"
eb12adc7 286 tools="$tools smerge"
21d0ba7e 287 fi
7b104229
RS
288 case "${VISUAL:-$EDITOR}" in
289 *vim*)
21d0ba7e 290 tools="$tools vimdiff emerge"
7b104229
RS
291 ;;
292 *)
21d0ba7e 293 tools="$tools emerge vimdiff"
7b104229
RS
294 ;;
295 esac
109859e2
JH
296}
297
4a8273a3 298show_tool_help () {
2b7ca916 299 tool_opt="'git ${TOOL_MODE}tool --tool=<tool>'"
88d3406a 300
17a1f1c5
DA
301 tab=' '
302 LF='
303'
304 any_shown=no
62b6f7e0
JK
305
306 cmd_name=${TOOL_MODE}tool
665682c9
JK
307 config_tools=$({
308 diff_mode && list_config_tools difftool "$tab$tab"
309 list_config_tools mergetool "$tab$tab"
310 } | sort)
311 extra_content=
312 if test -n "$config_tools"
313 then
314 extra_content="${tab}user-defined:${LF}$config_tools"
315 fi
316
17a1f1c5
DA
317 show_tool_names 'mode_ok && is_available' "$tab$tab" \
318 "$tool_opt may be set to one of the following:" \
665682c9
JK
319 "No suitable tool for 'git $cmd_name --tool=<tool>' found." \
320 "$extra_content" &&
17a1f1c5
DA
321 any_shown=yes
322
323 show_tool_names 'mode_ok && ! is_available' "$tab$tab" \
324 "${LF}The following tools are valid, but not currently available:" &&
325 any_shown=yes
326
327 if test "$any_shown" = yes
4a8273a3
JK
328 then
329 echo
330 echo "Some of the tools listed above only work in a windowed"
331 echo "environment. If run in a terminal-only session, they will fail."
332 fi
333 exit 0
334}
335
109859e2
JH
336guess_merge_tool () {
337 list_merge_tool_candidates
5338a6a9
DA
338 cat >&2 <<-EOF
339
340 This message is displayed because '$TOOL_MODE.tool' is not configured.
341 See 'git ${TOOL_MODE}tool --tool-help' or 'git help config' for more details.
342 'git ${TOOL_MODE}tool' will now attempt to use one of the following tools:
343 $tools
344 EOF
21d0ba7e
DA
345
346 # Loop over each candidate and stop when a valid merge tool is found.
f67986b9 347 IFS=' '
17a1f1c5 348 for tool in $tools
21d0ba7e 349 do
17a1f1c5 350 is_available "$tool" && echo "$tool" && return 0
21d0ba7e
DA
351 done
352
17a1f1c5 353 echo >&2 "No known ${TOOL_MODE} tool is available."
47d65924 354 return 1
21d0ba7e
DA
355}
356
357get_configured_merge_tool () {
60aced3d 358 keys=
240dc3e8
DA
359 if diff_mode
360 then
60aced3d
DL
361 if gui_mode
362 then
363 keys="diff.guitool merge.guitool diff.tool merge.tool"
364 else
365 keys="diff.tool merge.tool"
366 fi
47d65924 367 else
60aced3d
DL
368 if gui_mode
369 then
370 keys="merge.guitool merge.tool"
371 else
372 keys="merge.tool"
373 fi
21d0ba7e 374 fi
60aced3d
DL
375
376 merge_tool=$(
377 IFS=' '
378 for key in $keys
379 do
380 selected=$(git config $key)
381 if test -n "$selected"
382 then
383 echo "$selected"
384 return
385 fi
386 done)
387
240dc3e8
DA
388 if test -n "$merge_tool" && ! valid_tool "$merge_tool"
389 then
063f2bdb 390 echo >&2 "git config option $TOOL_MODE.${gui_prefix}tool set to unknown tool: $merge_tool"
21d0ba7e
DA
391 echo >&2 "Resetting to default..."
392 return 1
393 fi
47d65924 394 echo "$merge_tool"
21d0ba7e
DA
395}
396
397get_merge_tool_path () {
398 # A merge tool has been set, so verify that it's valid.
bc7a96a8 399 merge_tool="$1"
240dc3e8
DA
400 if ! valid_tool "$merge_tool"
401 then
21d0ba7e
DA
402 echo >&2 "Unknown merge tool $merge_tool"
403 exit 1
404 fi
240dc3e8
DA
405 if diff_mode
406 then
47d65924 407 merge_tool_path=$(git config difftool."$merge_tool".path ||
285c6cbf 408 git config mergetool."$merge_tool".path)
47d65924
DA
409 else
410 merge_tool_path=$(git config mergetool."$merge_tool".path)
21d0ba7e 411 fi
240dc3e8
DA
412 if test -z "$merge_tool_path"
413 then
80ff2b68 414 merge_tool_path=$(translate_merge_tool_path "$merge_tool")
21d0ba7e 415 fi
47d65924 416 if test -z "$(get_merge_tool_cmd "$merge_tool")" &&
240dc3e8
DA
417 ! type "$merge_tool_path" >/dev/null 2>&1
418 then
47d65924 419 echo >&2 "The $TOOL_MODE tool $merge_tool is not available as"\
285c6cbf 420 "'$merge_tool_path'"
21d0ba7e
DA
421 exit 1
422 fi
423 echo "$merge_tool_path"
424}
425
426get_merge_tool () {
05fb8726 427 is_guessed=false
21d0ba7e 428 # Check if a merge tool has been configured
80ff2b68 429 merge_tool=$(get_configured_merge_tool)
21d0ba7e 430 # Try to guess an appropriate merge tool if no tool has been set.
240dc3e8
DA
431 if test -z "$merge_tool"
432 then
80ff2b68 433 merge_tool=$(guess_merge_tool) || exit
05fb8726 434 is_guessed=true
21d0ba7e
DA
435 fi
436 echo "$merge_tool"
05fb8726 437 test "$is_guessed" = false
21d0ba7e 438}
e36d7167
JN
439
440mergetool_find_win32_cmd () {
441 executable=$1
442 sub_directory=$2
443
444 # Use $executable if it exists in $PATH
445 if type -p "$executable" >/dev/null 2>&1
446 then
447 printf '%s' "$executable"
448 return
449 fi
450
451 # Look for executable in the typical locations
452 for directory in $(env | grep -Ei '^PROGRAM(FILES(\(X86\))?|W6432)=' |
453 cut -d '=' -f 2- | sort -u)
454 do
455 if test -n "$directory" && test -x "$directory/$sub_directory/$executable"
456 then
457 printf '%s' "$directory/$sub_directory/$executable"
458 return
459 fi
460 done
461
462 printf '%s' "$executable"
463}