]> git.ipfire.org Git - thirdparty/git.git/blame - t/t7800-difftool.sh
Git 2.4.11
[thirdparty/git.git] / t / t7800-difftool.sh
CommitLineData
f92f2038
DA
1#!/bin/sh
2#
9e5a86f2 3# Copyright (c) 2009, 2010, 2012, 2013 David Aguilar
f92f2038
DA
4#
5
6test_description='git-difftool
7
8Testing basic diff tool invocation
9'
10
11. ./test-lib.sh
12
e42360c4 13difftool_test_setup ()
f92f2038 14{
e42360c4
DA
15 test_config diff.tool test-tool &&
16 test_config difftool.test-tool.cmd 'cat "$LOCAL"' &&
17 test_config difftool.bogus-tool.cmd false
f92f2038
DA
18}
19
e42360c4 20prompt_given ()
a904392e
DA
21{
22 prompt="$1"
ba959de1
SC
23 test "$prompt" = "Launch 'test-tool' [Y/n]: branch"
24}
25
f92f2038 26# Create a file on master and change it on branch
2c4f3026 27test_expect_success PERL 'setup' '
f92f2038
DA
28 echo master >file &&
29 git add file &&
30 git commit -m "added file" &&
31
32 git checkout -b branch master &&
33 echo branch >file &&
34 git commit -a -m "branch changed file" &&
35 git checkout master
36'
37
38# Configure a custom difftool.<tool>.cmd and use it
2c4f3026 39test_expect_success PERL 'custom commands' '
e42360c4
DA
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 &&
f92f2038 45
e42360c4
DA
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
f92f2038
DA
50'
51
e42360c4 52test_expect_success PERL 'custom tool commands override built-ins' '
f469e840 53 test_config difftool.vimdiff.cmd "cat \"\$REMOTE\"" &&
e42360c4 54 echo master >expect &&
f469e840 55 git difftool --tool vimdiff --no-prompt branch >actual &&
e42360c4 56 test_cmp expect actual
a427ef7a
DA
57'
58
2c4f3026 59test_expect_success PERL 'difftool ignores bad --tool values' '
e42360c4 60 : >expect &&
89294d14 61 test_must_fail \
e42360c4
DA
62 git difftool --no-prompt --tool=bad-tool branch >actual &&
63 test_cmp expect actual
f92f2038
DA
64'
65
d50b2c73 66test_expect_success PERL 'difftool forwards arguments to diff' '
e42360c4 67 difftool_test_setup &&
d50b2c73
DA
68 >for-diff &&
69 git add for-diff &&
70 echo changes>for-diff &&
71 git add for-diff &&
e42360c4
DA
72 : >expect &&
73 git difftool --cached --no-prompt -- for-diff >actual &&
74 test_cmp expect actual &&
d50b2c73
DA
75 git reset -- for-diff &&
76 rm for-diff
77'
78
2b52123f
DA
79test_expect_success PERL 'difftool ignores exit code' '
80 test_config difftool.error.cmd false &&
81 git difftool -y -t error branch
82'
83
84test_expect_success PERL 'difftool forwards exit code with --trust-exit-code' '
85 test_config difftool.error.cmd false &&
86 test_must_fail git difftool -y --trust-exit-code -t error branch
87'
88
99474b63
DA
89test_expect_success PERL 'difftool forwards exit code with --trust-exit-code for built-ins' '
90 test_config difftool.vimdiff.path false &&
91 test_must_fail git difftool -y --trust-exit-code -t vimdiff branch
92'
93
2b52123f
DA
94test_expect_success PERL 'difftool honors difftool.trustExitCode = true' '
95 test_config difftool.error.cmd false &&
96 test_config difftool.trustExitCode true &&
97 test_must_fail git difftool -y -t error branch
98'
99
100test_expect_success PERL 'difftool honors difftool.trustExitCode = false' '
101 test_config difftool.error.cmd false &&
102 test_config difftool.trustExitCode false &&
103 git difftool -y -t error branch
104'
105
106test_expect_success PERL 'difftool ignores exit code with --no-trust-exit-code' '
107 test_config difftool.error.cmd false &&
108 test_config difftool.trustExitCode true &&
109 git difftool -y --no-trust-exit-code -t error branch
110'
111
112test_expect_success PERL 'difftool stops on error with --trust-exit-code' '
113 test_when_finished "rm -f for-diff .git/fail-right-file" &&
114 test_when_finished "git reset -- for-diff" &&
115 write_script .git/fail-right-file <<-\EOF &&
116 echo "$2"
117 exit 1
118 EOF
119 >for-diff &&
120 git add for-diff &&
121 echo file >expect &&
122 test_must_fail git difftool -y --trust-exit-code \
123 --extcmd .git/fail-right-file branch >actual &&
124 test_cmp expect actual
125'
126
2c4f3026 127test_expect_success PERL 'difftool honors --gui' '
e42360c4
DA
128 difftool_test_setup &&
129 test_config merge.tool bogus-tool &&
130 test_config diff.tool bogus-tool &&
131 test_config diff.guitool test-tool &&
4cefa495 132
e42360c4
DA
133 echo branch >expect &&
134 git difftool --no-prompt --gui branch >actual &&
135 test_cmp expect actual
4cefa495
DA
136'
137
85089604 138test_expect_success PERL 'difftool --gui last setting wins' '
e42360c4
DA
139 difftool_test_setup &&
140 : >expect &&
141 git difftool --no-prompt --gui --no-gui >actual &&
142 test_cmp expect actual &&
85089604 143
e42360c4
DA
144 test_config merge.tool bogus-tool &&
145 test_config diff.tool bogus-tool &&
146 test_config diff.guitool test-tool &&
147 echo branch >expect &&
148 git difftool --no-prompt --no-gui --gui branch >actual &&
149 test_cmp expect actual
85089604
TH
150'
151
2c4f3026 152test_expect_success PERL 'difftool --gui works without configured diff.guitool' '
e42360c4
DA
153 difftool_test_setup &&
154 echo branch >expect &&
155 git difftool --no-prompt --gui branch >actual &&
156 test_cmp expect actual
42accaec
DA
157'
158
f92f2038 159# Specify the diff tool using $GIT_DIFF_TOOL
2c4f3026 160test_expect_success PERL 'GIT_DIFF_TOOL variable' '
e42360c4
DA
161 difftool_test_setup &&
162 git config --unset diff.tool &&
163 echo branch >expect &&
164 GIT_DIFF_TOOL=test-tool git difftool --no-prompt branch >actual &&
165 test_cmp expect actual
f92f2038
DA
166'
167
168# Test the $GIT_*_TOOL variables and ensure
169# that $GIT_DIFF_TOOL always wins unless --tool is specified
2c4f3026 170test_expect_success PERL 'GIT_DIFF_TOOL overrides' '
e42360c4
DA
171 difftool_test_setup &&
172 test_config diff.tool bogus-tool &&
173 test_config merge.tool bogus-tool &&
f92f2038 174
e42360c4
DA
175 echo branch >expect &&
176 GIT_DIFF_TOOL=test-tool git difftool --no-prompt branch >actual &&
177 test_cmp expect actual &&
f92f2038 178
e42360c4
DA
179 test_config diff.tool bogus-tool &&
180 test_config merge.tool bogus-tool &&
181 GIT_DIFF_TOOL=bogus-tool \
182 git difftool --no-prompt --tool=test-tool branch >actual &&
183 test_cmp expect actual
f92f2038
DA
184'
185
186# Test that we don't have to pass --no-prompt to difftool
187# when $GIT_DIFFTOOL_NO_PROMPT is true
2c4f3026 188test_expect_success PERL 'GIT_DIFFTOOL_NO_PROMPT variable' '
e42360c4
DA
189 difftool_test_setup &&
190 echo branch >expect &&
191 GIT_DIFFTOOL_NO_PROMPT=true git difftool branch >actual &&
192 test_cmp expect actual
f92f2038
DA
193'
194
a904392e
DA
195# git-difftool supports the difftool.prompt variable.
196# Test that GIT_DIFFTOOL_PROMPT can override difftool.prompt = false
2c4f3026 197test_expect_success PERL 'GIT_DIFFTOOL_PROMPT variable' '
e42360c4
DA
198 difftool_test_setup &&
199 test_config difftool.prompt false &&
200 echo >input &&
201 GIT_DIFFTOOL_PROMPT=true git difftool branch <input >output &&
202 prompt=$(tail -1 <output) &&
203 prompt_given "$prompt"
a904392e
DA
204'
205
206# Test that we don't have to pass --no-prompt when difftool.prompt is false
2c4f3026 207test_expect_success PERL 'difftool.prompt config variable is false' '
e42360c4
DA
208 difftool_test_setup &&
209 test_config difftool.prompt false &&
210 echo branch >expect &&
211 git difftool branch >actual &&
212 test_cmp expect actual
a904392e
DA
213'
214
a88183f1 215# Test that we don't have to pass --no-prompt when mergetool.prompt is false
2c4f3026 216test_expect_success PERL 'difftool merge.prompt = false' '
e42360c4 217 difftool_test_setup &&
bc0f35ca 218 test_might_fail git config --unset difftool.prompt &&
e42360c4
DA
219 test_config mergetool.prompt false &&
220 echo branch >expect &&
221 git difftool branch >actual &&
222 test_cmp expect actual
a88183f1
DA
223'
224
a904392e 225# Test that the -y flag can override difftool.prompt = true
2c4f3026 226test_expect_success PERL 'difftool.prompt can overridden with -y' '
e42360c4
DA
227 difftool_test_setup &&
228 test_config difftool.prompt true &&
229 echo branch >expect &&
230 git difftool -y branch >actual &&
231 test_cmp expect actual
a904392e
DA
232'
233
234# Test that the --prompt flag can override difftool.prompt = false
2c4f3026 235test_expect_success PERL 'difftool.prompt can overridden with --prompt' '
e42360c4
DA
236 difftool_test_setup &&
237 test_config difftool.prompt false &&
238 echo >input &&
239 git difftool --prompt branch <input >output &&
240 prompt=$(tail -1 <output) &&
241 prompt_given "$prompt"
a904392e
DA
242'
243
244# Test that the last flag passed on the command-line wins
2c4f3026 245test_expect_success PERL 'difftool last flag wins' '
e42360c4
DA
246 difftool_test_setup &&
247 echo branch >expect &&
248 git difftool --prompt --no-prompt branch >actual &&
249 test_cmp expect actual &&
250 echo >input &&
251 git difftool --no-prompt --prompt branch <input >output &&
252 prompt=$(tail -1 <output) &&
253 prompt_given "$prompt"
a904392e
DA
254'
255
f92f2038
DA
256# git-difftool falls back to git-mergetool config variables
257# so test that behavior here
2c4f3026 258test_expect_success PERL 'difftool + mergetool config variables' '
e42360c4
DA
259 test_config merge.tool test-tool &&
260 test_config mergetool.test-tool.cmd "cat \$LOCAL" &&
261 echo branch >expect &&
262 git difftool --no-prompt branch >actual &&
263 test_cmp expect actual &&
f92f2038
DA
264
265 # set merge.tool to something bogus, diff.tool to test-tool
e42360c4
DA
266 test_config merge.tool bogus-tool &&
267 test_config diff.tool test-tool &&
268 git difftool --no-prompt branch >actual &&
269 test_cmp expect actual
f92f2038
DA
270'
271
2c4f3026 272test_expect_success PERL 'difftool.<tool>.path' '
e42360c4
DA
273 test_config difftool.tkdiff.path echo &&
274 git difftool --tool=tkdiff --no-prompt branch >output &&
275 lines=$(grep file output | wc -l) &&
276 test "$lines" -eq 1
1c6f5b52
DA
277'
278
2c4f3026 279test_expect_success PERL 'difftool --extcmd=cat' '
e42360c4
DA
280 echo branch >expect &&
281 echo master >>expect &&
282 git difftool --no-prompt --extcmd=cat branch >actual &&
283 test_cmp expect actual
f47f1e2c 284'
1c6f5b52 285
2c4f3026 286test_expect_success PERL 'difftool --extcmd cat' '
e42360c4
DA
287 echo branch >expect &&
288 echo master >>expect &&
289 git difftool --no-prompt --extcmd=cat branch >actual &&
290 test_cmp expect actual
f47f1e2c 291'
1c6f5b52 292
2c4f3026 293test_expect_success PERL 'difftool -x cat' '
e42360c4
DA
294 echo branch >expect &&
295 echo master >>expect &&
296 git difftool --no-prompt -x cat branch >actual &&
297 test_cmp expect actual
9f3d54d1
DA
298'
299
2c4f3026 300test_expect_success PERL 'difftool --extcmd echo arg1' '
e42360c4
DA
301 echo file >expect &&
302 git difftool --no-prompt \
303 --extcmd sh\ -c\ \"echo\ \$1\" branch >actual &&
304 test_cmp expect actual
9f3d54d1 305'
1c6f5b52 306
2c4f3026 307test_expect_success PERL 'difftool --extcmd cat arg1' '
e42360c4
DA
308 echo master >expect &&
309 git difftool --no-prompt \
310 --extcmd sh\ -c\ \"cat\ \$1\" branch >actual &&
311 test_cmp expect actual
9f3d54d1 312'
1c6f5b52 313
2c4f3026 314test_expect_success PERL 'difftool --extcmd cat arg2' '
e42360c4
DA
315 echo branch >expect &&
316 git difftool --no-prompt \
317 --extcmd sh\ -c\ \"cat\ \$2\" branch >actual &&
318 test_cmp expect actual
f92f2038
DA
319'
320
ba959de1
SC
321# Create a second file on master and a different version on branch
322test_expect_success PERL 'setup with 2 files different' '
323 echo m2 >file2 &&
324 git add file2 &&
325 git commit -m "added file2" &&
326
327 git checkout branch &&
328 echo br2 >file2 &&
329 git add file2 &&
330 git commit -a -m "branch changed file2" &&
331 git checkout master
332'
333
334test_expect_success PERL 'say no to the first file' '
e42360c4
DA
335 (echo n && echo) >input &&
336 git difftool -x cat branch <input >output &&
472353a5
JK
337 grep m2 output &&
338 grep br2 output &&
339 ! grep master output &&
340 ! grep branch output
ba959de1
SC
341'
342
343test_expect_success PERL 'say no to the second file' '
e42360c4
DA
344 (echo && echo n) >input &&
345 git difftool -x cat branch <input >output &&
472353a5
JK
346 grep master output &&
347 grep branch output &&
348 ! grep m2 output &&
349 ! grep br2 output
ba959de1
SC
350'
351
25098690
JS
352test_expect_success PERL 'ending prompt input with EOF' '
353 git difftool -x cat branch </dev/null >output &&
354 ! grep master output &&
355 ! grep branch output &&
356 ! grep m2 output &&
357 ! grep br2 output
358'
359
bf73fc21 360test_expect_success PERL 'difftool --tool-help' '
e42360c4 361 git difftool --tool-help >output &&
472353a5 362 grep tool output
bf73fc21
TH
363'
364
7e0abcec
TH
365test_expect_success PERL 'setup change in subdirectory' '
366 git checkout master &&
367 mkdir sub &&
368 echo master >sub/sub &&
369 git add sub/sub &&
370 git commit -m "added sub/sub" &&
371 echo test >>file &&
372 echo test >>sub/sub &&
3caf5a93 373 git add file sub/sub &&
7e0abcec
TH
374 git commit -m "modified both"
375'
376
e01afdb7
JK
377run_dir_diff_test () {
378 test_expect_success PERL "$1 --no-symlinks" "
379 symlinks=--no-symlinks &&
380 $2
381 "
382 test_expect_success PERL,SYMLINKS "$1 --symlinks" "
383 symlinks=--symlinks &&
384 $2
385 "
386}
387
388run_dir_diff_test 'difftool -d' '
389 git difftool -d $symlinks --extcmd ls branch >output &&
472353a5
JK
390 grep sub output &&
391 grep file output
7e0abcec
TH
392'
393
e01afdb7
JK
394run_dir_diff_test 'difftool --dir-diff' '
395 git difftool --dir-diff $symlinks --extcmd ls branch >output &&
472353a5
JK
396 grep sub output &&
397 grep file output
7e0abcec
TH
398'
399
e01afdb7
JK
400run_dir_diff_test 'difftool --dir-diff ignores --prompt' '
401 git difftool --dir-diff $symlinks --prompt --extcmd ls branch >output &&
472353a5
JK
402 grep sub output &&
403 grep file output
bf341b90
JK
404'
405
e01afdb7 406run_dir_diff_test 'difftool --dir-diff from subdirectory' '
bf341b90
JK
407 (
408 cd sub &&
e01afdb7 409 git difftool --dir-diff $symlinks --extcmd ls branch >output &&
472353a5
JK
410 grep sub output &&
411 grep file output
bf341b90
JK
412 )
413'
414
1f197a1d
JK
415run_dir_diff_test 'difftool --dir-diff when worktree file is missing' '
416 test_when_finished git reset --hard &&
417 rm file2 &&
418 git difftool --dir-diff $symlinks --extcmd ls branch master >output &&
419 grep file2 output
420'
421
02c56314
JK
422write_script .git/CHECK_SYMLINKS <<\EOF
423for f in file file2 sub/sub
424do
425 echo "$f"
426 readlink "$2/$f"
427done >actual
428EOF
429
430test_expect_success PERL,SYMLINKS 'difftool --dir-diff --symlink without unstaged changes' '
431 cat >expect <<-EOF &&
432 file
433 $(pwd)/file
434 file2
435 $(pwd)/file2
436 sub/sub
437 $(pwd)/sub/sub
438 EOF
439 git difftool --dir-diff --symlink \
440 --extcmd "./.git/CHECK_SYMLINKS" branch HEAD &&
441 test_cmp actual expect
442'
443
32eaf1de
KS
444write_script modify-right-file <<\EOF
445echo "new content" >"$2/file"
446EOF
447
448run_dir_diff_test 'difftool --dir-diff syncs worktree with unstaged change' '
449 test_when_finished git reset --hard &&
450 echo "orig content" >file &&
451 git difftool -d $symlinks --extcmd "$(pwd)/modify-right-file" branch &&
452 echo "new content" >expect &&
453 test_cmp expect file
454'
455
456run_dir_diff_test 'difftool --dir-diff syncs worktree without unstaged change' '
457 test_when_finished git reset --hard &&
458 git difftool -d $symlinks --extcmd "$(pwd)/modify-right-file" branch &&
459 echo "new content" >expect &&
460 test_cmp expect file
461'
462
67aa147a
JK
463write_script modify-file <<\EOF
464echo "new content" >file
465EOF
466
467test_expect_success PERL 'difftool --no-symlinks does not overwrite working tree file ' '
468 echo "orig content" >file &&
469 git difftool --dir-diff --no-symlinks --extcmd "$(pwd)/modify-file" branch &&
470 echo "new content" >expect &&
471 test_cmp expect file
472'
473
474write_script modify-both-files <<\EOF
475echo "wt content" >file &&
476echo "tmp content" >"$2/file" &&
477echo "$2" >tmpdir
478EOF
479
480test_expect_success PERL 'difftool --no-symlinks detects conflict ' '
481 (
482 TMPDIR=$TRASH_DIRECTORY &&
483 export TMPDIR &&
484 echo "orig content" >file &&
485 test_must_fail git difftool --dir-diff --no-symlinks --extcmd "$(pwd)/modify-both-files" branch &&
486 echo "wt content" >expect &&
487 test_cmp expect file &&
488 echo "tmp content" >expect &&
489 test_cmp expect "$(cat tmpdir)/file"
490 )
491'
492
fcfec8bd
JH
493test_expect_success PERL 'difftool properly honors gitlink and core.worktree' '
494 git submodule add ./. submod/ule &&
495 (
496 cd submod/ule &&
497 test_config diff.tool checktrees &&
498 test_config difftool.checktrees.cmd '\''
499 test -d "$LOCAL" && test -d "$REMOTE" && echo good
500 '\'' &&
501 echo good >expect &&
502 git difftool --tool=checktrees --dir-diff HEAD~ >actual &&
503 test_cmp expect actual
504 )
505'
506
f92f2038 507test_done