diff: do not reuse worktree files that need "clean" conversion
authorJeff King <peff@peff.net>
Fri, 22 Jul 2016 15:27:53 +0000 (11:27 -0400)
committerJunio C Hamano <gitster@pobox.com>
Fri, 22 Jul 2016 19:31:24 +0000 (12:31 -0700)
When accessing a blob for a diff, we may try to reuse file
contents in the working tree, under the theory that it is
faster to mmap those file contents than it would be to
extract the content from the object database.

When we have to filter those contents, though, that
assumption does not hold. Even for our internal conversions
like CRLF, we have to allocate and fill a new buffer anyway.
But much worse, for external clean filters we have to exec
an arbitrary script, and we have no idea how expensive it
may be to run.

So let's skip this optimization when conversion into git's
"clean" form is required. This applies whenever the
"want_file" flag is false. When it's true, the caller
actually wants the smudged worktree contents, which the
reused file by definition already has (in fact, this is a
key optimization going the other direction, since reusing
the worktree file there lets us skip smudge filters).

Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
diff.c
t/t0021-conversion.sh
diff --git a/diff.c b/diff.c
index 059123c5dcef4129763895b0f2ad5a54728b0c07..918cedc4ad3f4ef21f5b8d04325bd7286ef83de7 100644 (file)
--- a/diff.c
+++ b/diff.c
@@ -2672,6 +2672,13 @@ static int reuse_worktree_file(const char *name, const unsigned char *sha1, int
        if (!FAST_WORKING_DIRECTORY && !want_file && has_sha1_pack(sha1))
                return 0;
 
+       /*
+        * Similarly, if we'd have to convert the file contents anyway, that
+        * makes the optimization not worthwhile.
+        */
+       if (!want_file && would_convert_to_git(name))
+               return 0;
+
        len = strlen(name);
        pos = cache_name_pos(name, len);
        if (pos < 0)
index 7bac2bcf260794bbfeb795b210ed0c9eceb45937..e799e5954437e08df4a013f9796c5ec5a9bb077d 100755 (executable)
@@ -268,4 +268,15 @@ test_expect_success 'disable filter with empty override' '
        test_must_be_empty err
 '
 
+test_expect_success 'diff does not reuse worktree files that need cleaning' '
+       test_config filter.counter.clean "echo . >>count; sed s/^/clean:/" &&
+       echo "file filter=counter" >.gitattributes &&
+       test_commit one file &&
+       test_commit two file &&
+
+       >count &&
+       git diff-tree -p HEAD &&
+       test_line_count = 0 count
+'
+
 test_done