difftool: fix dir-diff index creation when in a subdirectory

9ec26e7977 (difftool: fix argument handling in subdirs, 2016-07-18) corrected how path arguments are handled in a subdirectory, but it introduced a regression in how entries outside of the subdirectory are handled by dir-diff. When preparing the right-side of the diff we only include the changed paths in the temporary area. The left side of the diff is constructed from a temporary index that is built from the same set of changed files, but it was being constructed from within the subdirectory. This is a problem because the indexed paths are toplevel-relative, and thus they were not getting added to the index. Teach difftool to chdir to the toplevel of the repository before preparing its temporary indexes. This ensures that all of the toplevel-relative paths are valid. Add test cases to more thoroughly exercise this scenario. Reported-by: Frank Becker <fb@mooflu.com> Signed-off-by: David Aguilar <davvid@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

David Aguilar committed Dec 7, 2016 at 02:16 UTC 853e10c19755669bd4695b1d9fd81f3278466727
2 files changed +45 -3
git-difftool.perl
+4
@@ -182,6 +182,10 @@ EOF
182 }
183 }
184
185 + # Go to the root of the worktree so that the left index files
186 + # are properly setup -- the index is toplevel-relative.
187 + chdir($workdir);
188 +
189 # Setup temp directories
190 my $tmpdir = tempdir('git-difftool.XXXXX', CLEANUP => 0, TMPDIR => 1);
191 my $ldir = "$tmpdir/left";
t/t7800-difftool.sh
+41 -3
@@ -374,6 +374,7 @@ test_expect_success PERL 'setup change in subdirectory' '
374 echo master >sub/sub &&
375 git add sub/sub &&
376 git commit -m "added sub/sub" &&
377 + git tag v1 &&
378 echo test >>file &&
379 echo test >>sub/sub &&
380 git add file sub/sub &&
@@ -409,12 +410,49 @@ run_dir_diff_test 'difftool --dir-diff ignores --prompt' '
410 grep file output
411 '
412
412 -run_dir_diff_test 'difftool --dir-diff from subdirectory' '
413 +run_dir_diff_test 'difftool --dir-diff branch from subdirectory' '
414 (
415 cd sub &&
416 git difftool --dir-diff $symlinks --extcmd ls branch >output &&
416 - grep sub output &&
417 - grep file output
417 + # "sub" must only exist in "right"
418 + # "file" and "file2" must be listed in both "left" and "right"
419 + test "1" = $(grep sub output | wc -l) &&
420 + test "2" = $(grep file"$" output | wc -l) &&
421 + test "2" = $(grep file2 output | wc -l)
422 + )
423 +'
424 +
425 +run_dir_diff_test 'difftool --dir-diff v1 from subdirectory' '
426 + (
427 + cd sub &&
428 + git difftool --dir-diff $symlinks --extcmd ls v1 >output &&
429 + # "sub" and "file" exist in both v1 and HEAD.
430 + # "file2" is unchanged.
431 + test "2" = $(grep sub output | wc -l) &&
432 + test "2" = $(grep file output | wc -l) &&
433 + test "0" = $(grep file2 output | wc -l)
434 + )
435 +'
436 +
437 +run_dir_diff_test 'difftool --dir-diff branch from subdirectory w/ pathspec' '
438 + (
439 + cd sub &&
440 + git difftool --dir-diff $symlinks --extcmd ls branch -- .>output &&
441 + # "sub" only exists in "right"
442 + # "file" and "file2" must not be listed
443 + test "1" = $(grep sub output | wc -l) &&
444 + test "0" = $(grep file output | wc -l)
445 + )
446 +'
447 +
448 +run_dir_diff_test 'difftool --dir-diff v1 from subdirectory w/ pathspec' '
449 + (
450 + cd sub &&
451 + git difftool --dir-diff $symlinks --extcmd ls v1 -- .>output &&
452 + # "sub" exists in v1 and HEAD
453 + # "file" is filtered out by the pathspec
454 + test "2" = $(grep sub output | wc -l) &&
455 + test "0" = $(grep file output | wc -l)
456 )
457 '
458