2 # git-difftool--helper is a GIT_EXTERNAL_DIFF-compatible diff tool launcher.
3 # This script is typically launched by using the 'git difftool'
6 # Copyright (c) 2009 David Aguilar
8 # difftool.prompt controls the default prompt/no-prompt behavior
9 # and is overridden with $GIT_DIFFTOOL*_PROMPT.
11 prompt
=$
(git config
--bool difftool.prompt ||
echo true
)
12 if test "$prompt" = true
; then
13 test -z "$GIT_DIFFTOOL_NO_PROMPT"
15 test -n "$GIT_DIFFTOOL_PROMPT"
19 # This function prepares temporary files and launches the appropriate
21 launch_merge_tool
() {
22 # Merged is the filename as it appears in the work tree
23 # Local is the contents of a/filename
24 # Remote is the contents of b/filename
25 # Custom merge tool commands might use $BASE so we provide it
31 # $LOCAL and $REMOTE are temporary files so prompt
32 # the user with the real $MERGED name before launching $merge_tool.
33 if should_prompt
; then
34 printf "\nViewing: '$MERGED'\n"
35 printf "Hit return to launch '%s': " "$merge_tool"
39 # Run the appropriate merge tool command
42 basename=$
(basename "$MERGED")
43 "$merge_tool_path" --auto \
44 --L1 "$basename (A)" \
45 --L2 "$basename (B)" \
51 "$merge_tool_path" "$LOCAL" "$REMOTE"
55 "$merge_tool_path" "$LOCAL" "$REMOTE"
59 "$merge_tool_path" "$LOCAL" "$REMOTE"
63 "$merge_tool_path" "$LOCAL" "$REMOTE" |
cat
67 "$merge_tool_path" -d -c "wincmd l" "$LOCAL" "$REMOTE"
71 "$merge_tool_path" -d -c "wincmd l" -f "$LOCAL" "$REMOTE"
76 -R 'Accel.Search: "Ctrl+F"' \
77 -R 'Accel.SearchForward: "Ctrl-G"' \
82 "$merge_tool_path" "$LOCAL" "$REMOTE" |
cat
86 "$merge_tool_path" "$LOCAL" "$REMOTE" \
87 --default --mode=merge2
--to="$MERGED"
91 "$merge_tool_path" -f emerge-files-command \
92 "$LOCAL" "$REMOTE" "$(basename "$MERGED")"
96 if test -n "$merge_tool_cmd"; then
97 ( eval $merge_tool_cmd )
103 # Verifies that (difftool|mergetool).<tool>.cmd exists
104 valid_custom_tool
() {
105 merge_tool_cmd
="$(git config difftool.$1.cmd)"
106 test -z "$merge_tool_cmd" &&
107 merge_tool_cmd
="$(git config mergetool.$1.cmd)"
108 test -n "$merge_tool_cmd"
111 # Verifies that the chosen merge tool is properly setup.
112 # Built-in merge tools are always valid.
115 kdiff3 | kompare | tkdiff | xxdiff | meld | opendiff | emerge | vimdiff | gvimdiff | ecmerge
)
118 if ! valid_custom_tool
"$1"
126 # Sets up the merge_tool_path variable.
127 # This handles the difftool.<tool>.path configuration.
128 # This also falls back to mergetool defaults.
129 init_merge_tool_path
() {
130 merge_tool_path
=$
(git config difftool.
"$1".path
)
131 test -z "$merge_tool_path" &&
132 merge_tool_path
=$
(git config mergetool.
"$1".path
)
133 if test -z "$merge_tool_path"; then
142 merge_tool_path
=emacs
151 # Allow GIT_DIFF_TOOL and GIT_MERGE_TOOL to provide default values
152 test -n "$GIT_MERGE_TOOL" && merge_tool
="$GIT_MERGE_TOOL"
153 test -n "$GIT_DIFF_TOOL" && merge_tool
="$GIT_DIFF_TOOL"
155 # If merge tool was not specified then use the diff.tool
156 # configuration variable. If that's invalid then reset merge_tool.
157 # Fallback to merge.tool.
158 if test -z "$merge_tool"; then
159 merge_tool
=$
(git config
diff.tool
)
160 test -z "$merge_tool" &&
161 merge_tool
=$
(git config merge.tool
)
162 if test -n "$merge_tool" && ! valid_tool
"$merge_tool"; then
163 echo >&2 "git config option diff.tool set to unknown tool: $merge_tool"
164 echo >&2 "Resetting to default..."
169 # Try to guess an appropriate merge tool if no tool has been set.
170 if test -z "$merge_tool"; then
171 # We have a $DISPLAY so try some common UNIX merge tools
172 if test -n "$DISPLAY"; then
173 # If gnome then prefer meld, otherwise, prefer kdiff3 or kompare
174 if test -n "$GNOME_DESKTOP_SESSION_ID" ; then
175 merge_tool_candidates
="meld kdiff3 kompare tkdiff xxdiff gvimdiff diffuse"
177 merge_tool_candidates
="kdiff3 kompare tkdiff xxdiff meld gvimdiff diffuse"
180 if echo "${VISUAL:-$EDITOR}" |
grep 'emacs' > /dev
/null
2>&1; then
181 # $EDITOR is emacs so add emerge as a candidate
182 merge_tool_candidates
="$merge_tool_candidates emerge opendiff vimdiff"
183 elif echo "${VISUAL:-$EDITOR}" |
grep 'vim' > /dev
/null
2>&1; then
184 # $EDITOR is vim so add vimdiff as a candidate
185 merge_tool_candidates
="$merge_tool_candidates vimdiff opendiff emerge"
187 merge_tool_candidates
="$merge_tool_candidates opendiff emerge vimdiff"
189 echo "merge tool candidates: $merge_tool_candidates"
191 # Loop over each candidate and stop when a valid merge tool is found.
192 for i
in $merge_tool_candidates
194 init_merge_tool_path
$i
195 if type "$merge_tool_path" > /dev
/null
2>&1; then
201 if test -z "$merge_tool" ; then
202 echo "No known merge resolution program available."
207 # A merge tool has been set, so verify that it's valid.
208 if ! valid_tool
"$merge_tool"; then
209 echo >&2 "Unknown merge tool $merge_tool"
213 init_merge_tool_path
"$merge_tool"
215 if test -z "$merge_tool_cmd" && ! type "$merge_tool_path" > /dev
/null
2>&1; then
216 echo "The merge tool $merge_tool is not available as '$merge_tool_path'"
222 # Launch the merge tool on each path provided by 'git diff'
225 launch_merge_tool
"$1" "$2" "$5"