difftool--helper: exit when reading a prompt answer fails
[git/debian.git] / t / t7800-difftool.sh
blob9cf5dc9347971438b50e2a23cbb005758106ab5e
1 #!/bin/sh
3 # Copyright (c) 2009, 2010, 2012, 2013 David Aguilar
6 test_description='git-difftool
8 Testing basic diff tool invocation
11 . ./test-lib.sh
13 difftool_test_setup ()
15 test_config diff.tool test-tool &&
16 test_config difftool.test-tool.cmd 'cat "$LOCAL"' &&
17 test_config difftool.bogus-tool.cmd false
20 prompt_given ()
22 prompt="$1"
23 test "$prompt" = "Launch 'test-tool' [Y/n]: branch"
26 # Create a file on master and change it on branch
27 test_expect_success PERL 'setup' '
28 echo master >file &&
29 git add file &&
30 git commit -m "added file" &&
32 git checkout -b branch master &&
33 echo branch >file &&
34 git commit -a -m "branch changed file" &&
35 git checkout master
38 # Configure a custom difftool.<tool>.cmd and use it
39 test_expect_success PERL 'custom commands' '
40 difftool_test_setup &&
41 test_config difftool.test-tool.cmd "cat \"\$REMOTE\"" &&
42 echo master >expect &&
43 git difftool --no-prompt branch >actual &&
44 test_cmp expect actual &&
46 test_config difftool.test-tool.cmd "cat \"\$LOCAL\"" &&
47 echo branch >expect &&
48 git difftool --no-prompt branch >actual &&
49 test_cmp expect actual
52 test_expect_success PERL 'custom tool commands override built-ins' '
53 test_config difftool.vimdiff.cmd "cat \"\$REMOTE\"" &&
54 echo master >expect &&
55 git difftool --tool vimdiff --no-prompt branch >actual &&
56 test_cmp expect actual
59 test_expect_success PERL 'difftool ignores bad --tool values' '
60 : >expect &&
61 test_must_fail \
62 git difftool --no-prompt --tool=bad-tool branch >actual &&
63 test_cmp expect actual
66 test_expect_success PERL 'difftool forwards arguments to diff' '
67 difftool_test_setup &&
68 >for-diff &&
69 git add for-diff &&
70 echo changes>for-diff &&
71 git add for-diff &&
72 : >expect &&
73 git difftool --cached --no-prompt -- for-diff >actual &&
74 test_cmp expect actual &&
75 git reset -- for-diff &&
76 rm for-diff
79 test_expect_success PERL 'difftool honors --gui' '
80 difftool_test_setup &&
81 test_config merge.tool bogus-tool &&
82 test_config diff.tool bogus-tool &&
83 test_config diff.guitool test-tool &&
85 echo branch >expect &&
86 git difftool --no-prompt --gui branch >actual &&
87 test_cmp expect actual
90 test_expect_success PERL 'difftool --gui last setting wins' '
91 difftool_test_setup &&
92 : >expect &&
93 git difftool --no-prompt --gui --no-gui >actual &&
94 test_cmp expect actual &&
96 test_config merge.tool bogus-tool &&
97 test_config diff.tool bogus-tool &&
98 test_config diff.guitool test-tool &&
99 echo branch >expect &&
100 git difftool --no-prompt --no-gui --gui branch >actual &&
101 test_cmp expect actual
104 test_expect_success PERL 'difftool --gui works without configured diff.guitool' '
105 difftool_test_setup &&
106 echo branch >expect &&
107 git difftool --no-prompt --gui branch >actual &&
108 test_cmp expect actual
111 # Specify the diff tool using $GIT_DIFF_TOOL
112 test_expect_success PERL 'GIT_DIFF_TOOL variable' '
113 difftool_test_setup &&
114 git config --unset diff.tool &&
115 echo branch >expect &&
116 GIT_DIFF_TOOL=test-tool git difftool --no-prompt branch >actual &&
117 test_cmp expect actual
120 # Test the $GIT_*_TOOL variables and ensure
121 # that $GIT_DIFF_TOOL always wins unless --tool is specified
122 test_expect_success PERL 'GIT_DIFF_TOOL overrides' '
123 difftool_test_setup &&
124 test_config diff.tool bogus-tool &&
125 test_config merge.tool bogus-tool &&
127 echo branch >expect &&
128 GIT_DIFF_TOOL=test-tool git difftool --no-prompt branch >actual &&
129 test_cmp expect actual &&
131 test_config diff.tool bogus-tool &&
132 test_config merge.tool bogus-tool &&
133 GIT_DIFF_TOOL=bogus-tool \
134 git difftool --no-prompt --tool=test-tool branch >actual &&
135 test_cmp expect actual
138 # Test that we don't have to pass --no-prompt to difftool
139 # when $GIT_DIFFTOOL_NO_PROMPT is true
140 test_expect_success PERL 'GIT_DIFFTOOL_NO_PROMPT variable' '
141 difftool_test_setup &&
142 echo branch >expect &&
143 GIT_DIFFTOOL_NO_PROMPT=true git difftool branch >actual &&
144 test_cmp expect actual
147 # git-difftool supports the difftool.prompt variable.
148 # Test that GIT_DIFFTOOL_PROMPT can override difftool.prompt = false
149 test_expect_success PERL 'GIT_DIFFTOOL_PROMPT variable' '
150 difftool_test_setup &&
151 test_config difftool.prompt false &&
152 echo >input &&
153 GIT_DIFFTOOL_PROMPT=true git difftool branch <input >output &&
154 prompt=$(tail -1 <output) &&
155 prompt_given "$prompt"
158 # Test that we don't have to pass --no-prompt when difftool.prompt is false
159 test_expect_success PERL 'difftool.prompt config variable is false' '
160 difftool_test_setup &&
161 test_config difftool.prompt false &&
162 echo branch >expect &&
163 git difftool branch >actual &&
164 test_cmp expect actual
167 # Test that we don't have to pass --no-prompt when mergetool.prompt is false
168 test_expect_success PERL 'difftool merge.prompt = false' '
169 difftool_test_setup &&
170 test_might_fail git config --unset difftool.prompt &&
171 test_config mergetool.prompt false &&
172 echo branch >expect &&
173 git difftool branch >actual &&
174 test_cmp expect actual
177 # Test that the -y flag can override difftool.prompt = true
178 test_expect_success PERL 'difftool.prompt can overridden with -y' '
179 difftool_test_setup &&
180 test_config difftool.prompt true &&
181 echo branch >expect &&
182 git difftool -y branch >actual &&
183 test_cmp expect actual
186 # Test that the --prompt flag can override difftool.prompt = false
187 test_expect_success PERL 'difftool.prompt can overridden with --prompt' '
188 difftool_test_setup &&
189 test_config difftool.prompt false &&
190 echo >input &&
191 git difftool --prompt branch <input >output &&
192 prompt=$(tail -1 <output) &&
193 prompt_given "$prompt"
196 # Test that the last flag passed on the command-line wins
197 test_expect_success PERL 'difftool last flag wins' '
198 difftool_test_setup &&
199 echo branch >expect &&
200 git difftool --prompt --no-prompt branch >actual &&
201 test_cmp expect actual &&
202 echo >input &&
203 git difftool --no-prompt --prompt branch <input >output &&
204 prompt=$(tail -1 <output) &&
205 prompt_given "$prompt"
208 # git-difftool falls back to git-mergetool config variables
209 # so test that behavior here
210 test_expect_success PERL 'difftool + mergetool config variables' '
211 test_config merge.tool test-tool &&
212 test_config mergetool.test-tool.cmd "cat \$LOCAL" &&
213 echo branch >expect &&
214 git difftool --no-prompt branch >actual &&
215 test_cmp expect actual &&
217 # set merge.tool to something bogus, diff.tool to test-tool
218 test_config merge.tool bogus-tool &&
219 test_config diff.tool test-tool &&
220 git difftool --no-prompt branch >actual &&
221 test_cmp expect actual
224 test_expect_success PERL 'difftool.<tool>.path' '
225 test_config difftool.tkdiff.path echo &&
226 git difftool --tool=tkdiff --no-prompt branch >output &&
227 lines=$(grep file output | wc -l) &&
228 test "$lines" -eq 1
231 test_expect_success PERL 'difftool --extcmd=cat' '
232 echo branch >expect &&
233 echo master >>expect &&
234 git difftool --no-prompt --extcmd=cat branch >actual &&
235 test_cmp expect actual
238 test_expect_success PERL 'difftool --extcmd cat' '
239 echo branch >expect &&
240 echo master >>expect &&
241 git difftool --no-prompt --extcmd=cat branch >actual &&
242 test_cmp expect actual
245 test_expect_success PERL 'difftool -x cat' '
246 echo branch >expect &&
247 echo master >>expect &&
248 git difftool --no-prompt -x cat branch >actual &&
249 test_cmp expect actual
252 test_expect_success PERL 'difftool --extcmd echo arg1' '
253 echo file >expect &&
254 git difftool --no-prompt \
255 --extcmd sh\ -c\ \"echo\ \$1\" branch >actual &&
256 test_cmp expect actual
259 test_expect_success PERL 'difftool --extcmd cat arg1' '
260 echo master >expect &&
261 git difftool --no-prompt \
262 --extcmd sh\ -c\ \"cat\ \$1\" branch >actual &&
263 test_cmp expect actual
266 test_expect_success PERL 'difftool --extcmd cat arg2' '
267 echo branch >expect &&
268 git difftool --no-prompt \
269 --extcmd sh\ -c\ \"cat\ \$2\" branch >actual &&
270 test_cmp expect actual
273 # Create a second file on master and a different version on branch
274 test_expect_success PERL 'setup with 2 files different' '
275 echo m2 >file2 &&
276 git add file2 &&
277 git commit -m "added file2" &&
279 git checkout branch &&
280 echo br2 >file2 &&
281 git add file2 &&
282 git commit -a -m "branch changed file2" &&
283 git checkout master
286 test_expect_success PERL 'say no to the first file' '
287 (echo n && echo) >input &&
288 git difftool -x cat branch <input >output &&
289 grep m2 output &&
290 grep br2 output &&
291 ! grep master output &&
292 ! grep branch output
295 test_expect_success PERL 'say no to the second file' '
296 (echo && echo n) >input &&
297 git difftool -x cat branch <input >output &&
298 grep master output &&
299 grep branch output &&
300 ! grep m2 output &&
301 ! grep br2 output
304 test_expect_success PERL 'ending prompt input with EOF' '
305 git difftool -x cat branch </dev/null >output &&
306 ! grep master output &&
307 ! grep branch output &&
308 ! grep m2 output &&
309 ! grep br2 output
312 test_expect_success PERL 'difftool --tool-help' '
313 git difftool --tool-help >output &&
314 grep tool output
317 test_expect_success PERL 'setup change in subdirectory' '
318 git checkout master &&
319 mkdir sub &&
320 echo master >sub/sub &&
321 git add sub/sub &&
322 git commit -m "added sub/sub" &&
323 echo test >>file &&
324 echo test >>sub/sub &&
325 git add file sub/sub &&
326 git commit -m "modified both"
329 run_dir_diff_test () {
330 test_expect_success PERL "$1 --no-symlinks" "
331 symlinks=--no-symlinks &&
334 test_expect_success PERL,SYMLINKS "$1 --symlinks" "
335 symlinks=--symlinks &&
340 run_dir_diff_test 'difftool -d' '
341 git difftool -d $symlinks --extcmd ls branch >output &&
342 grep sub output &&
343 grep file output
346 run_dir_diff_test 'difftool --dir-diff' '
347 git difftool --dir-diff $symlinks --extcmd ls branch >output &&
348 grep sub output &&
349 grep file output
352 run_dir_diff_test 'difftool --dir-diff ignores --prompt' '
353 git difftool --dir-diff $symlinks --prompt --extcmd ls branch >output &&
354 grep sub output &&
355 grep file output
358 run_dir_diff_test 'difftool --dir-diff from subdirectory' '
360 cd sub &&
361 git difftool --dir-diff $symlinks --extcmd ls branch >output &&
362 grep sub output &&
363 grep file output
367 run_dir_diff_test 'difftool --dir-diff when worktree file is missing' '
368 test_when_finished git reset --hard &&
369 rm file2 &&
370 git difftool --dir-diff $symlinks --extcmd ls branch master >output &&
371 grep file2 output
374 write_script .git/CHECK_SYMLINKS <<\EOF
375 for f in file file2 sub/sub
377 echo "$f"
378 readlink "$2/$f"
379 done >actual
382 test_expect_success PERL,SYMLINKS 'difftool --dir-diff --symlink without unstaged changes' '
383 cat >expect <<-EOF &&
384 file
385 $(pwd)/file
386 file2
387 $(pwd)/file2
388 sub/sub
389 $(pwd)/sub/sub
391 git difftool --dir-diff --symlink \
392 --extcmd "./.git/CHECK_SYMLINKS" branch HEAD &&
393 test_cmp actual expect
396 write_script modify-right-file <<\EOF
397 echo "new content" >"$2/file"
400 run_dir_diff_test 'difftool --dir-diff syncs worktree with unstaged change' '
401 test_when_finished git reset --hard &&
402 echo "orig content" >file &&
403 git difftool -d $symlinks --extcmd "$(pwd)/modify-right-file" branch &&
404 echo "new content" >expect &&
405 test_cmp expect file
408 run_dir_diff_test 'difftool --dir-diff syncs worktree without unstaged change' '
409 test_when_finished git reset --hard &&
410 git difftool -d $symlinks --extcmd "$(pwd)/modify-right-file" branch &&
411 echo "new content" >expect &&
412 test_cmp expect file
415 write_script modify-file <<\EOF
416 echo "new content" >file
419 test_expect_success PERL 'difftool --no-symlinks does not overwrite working tree file ' '
420 echo "orig content" >file &&
421 git difftool --dir-diff --no-symlinks --extcmd "$(pwd)/modify-file" branch &&
422 echo "new content" >expect &&
423 test_cmp expect file
426 write_script modify-both-files <<\EOF
427 echo "wt content" >file &&
428 echo "tmp content" >"$2/file" &&
429 echo "$2" >tmpdir
432 test_expect_success PERL 'difftool --no-symlinks detects conflict ' '
434 TMPDIR=$TRASH_DIRECTORY &&
435 export TMPDIR &&
436 echo "orig content" >file &&
437 test_must_fail git difftool --dir-diff --no-symlinks --extcmd "$(pwd)/modify-both-files" branch &&
438 echo "wt content" >expect &&
439 test_cmp expect file &&
440 echo "tmp content" >expect &&
441 test_cmp expect "$(cat tmpdir)/file"
445 test_expect_success PERL 'difftool properly honors gitlink and core.worktree' '
446 git submodule add ./. submod/ule &&
448 cd submod/ule &&
449 test_config diff.tool checktrees &&
450 test_config difftool.checktrees.cmd '\''
451 test -d "$LOCAL" && test -d "$REMOTE" && echo good
452 '\'' &&
453 echo good >expect &&
454 git difftool --tool=checktrees --dir-diff HEAD~ >actual &&
455 test_cmp expect actual
459 test_done