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

[PATCH] cache-tree: do not cache empty trees

From
Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Date
Feb 5, 2011, 09:50 UTC
Message-ID
<1296899427-1394-1-git-send-email-pclouds@gmail.com>
In-Reply-To
<1296894611-29398-1-git-send-email-pclouds@gmail.com>

Current index does not support empty trees. But users can construct empty trees directly using plumbing. When empty trees are checked out, things become inconsistent:

 - If cache-tree somehow is invalidated, when a tree is read to index,
   empty trees disappear. When we write trees back, empty trees will
   be gone.
 - If cache-tree is generated by read-tree and remains valid by the
   time trees are written back, empty trees remain.

Let's do it in a consistent way, always disregard empty trees in index. If users choose to create empty trees their own way, they should not use index at all.

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 Scratch the first version. This one actually works.
 cache-tree.c               |    9 +++++++++
 t/t1013-read-tree-empty.sh |   38 ++++++++++++++++++++++++++++++++++++++
 2 files changed, 47 insertions(+), 0 deletions(-)
 create mode 100755 t/t1013-read-tree-empty.sh
diff --git a/cache-tree.c b/cache-tree.c
index f755590..03732ad 100644
--- a/cache-tree.c
+++ b/cache-tree.c
@@ -621,9 +621,18 @@ static void prime_cache_tree_rec(struct cache_tree *it, struct tree *tree)
 			struct tree *subtree = lookup_tree(entry.sha1);
 			if (!subtree->object.parsed)
 				parse_tree(subtree);
+			if (!hashcmp(entry.sha1, (unsigned char *)EMPTY_TREE_SHA1_BIN)) {
+				warning("empty tree detected! Will be removed from new commits");
+				cnt = -1;
+				break;
+			}
 			sub = cache_tree_sub(it, entry.path);
 			sub->cache_tree = cache_tree();
 			prime_cache_tree_rec(sub->cache_tree, subtree);
+			if (sub->cache_tree->entry_count == -1) {
+				cnt = -1;
+				break;
+			}
 			cnt += sub->cache_tree->entry_count;
 		}
 	}
diff --git a/t/t1013-read-tree-empty.sh b/t/t1013-read-tree-empty.sh
new file mode 100755
index 0000000..a9279f0
--- /dev/null
+++ b/t/t1013-read-tree-empty.sh
@@ -0,0 +1,38 @@
+#!/bin/sh
+
+test_description='read-tree with empty trees'
+
+. ./test-lib.sh
+
+T1=f4ec99e8174c01eab488469b4c2680500bbb18da
+T2=4b825dc642cb6eb9a060e54bf8d69288fbee4904
+
+test_expect_success 'setup' '
+	printf "40000 empty\0\x4b\x82\x5d\xc6\x42\xcb\x6e\xb9\xa0\x60\xe5\x4b\xf8\xd6\x92\x88\xfb\xee\x49\x04" >newtree &&
+	git hash-object -w -t tree newtree >actual &&
+	echo $T1 >expected
+	test_cmp expected actual
+'
+
+test_expect_success 'ls-tree T1 (with empty tree)' '
+	git ls-tree $T1 >actual &&
+	cat <<EOF >expected &&
+040000 tree 4b825dc642cb6eb9a060e54bf8d69288fbee4904	empty
+EOF
+	test_cmp expected actual
+'
+
+test_expect_success 'write-tree removes empty tree' '
+	git read-tree "$T1" &&
+	git write-tree >actual
+	echo $T2 >expected
+	test_cmp expected actual
+'
+
+test_expect_success 'ls-tree T2 (without empty tree)' '
+	git ls-tree $T2 >actual &&
+	: >expected &&
+	test_cmp expected actual
+'
+
+test_done
-- 
1.7.3.4.878.g439c7
Previous: Nguyễn Thái Ngọc DuyNext: Jonathan Nieder
Message 2 of 20 in “cache-tree: do not cache empty trees”
  1. cache-tree: do not cache empty treesNguyễn Thái Ngọc Duy, Feb 5, 2011
  2. cache-tree: do not cache empty treesNguyễn Thái Ngọc Duy, Feb 5, 2011
  3. Jonathan NiederFeb 5, 2011
  4. Nguyen Thai Ngoc DuyFeb 5, 2011
  5. cache-tree: do not cache empty treesNguyễn Thái Ngọc Duy, Feb 5, 2011
  6. Junio C HamanoFeb 7, 2011
  7. Nguyen Thai Ngoc DuyFeb 7, 2011
  8. correct type of EMPTY_TREE_SHA1_BINJonathan Nieder, Feb 7, 2011
  9. Junio C HamanoFeb 9, 2011
  10. Jonathan NiederFeb 7, 2011
  11. Nguyen Thai Ngoc DuyFeb 7, 2011
  12. Ilari LiusvaaraFeb 7, 2011
  13. Nguyen Thai Ngoc DuyFeb 7, 2011
  14. Jonathan NiederFeb 7, 2011
  15. Junio C HamanoFeb 7, 2011
  16. Nguyen Thai Ngoc DuyFeb 8, 2011
  17. Jonathan NiederFeb 8, 2011
  18. Yann DirsonFeb 15, 2011
  19. Jakub NarebskiFeb 16, 2011
  20. Ilari LiusvaaraFeb 8, 2011

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.