difftool: gracefully handle symlinks to directories
PATCH 1/1 difftool: gracefully handle symlinks to directories
difftool's dir-diff feature was blindly feeding worktree paths to hash-object without checking whether the path was indeed a file, causing the feature to fail when repositories contain symlinks to directories.
Ensure that only files are ever given to hash-object. Add a test to demonstrate the breakage.
--- git-difftool.perl | 4 +--- t/t7800-difftool.sh | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 3 deletions(-)
git-difftool.perl +1 -3
@@ -70,9 +70,7 @@ sub use_wt_file my ($repo, $workdir, $file, $sha1) = @_; my $null_sha1 = '0' x 40; - if (! -e "$workdir/$file") { - # If the file doesn't exist in the working tree, we cannot - # use it. + if (! -f "$workdir/$file") { return (0, $null_sha1); }
t/t7800-difftool.sh +19 -0
@@ -504,4 +504,23 @@ test_expect_success PERL 'difftool properly honors gitlink and core.worktree' ' ) ' +test_expect_success PERL,SYMLINKS 'difftool --dir-diff symlinked directories' ' + git init dirlinks && + ( + cd dirlinks && + git config diff.tool checktrees && + git config difftool.checktrees.cmd "echo good" && + mkdir foo && + : >foo/bar && + git add foo/bar && + test_commit symlink-one && + ln -s foo link && + git add link && + test_commit symlink-two && + echo good >expect && + git difftool --tool=checktrees --dir-diff HEAD~ >actual && + test_cmp expect actual + ) +' + test_done
Signature · 1 line
2.6.2.281.gac28444
Quoted 4 lines · David Aguilar
difftool's dir-diff feature was blindly feeding worktree paths to hash-object without checking whether the path was indeed a file, causing the feature to fail when repositories contain symlinks to directories.
Wait. Anything that considers symlinks "to directories" any special smells like a misdesign here. Why is it safe to substitute a symbolic link that happens to point at a file with the file it points at?
Because the way you would hash a symblic link is not by hashing the file it points at, but by hashing the result of readlink(2) of it, we must not reuse the working tree files for any symbolic link, regardless of its target, I would think.
After all, a symbolic link may even be dangling and not pointing at anything.
Quoted 54 lines
Ensure that only files are ever given to hash-object. Add a test to demonstrate the breakage.
Reported-by Ismail Badawi <[email protected]>Signed-off-by David Aguilar <[email protected]>--- git-difftool.perl | 4 +--- t/t7800-difftool.sh | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 3 deletions(-)git-difftool.perl +1
-3@@ -70,9 +70,7 @@ sub use_wt_file my ($repo, $workdir, $file, $sha1) = @_; my $null_sha1 = '0' x 40; - if (! -e "$workdir/$file") { - # If the file doesn't exist in the working tree, we cannot - # use it. + if (! -f "$workdir/$file") { return (0, $null_sha1); }t/t7800-difftool.sh +19
-0@@ -504,4 +504,23 @@ test_expect_success PERL 'difftool properly honors gitlink and core.worktree' ' ) ' +test_expect_success PERL,SYMLINKS 'difftool --dir-diff symlinked directories' ' + git init dirlinks && + ( + cd dirlinks && + git config diff.tool checktrees && + git config difftool.checktrees.cmd "echo good" && + mkdir foo && + : >foo/bar && + git add foo/bar && + test_commit symlink-one && + ln -s foo link && + git add link && + test_commit symlink-two && + echo good >expect && + git difftool --tool=checktrees --dir-diff HEAD~ >actual && + test_cmp expect actual + ) +' + test_done
Quoted 12 lines · Junio C Hamano
Quoted 4 lines · David Aguilar
difftool's dir-diff feature was blindly feeding worktree paths to hash-object without checking whether the path was indeed a file, causing the feature to fail when repositories contain symlinks to directories.
Wait. Anything that considers symlinks "to directories" any special smells like a misdesign here. Why is it safe to substitute a symbolic link that happens to point at a file with the file it points at?
Quoted 7 lines
Because the way you would hash a symblic link is not by hashing the file it points at, but by hashing the result of readlink(2) of it, we must not reuse the working tree files for any symbolic link, regardless of its target, I would think.
After all, a symbolic link may even be dangling and not pointing at anything.
Ah, right. I think the simplest thing to do is to tighten use_wt_file() so that it always rejects symlinks. That seems like a safe way to go for now without needing to invent a new paradigm for how to handle symlinks in the dir-diff code.
I just sent a follow-up patch that does just that. Let me know if you'd like a replacement patch that combines the two patches instead.
Thanks for the review,
Signature · 1 line
David