Add the diff option --no-defaults
[git/dscho.git] / git-difftool-helper.sh
blobb91002ba94d2132bd5cc7d54e129d90c97167cc6
1 #!/bin/sh
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'
6 # convenience command.
8 # Copyright (c) 2009 David Aguilar
10 # Set GIT_DIFFTOOL_NO_PROMPT to bypass the per-file prompt.
11 should_prompt () {
12 test -z "$GIT_DIFFTOOL_NO_PROMPT"
15 # This function prepares temporary files and launches the appropriate
16 # merge tool.
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
22 MERGED="$1"
23 LOCAL="$2"
24 REMOTE="$3"
25 BASE="$1"
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"
32 read ans
35 # Run the appropriate merge tool command
36 case "$merge_tool" in
37 kdiff3)
38 basename=$(basename "$MERGED")
39 "$merge_tool_path" --auto \
40 --L1 "$basename (A)" \
41 --L2 "$basename (B)" \
42 -o "$MERGED" "$LOCAL" "$REMOTE" \
43 > /dev/null 2>&1
46 kompare)
47 "$merge_tool_path" "$LOCAL" "$REMOTE"
50 tkdiff)
51 "$merge_tool_path" -o "$MERGED" "$LOCAL" "$REMOTE"
54 meld)
55 "$merge_tool_path" "$LOCAL" "$REMOTE"
58 vimdiff)
59 "$merge_tool_path" -c "wincmd l" "$LOCAL" "$REMOTE"
62 gvimdiff)
63 "$merge_tool_path" -c "wincmd l" -f "$LOCAL" "$REMOTE"
66 xxdiff)
67 "$merge_tool_path" \
68 -X \
69 -R 'Accel.SaveAsMerged: "Ctrl-S"' \
70 -R 'Accel.Search: "Ctrl+F"' \
71 -R 'Accel.SearchForward: "Ctrl-G"' \
72 --merged-file "$MERGED" \
73 "$LOCAL" "$REMOTE"
76 opendiff)
77 "$merge_tool_path" "$LOCAL" "$REMOTE" \
78 -merge "$MERGED" | cat
81 ecmerge)
82 "$merge_tool_path" "$LOCAL" "$REMOTE" \
83 --default --mode=merge2 --to="$MERGED"
86 emerge)
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 )
96 esac
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.
109 valid_tool() {
110 case "$1" in
111 kdiff3 | kompare | tkdiff | xxdiff | meld | opendiff | emerge | vimdiff | gvimdiff | ecmerge)
112 ;; # happy
114 if ! valid_custom_tool "$1"
115 then
116 return 1
119 esac
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
130 case "$1" in
131 emerge)
132 merge_tool_path=emacs
135 merge_tool_path="$1"
137 esac
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..."
155 unset merge_tool
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"
166 else
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"
176 else
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
186 merge_tool=$i
187 break
189 done
191 if test -z "$merge_tool" ; then
192 echo "No known merge resolution program available."
193 exit 1
196 else
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"
200 exit 1
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'"
207 exit 1
212 # Launch the merge tool on each path provided by 'git diff'
213 while test $# -gt 6
215 launch_merge_tool "$1" "$2" "$5"
216 shift 7
217 done