git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v7 1/1] read-cache: do not trust a size change when conversion is active

From
tboegi@web.de <tboegi@web.de>
Date
Nov 29, 2026, 15:28 UTC
Message-ID
<20261129152805.758456-1-tboegi@web.de>
In-Reply-To
<xmqqwm1no29m.fsf@gitster.g>
From: Torsten Bögershausen <tboegi@web.de>

"git status" can report a file as modified while "git diff" and "git add" agree that its contents are unchanged:

  git init t && cd t
  printf '* text eol=lf\n' >.gitattributes
  printf 'one\ntwo\nthree\n' >file.txt
  git add . && git commit -m init
  git ls-files --debug file.txt | grep size # size: 14 flags: 0
  printf 'one\r\ntwo\r\nthree\r\n' >file.txt
  git ls-files --debug file.txt | grep size # size: 14 flags: 0
  git status --short      # ->  M file.txt
  git ls-files --debug file.txt | grep size # size: 14 (*)
  git diff                # -> empty
  git add file.txt        # -> nothing to commit
  git ls-files --debug file.txt  | grep size #  size: 17 flags: 0

The problem is that `git status` assumes that a file is modified when the size on disk is different from what is recorded in the index.

This assumption was valid when Git was created in 2005. It is no longer valid since core.autocrlf and the 'crlf' attributes had been introduced in 2007, followed by clean and other input filters.

Today different different file contents and/or file length may result in the same content in the index using `git add`, when all input conversions are run. (*) With this change: size: 17

This change removes the 'shortcut' in read-cache/ie_modified(): when a regular file has a different size compared to what is recorded in the index then the file is modified. When the path is subject to conversion (the "text" and "eol" attributes, core.autocrlf, a clean filter), different bytes can convert to the same blob, as the CRLF rewrite above does.

Now `git update-index --refresh` succeeds instead of reporting "needs update", `git ls-files -m` and `git status` no longer report the file as modified. `git rebase` and `git pull --rebase` no longer refuse to run. `git diff` itself is unchanged; it already reads and converts the file and prints nothing.

The analyses of the problem, the new tests in t0020-crlf.sh and the first versions of a fix was done by Curtis Allen Smith.

Helped-by: Curtis Allen Smith <curtis.allen.smith@gmail.com>
Signed-off-by: Torsten Bögershausen <tboegi@web.de>
---
Next version of a patch - only 1 commit, achiving the final code
in one go.
The commit message and comments may need improvements,
as well as the comments. In short: A review is appreciated.
And yes, there is no artifical i involved.
And no, the "racy git" code path is not involved or changed.
read-cache.c    | 26 ++++++++++++--------------
 t/t0020-crlf.sh | 39 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 51 insertions(+), 14 deletions(-)
