David Aguilar | 5c38ea3 | 2009-01-16 00:00:02 -0800 | [diff] [blame] | 1 | #!/bin/sh |
David Aguilar | afcbc8e | 2009-04-07 01:21:20 -0700 | [diff] [blame] | 2 | # git-difftool--helper is a GIT_EXTERNAL_DIFF-compatible diff tool launcher. |
David Aguilar | 5c38ea3 | 2009-01-16 00:00:02 -0800 | [diff] [blame] | 3 | # This script is typically launched by using the 'git difftool' |
| 4 | # convenience command. |
| 5 | # |
David Aguilar | c8a5672 | 2010-01-15 19:10:03 -0800 | [diff] [blame] | 6 | # Copyright (c) 2009, 2010 David Aguilar |
David Aguilar | 5c38ea3 | 2009-01-16 00:00:02 -0800 | [diff] [blame] | 7 | |
David Aguilar | 21d0ba7 | 2009-04-08 00:17:20 -0700 | [diff] [blame] | 8 | TOOL_MODE=diff |
| 9 | . git-mergetool--lib |
| 10 | |
David Aguilar | a904392 | 2009-04-07 01:21:22 -0700 | [diff] [blame] | 11 | # difftool.prompt controls the default prompt/no-prompt behavior |
| 12 | # and is overridden with $GIT_DIFFTOOL*_PROMPT. |
David Aguilar | 5c38ea3 | 2009-01-16 00:00:02 -0800 | [diff] [blame] | 13 | should_prompt () { |
Sebastian Schuberth | 4cacc62 | 2010-01-22 17:36:36 +0100 | [diff] [blame] | 14 | prompt_merge=$(git config --bool mergetool.prompt || echo true) |
| 15 | prompt=$(git config --bool difftool.prompt || echo $prompt_merge) |
David Aguilar | fdd7aa1 | 2011-08-18 00:23:44 -0700 | [diff] [blame] | 16 | if test "$prompt" = true |
| 17 | then |
David Aguilar | a904392 | 2009-04-07 01:21:22 -0700 | [diff] [blame] | 18 | test -z "$GIT_DIFFTOOL_NO_PROMPT" |
| 19 | else |
| 20 | test -n "$GIT_DIFFTOOL_PROMPT" |
| 21 | fi |
David Aguilar | 5c38ea3 | 2009-01-16 00:00:02 -0800 | [diff] [blame] | 22 | } |
| 23 | |
David Aguilar | 1c6f5b5 | 2010-01-09 20:02:42 -0800 | [diff] [blame] | 24 | # Indicates that --extcmd=... was specified |
| 25 | use_ext_cmd () { |
| 26 | test -n "$GIT_DIFFTOOL_EXTCMD" |
| 27 | } |
| 28 | |
David Aguilar | 5c38ea3 | 2009-01-16 00:00:02 -0800 | [diff] [blame] | 29 | launch_merge_tool () { |
| 30 | # Merged is the filename as it appears in the work tree |
| 31 | # Local is the contents of a/filename |
| 32 | # Remote is the contents of b/filename |
| 33 | # Custom merge tool commands might use $BASE so we provide it |
| 34 | MERGED="$1" |
| 35 | LOCAL="$2" |
| 36 | REMOTE="$3" |
| 37 | BASE="$1" |
David Aguilar | 5c38ea3 | 2009-01-16 00:00:02 -0800 | [diff] [blame] | 38 | |
| 39 | # $LOCAL and $REMOTE are temporary files so prompt |
| 40 | # the user with the real $MERGED name before launching $merge_tool. |
David Aguilar | fdd7aa1 | 2011-08-18 00:23:44 -0700 | [diff] [blame] | 41 | if should_prompt |
| 42 | then |
Zoltan Klinger | ee7fb0b | 2013-12-06 10:38:46 +1100 | [diff] [blame] | 43 | printf "\nViewing (%s/%s): '%s'\n" "$GIT_DIFF_PATH_COUNTER" \ |
| 44 | "$GIT_DIFF_PATH_TOTAL" "$MERGED" |
David Aguilar | fdd7aa1 | 2011-08-18 00:23:44 -0700 | [diff] [blame] | 45 | if use_ext_cmd |
| 46 | then |
Nikola Forró | cce076e | 2016-04-12 16:44:20 +0200 | [diff] [blame] | 47 | printf "Launch '%s' [Y/n]? " \ |
David Aguilar | 1c6f5b5 | 2010-01-09 20:02:42 -0800 | [diff] [blame] | 48 | "$GIT_DIFFTOOL_EXTCMD" |
| 49 | else |
Nikola Forró | cce076e | 2016-04-12 16:44:20 +0200 | [diff] [blame] | 50 | printf "Launch '%s' [Y/n]? " "$merge_tool" |
David Aguilar | 1c6f5b5 | 2010-01-09 20:02:42 -0800 | [diff] [blame] | 51 | fi |
Johannes Sixt | 2509869 | 2014-10-26 09:09:20 +0100 | [diff] [blame] | 52 | read ans || return |
| 53 | if test "$ans" = n |
Sitaram Chamarty | ba959de | 2011-10-08 18:40:15 +0530 | [diff] [blame] | 54 | then |
| 55 | return |
| 56 | fi |
David Aguilar | 5c38ea3 | 2009-01-16 00:00:02 -0800 | [diff] [blame] | 57 | fi |
| 58 | |
David Aguilar | fdd7aa1 | 2011-08-18 00:23:44 -0700 | [diff] [blame] | 59 | if use_ext_cmd |
| 60 | then |
Michael J Gruber | 4a689af | 2010-12-14 10:18:35 +0100 | [diff] [blame] | 61 | export BASE |
David Aguilar | 9f3d54d | 2010-01-15 14:03:44 -0800 | [diff] [blame] | 62 | eval $GIT_DIFFTOOL_EXTCMD '"$LOCAL"' '"$REMOTE"' |
David Aguilar | 1c6f5b5 | 2010-01-09 20:02:42 -0800 | [diff] [blame] | 63 | else |
Seth House | de8dafb | 2021-02-09 13:07:11 -0700 | [diff] [blame] | 64 | initialize_merge_tool "$merge_tool" |
| 65 | # ignore the error from the above --- run_merge_tool |
| 66 | # will diagnose unusable tool by itself |
David Aguilar | 1c6f5b5 | 2010-01-09 20:02:42 -0800 | [diff] [blame] | 67 | run_merge_tool "$merge_tool" |
| 68 | fi |
David Aguilar | 5c38ea3 | 2009-01-16 00:00:02 -0800 | [diff] [blame] | 69 | } |
| 70 | |
David Aguilar | fdd7aa1 | 2011-08-18 00:23:44 -0700 | [diff] [blame] | 71 | if ! use_ext_cmd |
| 72 | then |
| 73 | if test -n "$GIT_DIFF_TOOL" |
| 74 | then |
David Aguilar | 1c6f5b5 | 2010-01-09 20:02:42 -0800 | [diff] [blame] | 75 | merge_tool="$GIT_DIFF_TOOL" |
| 76 | else |
Denton Liu | 05fb872 | 2019-04-29 02:21:08 -0400 | [diff] [blame] | 77 | merge_tool="$(get_merge_tool)" |
Tao Klerks | 42943b9 | 2023-03-18 15:27:43 +0000 | [diff] [blame] | 78 | subshell_exit_status=$? |
| 79 | if test $subshell_exit_status -gt 1 |
| 80 | then |
| 81 | exit $subshell_exit_status |
| 82 | fi |
David Aguilar | 1c6f5b5 | 2010-01-09 20:02:42 -0800 | [diff] [blame] | 83 | fi |
David Aguilar | 47d6592 | 2009-04-11 20:41:56 -0700 | [diff] [blame] | 84 | fi |
David Aguilar | 5c38ea3 | 2009-01-16 00:00:02 -0800 | [diff] [blame] | 85 | |
Tim Henigan | 7e0abce | 2012-04-23 14:23:41 -0400 | [diff] [blame] | 86 | if test -n "$GIT_DIFFTOOL_DIRDIFF" |
| 87 | then |
| 88 | LOCAL="$1" |
| 89 | REMOTE="$2" |
Seth House | de8dafb | 2021-02-09 13:07:11 -0700 | [diff] [blame] | 90 | initialize_merge_tool "$merge_tool" |
| 91 | # ignore the error from the above --- run_merge_tool |
| 92 | # will diagnose unusable tool by itself |
Tim Henigan | 7e0abce | 2012-04-23 14:23:41 -0400 | [diff] [blame] | 93 | run_merge_tool "$merge_tool" false |
| 94 | else |
| 95 | # Launch the merge tool on each path provided by 'git diff' |
| 96 | while test $# -gt 6 |
| 97 | do |
| 98 | launch_merge_tool "$1" "$2" "$5" |
David Aguilar | 2b52123 | 2014-10-26 18:15:42 -0700 | [diff] [blame] | 99 | status=$? |
John Keeping | 45a4f5d | 2016-08-15 22:54:39 +0100 | [diff] [blame] | 100 | if test $status -ge 126 |
| 101 | then |
| 102 | # Command not found (127), not executable (126) or |
| 103 | # exited via a signal (>= 128). |
| 104 | exit $status |
| 105 | fi |
| 106 | |
David Aguilar | 2b52123 | 2014-10-26 18:15:42 -0700 | [diff] [blame] | 107 | if test "$status" != 0 && |
| 108 | test "$GIT_DIFFTOOL_TRUST_EXIT_CODE" = true |
| 109 | then |
| 110 | exit $status |
| 111 | fi |
Tim Henigan | 7e0abce | 2012-04-23 14:23:41 -0400 | [diff] [blame] | 112 | shift 7 |
| 113 | done |
| 114 | fi |
David Aguilar | c41d3fe | 2014-11-20 17:20:28 -0800 | [diff] [blame] | 115 | |
| 116 | exit 0 |