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