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