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

[PATCH v3 5/6] diff --stat: mark any file larger than core.bigfilethreshold binary

From
Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Date
Aug 13, 2014, 10:57 UTC
Message-ID
<1407927454-9268-6-git-send-email-pclouds@gmail.com>
In-Reply-To
<1407927454-9268-1-git-send-email-pclouds@gmail.com>

Too large files may lead to failure to allocate memory. If it happens here, it could impact quite a few commands that involve diff. Moreover, too large files are inefficient to compare anyway (and most likely non-text), so mark them binary and skip looking at their content.

Noticed-by: Dale R. Worley <worley@alum.mit.edu>
Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 Documentation/config.txt        |  3 ++-
 Documentation/gitattributes.txt |  4 ++--
 diff.c                          | 26 ++++++++++++++++++--------
 diffcore.h                      |  1 +
 t/t1050-large.sh                |  4 ++++
 5 files changed, 27 insertions(+), 11 deletions(-)
diff --git a/Documentation/config.txt b/Documentation/config.txt
index c55c22a..53df40e 100644
--- a/Documentation/config.txt
+++ b/Documentation/config.txt
@@ -499,7 +499,8 @@ core.bigFileThreshold::
 	Files larger than this size are stored deflated, without
 	attempting delta compression.  Storing large files without
 	delta compression avoids excessive memory usage, at the
-	slight expense of increased disk usage.
+	slight expense of increased disk usage. Additionally files
+	larger than this size are allways treated as binary.
 +
 Default is 512 MiB on all platforms.  This should be reasonable
 for most projects as source code and other text files can still
diff --git a/Documentation/gitattributes.txt b/Documentation/gitattributes.txt
index 643c1ba..9b45bda 100644
--- a/Documentation/gitattributes.txt
+++ b/Documentation/gitattributes.txt
@@ -440,8 +440,8 @@ Unspecified::
 
 	A path to which the `diff` attribute is unspecified
 	first gets its contents inspected, and if it looks like
-	text, it is treated as text.  Otherwise it would
-	generate `Binary files differ`.
+	text and is smaller than core.bigFileThreshold, it is treated
+	as text. Otherwise it would generate `Binary files differ`.
 
 String::
 
diff --git a/diff.c b/diff.c
index f4b7421..d381a6f 100644
--- a/diff.c
+++ b/diff.c
@@ -2188,8 +2188,8 @@ int diff_filespec_is_binary(struct diff_filespec *one)
 			one->is_binary = one->driver->binary;
 		else {
 			if (!one->data && DIFF_FILE_VALID(one))
-				diff_populate_filespec(one, 0);
-			if (one->data)
+				diff_populate_filespec(one, CHECK_BINARY);
+			if (one->is_binary == -1 && one->data)
 				one->is_binary = buffer_is_binary(one->data,
 						one->size);
 			if (one->is_binary == -1)
@@ -2725,6 +2725,11 @@ int diff_populate_filespec(struct diff_filespec *s, unsigned int flags)
 		}
 		if (size_only)
 			return 0;
+		if ((flags & CHECK_BINARY) &&
+		    s->size > big_file_threshold && s->is_binary == -1) {
+			s->is_binary = 1;
+			return 0;
+		}
 		fd = open(s->path, O_RDONLY);
 		if (fd < 0)
 			goto err_empty;
@@ -2746,16 +2751,21 @@ int diff_populate_filespec(struct diff_filespec *s, unsigned int flags)
 	}
 	else {
 		enum object_type type;
-		if (size_only) {
+		if (size_only || (flags & CHECK_BINARY)) {
 			type = sha1_object_info(s->sha1, &s->size);
 			if (type < 0)
 				die("unable to read %s", sha1_to_hex(s->sha1));
-		} else {
-			s->data = read_sha1_file(s->sha1, &type, &s->size);
-			if (!s->data)
-				die("unable to read %s", sha1_to_hex(s->sha1));
-			s->should_free = 1;
+			if (size_only)
+				return 0;
+			if (s->size > big_file_threshold && s->is_binary == -1) {
+				s->is_binary = 1;
+				return 0;
+			}
 		}
+		s->data = read_sha1_file(s->sha1, &type, &s->size);
+		if (!s->data)
+			die("unable to read %s", sha1_to_hex(s->sha1));
+		s->should_free = 1;
 	}
 	return 0;
 }
diff --git a/diffcore.h b/diffcore.h
index c80df18..33ea2de 100644
--- a/diffcore.h
+++ b/diffcore.h
@@ -56,6 +56,7 @@ extern void fill_filespec(struct diff_filespec *, const unsigned char *,
 			  int, unsigned short);
 
 #define CHECK_SIZE_ONLY 1
+#define CHECK_BINARY    2
 extern int diff_populate_filespec(struct diff_filespec *, unsigned int);
 extern void diff_free_filespec_data(struct diff_filespec *);
 extern void diff_free_filespec_blob(struct diff_filespec *);
