Skip to content

Commit 12426e1

Browse files
committed
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 <[email protected]> Helped-by: Torsten Bögershausen <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 0b65a8d commit 12426e1

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

diff.c

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2783,8 +2783,25 @@ int diff_populate_filespec(struct diff_filespec *s, unsigned int flags)
27832783
s->should_free = 1;
27842784
return 0;
27852785
}
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))
27872794
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+
*/
27882805
if ((flags & CHECK_BINARY) &&
27892806
s->size > big_file_threshold && s->is_binary == -1) {
27902807
s->is_binary = 1;

t/t4035-diff-quiet.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,4 +152,13 @@ test_expect_success 'git diff --quiet ignores stat-change only entries' '
152152
test_expect_code 1 git diff --quiet
153153
'
154154

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+
155164
test_done

0 commit comments

Comments
 (0)