blob: e4e820e68095928765940be51fba0cdb8e5d609c [file] [log] [blame]
David Aguilar5c38ea32009-01-16 00:00:02 -08001#!/bin/sh
David Aguilarafcbc8e2009-04-07 01:21:20 -07002# git-difftool--helper is a GIT_EXTERNAL_DIFF-compatible diff tool launcher.
David Aguilar5c38ea32009-01-16 00:00:02 -08003# This script is typically launched by using the 'git difftool'
4# convenience command.
5#
David Aguilarc8a56722010-01-15 19:10:03 -08006# Copyright (c) 2009, 2010 David Aguilar
David Aguilar5c38ea32009-01-16 00:00:02 -08007
David Aguilar21d0ba72009-04-08 00:17:20 -07008TOOL_MODE=diff
9. git-mergetool--lib
10
David Aguilara9043922009-04-07 01:21:22 -070011# difftool.prompt controls the default prompt/no-prompt behavior
12# and is overridden with $GIT_DIFFTOOL*_PROMPT.
David Aguilar5c38ea32009-01-16 00:00:02 -080013should_prompt () {
Sebastian Schuberth4cacc622010-01-22 17:36:36 +010014 prompt_merge=$(git config --bool mergetool.prompt || echo true)
15 prompt=$(git config --bool difftool.prompt || echo $prompt_merge)
David Aguilarfdd7aa12011-08-18 00:23:44 -070016 if test "$prompt" = true
17 then
David Aguilara9043922009-04-07 01:21:22 -070018 test -z "$GIT_DIFFTOOL_NO_PROMPT"
19 else
20 test -n "$GIT_DIFFTOOL_PROMPT"
21 fi
David Aguilar5c38ea32009-01-16 00:00:02 -080022}
23
David Aguilar1c6f5b52010-01-09 20:02:42 -080024# Indicates that --extcmd=... was specified
25use_ext_cmd () {
26 test -n "$GIT_DIFFTOOL_EXTCMD"
27}
28
David Aguilar5c38ea32009-01-16 00:00:02 -080029launch_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 Aguilar5c38ea32009-01-16 00:00:02 -080038
39 # $LOCAL and $REMOTE are temporary files so prompt
40 # the user with the real $MERGED name before launching $merge_tool.
David Aguilarfdd7aa12011-08-18 00:23:44 -070041 if should_prompt
42 then
Zoltan Klingeree7fb0b2013-12-06 10:38:46 +110043 printf "\nViewing (%s/%s): '%s'\n" "$GIT_DIFF_PATH_COUNTER" \
44 "$GIT_DIFF_PATH_TOTAL" "$MERGED"
David Aguilarfdd7aa12011-08-18 00:23:44 -070045 if use_ext_cmd
46 then
Nikola Forrócce076e2016-04-12 16:44:20 +020047 printf "Launch '%s' [Y/n]? " \
David Aguilar1c6f5b52010-01-09 20:02:42 -080048 "$GIT_DIFFTOOL_EXTCMD"
49 else
Nikola Forrócce076e2016-04-12 16:44:20 +020050 printf "Launch '%s' [Y/n]? " "$merge_tool"
David Aguilar1c6f5b52010-01-09 20:02:42 -080051 fi
Johannes Sixt25098692014-10-26 09:09:20 +010052 read ans || return
53 if test "$ans" = n
Sitaram Chamartyba959de2011-10-08 18:40:15 +053054 then
55 return
56 fi
David Aguilar5c38ea32009-01-16 00:00:02 -080057 fi
58
David Aguilarfdd7aa12011-08-18 00:23:44 -070059 if use_ext_cmd
60 then
Michael J Gruber4a689af2010-12-14 10:18:35 +010061 export BASE
David Aguilar9f3d54d2010-01-15 14:03:44 -080062 eval $GIT_DIFFTOOL_EXTCMD '"$LOCAL"' '"$REMOTE"'
David Aguilar1c6f5b52010-01-09 20:02:42 -080063 else
Seth Housede8dafb2021-02-09 13:07:11 -070064 initialize_merge_tool "$merge_tool"
65 # ignore the error from the above --- run_merge_tool
66 # will diagnose unusable tool by itself
David Aguilar1c6f5b52010-01-09 20:02:42 -080067 run_merge_tool "$merge_tool"
68 fi
David Aguilar5c38ea32009-01-16 00:00:02 -080069}
70
David Aguilarfdd7aa12011-08-18 00:23:44 -070071if ! use_ext_cmd
72then
73 if test -n "$GIT_DIFF_TOOL"
74 then
David Aguilar1c6f5b52010-01-09 20:02:42 -080075 merge_tool="$GIT_DIFF_TOOL"
76 else
Denton Liu05fb8722019-04-29 02:21:08 -040077 merge_tool="$(get_merge_tool)"
Tao Klerks42943b92023-03-18 15:27:43 +000078 subshell_exit_status=$?
79 if test $subshell_exit_status -gt 1
80 then
81 exit $subshell_exit_status
82 fi
David Aguilar1c6f5b52010-01-09 20:02:42 -080083 fi
David Aguilar47d65922009-04-11 20:41:56 -070084fi
David Aguilar5c38ea32009-01-16 00:00:02 -080085
Tim Henigan7e0abce2012-04-23 14:23:41 -040086if test -n "$GIT_DIFFTOOL_DIRDIFF"
87then
88 LOCAL="$1"
89 REMOTE="$2"
Seth Housede8dafb2021-02-09 13:07:11 -070090 initialize_merge_tool "$merge_tool"
91 # ignore the error from the above --- run_merge_tool
92 # will diagnose unusable tool by itself
Tim Henigan7e0abce2012-04-23 14:23:41 -040093 run_merge_tool "$merge_tool" false
94else
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 Aguilar2b521232014-10-26 18:15:42 -070099 status=$?
John Keeping45a4f5d2016-08-15 22:54:39 +0100100 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 Aguilar2b521232014-10-26 18:15:42 -0700107 if test "$status" != 0 &&
108 test "$GIT_DIFFTOOL_TRUST_EXIT_CODE" = true
109 then
110 exit $status
111 fi
Tim Henigan7e0abce2012-04-23 14:23:41 -0400112 shift 7
113 done
114fi
David Aguilarc41d3fe2014-11-20 17:20:28 -0800115
116exit 0