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