archive: handle commits with an empty tree
[git/jnareb-git.git] / t / t7800-difftool.sh
blobeb1d3f85b59b2e74231f0285fc4e90d95d0658fa
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 a custom difftool.<tool>.cmd overrides built-ins
80 test_expect_success PERL 'custom commands override built-ins' '
81 restore_test_defaults &&
82 git config difftool.defaults.cmd "cat \$REMOTE" &&
84 diff=$(git difftool --tool defaults --no-prompt branch) &&
85 test "$diff" = "master" &&
87 git config --unset difftool.defaults.cmd
90 # Ensures that git-difftool ignores bogus --tool values
91 test_expect_success PERL 'difftool ignores bad --tool values' '
92 diff=$(git difftool --no-prompt --tool=bad-tool branch)
93 test "$?" = 1 &&
94 test "$diff" = ""
97 test_expect_success PERL 'difftool forwards arguments to diff' '
98 >for-diff &&
99 git add for-diff &&
100 echo changes>for-diff &&
101 git add for-diff &&
102 diff=$(git difftool --cached --no-prompt -- for-diff) &&
103 test "$diff" = "" &&
104 git reset -- for-diff &&
105 rm for-diff
108 test_expect_success PERL 'difftool honors --gui' '
109 git config merge.tool bogus-tool &&
110 git config diff.tool bogus-tool &&
111 git config diff.guitool test-tool &&
113 diff=$(git difftool --no-prompt --gui branch) &&
114 test "$diff" = "branch" &&
116 restore_test_defaults
119 test_expect_success PERL 'difftool --gui last setting wins' '
120 git config diff.guitool bogus-tool &&
121 git difftool --no-prompt --gui --no-gui &&
123 git config merge.tool bogus-tool &&
124 git config diff.tool bogus-tool &&
125 git config diff.guitool test-tool &&
126 diff=$(git difftool --no-prompt --no-gui --gui branch) &&
127 test "$diff" = "branch" &&
129 restore_test_defaults
132 test_expect_success PERL 'difftool --gui works without configured diff.guitool' '
133 git config diff.tool test-tool &&
135 diff=$(git difftool --no-prompt --gui branch) &&
136 test "$diff" = "branch" &&
138 restore_test_defaults
141 # Specify the diff tool using $GIT_DIFF_TOOL
142 test_expect_success PERL 'GIT_DIFF_TOOL variable' '
143 test_might_fail git config --unset diff.tool &&
144 GIT_DIFF_TOOL=test-tool &&
145 export GIT_DIFF_TOOL &&
147 diff=$(git difftool --no-prompt branch) &&
148 test "$diff" = "branch" &&
150 restore_test_defaults
153 # Test the $GIT_*_TOOL variables and ensure
154 # that $GIT_DIFF_TOOL always wins unless --tool is specified
155 test_expect_success PERL 'GIT_DIFF_TOOL overrides' '
156 git config diff.tool bogus-tool &&
157 git config merge.tool bogus-tool &&
159 GIT_DIFF_TOOL=test-tool &&
160 export GIT_DIFF_TOOL &&
162 diff=$(git difftool --no-prompt branch) &&
163 test "$diff" = "branch" &&
165 GIT_DIFF_TOOL=bogus-tool &&
166 export GIT_DIFF_TOOL &&
168 diff=$(git difftool --no-prompt --tool=test-tool branch) &&
169 test "$diff" = "branch" &&
171 restore_test_defaults
174 # Test that we don't have to pass --no-prompt to difftool
175 # when $GIT_DIFFTOOL_NO_PROMPT is true
176 test_expect_success PERL 'GIT_DIFFTOOL_NO_PROMPT variable' '
177 GIT_DIFFTOOL_NO_PROMPT=true &&
178 export GIT_DIFFTOOL_NO_PROMPT &&
180 diff=$(git difftool branch) &&
181 test "$diff" = "branch" &&
183 restore_test_defaults
186 # git-difftool supports the difftool.prompt variable.
187 # Test that GIT_DIFFTOOL_PROMPT can override difftool.prompt = false
188 test_expect_success PERL 'GIT_DIFFTOOL_PROMPT variable' '
189 git config difftool.prompt false &&
190 GIT_DIFFTOOL_PROMPT=true &&
191 export GIT_DIFFTOOL_PROMPT &&
193 prompt=$(echo | git difftool branch | tail -1) &&
194 prompt_given "$prompt" &&
196 restore_test_defaults
199 # Test that we don't have to pass --no-prompt when difftool.prompt is false
200 test_expect_success PERL 'difftool.prompt config variable is false' '
201 git config difftool.prompt false &&
203 diff=$(git difftool branch) &&
204 test "$diff" = "branch" &&
206 restore_test_defaults
209 # Test that we don't have to pass --no-prompt when mergetool.prompt is false
210 test_expect_success PERL 'difftool merge.prompt = false' '
211 test_might_fail git config --unset difftool.prompt &&
212 git config mergetool.prompt false &&
214 diff=$(git difftool branch) &&
215 test "$diff" = "branch" &&
217 restore_test_defaults
220 # Test that the -y flag can override difftool.prompt = true
221 test_expect_success PERL 'difftool.prompt can overridden with -y' '
222 git config difftool.prompt true &&
224 diff=$(git difftool -y branch) &&
225 test "$diff" = "branch" &&
227 restore_test_defaults
230 # Test that the --prompt flag can override difftool.prompt = false
231 test_expect_success PERL 'difftool.prompt can overridden with --prompt' '
232 git config difftool.prompt false &&
234 prompt=$(echo | git difftool --prompt branch | tail -1) &&
235 prompt_given "$prompt" &&
237 restore_test_defaults
240 # Test that the last flag passed on the command-line wins
241 test_expect_success PERL 'difftool last flag wins' '
242 diff=$(git difftool --prompt --no-prompt branch) &&
243 test "$diff" = "branch" &&
245 restore_test_defaults &&
247 prompt=$(echo | git difftool --no-prompt --prompt branch | tail -1) &&
248 prompt_given "$prompt" &&
250 restore_test_defaults
253 # git-difftool falls back to git-mergetool config variables
254 # so test that behavior here
255 test_expect_success PERL 'difftool + mergetool config variables' '
256 remove_config_vars &&
257 git config merge.tool test-tool &&
258 git config mergetool.test-tool.cmd "cat \$LOCAL" &&
260 diff=$(git difftool --no-prompt branch) &&
261 test "$diff" = "branch" &&
263 # set merge.tool to something bogus, diff.tool to test-tool
264 git config merge.tool bogus-tool &&
265 git config diff.tool test-tool &&
267 diff=$(git difftool --no-prompt branch) &&
268 test "$diff" = "branch" &&
270 restore_test_defaults
273 test_expect_success PERL 'difftool.<tool>.path' '
274 git config difftool.tkdiff.path echo &&
275 diff=$(git difftool --tool=tkdiff --no-prompt branch) &&
276 git config --unset difftool.tkdiff.path &&
277 lines=$(echo "$diff" | grep file | wc -l) &&
278 test "$lines" -eq 1 &&
280 restore_test_defaults
283 test_expect_success PERL 'difftool --extcmd=cat' '
284 diff=$(git difftool --no-prompt --extcmd=cat branch) &&
285 test "$diff" = branch"$LF"master
288 test_expect_success PERL 'difftool --extcmd cat' '
289 diff=$(git difftool --no-prompt --extcmd cat branch) &&
290 test "$diff" = branch"$LF"master
293 test_expect_success PERL 'difftool -x cat' '
294 diff=$(git difftool --no-prompt -x cat branch) &&
295 test "$diff" = branch"$LF"master
298 test_expect_success PERL 'difftool --extcmd echo arg1' '
299 diff=$(git difftool --no-prompt --extcmd sh\ -c\ \"echo\ \$1\" branch) &&
300 test "$diff" = file
303 test_expect_success PERL 'difftool --extcmd cat arg1' '
304 diff=$(git difftool --no-prompt --extcmd sh\ -c\ \"cat\ \$1\" branch) &&
305 test "$diff" = master
308 test_expect_success PERL 'difftool --extcmd cat arg2' '
309 diff=$(git difftool --no-prompt --extcmd sh\ -c\ \"cat\ \$2\" branch) &&
310 test "$diff" = branch
313 # Create a second file on master and a different version on branch
314 test_expect_success PERL 'setup with 2 files different' '
315 echo m2 >file2 &&
316 git add file2 &&
317 git commit -m "added file2" &&
319 git checkout branch &&
320 echo br2 >file2 &&
321 git add file2 &&
322 git commit -a -m "branch changed file2" &&
323 git checkout master
326 test_expect_success PERL 'say no to the first file' '
327 diff=$( (echo n; echo) | git difftool -x cat branch ) &&
329 echo "$diff" | stdin_contains m2 &&
330 echo "$diff" | stdin_contains br2 &&
331 echo "$diff" | stdin_doesnot_contain master &&
332 echo "$diff" | stdin_doesnot_contain branch
335 test_expect_success PERL 'say no to the second file' '
336 diff=$( (echo; echo n) | git difftool -x cat branch ) &&
338 echo "$diff" | stdin_contains master &&
339 echo "$diff" | stdin_contains branch &&
340 echo "$diff" | stdin_doesnot_contain m2 &&
341 echo "$diff" | stdin_doesnot_contain br2
344 test_expect_success PERL 'difftool --tool-help' '
345 tool_help=$(git difftool --tool-help) &&
346 echo "$tool_help" | stdin_contains tool
349 test_expect_success PERL 'setup change in subdirectory' '
350 git checkout master &&
351 mkdir sub &&
352 echo master >sub/sub &&
353 git add sub/sub &&
354 git commit -m "added sub/sub" &&
355 echo test >>file &&
356 echo test >>sub/sub &&
357 git add . &&
358 git commit -m "modified both"
361 test_expect_success PERL 'difftool -d' '
362 diff=$(git difftool -d --extcmd ls branch) &&
363 echo "$diff" | stdin_contains sub &&
364 echo "$diff" | stdin_contains file
367 test_expect_success PERL 'difftool --dir-diff' '
368 diff=$(git difftool --dir-diff --extcmd ls branch) &&
369 echo "$diff" | stdin_contains sub &&
370 echo "$diff" | stdin_contains file
373 test_expect_success PERL 'difftool --dir-diff ignores --prompt' '
374 diff=$(git difftool --dir-diff --prompt --extcmd ls branch) &&
375 echo "$diff" | stdin_contains sub &&
376 echo "$diff" | stdin_contains file
379 test_expect_success PERL 'difftool --dir-diff from subdirectory' '
381 cd sub &&
382 diff=$(git difftool --dir-diff --extcmd ls branch) &&
383 echo "$diff" | stdin_contains sub &&
384 echo "$diff" | stdin_contains file
388 test_done