builtin/submodule--helper: fix leaking remote ref on errors
[git/gitster.git] / git-difftool--helper.sh
blobdd0c9a5b7f2b078205000f3051df0bade8b0cfab
1 #!/bin/sh
2 # git-difftool--helper is a GIT_EXTERNAL_DIFF-compatible diff tool launcher.
3 # This script is typically launched by using the 'git difftool'
4 # convenience command.
6 # Copyright (c) 2009, 2010 David Aguilar
8 TOOL_MODE=diff
9 . git-mergetool--lib
11 # difftool.prompt controls the default prompt/no-prompt behavior
12 # and is overridden with $GIT_DIFFTOOL*_PROMPT.
13 should_prompt () {
14 prompt_merge=$(git config --bool mergetool.prompt || echo true)
15 prompt=$(git config --bool difftool.prompt || echo $prompt_merge)
16 if test "$prompt" = true
17 then
18 test -z "$GIT_DIFFTOOL_NO_PROMPT"
19 else
20 test -n "$GIT_DIFFTOOL_PROMPT"
24 # Indicates that --extcmd=... was specified
25 use_ext_cmd () {
26 test -n "$GIT_DIFFTOOL_EXTCMD"
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"
39 # $LOCAL and $REMOTE are temporary files so prompt
40 # the user with the real $MERGED name before launching $merge_tool.
41 if should_prompt
42 then
43 printf "\nViewing (%s/%s): '%s'\n" "$GIT_DIFF_PATH_COUNTER" \
44 "$GIT_DIFF_PATH_TOTAL" "$MERGED"
45 if use_ext_cmd
46 then
47 printf "Launch '%s' [Y/n]? " \
48 "$GIT_DIFFTOOL_EXTCMD"
49 else
50 printf "Launch '%s' [Y/n]? " "$merge_tool"
52 read ans || return
53 if test "$ans" = n
54 then
55 return
59 if use_ext_cmd
60 then
61 export BASE
62 eval $GIT_DIFFTOOL_EXTCMD '"$LOCAL"' '"$REMOTE"'
63 else
64 initialize_merge_tool "$merge_tool"
65 # ignore the error from the above --- run_merge_tool
66 # will diagnose unusable tool by itself
67 run_merge_tool "$merge_tool"
71 if ! use_ext_cmd
72 then
73 if test -n "$GIT_DIFF_TOOL"
74 then
75 merge_tool="$GIT_DIFF_TOOL"
76 else
77 merge_tool="$(get_merge_tool)"
78 subshell_exit_status=$?
79 if test $subshell_exit_status -gt 1
80 then
81 exit $subshell_exit_status
86 if test -n "$GIT_DIFFTOOL_DIRDIFF"
87 then
88 LOCAL="$1"
89 REMOTE="$2"
90 initialize_merge_tool "$merge_tool"
91 # ignore the error from the above --- run_merge_tool
92 # will diagnose unusable tool by itself
93 run_merge_tool "$merge_tool" false
95 status=$?
96 if test $status -ge 126
97 then
98 # Command not found (127), not executable (126) or
99 # exited via a signal (>= 128).
100 exit $status
103 if test "$GIT_DIFFTOOL_TRUST_EXIT_CODE" = true
104 then
105 exit $status
107 else
108 # Launch the merge tool on each path provided by 'git diff'
109 while test $# -gt 6
111 launch_merge_tool "$1" "$2" "$5"
112 status=$?
113 if test $status -ge 126
114 then
115 # Command not found (127), not executable (126) or
116 # exited via a signal (>= 128).
117 exit $status
120 if test "$status" != 0 &&
121 test "$GIT_DIFFTOOL_TRUST_EXIT_CODE" = true
122 then
123 exit $status
125 shift 7
126 done
129 exit 0