difftool: print list of valid tools with '--tool-help'
[git/dscho.git] / t / t7800-difftool.sh
blobbbe71e5f73a0b1a95c8afc4365a8b7c93bfcf40d
1 #!/bin/sh
3 # Copyright (c) 2009, 2010 David Aguilar
6 test_description='git-difftool
8 Testing basic diff tool invocation
11 . ./test-lib.sh
13 remove_config_vars()
15 # Unset all config variables used by git-difftool
16 git config --unset diff.tool
17 git config --unset diff.guitool
18 git config --unset difftool.test-tool.cmd
19 git config --unset difftool.prompt
20 git config --unset merge.tool
21 git config --unset mergetool.test-tool.cmd
22 git config --unset mergetool.prompt
23 return 0
26 restore_test_defaults()
28 # Restores the test defaults used by several tests
29 remove_config_vars
30 unset GIT_DIFF_TOOL
31 unset GIT_DIFFTOOL_PROMPT
32 unset GIT_DIFFTOOL_NO_PROMPT
33 git config diff.tool test-tool &&
34 git config difftool.test-tool.cmd 'cat $LOCAL'
35 git config difftool.bogus-tool.cmd false
38 prompt_given()
40 prompt="$1"
41 test "$prompt" = "Launch 'test-tool' [Y/n]: branch"
44 stdin_contains()
46 grep >/dev/null "$1"
49 stdin_doesnot_contain()
51 ! stdin_contains "$1"
54 # Create a file on master and change it on branch
55 test_expect_success PERL 'setup' '
56 echo master >file &&
57 git add file &&
58 git commit -m "added file" &&
60 git checkout -b branch master &&
61 echo branch >file &&
62 git commit -a -m "branch changed file" &&
63 git checkout master
66 # Configure a custom difftool.<tool>.cmd and use it
67 test_expect_success PERL 'custom commands' '
68 restore_test_defaults &&
69 git config difftool.test-tool.cmd "cat \$REMOTE" &&
71 diff=$(git difftool --no-prompt branch) &&
72 test "$diff" = "master" &&
74 restore_test_defaults &&
75 diff=$(git difftool --no-prompt branch) &&
76 test "$diff" = "branch"
79 # Ensures that git-difftool ignores bogus --tool values
80 test_expect_success PERL 'difftool ignores bad --tool values' '
81 diff=$(git difftool --no-prompt --tool=bad-tool branch)
82 test "$?" = 1 &&
83 test "$diff" = ""
86 test_expect_success PERL 'difftool honors --gui' '
87 git config merge.tool bogus-tool &&
88 git config diff.tool bogus-tool &&
89 git config diff.guitool test-tool &&
91 diff=$(git difftool --no-prompt --gui branch) &&
92 test "$diff" = "branch" &&
94 restore_test_defaults
97 test_expect_success PERL 'difftool --gui last setting wins' '
98 git config diff.guitool bogus-tool &&
99 git difftool --no-prompt --gui --no-gui &&
101 git config merge.tool bogus-tool &&
102 git config diff.tool bogus-tool &&
103 git config diff.guitool test-tool &&
104 diff=$(git difftool --no-prompt --no-gui --gui branch) &&
105 test "$diff" = "branch" &&
107 restore_test_defaults
110 test_expect_success PERL 'difftool --gui works without configured diff.guitool' '
111 git config diff.tool test-tool &&
113 diff=$(git difftool --no-prompt --gui branch) &&
114 test "$diff" = "branch" &&
116 restore_test_defaults
119 # Specify the diff tool using $GIT_DIFF_TOOL
120 test_expect_success PERL 'GIT_DIFF_TOOL variable' '
121 test_might_fail git config --unset diff.tool &&
122 GIT_DIFF_TOOL=test-tool &&
123 export GIT_DIFF_TOOL &&
125 diff=$(git difftool --no-prompt branch) &&
126 test "$diff" = "branch" &&
128 restore_test_defaults
131 # Test the $GIT_*_TOOL variables and ensure
132 # that $GIT_DIFF_TOOL always wins unless --tool is specified
133 test_expect_success PERL 'GIT_DIFF_TOOL overrides' '
134 git config diff.tool bogus-tool &&
135 git config merge.tool bogus-tool &&
137 GIT_DIFF_TOOL=test-tool &&
138 export GIT_DIFF_TOOL &&
140 diff=$(git difftool --no-prompt branch) &&
141 test "$diff" = "branch" &&
143 GIT_DIFF_TOOL=bogus-tool &&
144 export GIT_DIFF_TOOL &&
146 diff=$(git difftool --no-prompt --tool=test-tool branch) &&
147 test "$diff" = "branch" &&
149 restore_test_defaults
152 # Test that we don't have to pass --no-prompt to difftool
153 # when $GIT_DIFFTOOL_NO_PROMPT is true
154 test_expect_success PERL 'GIT_DIFFTOOL_NO_PROMPT variable' '
155 GIT_DIFFTOOL_NO_PROMPT=true &&
156 export GIT_DIFFTOOL_NO_PROMPT &&
158 diff=$(git difftool branch) &&
159 test "$diff" = "branch" &&
161 restore_test_defaults
164 # git-difftool supports the difftool.prompt variable.
165 # Test that GIT_DIFFTOOL_PROMPT can override difftool.prompt = false
166 test_expect_success PERL 'GIT_DIFFTOOL_PROMPT variable' '
167 git config difftool.prompt false &&
168 GIT_DIFFTOOL_PROMPT=true &&
169 export GIT_DIFFTOOL_PROMPT &&
171 prompt=$(echo | git difftool branch | tail -1) &&
172 prompt_given "$prompt" &&
174 restore_test_defaults
177 # Test that we don't have to pass --no-prompt when difftool.prompt is false
178 test_expect_success PERL 'difftool.prompt config variable is false' '
179 git config difftool.prompt false &&
181 diff=$(git difftool branch) &&
182 test "$diff" = "branch" &&
184 restore_test_defaults
187 # Test that we don't have to pass --no-prompt when mergetool.prompt is false
188 test_expect_success PERL 'difftool merge.prompt = false' '
189 test_might_fail git config --unset difftool.prompt &&
190 git config mergetool.prompt false &&
192 diff=$(git difftool branch) &&
193 test "$diff" = "branch" &&
195 restore_test_defaults
198 # Test that the -y flag can override difftool.prompt = true
199 test_expect_success PERL 'difftool.prompt can overridden with -y' '
200 git config difftool.prompt true &&
202 diff=$(git difftool -y branch) &&
203 test "$diff" = "branch" &&
205 restore_test_defaults
208 # Test that the --prompt flag can override difftool.prompt = false
209 test_expect_success PERL 'difftool.prompt can overridden with --prompt' '
210 git config difftool.prompt false &&
212 prompt=$(echo | git difftool --prompt branch | tail -1) &&
213 prompt_given "$prompt" &&
215 restore_test_defaults
218 # Test that the last flag passed on the command-line wins
219 test_expect_success PERL 'difftool last flag wins' '
220 diff=$(git difftool --prompt --no-prompt branch) &&
221 test "$diff" = "branch" &&
223 restore_test_defaults &&
225 prompt=$(echo | git difftool --no-prompt --prompt branch | tail -1) &&
226 prompt_given "$prompt" &&
228 restore_test_defaults
231 # git-difftool falls back to git-mergetool config variables
232 # so test that behavior here
233 test_expect_success PERL 'difftool + mergetool config variables' '
234 remove_config_vars &&
235 git config merge.tool test-tool &&
236 git config mergetool.test-tool.cmd "cat \$LOCAL" &&
238 diff=$(git difftool --no-prompt branch) &&
239 test "$diff" = "branch" &&
241 # set merge.tool to something bogus, diff.tool to test-tool
242 git config merge.tool bogus-tool &&
243 git config diff.tool test-tool &&
245 diff=$(git difftool --no-prompt branch) &&
246 test "$diff" = "branch" &&
248 restore_test_defaults
251 test_expect_success PERL 'difftool.<tool>.path' '
252 git config difftool.tkdiff.path echo &&
253 diff=$(git difftool --tool=tkdiff --no-prompt branch) &&
254 git config --unset difftool.tkdiff.path &&
255 lines=$(echo "$diff" | grep file | wc -l) &&
256 test "$lines" -eq 1 &&
258 restore_test_defaults
261 test_expect_success PERL 'difftool --extcmd=cat' '
262 diff=$(git difftool --no-prompt --extcmd=cat branch) &&
263 test "$diff" = branch"$LF"master
266 test_expect_success PERL 'difftool --extcmd cat' '
267 diff=$(git difftool --no-prompt --extcmd cat branch) &&
268 test "$diff" = branch"$LF"master
271 test_expect_success PERL 'difftool -x cat' '
272 diff=$(git difftool --no-prompt -x cat branch) &&
273 test "$diff" = branch"$LF"master
276 test_expect_success PERL 'difftool --extcmd echo arg1' '
277 diff=$(git difftool --no-prompt --extcmd sh\ -c\ \"echo\ \$1\" branch) &&
278 test "$diff" = file
281 test_expect_success PERL 'difftool --extcmd cat arg1' '
282 diff=$(git difftool --no-prompt --extcmd sh\ -c\ \"cat\ \$1\" branch) &&
283 test "$diff" = master
286 test_expect_success PERL 'difftool --extcmd cat arg2' '
287 diff=$(git difftool --no-prompt --extcmd sh\ -c\ \"cat\ \$2\" branch) &&
288 test "$diff" = branch
291 # Create a second file on master and a different version on branch
292 test_expect_success PERL 'setup with 2 files different' '
293 echo m2 >file2 &&
294 git add file2 &&
295 git commit -m "added file2" &&
297 git checkout branch &&
298 echo br2 >file2 &&
299 git add file2 &&
300 git commit -a -m "branch changed file2" &&
301 git checkout master
304 test_expect_success PERL 'say no to the first file' '
305 diff=$( (echo n; echo) | git difftool -x cat branch ) &&
307 echo "$diff" | stdin_contains m2 &&
308 echo "$diff" | stdin_contains br2 &&
309 echo "$diff" | stdin_doesnot_contain master &&
310 echo "$diff" | stdin_doesnot_contain branch
313 test_expect_success PERL 'say no to the second file' '
314 diff=$( (echo; echo n) | git difftool -x cat branch ) &&
316 echo "$diff" | stdin_contains master &&
317 echo "$diff" | stdin_contains branch &&
318 echo "$diff" | stdin_doesnot_contain m2 &&
319 echo "$diff" | stdin_doesnot_contain br2
322 test_expect_success PERL 'difftool --tool-help' '
323 tool_help=$(git difftool --tool-help) &&
324 echo "$tool_help" | stdin_contains tool
327 test_expect_success PERL 'setup change in subdirectory' '
328 git checkout master &&
329 mkdir sub &&
330 echo master >sub/sub &&
331 git add sub/sub &&
332 git commit -m "added sub/sub" &&
333 echo test >>file &&
334 echo test >>sub/sub &&
335 git add . &&
336 git commit -m "modified both"
339 test_expect_success PERL 'difftool -d' '
340 diff=$(git difftool -d --extcmd ls branch) &&
341 echo "$diff" | stdin_contains sub &&
342 echo "$diff" | stdin_contains file
345 test_expect_success PERL 'difftool --dir-diff' '
346 diff=$(git difftool --dir-diff --extcmd ls branch) &&
347 echo "$diff" | stdin_contains sub &&
348 echo "$diff" | stdin_contains file
351 test_expect_success PERL 'difftool --dir-diff ignores --prompt' '
352 diff=$(git difftool --dir-diff --prompt --extcmd ls branch) &&
353 echo "$diff" | stdin_contains sub &&
354 echo "$diff" | stdin_contains file
357 test_expect_success PERL 'difftool --dir-diff from subdirectory' '
359 cd sub &&
360 diff=$(git difftool --dir-diff --extcmd ls branch) &&
361 echo "$diff" | stdin_contains sub &&
362 echo "$diff" | stdin_contains file
366 test_done