3 # Copyright (c) 2009, 2010, 2012, 2013 David Aguilar
6 test_description
='git-difftool
8 Testing basic diff tool invocation
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
23 test "$prompt" = "Launch 'test-tool' [Y/n]? branch"
26 test_expect_success
'basic usage requires no repo' '
27 test_expect_code 129 git difftool -h >output &&
28 test_i18ngrep ^usage: output &&
29 # create a ceiling directory to prevent Git from finding a repo
31 test_when_finished rm -r not &&
32 test_expect_code 129 \
33 env GIT_CEILING_DIRECTORIES="$(pwd)/not" \
34 git -C not/repo difftool -h >output &&
35 test_i18ngrep ^usage: output
38 # Create a file on master and change it on branch
39 test_expect_success
'setup' '
42 git commit -m "added file" &&
44 git checkout -b branch master &&
46 git commit -a -m "branch changed file" &&
50 # Configure a custom difftool.<tool>.cmd and use it
51 test_expect_success
'custom commands' '
52 difftool_test_setup &&
53 test_config difftool.test-tool.cmd "cat \"\$REMOTE\"" &&
54 echo master >expect &&
55 git difftool --no-prompt branch >actual &&
56 test_cmp expect actual &&
58 test_config difftool.test-tool.cmd "cat \"\$LOCAL\"" &&
59 echo branch >expect &&
60 git difftool --no-prompt branch >actual &&
61 test_cmp expect actual
64 test_expect_success
'custom tool commands override built-ins' '
65 test_config difftool.vimdiff.cmd "cat \"\$REMOTE\"" &&
66 echo master >expect &&
67 git difftool --tool vimdiff --no-prompt branch >actual &&
68 test_cmp expect actual
71 test_expect_success
'difftool ignores bad --tool values' '
74 git difftool --no-prompt --tool=bad-tool branch >actual &&
75 test_cmp expect actual
78 test_expect_success
'difftool forwards arguments to diff' '
79 difftool_test_setup &&
82 echo changes>for-diff &&
85 git difftool --cached --no-prompt -- for-diff >actual &&
86 test_cmp expect actual &&
87 git reset -- for-diff &&
91 test_expect_success
'difftool ignores exit code' '
92 test_config difftool.error.cmd false &&
93 git difftool -y -t error branch
96 test_expect_success
'difftool forwards exit code with --trust-exit-code' '
97 test_config difftool.error.cmd false &&
98 test_must_fail git difftool -y --trust-exit-code -t error branch
101 test_expect_success
'difftool forwards exit code with --trust-exit-code for built-ins' '
102 test_config difftool.vimdiff.path false &&
103 test_must_fail git difftool -y --trust-exit-code -t vimdiff branch
106 test_expect_success
'difftool honors difftool.trustExitCode = true' '
107 test_config difftool.error.cmd false &&
108 test_config difftool.trustExitCode true &&
109 test_must_fail git difftool -y -t error branch
112 test_expect_success
'difftool honors difftool.trustExitCode = false' '
113 test_config difftool.error.cmd false &&
114 test_config difftool.trustExitCode false &&
115 git difftool -y -t error branch
118 test_expect_success
'difftool ignores exit code with --no-trust-exit-code' '
119 test_config difftool.error.cmd false &&
120 test_config difftool.trustExitCode true &&
121 git difftool -y --no-trust-exit-code -t error branch
124 test_expect_success
'difftool stops on error with --trust-exit-code' '
125 test_when_finished "rm -f for-diff .git/fail-right-file" &&
126 test_when_finished "git reset -- for-diff" &&
127 write_script .git/fail-right-file <<-\EOF &&
133 test_must_fail git difftool -y --trust-exit-code \
134 --extcmd .git/fail-right-file branch >actual &&
135 test_line_count = 1 actual
138 test_expect_success
'difftool honors exit status if command not found' '
139 test_config difftool.nonexistent.cmd i-dont-exist &&
140 test_config difftool.trustExitCode false &&
141 test_must_fail git difftool -y -t nonexistent branch
144 test_expect_success
'difftool honors --gui' '
145 difftool_test_setup &&
146 test_config merge.tool bogus-tool &&
147 test_config diff.tool bogus-tool &&
148 test_config diff.guitool test-tool &&
150 echo branch >expect &&
151 git difftool --no-prompt --gui branch >actual &&
152 test_cmp expect actual
155 test_expect_success
'difftool --gui last setting wins' '
156 difftool_test_setup &&
158 git difftool --no-prompt --gui --no-gui >actual &&
159 test_cmp expect actual &&
161 test_config merge.tool bogus-tool &&
162 test_config diff.tool bogus-tool &&
163 test_config diff.guitool test-tool &&
164 echo branch >expect &&
165 git difftool --no-prompt --no-gui --gui branch >actual &&
166 test_cmp expect actual
169 test_expect_success
'difftool --gui works without configured diff.guitool' '
170 difftool_test_setup &&
171 echo branch >expect &&
172 git difftool --no-prompt --gui branch >actual &&
173 test_cmp expect actual
176 # Specify the diff tool using $GIT_DIFF_TOOL
177 test_expect_success
'GIT_DIFF_TOOL variable' '
178 difftool_test_setup &&
179 git config --unset diff.tool &&
180 echo branch >expect &&
181 GIT_DIFF_TOOL=test-tool git difftool --no-prompt branch >actual &&
182 test_cmp expect actual
185 # Test the $GIT_*_TOOL variables and ensure
186 # that $GIT_DIFF_TOOL always wins unless --tool is specified
187 test_expect_success
'GIT_DIFF_TOOL overrides' '
188 difftool_test_setup &&
189 test_config diff.tool bogus-tool &&
190 test_config merge.tool bogus-tool &&
192 echo branch >expect &&
193 GIT_DIFF_TOOL=test-tool git difftool --no-prompt branch >actual &&
194 test_cmp expect actual &&
196 test_config diff.tool bogus-tool &&
197 test_config merge.tool bogus-tool &&
198 GIT_DIFF_TOOL=bogus-tool \
199 git difftool --no-prompt --tool=test-tool branch >actual &&
200 test_cmp expect actual
203 # Test that we don't have to pass --no-prompt to difftool
204 # when $GIT_DIFFTOOL_NO_PROMPT is true
205 test_expect_success
'GIT_DIFFTOOL_NO_PROMPT variable' '
206 difftool_test_setup &&
207 echo branch >expect &&
208 GIT_DIFFTOOL_NO_PROMPT=true git difftool branch >actual &&
209 test_cmp expect actual
212 # git-difftool supports the difftool.prompt variable.
213 # Test that GIT_DIFFTOOL_PROMPT can override difftool.prompt = false
214 test_expect_success
'GIT_DIFFTOOL_PROMPT variable' '
215 difftool_test_setup &&
216 test_config difftool.prompt false &&
218 GIT_DIFFTOOL_PROMPT=true git difftool branch <input >output &&
219 prompt=$(tail -1 <output) &&
220 prompt_given "$prompt"
223 # Test that we don't have to pass --no-prompt when difftool.prompt is false
224 test_expect_success
'difftool.prompt config variable is false' '
225 difftool_test_setup &&
226 test_config difftool.prompt false &&
227 echo branch >expect &&
228 git difftool branch >actual &&
229 test_cmp expect actual
232 # Test that we don't have to pass --no-prompt when mergetool.prompt is false
233 test_expect_success
'difftool merge.prompt = false' '
234 difftool_test_setup &&
235 test_might_fail git config --unset difftool.prompt &&
236 test_config mergetool.prompt false &&
237 echo branch >expect &&
238 git difftool branch >actual &&
239 test_cmp expect actual
242 # Test that the -y flag can override difftool.prompt = true
243 test_expect_success
'difftool.prompt can overridden with -y' '
244 difftool_test_setup &&
245 test_config difftool.prompt true &&
246 echo branch >expect &&
247 git difftool -y branch >actual &&
248 test_cmp expect actual
251 # Test that the --prompt flag can override difftool.prompt = false
252 test_expect_success
'difftool.prompt can overridden with --prompt' '
253 difftool_test_setup &&
254 test_config difftool.prompt false &&
256 git difftool --prompt branch <input >output &&
257 prompt=$(tail -1 <output) &&
258 prompt_given "$prompt"
261 # Test that the last flag passed on the command-line wins
262 test_expect_success
'difftool last flag wins' '
263 difftool_test_setup &&
264 echo branch >expect &&
265 git difftool --prompt --no-prompt branch >actual &&
266 test_cmp expect actual &&
268 git difftool --no-prompt --prompt branch <input >output &&
269 prompt=$(tail -1 <output) &&
270 prompt_given "$prompt"
273 # git-difftool falls back to git-mergetool config variables
274 # so test that behavior here
275 test_expect_success
'difftool + mergetool config variables' '
276 test_config merge.tool test-tool &&
277 test_config mergetool.test-tool.cmd "cat \$LOCAL" &&
278 echo branch >expect &&
279 git difftool --no-prompt branch >actual &&
280 test_cmp expect actual &&
281 git difftool --gui --no-prompt branch >actual &&
282 test_cmp expect actual &&
284 # set merge.tool to something bogus, diff.tool to test-tool
285 test_config merge.tool bogus-tool &&
286 test_config diff.tool test-tool &&
287 git difftool --no-prompt branch >actual &&
288 test_cmp expect actual &&
289 git difftool --gui --no-prompt branch >actual &&
290 test_cmp expect actual &&
292 # set merge.tool, diff.tool to something bogus, merge.guitool to test-tool
293 test_config diff.tool bogus-tool &&
294 test_config merge.guitool test-tool &&
295 git difftool --gui --no-prompt branch >actual &&
296 test_cmp expect actual &&
298 # set merge.tool, diff.tool, merge.guitool to something bogus, diff.guitool to test-tool
299 test_config merge.guitool bogus-tool &&
300 test_config diff.guitool test-tool &&
301 git difftool --gui --no-prompt branch >actual &&
302 test_cmp expect actual
305 test_expect_success
'difftool.<tool>.path' '
306 test_config difftool.tkdiff.path echo &&
307 git difftool --tool=tkdiff --no-prompt branch >output &&
308 grep file output >grep-output &&
309 test_line_count = 1 grep-output
312 test_expect_success
'difftool --extcmd=cat' '
313 echo branch >expect &&
314 echo master >>expect &&
315 git difftool --no-prompt --extcmd=cat branch >actual &&
316 test_cmp expect actual
319 test_expect_success
'difftool --extcmd cat' '
320 echo branch >expect &&
321 echo master >>expect &&
322 git difftool --no-prompt --extcmd=cat branch >actual &&
323 test_cmp expect actual
326 test_expect_success
'difftool -x cat' '
327 echo branch >expect &&
328 echo master >>expect &&
329 git difftool --no-prompt -x cat branch >actual &&
330 test_cmp expect actual
333 test_expect_success
'difftool --extcmd echo arg1' '
335 git difftool --no-prompt \
336 --extcmd sh\ -c\ \"echo\ \$1\" branch >actual &&
337 test_cmp expect actual
340 test_expect_success
'difftool --extcmd cat arg1' '
341 echo master >expect &&
342 git difftool --no-prompt \
343 --extcmd sh\ -c\ \"cat\ \$1\" branch >actual &&
344 test_cmp expect actual
347 test_expect_success
'difftool --extcmd cat arg2' '
348 echo branch >expect &&
349 git difftool --no-prompt \
350 --extcmd sh\ -c\ \"cat\ \\\"\$2\\\"\" branch >actual &&
351 test_cmp expect actual
354 # Create a second file on master and a different version on branch
355 test_expect_success
'setup with 2 files different' '
358 git commit -m "added file2" &&
360 git checkout branch &&
363 git commit -a -m "branch changed file2" &&
367 test_expect_success
'say no to the first file' '
368 (echo n && echo) >input &&
369 git difftool -x cat branch <input >output &&
372 ! grep master output &&
376 test_expect_success
'say no to the second file' '
377 (echo && echo n) >input &&
378 git difftool -x cat branch <input >output &&
379 grep master output &&
380 grep branch output &&
385 test_expect_success
'ending prompt input with EOF' '
386 git difftool -x cat branch </dev/null >output &&
387 ! grep master output &&
388 ! grep branch output &&
393 test_expect_success
'difftool --tool-help' '
394 git difftool --tool-help >output &&
398 test_expect_success
'setup change in subdirectory' '
399 git checkout master &&
401 echo master >sub/sub &&
403 git commit -m "added sub/sub" &&
406 echo test >>sub/sub &&
407 git add file sub/sub &&
408 git commit -m "modified both"
411 test_expect_success
'difftool -d with growing paths' '
412 a=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&
416 echo "test -f \"\$2/b\"" | write_script .git/test-for-b.sh &&
417 one=$(printf 1 | git hash-object -w --stdin) &&
418 two=$(printf 2 | git hash-object -w --stdin) &&
419 git update-index --add \
420 --cacheinfo 100644,$one,$a --cacheinfo 100644,$two,b &&
421 tree1=$(git write-tree) &&
422 git update-index --add \
423 --cacheinfo 100644,$two,$a --cacheinfo 100644,$one,b &&
424 tree2=$(git write-tree) &&
425 git checkout -- $a &&
426 git difftool -d --extcmd .git/test-for-b.sh $tree1 $tree2
430 run_dir_diff_test
() {
431 test_expect_success
"$1 --no-symlinks" "
432 symlinks=--no-symlinks &&
435 test_expect_success SYMLINKS
"$1 --symlinks" "
436 symlinks=--symlinks &&
441 run_dir_diff_test
'difftool -d' '
442 git difftool -d $symlinks --extcmd ls branch >output &&
447 run_dir_diff_test
'difftool --dir-diff' '
448 git difftool --dir-diff $symlinks --extcmd ls branch >output &&
453 run_dir_diff_test
'difftool --dir-diff ignores --prompt' '
454 git difftool --dir-diff $symlinks --prompt --extcmd ls branch >output &&
459 run_dir_diff_test
'difftool --dir-diff branch from subdirectory' '
462 git difftool --dir-diff $symlinks --extcmd ls branch >output &&
463 # "sub" must only exist in "right"
464 # "file" and "file2" must be listed in both "left" and "right"
465 grep sub output >sub-output &&
466 test_line_count = 1 sub-output &&
467 grep file"$" output >file-output &&
468 test_line_count = 2 file-output &&
469 grep file2 output >file2-output &&
470 test_line_count = 2 file2-output
474 run_dir_diff_test
'difftool --dir-diff v1 from subdirectory' '
477 git difftool --dir-diff $symlinks --extcmd ls v1 >output &&
478 # "sub" and "file" exist in both v1 and HEAD.
479 # "file2" is unchanged.
480 grep sub output >sub-output &&
481 test_line_count = 2 sub-output &&
482 grep file output >file-output &&
483 test_line_count = 2 file-output &&
488 run_dir_diff_test
'difftool --dir-diff branch from subdirectory w/ pathspec' '
491 git difftool --dir-diff $symlinks --extcmd ls branch -- .>output &&
492 # "sub" only exists in "right"
493 # "file" and "file2" must not be listed
494 grep sub output >sub-output &&
495 test_line_count = 1 sub-output &&
500 run_dir_diff_test
'difftool --dir-diff v1 from subdirectory w/ pathspec' '
503 git difftool --dir-diff $symlinks --extcmd ls v1 -- .>output &&
504 # "sub" exists in v1 and HEAD
505 # "file" is filtered out by the pathspec
506 grep sub output >sub-output &&
507 test_line_count = 2 sub-output &&
512 run_dir_diff_test
'difftool --dir-diff from subdirectory with GIT_DIR set' '
514 GIT_DIR=$(pwd)/.git &&
516 GIT_WORK_TREE=$(pwd) &&
517 export GIT_WORK_TREE &&
519 git difftool --dir-diff $symlinks --extcmd ls \
520 branch -- sub >output &&
526 run_dir_diff_test
'difftool --dir-diff when worktree file is missing' '
527 test_when_finished git reset --hard &&
529 git difftool --dir-diff $symlinks --extcmd ls branch master >output &&
533 run_dir_diff_test
'difftool --dir-diff with unmerged files' '
534 test_when_finished git reset --hard &&
535 test_config difftool.echo.cmd "echo ok" &&
536 git checkout -B conflict-a &&
537 git checkout -B conflict-b &&
538 git checkout conflict-a &&
541 git commit -m conflict-a &&
542 git checkout conflict-b &&
545 git commit -m conflict-b &&
546 git checkout master &&
547 git merge conflict-a &&
548 test_must_fail git merge conflict-b &&
549 cat >expect <<-EOF &&
552 git difftool --dir-diff $symlinks -t echo >actual &&
553 test_cmp expect actual
556 write_script .git
/CHECK_SYMLINKS
<<\EOF
557 for f
in file file2 sub
/sub
560 ls -ld "$2/$f" |
sed -e 's/.* -> //'
564 test_expect_success SYMLINKS
'difftool --dir-diff --symlinks without unstaged changes' '
565 cat >expect <<-EOF &&
573 git difftool --dir-diff --symlinks \
574 --extcmd "./.git/CHECK_SYMLINKS" branch HEAD &&
575 test_cmp expect actual
578 write_script modify-right-file
<<\EOF
579 echo "new content" >"$2/file"
582 run_dir_diff_test
'difftool --dir-diff syncs worktree with unstaged change' '
583 test_when_finished git reset --hard &&
584 echo "orig content" >file &&
585 git difftool -d $symlinks --extcmd "$PWD/modify-right-file" branch &&
586 echo "new content" >expect &&
590 run_dir_diff_test
'difftool --dir-diff syncs worktree without unstaged change' '
591 test_when_finished git reset --hard &&
592 git difftool -d $symlinks --extcmd "$PWD/modify-right-file" branch &&
593 echo "new content" >expect &&
597 write_script modify-file
<<\EOF
598 echo "new content" >file
601 test_expect_success
'difftool --no-symlinks does not overwrite working tree file ' '
602 echo "orig content" >file &&
603 git difftool --dir-diff --no-symlinks --extcmd "$PWD/modify-file" branch &&
604 echo "new content" >expect &&
608 write_script modify-both-files
<<\EOF
609 echo "wt content" >file &&
610 echo "tmp content" >"$2/file" &&
614 test_expect_success
'difftool --no-symlinks detects conflict ' '
616 TMPDIR=$TRASH_DIRECTORY &&
618 echo "orig content" >file &&
619 test_must_fail git difftool --dir-diff --no-symlinks --extcmd "$PWD/modify-both-files" branch &&
620 echo "wt content" >expect &&
621 test_cmp expect file &&
622 echo "tmp content" >expect &&
623 test_cmp expect "$(cat tmpdir)/file"
627 test_expect_success
'difftool properly honors gitlink and core.worktree' '
628 test_when_finished rm -rf submod/ule &&
629 git submodule add ./. submod/ule &&
630 test_config -C submod/ule diff.tool checktrees &&
631 test_config -C submod/ule difftool.checktrees.cmd '\''
632 test -d "$LOCAL" && test -d "$REMOTE" && echo good
637 git difftool --tool=checktrees --dir-diff HEAD~ >actual &&
638 test_cmp expect actual &&
643 test_expect_success SYMLINKS
'difftool --dir-diff symlinked directories' '
644 test_when_finished git reset --hard &&
648 git config diff.tool checktrees &&
649 git config difftool.checktrees.cmd "echo good" &&
653 test_commit symlink-one &&
656 test_commit symlink-two &&
658 git difftool --tool=checktrees --dir-diff HEAD~ >actual &&
659 test_cmp expect actual
663 test_expect_success SYMLINKS
'difftool --dir-diff handles modified symlinks' '
664 test_when_finished git reset --hard &&
669 git commit -m initial &&
673 cat >expect <<-EOF &&
679 git difftool --symlinks --dir-diff --extcmd ls >output &&
680 grep -v ^/ output >actual &&
681 test_cmp expect actual &&
683 git difftool --no-symlinks --dir-diff --extcmd ls >output &&
684 grep -v ^/ output >actual &&
685 test_cmp expect actual &&
687 # The left side contains symlink "c" that points to "b"
688 test_config difftool.cat.cmd "cat \$LOCAL/c" &&
689 printf "%s\n" b >expect &&
691 git difftool --symlinks --dir-diff --tool cat >actual &&
692 test_cmp expect actual &&
694 git difftool --symlinks --no-symlinks --dir-diff --tool cat >actual &&
695 test_cmp expect actual &&
697 # The right side contains symlink "c" that points to "d"
698 test_config difftool.cat.cmd "cat \$REMOTE/c" &&
699 printf "%s\n" d >expect &&
701 git difftool --symlinks --dir-diff --tool cat >actual &&
702 test_cmp expect actual &&
704 git difftool --no-symlinks --dir-diff --tool cat >actual &&
705 test_cmp expect actual &&
709 cat >expect <<-EOF &&
714 git difftool --symlinks --dir-diff --extcmd ls >output &&
715 grep -v ^/ output >actual &&
716 test_cmp expect actual &&
718 git difftool --no-symlinks --dir-diff --extcmd ls >output &&
719 grep -v ^/ output >actual &&
720 test_cmp expect actual
723 test_expect_success
'add -N and difftool -d' '
724 test_when_finished git reset --hard &&
726 test_write_lines A B C >intent-to-add &&
727 git add -N intent-to-add &&
728 git difftool --dir-diff --extcmd ls
731 test_expect_success
'outside worktree' '
734 test_expect_code 1 nongit git \
735 -c diff.tool=echo -c difftool.echo.cmd="echo \$LOCAL \$REMOTE" \
736 difftool --no-prompt --no-index ../1 ../2 >actual &&
737 echo "../1 ../2" >expect &&
738 test_cmp expect actual
741 test_expect_success
'difftool --gui, --tool and --extcmd are mutually exclusive' '
742 difftool_test_setup &&
743 test_must_fail git difftool --gui --tool=test-tool &&
744 test_must_fail git difftool --gui --extcmd=cat &&
745 test_must_fail git difftool --tool=test-tool --extcmd=cat &&
746 test_must_fail git difftool --gui --tool=test-tool --extcmd=cat