Git fork
1#!/bin/sh
2#
3# Copyright (c) 2009, 2010, 2012, 2013 David Aguilar
4#
5
6test_description='git-difftool
7
8Testing basic diff tool invocation
9'
10
11GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
12export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
13
14. ./test-lib.sh
15
16difftool_test_setup ()
17{
18 test_config diff.tool test-tool &&
19 test_config difftool.test-tool.cmd 'cat "$LOCAL"' &&
20 test_config difftool.bogus-tool.cmd false
21}
22
23prompt_given ()
24{
25 prompt="$1"
26 test "$prompt" = "Launch 'test-tool' [Y/n]? branch"
27}
28
29test_expect_success 'basic usage requires no repo' '
30 test_expect_code 129 git difftool -h >output &&
31 test_grep ^usage: output &&
32 # create a ceiling directory to prevent Git from finding a repo
33 mkdir -p not/repo &&
34 test_when_finished rm -r not &&
35 test_expect_code 129 \
36 env GIT_CEILING_DIRECTORIES="$(pwd)/not" \
37 git -C not/repo difftool -h >output &&
38 test_grep ^usage: output
39'
40
41# Create a file on main and change it on branch
42test_expect_success 'setup' '
43 echo main >file &&
44 git add file &&
45 git commit -m "added file" &&
46
47 git checkout -b branch main &&
48 echo branch >file &&
49 git commit -a -m "branch changed file" &&
50 git checkout main
51'
52
53# Configure a custom difftool.<tool>.cmd and use it
54test_expect_success 'custom commands' '
55 difftool_test_setup &&
56 test_config difftool.test-tool.cmd "cat \"\$REMOTE\"" &&
57 echo main >expect &&
58 git difftool --no-prompt branch >actual &&
59 test_cmp expect actual &&
60
61 test_config difftool.test-tool.cmd "cat \"\$LOCAL\"" &&
62 echo branch >expect &&
63 git difftool --no-prompt branch >actual &&
64 test_cmp expect actual
65'
66
67test_expect_success 'custom tool commands override built-ins' '
68 test_config difftool.vimdiff.cmd "cat \"\$REMOTE\"" &&
69 echo main >expect &&
70 git difftool --tool vimdiff --no-prompt branch >actual &&
71 test_cmp expect actual
72'
73
74test_expect_success 'difftool ignores bad --tool values' '
75 : >expect &&
76 test_must_fail \
77 git difftool --no-prompt --tool=bad-tool branch >actual &&
78 test_cmp expect actual
79'
80
81test_expect_success 'difftool forwards arguments to diff' '
82 difftool_test_setup &&
83 >for-diff &&
84 git add for-diff &&
85 echo changes>for-diff &&
86 git add for-diff &&
87 : >expect &&
88 git difftool --cached --no-prompt -- for-diff >actual &&
89 test_cmp expect actual &&
90 git reset -- for-diff &&
91 rm for-diff
92'
93
94for opt in '' '--dir-diff'
95do
96 test_expect_success "difftool ${opt:-without options} ignores exit code" '
97 test_config difftool.error.cmd false &&
98 git difftool ${opt} -y -t error branch
99 '
100
101 test_expect_success "difftool ${opt:-without options} forwards exit code with --trust-exit-code" '
102 test_config difftool.error.cmd false &&
103 test_must_fail git difftool ${opt} -y --trust-exit-code -t error branch
104 '
105
106 test_expect_success "difftool ${opt:-without options} forwards exit code with --trust-exit-code for built-ins" '
107 test_config difftool.vimdiff.path false &&
108 test_must_fail git difftool ${opt} -y --trust-exit-code -t vimdiff branch
109 '
110
111 test_expect_success "difftool ${opt:-without options} honors difftool.trustExitCode = true" '
112 test_config difftool.error.cmd false &&
113 test_config difftool.trustExitCode true &&
114 test_must_fail git difftool ${opt} -y -t error branch
115 '
116
117 test_expect_success "difftool ${opt:-without options} honors difftool.trustExitCode = false" '
118 test_config difftool.error.cmd false &&
119 test_config difftool.trustExitCode false &&
120 git difftool ${opt} -y -t error branch
121 '
122
123 test_expect_success "difftool ${opt:-without options} ignores exit code with --no-trust-exit-code" '
124 test_config difftool.error.cmd false &&
125 test_config difftool.trustExitCode true &&
126 git difftool ${opt} -y --no-trust-exit-code -t error branch
127 '
128
129 test_expect_success "difftool ${opt:-without options} stops on error with --trust-exit-code" '
130 test_when_finished "rm -f for-diff .git/fail-right-file" &&
131 test_when_finished "git reset -- for-diff" &&
132 write_script .git/fail-right-file <<-\EOF &&
133 echo failed
134 exit 1
135 EOF
136 >for-diff &&
137 git add for-diff &&
138 test_must_fail git difftool ${opt} -y --trust-exit-code \
139 --extcmd .git/fail-right-file branch >actual &&
140 test_line_count = 1 actual
141 '
142
143 test_expect_success "difftool ${opt:-without options} honors exit status if command not found" '
144 test_config difftool.nonexistent.cmd i-dont-exist &&
145 test_config difftool.trustExitCode false &&
146 if test "${opt}" = --dir-diff
147 then
148 expected_code=127
149 else
150 expected_code=128
151 fi &&
152 test_expect_code ${expected_code} git difftool ${opt} -y -t nonexistent branch
153 '
154done
155
156test_expect_success 'difftool honors --gui' '
157 difftool_test_setup &&
158 test_config merge.tool bogus-tool &&
159 test_config diff.tool bogus-tool &&
160 test_config diff.guitool test-tool &&
161
162 echo branch >expect &&
163 git difftool --no-prompt --gui branch >actual &&
164 test_cmp expect actual
165'
166
167test_expect_success 'difftool with guiDefault auto selects gui tool when there is DISPLAY' '
168 difftool_test_setup &&
169 test_config merge.tool bogus-tool &&
170 test_config diff.tool bogus-tool &&
171 test_config diff.guitool test-tool &&
172 test_config difftool.guiDefault auto &&
173 DISPLAY=SOMETHING && export DISPLAY &&
174
175 echo branch >expect &&
176 git difftool --no-prompt branch >actual &&
177 test_cmp expect actual
178'
179test_expect_success 'difftool with guiDefault auto selects regular tool when no DISPLAY' '
180 difftool_test_setup &&
181 test_config diff.guitool bogus-tool &&
182 test_config diff.tool test-tool &&
183 test_config difftool.guiDefault Auto &&
184 DISPLAY= && export DISPLAY &&
185
186 echo branch >expect &&
187 git difftool --no-prompt branch >actual &&
188 test_cmp expect actual
189'
190
191test_expect_success 'difftool with guiDefault true selects gui tool' '
192 difftool_test_setup &&
193 test_config diff.tool bogus-tool &&
194 test_config diff.guitool test-tool &&
195 test_config difftool.guiDefault true &&
196
197 DISPLAY= && export DISPLAY &&
198 echo branch >expect &&
199 git difftool --no-prompt branch >actual &&
200 test_cmp expect actual &&
201
202 DISPLAY=Something && export DISPLAY &&
203 echo branch >expect &&
204 git difftool --no-prompt branch >actual &&
205 test_cmp expect actual
206'
207
208test_expect_success 'difftool --no-gui trumps config guiDefault' '
209 difftool_test_setup &&
210 test_config diff.guitool bogus-tool &&
211 test_config diff.tool test-tool &&
212 test_config difftool.guiDefault true &&
213
214 echo branch >expect &&
215 git difftool --no-prompt --no-gui branch >actual &&
216 test_cmp expect actual
217'
218
219test_expect_success 'difftool --gui last setting wins' '
220 difftool_test_setup &&
221 : >expect &&
222 git difftool --no-prompt --gui --no-gui >actual &&
223 test_cmp expect actual &&
224
225 test_config merge.tool bogus-tool &&
226 test_config diff.tool bogus-tool &&
227 test_config diff.guitool test-tool &&
228 echo branch >expect &&
229 git difftool --no-prompt --no-gui --gui branch >actual &&
230 test_cmp expect actual
231'
232
233test_expect_success 'difftool --gui works without configured diff.guitool' '
234 difftool_test_setup &&
235 echo branch >expect &&
236 git difftool --no-prompt --gui branch >actual &&
237 test_cmp expect actual
238'
239
240# Specify the diff tool using $GIT_DIFF_TOOL
241test_expect_success 'GIT_DIFF_TOOL variable' '
242 difftool_test_setup &&
243 git config --unset diff.tool &&
244 echo branch >expect &&
245 GIT_DIFF_TOOL=test-tool git difftool --no-prompt branch >actual &&
246 test_cmp expect actual
247'
248
249# Test the $GIT_*_TOOL variables and ensure
250# that $GIT_DIFF_TOOL always wins unless --tool is specified
251test_expect_success 'GIT_DIFF_TOOL overrides' '
252 difftool_test_setup &&
253 test_config diff.tool bogus-tool &&
254 test_config merge.tool bogus-tool &&
255
256 echo branch >expect &&
257 GIT_DIFF_TOOL=test-tool git difftool --no-prompt branch >actual &&
258 test_cmp expect actual &&
259
260 test_config diff.tool bogus-tool &&
261 test_config merge.tool bogus-tool &&
262 GIT_DIFF_TOOL=bogus-tool \
263 git difftool --no-prompt --tool=test-tool branch >actual &&
264 test_cmp expect actual
265'
266
267# Test that we don't have to pass --no-prompt to difftool
268# when $GIT_DIFFTOOL_NO_PROMPT is true
269test_expect_success 'GIT_DIFFTOOL_NO_PROMPT variable' '
270 difftool_test_setup &&
271 echo branch >expect &&
272 GIT_DIFFTOOL_NO_PROMPT=true git difftool branch >actual &&
273 test_cmp expect actual
274'
275
276# git-difftool supports the difftool.prompt variable.
277# Test that GIT_DIFFTOOL_PROMPT can override difftool.prompt = false
278test_expect_success 'GIT_DIFFTOOL_PROMPT variable' '
279 difftool_test_setup &&
280 test_config difftool.prompt false &&
281 echo >input &&
282 GIT_DIFFTOOL_PROMPT=true git difftool branch <input >output &&
283 prompt=$(tail -1 <output) &&
284 prompt_given "$prompt"
285'
286
287# Test that we don't have to pass --no-prompt when difftool.prompt is false
288test_expect_success 'difftool.prompt config variable is false' '
289 difftool_test_setup &&
290 test_config difftool.prompt false &&
291 echo branch >expect &&
292 git difftool branch >actual &&
293 test_cmp expect actual
294'
295
296# Test that we don't have to pass --no-prompt when mergetool.prompt is false
297test_expect_success 'difftool merge.prompt = false' '
298 difftool_test_setup &&
299 test_might_fail git config --unset difftool.prompt &&
300 test_config mergetool.prompt false &&
301 echo branch >expect &&
302 git difftool branch >actual &&
303 test_cmp expect actual
304'
305
306# Test that the -y flag can override difftool.prompt = true
307test_expect_success 'difftool.prompt can overridden with -y' '
308 difftool_test_setup &&
309 test_config difftool.prompt true &&
310 echo branch >expect &&
311 git difftool -y branch >actual &&
312 test_cmp expect actual
313'
314
315# Test that the --prompt flag can override difftool.prompt = false
316test_expect_success 'difftool.prompt can overridden with --prompt' '
317 difftool_test_setup &&
318 test_config difftool.prompt false &&
319 echo >input &&
320 git difftool --prompt branch <input >output &&
321 prompt=$(tail -1 <output) &&
322 prompt_given "$prompt"
323'
324
325# Test that the last flag passed on the command-line wins
326test_expect_success 'difftool last flag wins' '
327 difftool_test_setup &&
328 echo branch >expect &&
329 git difftool --prompt --no-prompt branch >actual &&
330 test_cmp expect actual &&
331 echo >input &&
332 git difftool --no-prompt --prompt branch <input >output &&
333 prompt=$(tail -1 <output) &&
334 prompt_given "$prompt"
335'
336
337# git-difftool falls back to git-mergetool config variables
338# so test that behavior here
339test_expect_success 'difftool + mergetool config variables' '
340 test_config merge.tool test-tool &&
341 test_config mergetool.test-tool.cmd "cat \$LOCAL" &&
342 echo branch >expect &&
343 git difftool --no-prompt branch >actual &&
344 test_cmp expect actual &&
345 git difftool --gui --no-prompt branch >actual &&
346 test_cmp expect actual &&
347
348 # set merge.tool to something bogus, diff.tool to test-tool
349 test_config merge.tool bogus-tool &&
350 test_config diff.tool test-tool &&
351 git difftool --no-prompt branch >actual &&
352 test_cmp expect actual &&
353 git difftool --gui --no-prompt branch >actual &&
354 test_cmp expect actual &&
355
356 # set merge.tool, diff.tool to something bogus, merge.guitool to test-tool
357 test_config diff.tool bogus-tool &&
358 test_config merge.guitool test-tool &&
359 git difftool --gui --no-prompt branch >actual &&
360 test_cmp expect actual &&
361
362 # set merge.tool, diff.tool, merge.guitool to something bogus, diff.guitool to test-tool
363 test_config merge.guitool bogus-tool &&
364 test_config diff.guitool test-tool &&
365 git difftool --gui --no-prompt branch >actual &&
366 test_cmp expect actual
367'
368
369test_expect_success 'difftool.<tool>.path' '
370 test_config difftool.tkdiff.path echo &&
371 git difftool --tool=tkdiff --no-prompt branch >output &&
372 grep file output >grep-output &&
373 test_line_count = 1 grep-output
374'
375
376test_expect_success 'difftool --extcmd=cat' '
377 echo branch >expect &&
378 echo main >>expect &&
379 git difftool --no-prompt --extcmd=cat branch >actual &&
380 test_cmp expect actual
381'
382
383test_expect_success 'difftool --extcmd cat' '
384 echo branch >expect &&
385 echo main >>expect &&
386 git difftool --no-prompt --extcmd=cat branch >actual &&
387 test_cmp expect actual
388'
389
390test_expect_success 'difftool -x cat' '
391 echo branch >expect &&
392 echo main >>expect &&
393 git difftool --no-prompt -x cat branch >actual &&
394 test_cmp expect actual
395'
396
397test_expect_success 'difftool --extcmd echo arg1' '
398 echo file >expect &&
399 git difftool --no-prompt \
400 --extcmd sh\ -c\ \"echo\ \$1\" branch >actual &&
401 test_cmp expect actual
402'
403
404test_expect_success 'difftool --extcmd cat arg1' '
405 echo main >expect &&
406 git difftool --no-prompt \
407 --extcmd sh\ -c\ \"cat\ \$1\" branch >actual &&
408 test_cmp expect actual
409'
410
411test_expect_success 'difftool --extcmd cat arg2' '
412 echo branch >expect &&
413 git difftool --no-prompt \
414 --extcmd sh\ -c\ \"cat\ \\\"\$2\\\"\" branch >actual &&
415 test_cmp expect actual
416'
417
418# Create a second file on main and a different version on branch
419test_expect_success 'setup with 2 files different' '
420 echo m2 >file2 &&
421 git add file2 &&
422 git commit -m "added file2" &&
423
424 git checkout branch &&
425 echo br2 >file2 &&
426 git add file2 &&
427 git commit -a -m "branch changed file2" &&
428 git checkout main
429'
430
431test_expect_success 'say no to the first file' '
432 (echo n && echo) >input &&
433 git difftool -x cat branch <input >output &&
434 grep m2 output &&
435 grep br2 output &&
436 ! grep main output &&
437 ! grep branch output
438'
439
440test_expect_success 'say no to the second file' '
441 (echo && echo n) >input &&
442 git difftool -x cat branch <input >output &&
443 grep main output &&
444 grep branch output &&
445 ! grep m2 output &&
446 ! grep br2 output
447'
448
449test_expect_success 'ending prompt input with EOF' '
450 git difftool -x cat branch </dev/null >output &&
451 ! grep main output &&
452 ! grep branch output &&
453 ! grep m2 output &&
454 ! grep br2 output
455'
456
457test_expect_success 'difftool --tool-help' '
458 git difftool --tool-help >output &&
459 grep tool output
460'
461
462test_expect_success 'setup change in subdirectory' '
463 git checkout main &&
464 mkdir sub &&
465 echo main >sub/sub &&
466 git add sub/sub &&
467 git commit -m "added sub/sub" &&
468 git tag v1 &&
469 echo test >>file &&
470 echo test >>sub/sub &&
471 git add file sub/sub &&
472 git commit -m "modified both"
473'
474
475test_expect_success 'difftool -d with growing paths' '
476 a=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&
477 git init growing &&
478 (
479 cd growing &&
480 echo "test -f \"\$2/b\"" | write_script .git/test-for-b.sh &&
481 one=$(printf 1 | git hash-object -w --stdin) &&
482 two=$(printf 2 | git hash-object -w --stdin) &&
483 git update-index --add \
484 --cacheinfo 100644,$one,$a --cacheinfo 100644,$two,b &&
485 tree1=$(git write-tree) &&
486 git update-index --add \
487 --cacheinfo 100644,$two,$a --cacheinfo 100644,$one,b &&
488 tree2=$(git write-tree) &&
489 git checkout -- $a &&
490 git difftool -d --extcmd .git/test-for-b.sh $tree1 $tree2
491 )
492'
493
494run_dir_diff_test () {
495 test_expect_success "$1 --no-symlinks" "
496 symlinks=--no-symlinks &&
497 $2
498 "
499 test_expect_success SYMLINKS "$1 --symlinks" "
500 symlinks=--symlinks &&
501 $2
502 "
503}
504
505run_dir_diff_test 'difftool -d' '
506 git difftool -d $symlinks --extcmd ls branch >output &&
507 grep "^sub$" output &&
508 grep "^file$" output
509'
510
511run_dir_diff_test 'difftool --dir-diff' '
512 git difftool --dir-diff $symlinks --extcmd ls branch >output &&
513 grep "^sub$" output &&
514 grep "^file$" output
515'
516
517run_dir_diff_test 'difftool --dir-diff avoids repeated slashes in TMPDIR' '
518 TMPDIR="${TMPDIR:-/tmp}////" \
519 git difftool --dir-diff $symlinks --extcmd echo branch >output &&
520 grep -v // output >actual &&
521 test_line_count = 1 actual
522'
523
524run_dir_diff_test 'difftool --dir-diff ignores --prompt' '
525 git difftool --dir-diff $symlinks --prompt --extcmd ls branch >output &&
526 grep "^sub$" output &&
527 grep "^file$" output
528'
529
530run_dir_diff_test 'difftool --dir-diff branch from subdirectory' '
531 (
532 cd sub &&
533 git difftool --dir-diff $symlinks --extcmd ls branch >output &&
534 # "sub" must only exist in "right"
535 # "file" and "file2" must be listed in both "left" and "right"
536 grep "^sub$" output >sub-output &&
537 test_line_count = 1 sub-output &&
538 grep "^file$" output >file-output &&
539 test_line_count = 2 file-output &&
540 grep "^file2$" output >file2-output &&
541 test_line_count = 2 file2-output
542 )
543'
544
545run_dir_diff_test 'difftool --dir-diff v1 from subdirectory' '
546 (
547 cd sub &&
548 git difftool --dir-diff $symlinks --extcmd ls v1 >output &&
549 # "sub" and "file" exist in both v1 and HEAD.
550 # "file2" is unchanged.
551 grep "^sub$" output >sub-output &&
552 test_line_count = 2 sub-output &&
553 grep "^file$" output >file-output &&
554 test_line_count = 2 file-output &&
555 ! grep "^file2$" output
556 )
557'
558
559run_dir_diff_test 'difftool --dir-diff branch from subdirectory w/ pathspec' '
560 (
561 cd sub &&
562 git difftool --dir-diff $symlinks --extcmd ls branch -- .>output &&
563 # "sub" only exists in "right"
564 # "file" and "file2" must not be listed
565 grep "^sub$" output >sub-output &&
566 test_line_count = 1 sub-output &&
567 ! grep "^file$" output
568 )
569'
570
571run_dir_diff_test 'difftool --dir-diff v1 from subdirectory w/ pathspec' '
572 (
573 cd sub &&
574 git difftool --dir-diff $symlinks --extcmd ls v1 -- .>output &&
575 # "sub" exists in v1 and HEAD
576 # "file" is filtered out by the pathspec
577 grep "^sub$" output >sub-output &&
578 test_line_count = 2 sub-output &&
579 ! grep "^file$" output
580 )
581'
582
583run_dir_diff_test 'difftool --dir-diff from subdirectory with GIT_DIR set' '
584 (
585 GIT_DIR=$(pwd)/.git &&
586 export GIT_DIR &&
587 GIT_WORK_TREE=$(pwd) &&
588 export GIT_WORK_TREE &&
589 cd sub &&
590 git difftool --dir-diff $symlinks --extcmd ls \
591 branch -- sub >output &&
592 grep "^sub$" output &&
593 ! grep "^file$" output
594 )
595'
596
597run_dir_diff_test 'difftool --dir-diff when worktree file is missing' '
598 test_when_finished git reset --hard &&
599 rm file2 &&
600 git difftool --dir-diff $symlinks --extcmd ls branch main >output &&
601 grep "^file2$" output
602'
603
604run_dir_diff_test 'difftool --dir-diff with unmerged files' '
605 test_when_finished git reset --hard &&
606 test_config difftool.echo.cmd "echo ok" &&
607 git checkout -B conflict-a &&
608 git checkout -B conflict-b &&
609 git checkout conflict-a &&
610 echo a >>file &&
611 git add file &&
612 git commit -m conflict-a &&
613 git checkout conflict-b &&
614 echo b >>file &&
615 git add file &&
616 git commit -m conflict-b &&
617 git checkout main &&
618 git merge conflict-a &&
619 test_must_fail git merge conflict-b &&
620 cat >expect <<-EOF &&
621 ok
622 EOF
623 git difftool --dir-diff $symlinks -t echo >actual &&
624 test_cmp expect actual
625'
626
627write_script .git/CHECK_SYMLINKS <<\EOF
628for f in file file2 sub/sub
629do
630 echo "$f"
631 ls -ld "$2/$f" | sed -e 's/.* -> //'
632done >actual
633EOF
634
635test_expect_success SYMLINKS 'difftool --dir-diff --symlinks without unstaged changes' '
636 cat >expect <<-EOF &&
637 file
638 $PWD/file
639 file2
640 $PWD/file2
641 sub/sub
642 $PWD/sub/sub
643 EOF
644 git difftool --dir-diff --symlinks \
645 --extcmd "./.git/CHECK_SYMLINKS" branch HEAD &&
646 test_cmp expect actual
647'
648
649write_script modify-right-file <<\EOF
650echo "new content" >"$2/file"
651EOF
652
653run_dir_diff_test 'difftool --dir-diff syncs worktree with unstaged change' '
654 test_when_finished git reset --hard &&
655 echo "orig content" >file &&
656 git difftool -d $symlinks --extcmd "$PWD/modify-right-file" branch &&
657 echo "new content" >expect &&
658 test_cmp expect file
659'
660
661run_dir_diff_test 'difftool --dir-diff syncs worktree without unstaged change' '
662 test_when_finished git reset --hard &&
663 git difftool -d $symlinks --extcmd "$PWD/modify-right-file" branch &&
664 echo "new content" >expect &&
665 test_cmp expect file
666'
667
668run_dir_diff_test 'difftool --dir-diff with no diff' '
669 git difftool -d main main
670'
671
672write_script modify-file <<\EOF
673echo "new content" >file
674EOF
675
676test_expect_success 'difftool --no-symlinks does not overwrite working tree file ' '
677 echo "orig content" >file &&
678 git difftool --dir-diff --no-symlinks --extcmd "$PWD/modify-file" branch &&
679 echo "new content" >expect &&
680 test_cmp expect file
681'
682
683write_script modify-both-files <<\EOF
684echo "wt content" >file &&
685echo "tmp content" >"$2/file" &&
686echo "$2" >tmpdir
687EOF
688
689test_expect_success 'difftool --no-symlinks detects conflict ' '
690 (
691 TMPDIR=$TRASH_DIRECTORY &&
692 export TMPDIR &&
693 echo "orig content" >file &&
694 test_must_fail git difftool --dir-diff --no-symlinks --extcmd "$PWD/modify-both-files" branch &&
695 echo "wt content" >expect &&
696 test_cmp expect file &&
697 echo "tmp content" >expect &&
698 test_cmp expect "$(cat tmpdir)/file"
699 )
700'
701
702test_expect_success 'difftool properly honors gitlink and core.worktree' '
703 test_when_finished rm -rf submod/ule &&
704 test_config_global protocol.file.allow always &&
705 git submodule add ./. submod/ule &&
706 test_config -C submod/ule diff.tool checktrees &&
707 test_config -C submod/ule difftool.checktrees.cmd '\''
708 test -d "$LOCAL" && test -d "$REMOTE" && echo good
709 '\'' &&
710 (
711 cd submod/ule &&
712 echo good >expect &&
713 git difftool --tool=checktrees --dir-diff HEAD~ >actual &&
714 test_cmp expect actual &&
715 rm -f expect actual
716 )
717'
718
719test_expect_success SYMLINKS 'difftool --dir-diff symlinked directories' '
720 test_when_finished git reset --hard &&
721 git init dirlinks &&
722 (
723 cd dirlinks &&
724 git config diff.tool checktrees &&
725 git config difftool.checktrees.cmd "echo good" &&
726 mkdir foo &&
727 : >foo/bar &&
728 git add foo/bar &&
729 test_commit symlink-one &&
730 ln -s foo link &&
731 git add link &&
732 test_commit symlink-two &&
733 echo good >expect &&
734 git difftool --tool=checktrees --dir-diff HEAD~ >actual &&
735 test_cmp expect actual
736 )
737'
738
739test_expect_success SYMLINKS 'difftool --dir-diff handles modified symlinks' '
740 test_when_finished git reset --hard &&
741 touch b &&
742 ln -s b c &&
743 git add b c &&
744 test_tick &&
745 git commit -m initial &&
746 touch d &&
747 rm c &&
748 ln -s d c &&
749 cat >expect <<-EOF &&
750 c
751
752 c
753 EOF
754 git difftool --symlinks --dir-diff --extcmd ls >output &&
755 grep -v ^/ output >actual &&
756 test_cmp expect actual &&
757
758 git difftool --no-symlinks --dir-diff --extcmd ls >output &&
759 grep -v ^/ output >actual &&
760 test_cmp expect actual &&
761
762 # The left side contains symlink "c" that points to "b"
763 test_config difftool.cat.cmd "cat \$LOCAL/c" &&
764 printf "%s\n" b >expect &&
765
766 git difftool --symlinks --dir-diff --tool cat >actual &&
767 test_cmp expect actual &&
768
769 git difftool --symlinks --no-symlinks --dir-diff --tool cat >actual &&
770 test_cmp expect actual &&
771
772 # The right side contains symlink "c" that points to "d"
773 test_config difftool.cat.cmd "cat \$REMOTE/c" &&
774 printf "%s\n" d >expect &&
775
776 git difftool --symlinks --dir-diff --tool cat >actual &&
777 test_cmp expect actual &&
778
779 git difftool --no-symlinks --dir-diff --tool cat >actual &&
780 test_cmp expect actual &&
781
782 # Deleted symlinks
783 rm -f c &&
784 cat >expect <<-EOF &&
785 c
786
787 EOF
788 git difftool --symlinks --dir-diff --extcmd ls >output &&
789 grep -v ^/ output >actual &&
790 test_cmp expect actual &&
791
792 git difftool --no-symlinks --dir-diff --extcmd ls >output &&
793 grep -v ^/ output >actual &&
794 test_cmp expect actual
795'
796
797test_expect_success SYMLINKS 'difftool --dir-diff writes symlinks as raw text' '
798 # Start out on a branch called "branch-init".
799 git init -b branch-init symlink-files &&
800 (
801 cd symlink-files &&
802 # This test ensures that symlinks are written as raw text.
803 # The "cat" tools output link and file contents.
804 git config difftool.cat-left-link.cmd "cat \"\$LOCAL/link\"" &&
805 git config difftool.cat-left-a.cmd "cat \"\$LOCAL/file-a\"" &&
806 git config difftool.cat-right-link.cmd "cat \"\$REMOTE/link\"" &&
807 git config difftool.cat-right-b.cmd "cat \"\$REMOTE/file-b\"" &&
808
809 # Record the empty initial state so that we can come back here
810 # later and not have to consider the any cases where difftool
811 # will create symlinks back into the worktree.
812 test_tick &&
813 git commit --allow-empty -m init &&
814
815 # Create a file called "file-a" with a symlink pointing to it.
816 git switch -c branch-a &&
817 echo a >file-a &&
818 ln -s file-a link &&
819 git add file-a link &&
820 test_tick &&
821 git commit -m link-to-file-a &&
822
823 # Create a file called "file-b" and point the symlink to it.
824 git switch -c branch-b &&
825 echo b >file-b &&
826 rm link &&
827 ln -s file-b link &&
828 git add file-b link &&
829 git rm file-a &&
830 test_tick &&
831 git commit -m link-to-file-b &&
832
833 # Checkout the initial branch so that the --symlinks behavior is
834 # not activated. The two directories should be completely
835 # independent with no symlinks pointing back here.
836 git switch branch-init &&
837
838 # The left link must be "file-a" and "file-a" must contain "a".
839 echo file-a >expect &&
840 git difftool --symlinks --dir-diff --tool cat-left-link \
841 branch-a branch-b >actual &&
842 test_cmp expect actual &&
843
844 echo a >expect &&
845 git difftool --symlinks --dir-diff --tool cat-left-a \
846 branch-a branch-b >actual &&
847 test_cmp expect actual &&
848
849 # The right link must be "file-b" and "file-b" must contain "b".
850 echo file-b >expect &&
851 git difftool --symlinks --dir-diff --tool cat-right-link \
852 branch-a branch-b >actual &&
853 test_cmp expect actual &&
854
855 echo b >expect &&
856 git difftool --symlinks --dir-diff --tool cat-right-b \
857 branch-a branch-b >actual &&
858 test_cmp expect actual
859 )
860'
861
862test_expect_success 'add -N and difftool -d' '
863 test_when_finished git reset --hard &&
864
865 test_write_lines A B C >intent-to-add &&
866 git add -N intent-to-add &&
867 git difftool --dir-diff --extcmd ls
868'
869
870test_expect_success 'difftool --cached with unmerged files' '
871 test_when_finished git reset --hard &&
872
873 test_commit conflicting &&
874 test_commit conflict-a conflict.t a &&
875 git reset --hard conflicting &&
876 test_commit conflict-b conflict.t b &&
877 test_must_fail git merge conflict-a &&
878
879 git difftool --cached --no-prompt >output &&
880 test_must_be_empty output
881'
882
883test_expect_success 'outside worktree' '
884 echo 1 >1 &&
885 echo 2 >2 &&
886 test_expect_code 1 nongit git \
887 -c diff.tool=echo -c difftool.echo.cmd="echo \$LOCAL \$REMOTE" \
888 difftool --no-prompt --no-index ../1 ../2 >actual &&
889 echo "../1 ../2" >expect &&
890 test_cmp expect actual
891'
892
893test_expect_success 'difftool --gui, --tool and --extcmd are mutually exclusive' '
894 difftool_test_setup &&
895 test_must_fail git difftool --gui --tool=test-tool &&
896 test_must_fail git difftool --gui --extcmd=cat &&
897 test_must_fail git difftool --tool=test-tool --extcmd=cat &&
898 test_must_fail git difftool --gui --tool=test-tool --extcmd=cat
899'
900
901test_expect_success 'difftool --rotate-to' '
902 difftool_test_setup &&
903 test_when_finished git reset --hard &&
904 echo 1 >1 &&
905 echo 2 >2 &&
906 echo 4 >4 &&
907 git add 1 2 4 &&
908 git commit -a -m "124" &&
909 git difftool --no-prompt --extcmd=cat --rotate-to="2" HEAD^ >output &&
910 cat >expect <<-\EOF &&
911 2
912 4
913 1
914 EOF
915 test_cmp output expect
916'
917
918test_expect_success 'difftool --skip-to' '
919 difftool_test_setup &&
920 test_when_finished git reset --hard &&
921 git difftool --no-prompt --extcmd=cat --skip-to="2" HEAD^ >output &&
922 cat >expect <<-\EOF &&
923 2
924 4
925 EOF
926 test_cmp output expect
927'
928
929test_expect_success 'difftool --rotate/skip-to error condition' '
930 test_must_fail git difftool --no-prompt --extcmd=cat --rotate-to="3" HEAD^ &&
931 test_must_fail git difftool --no-prompt --extcmd=cat --skip-to="3" HEAD^
932'
933test_done