diff --git a/t/t1050-large.sh b/t/t1050-large.sh
index eec2cca..711f22c 100755
--- a/t/t1050-large.sh
+++ b/t/t1050-large.sh
@@ -112,6 +112,10 @@ test_expect_success 'diff --raw' '
 	git diff --raw HEAD^
 '
 
+test_expect_success 'diff --stat' '
+	git diff --stat HEAD^ HEAD
+'
+
 test_expect_success 'hash-object' '
 	git hash-object large1
 '
-- 
2.1.0.rc0.78.gc0d8480
Previous: Nguyễn Thái Ngọc DuyNext: Eric Sunshine
Message 37 of 49 in “Git chokes on large file”
  1. Dale R. WorleyMay 27, 2014
  2. Duy NguyenMay 28, 2014
  3. Junio C HamanoMay 28, 2014
  4. Dale R. WorleyMay 28, 2014
  5. Dale R. WorleyMay 28, 2014
  6. David LangMay 28, 2014
  7. Dale R. WorleyMay 28, 2014
  8. David LangMay 28, 2014
  9. Dale R. WorleyMay 29, 2014
  10. Junio C HamanoMay 28, 2014
  11. David LangMay 28, 2014
  12. 1/4 wrapper.c: introduce gentle xmallocz that does not die()Nguyễn Thái Ngọc Duy, May 29, 2014
  13. 2/4 fsck: do not die when not enough memory to examine a pack entryNguyễn Thái Ngọc Duy, May 29, 2014
  14. 3/4 diff.c: allow to pass more flags to diff_populate_filespecNguyễn Thái Ngọc Duy, May 29, 2014
  15. 4/4 diff: mark any file larger than core.bigfilethreshold binaryNguyễn Thái Ngọc Duy, May 29, 2014
  16. Thomas BraunJun 19, 2014
  17. Duy NguyenJun 23, 2014
  18. Thomas BraunJun 23, 2014
  19. 1/4 wrapper.c: introduce gentle xmallocz that does not die()Nguyễn Thái Ngọc Duy, Jun 24, 2014
  20. 2/4 fsck: do not die when not enough memory to examine a pack entryNguyễn Thái Ngọc Duy, Jun 24, 2014
  21. Junio C HamanoJun 26, 2014
  22. Duy NguyenJun 29, 2014
  23. 3/4 diff.c: allow to pass more flags to diff_populate_filespecNguyễn Thái Ngọc Duy, Jun 24, 2014
  24. 4/4 diff: mark any file larger than core.bigfilethreshold binaryNguyễn Thái Ngọc Duy, Jun 24, 2014
  25. Junio C HamanoJun 26, 2014
  26. Thomas BraunJun 27, 2014
  27. Duy NguyenJun 29, 2014
  28. 0/6 Large file improvementsNguyễn Thái Ngọc Duy, Aug 13, 2014
  29. 1/6 wrapper.c: introduce gentle xmalloc(z) that does not die()Nguyễn Thái Ngọc Duy, Aug 13, 2014
  30. Junio C HamanoAug 14, 2014
  31. 2/6 sha1_file.c: do not die failing to malloc in unpack_compressed_entryNguyễn Thái Ngọc Duy, Aug 13, 2014
  32. Junio C HamanoAug 13, 2014
  33. 3/6 unpack-objects: continue when fail to malloc due to large objectsNguyễn Thái Ngọc Duy, Aug 13, 2014
  34. Junio C HamanoAug 14, 2014
  35. Duy NguyenAug 15, 2014
  36. 4/6 diff.c: allow to pass more flags to diff_populate_filespecNguyễn Thái Ngọc Duy, Aug 13, 2014
  37. 5/6 diff --stat: mark any file larger than core.bigfilethreshold binaryNguyễn Thái Ngọc Duy, Aug 13, 2014
  38. Eric SunshineAug 13, 2014
  39. 6/6 diff: shortcut for diff'ing two binary SHA-1 objectsNguyễn Thái Ngọc Duy, Aug 13, 2014
  40. Junio C HamanoAug 14, 2014
  41. Duy NguyenAug 15, 2014
  42. Junio C HamanoAug 14, 2014
  43. 0/5 Large file improvementsNguyễn Thái Ngọc Duy, Aug 16, 2014
  44. 1/5 wrapper.c: introduce gentle xmallocz that does not die()Nguyễn Thái Ngọc Duy, Aug 16, 2014
  45. 2/5 sha1_file.c: do not die failing to malloc in unpack_compressed_entryNguyễn Thái Ngọc Duy, Aug 16, 2014
  46. 3/5 diff.c: allow to pass more flags to diff_populate_filespecNguyễn Thái Ngọc Duy, Aug 16, 2014
  47. 4/5 diff --stat: mark any file larger than core.bigfilethreshold binaryNguyễn Thái Ngọc Duy, Aug 16, 2014
  48. 5/5 diff: shortcut for diff'ing two binary SHA-1 objectsNguyễn Thái Ngọc Duy, Aug 16, 2014
  49. Thomas BraunMay 28, 2014

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.