diff --git a/read-cache.c b/read-cache.c
index 595497d14c..2757bbb6ff 100644
--- a/read-cache.c
+++ b/read-cache.c
@@ -593,20 +593,18 @@ int ie_modified(struct index_state *istate,
 		return changed;
 
 	/*
-	 * Immediately after read-tree or update-index --cacheinfo,
-	 * the length field is zero, as we have never even read the
-	 * lstat(2) information once, and we cannot trust DATA_CHANGED
-	 * returned by ie_match_stat() which in turn was returned by
-	 * ce_match_stat_basic() to signal that the filesize of the
-	 * blob changed.  We have to actually go to the filesystem to
-	 * see if the contents match, and if so, should answer "unchanged".
+	 * gitlinks do not go through any conversion, see below.
+	 * When the size if different, the content is different.
+	 *
+	 * When the length field of a regular file is zero or changed,
+	 * this does not always mean a changed blob in the index.
+	 * All input filters (CRLF line endings, clean filter..)
+	 * need to be applied followed by a hash and oideq().
+	 * This is done in ce_modified_check_fs() below,
+	 * which calls ce_compare_data().
 	 *
-	 * The logic does not apply to gitlinks, as ce_match_stat_basic()
-	 * already has checked the actual HEAD from the filesystem in the
-	 * subproject.  If ie_match_stat() already said it is different,
-	 * then we know it is.
 	 */
-	if ((changed & DATA_CHANGED) &&
+	if ((S_ISGITLINK(ce->ce_mode) &&
 #ifdef GIT_WINDOWS_NATIVE
 	    /*
 	     * Work around Git for Windows v2.27.0 fixing a bug where symlinks'
@@ -616,9 +614,9 @@ int ie_modified(struct index_state *istate,
 	     * So let's just special-case symlinks with a target path length
 	     * (i.e. `sd_size`) of 4096 and force them to be re-checked.
 	     */
-	    (!S_ISLNK(st->st_mode) || ce->ce_stat_data.sd_size != MAX_PATH) &&
+	    (ce->ce_stat_data.sd_size != MAX_PATH) &&
 #endif
-	    (S_ISGITLINK(ce->ce_mode) || ce->ce_stat_data.sd_size != 0))
+	    (changed & DATA_CHANGED)))
 		return changed;
 
 	changed_fs = ce_modified_check_fs(istate, ce, st);
diff --git a/t/t0020-crlf.sh b/t/t0020-crlf.sh
index fd1cae09ed..9c8d07cfc4 100755
--- a/t/t0020-crlf.sh
+++ b/t/t0020-crlf.sh
@@ -397,4 +397,43 @@ test_expect_success 'New CRLF file gets LF in repo' '
 	test_cmp alllf alllf2
 '
 
+test_expect_success 'status does not report a CRLF-only rewrite as modified' '
+	test_when_finished "git reset --hard autocrlf-checkpoint" &&
+	echo "eol.txt text eol=lf" >.gitattributes &&
+	printf "one\ntwo\nthree\n" >eol.txt &&
+	git add .gitattributes eol.txt &&
+	git commit -m "eol.txt" &&
+
+	# a generator rewrites the file with CRLF, same content
+	printf "one\r\ntwo\r\nthree\r\n" >eol.txt &&
+	git status --porcelain -uno >actual &&
+	test_must_be_empty actual &&
+	git diff --exit-code &&
+
+	# a real change is still reported
+	printf "one\r\ntwo\r\nfour\r\n" >eol.txt &&
+	git status --porcelain -uno >actual &&
+	echo " M eol.txt" >expect &&
+	test_cmp expect actual
+'
+
+test_expect_success 'status sees through a CRLF rewrite of a file with a lone CR' '
+	test_when_finished "git reset --hard autocrlf-checkpoint" &&
+	echo "cr.txt text eol=lf" >.gitattributes &&
+	printf "one\rtwo\nthree\n" >cr.txt &&
+	git add .gitattributes cr.txt &&
+	git commit -m "cr.txt" &&
+
+	# "git add" keeps the lone CR and drops the others
+	printf "one\rtwo\r\nthree\r\n" >cr.txt &&
+	git status --porcelain -uno >actual &&
+	test_must_be_empty actual &&
+
+	# the converted length matches the blob, the content does not
+	printf "one\rtwo\nthrEE\r\n" >cr.txt &&
+	git status --porcelain -uno >actual &&
+	echo " M cr.txt" >expect &&
+	test_cmp expect actual
+'
+
 test_done
-- 
2.56.1.713.geb3bf631d6
Previous: Junio C HamanoNext: Junio C Hamano
Message 32 of 37 in “utf8.c: Prepare workaround for iconv under macOS 14/15”
  1. 1/2 utf8.c: Prepare workaround for iconv under macOS 14/15tboegi@web.de, Jan 11, 2026
  2. Junio C HamanoJan 12, 2026
  3. 1/2 utf8.c: Prepare workaround for iconv under macOS 14/15tboegi@web.de, Jan 12, 2026
  4. 0/2 Workaround for iconv under macOS 14/15tboegi@web.de, Jan 12, 2026
  5. 2/2 utf8.c: Enable workaround for iconv under macOS 14/15tboegi@web.de, Jan 12, 2026
  6. 0/1 read-cache: do not trust a size change when conversiontboegi@web.de, Oct 12, 2026
  7. 1/1 read-cache: do not trust a size change when conversion is activetboegi@web.de, Oct 12, 2026
  8. Junio C HamanoOct 12, 2026
  9. Junio C HamanoOct 13, 2026
  10. Curtis SmithOct 13, 2026
  11. Johannes SixtOct 13, 2026
  12. Curtis SmithOct 13, 2026
  13. read-cache: do not trust a size change when conversion is activeCurtis Allen Smith, Oct 13, 2026
  14. Phillip WoodOct 13, 2026
  15. Curtis SmithOct 13, 2026
  16. Torsten BögershausenOct 13, 2026
  17. read-cache: do not trust a size change when conversion is activeCurtis Allen Smith, Oct 13, 2026
  18. Junio C HamanoOct 13, 2026
  19. Torsten BögershausenOct 18, 2026
  20. Junio C HamanoOct 19, 2026
  21. Torsten BögershausenOct 20, 2026
  22. 0/1 More cs/read-cache-conversion-sizetboegi@web.de, Oct 26, 2026
  23. 1/1 read-cache: optimize file size change when conversion is activetboegi@web.de, Oct 26, 2026
  24. 0/2 read-cache: do not trust a size change when conversion is activetboegi@web.de, Nov 27, 2026
  25. Junio C HamanoNov 28, 2026
  26. 1/2 read-cache: do not trust a size change when conversion is activetboegi@web.de, Nov 27, 2026
  27. Junio C HamanoNov 28, 2026
  28. Torsten BögershausenNov 28, 2026
  29. Junio C HamanoNov 29, 2026
  30. 2/2 read-cache: optimize file size change when conversion is activetboegi@web.de, Nov 27, 2026
  31. Junio C HamanoNov 28, 2026
  32. 1/1 read-cache: do not trust a size change when conversion is activetboegi@web.de, Nov 29, 2026
  33. Junio C HamanoNov 29, 2026
  34. Junio C HamanoNov 29, 2026
  35. Junio C HamanoNov 29, 2026
  36. Torsten BögershausenNov 29, 2026
  37. Junio C HamanoNov 29, 2026

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.