2 # git-difftool-helper is a GIT_EXTERNAL_DIFF-compatible diff tool launcher.
3 # It supports kdiff3, kompare, tkdiff, xxdiff, meld, opendiff,
4 # emerge, ecmerge, vimdiff, gvimdiff, and custom user-configurable tools.
5 # This script is typically launched by using the 'git difftool'
8 # Copyright (c) 2009 David Aguilar
10 # Set GIT_DIFFTOOL_NO_PROMPT to bypass the per-file prompt.
12 test -z "$GIT_DIFFTOOL_NO_PROMPT"
15 # This function prepares temporary files and launches the appropriate
17 launch_merge_tool
() {
18 # Merged is the filename as it appears in the work tree
19 # Local is the contents of a/filename
20 # Remote is the contents of b/filename
21 # Custom merge tool commands might use $BASE so we provide it
27 # $LOCAL and $REMOTE are temporary files so prompt
28 # the user with the real $MERGED name before launching $merge_tool.
29 if should_prompt
; then
30 printf "\nViewing: '$MERGED'\n"
31 printf "Hit return to launch '%s': " "$merge_tool"
35 # Run the appropriate merge tool command
38 basename=$
(basename "$MERGED")
39 "$merge_tool_path" --auto \
40 --L1 "$basename (A)" \
41 --L2 "$basename (B)" \
42 -o "$MERGED" "$LOCAL" "$REMOTE" \
47 "$merge_tool_path" "$LOCAL" "$REMOTE"
51 "$merge_tool_path" -o "$MERGED" "$LOCAL" "$REMOTE"
55 "$merge_tool_path" "$LOCAL" "$REMOTE"
59 "$merge_tool_path" -c "wincmd l" "$LOCAL" "$REMOTE"
63 "$merge_tool_path" -c "wincmd l" -f "$LOCAL" "$REMOTE"
69 -R 'Accel.SaveAsMerged: "Ctrl-S"' \
70 -R 'Accel.Search: "Ctrl+F"' \
71 -R 'Accel.SearchForward: "Ctrl-G"' \
72 --merged-file "$MERGED" \
77 "$merge_tool_path" "$LOCAL" "$REMOTE" \
78 -merge "$MERGED" |
cat
82 "$merge_tool_path" "$LOCAL" "$REMOTE" \
83 --default --mode=merge2
--to="$MERGED"
87 "$merge_tool_path" -f emerge-files-command \
88 "$LOCAL" "$REMOTE" "$(basename "$MERGED")"
92 if test -n "$merge_tool_cmd"; then
93 ( eval $merge_tool_cmd )
99 # Verifies that (difftool|mergetool).<tool>.cmd exists
100 valid_custom_tool
() {
101 merge_tool_cmd
="$(git config difftool.$1.cmd)"
102 test -z "$merge_tool_cmd" &&
103 merge_tool_cmd
="$(git config mergetool.$1.cmd)"
104 test -n "$merge_tool_cmd"
107 # Verifies that the chosen merge tool is properly setup.
108 # Built-in merge tools are always valid.
111 kdiff3 | kompare | tkdiff | xxdiff | meld | opendiff | emerge | vimdiff | gvimdiff | ecmerge
)
114 if ! valid_custom_tool
"$1"
122 # Sets up the merge_tool_path variable.
123 # This handles the difftool.<tool>.path configuration.
124 # This also falls back to mergetool defaults.
125 init_merge_tool_path
() {
126 merge_tool_path
=$
(git config difftool.
"$1".path
)
127 test -z "$merge_tool_path" &&
128 merge_tool_path
=$
(git config mergetool.
"$1".path
)
129 if test -z "$merge_tool_path"; then
132 merge_tool_path
=emacs
141 # Allow GIT_DIFF_TOOL and GIT_MERGE_TOOL to provide default values
142 test -n "$GIT_MERGE_TOOL" && merge_tool
="$GIT_MERGE_TOOL"
143 test -n "$GIT_DIFF_TOOL" && merge_tool
="$GIT_DIFF_TOOL"
145 # If merge tool was not specified then use the diff.tool
146 # configuration variable. If that's invalid then reset merge_tool.
147 # Fallback to merge.tool.
148 if test -z "$merge_tool"; then
149 merge_tool
=$
(git config
diff.tool
)
150 test -z "$merge_tool" &&
151 merge_tool
=$
(git config merge.tool
)
152 if test -n "$merge_tool" && ! valid_tool
"$merge_tool"; then
153 echo >&2 "git config option diff.tool set to unknown tool: $merge_tool"
154 echo >&2 "Resetting to default..."
159 # Try to guess an appropriate merge tool if no tool has been set.
160 if test -z "$merge_tool"; then
161 # We have a $DISPLAY so try some common UNIX merge tools
162 if test -n "$DISPLAY"; then
163 # If gnome then prefer meld, otherwise, prefer kdiff3 or kompare
164 if test -n "$GNOME_DESKTOP_SESSION_ID" ; then
165 merge_tool_candidates
="meld kdiff3 kompare tkdiff xxdiff gvimdiff"
167 merge_tool_candidates
="kdiff3 kompare tkdiff xxdiff meld gvimdiff"
170 if echo "${VISUAL:-$EDITOR}" |
grep 'emacs' > /dev
/null
2>&1; then
171 # $EDITOR is emacs so add emerge as a candidate
172 merge_tool_candidates
="$merge_tool_candidates emerge opendiff vimdiff"
173 elif echo "${VISUAL:-$EDITOR}" |
grep 'vim' > /dev
/null
2>&1; then
174 # $EDITOR is vim so add vimdiff as a candidate
175 merge_tool_candidates
="$merge_tool_candidates vimdiff opendiff emerge"
177 merge_tool_candidates
="$merge_tool_candidates opendiff emerge vimdiff"
179 echo "merge tool candidates: $merge_tool_candidates"
181 # Loop over each candidate and stop when a valid merge tool is found.
182 for i
in $merge_tool_candidates
184 init_merge_tool_path
$i
185 if type "$merge_tool_path" > /dev
/null
2>&1; then
191 if test -z "$merge_tool" ; then
192 echo "No known merge resolution program available."
197 # A merge tool has been set, so verify that it's valid.
198 if ! valid_tool
"$merge_tool"; then
199 echo >&2 "Unknown merge tool $merge_tool"
203 init_merge_tool_path
"$merge_tool"
205 if test -z "$merge_tool_cmd" && ! type "$merge_tool_path" > /dev
/null
2>&1; then
206 echo "The merge tool $merge_tool is not available as '$merge_tool_path'"
212 # Launch the merge tool on each path provided by 'git diff'
215 launch_merge_tool
"$1" "$2" "$5"