diff: do not short-cut CHECK_SIZE_ONLY check in diff_populate_filespec()

Callers of diff_populate_filespec() can choose to ask only for the size of the blob without grabbing the blob data, and the function, after running lstat() when the filespec points at a working tree file, returns by copying the value in size field of the stat structure into the size field of the filespec when this is the case. However, this short-cut cannot be taken if the contents from the path needs to go through convert_to_git(), whose resulting real blob data may be different from what is in the working tree file. As "git diff --quiet" compares the .size fields of filespec structures to skip content comparison, this bug manifests as a false "there are differences" for a file that needs eol conversion, for example. Reported-by: Mike Crowe <mac@mcrowe.com> Helped-by: Torsten Bögershausen <tboegi@web.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Mar 1, 2017 at 18:04 UTC 12426e114b252d130d83c3f098c4ceae3cf217e3
2 files changed +27 -1
diff.c
+18 -1
@@ -2783,8 +2783,25 @@ int diff_populate_filespec(struct diff_filespec *s, unsigned int flags)
2783 s->should_free = 1;
2784 return 0;
2785 }
2786 - if (size_only)
2786 +
2787 + /*
2788 + * Even if the caller would be happy with getting
2789 + * only the size, we cannot return early at this
2790 + * point if the path requires us to run the content
2791 + * conversion.
2792 + */
2793 + if (size_only && !would_convert_to_git(s->path))
2794 return 0;
2795 +
2796 + /*
2797 + * Note: this check uses xsize_t(st.st_size) that may
2798 + * not be the true size of the blob after it goes
2799 + * through convert_to_git(). This may not strictly be
2800 + * correct, but the whole point of big_file_threshold
2801 + * and is_binary check being that we want to avoid
2802 + * opening the file and inspecting the contents, this
2803 + * is probably fine.
2804 + */
2805 if ((flags & CHECK_BINARY) &&
2806 s->size > big_file_threshold && s->is_binary == -1) {
2807 s->is_binary = 1;
t/t4035-diff-quiet.sh
+9
@@ -152,4 +152,13 @@ test_expect_success 'git diff --quiet ignores stat-change only entries' '
152 test_expect_code 1 git diff --quiet
153 '
154
155 +test_expect_success 'git diff --quiet on a path that need conversion' '
156 + echo "crlf.txt text=auto" >.gitattributes &&
157 + printf "Hello\r\nWorld\r\n" >crlf.txt &&
158 + git add .gitattributes crlf.txt &&
159 +
160 + printf "Hello\r\nWorld\n" >crlf.txt &&
161 + git diff --quiet crlf.txt
162 +'
163 +
164 test_done