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

Re: [PATCH v2 2/2] cache-tree: do not generate empty trees as a result of all i-t-a subentries

From
Duy Nguyen <pclouds@gmail.com>
Date
Jul 7, 2016, 17:12 UTC
Message-ID
<20160707171237.GA31445@duynguyen>
In-Reply-To
<xmqqeg76sh44.fsf@gitster.mtv.corp.google.com>
On Wed, Jul 06, 2016 at 12:26:19PM -0700, Junio C Hamano wrote:
Show 27 quoted lines
> Nguyễn Thái Ngọc Duy  <pclouds@gmail.com> writes:
> 
> > @@ -426,6 +433,15 @@ int cache_tree_update(struct index_state *istate, int flags)
> >  	i = update_one(it, cache, entries, "", 0, &skip, flags);
> >  	if (i < 0)
> >  		return i;
> > +	/*
> > +	 * Top dir can become empty if all entries are i-t-a (even
> > +	 * from subdirs). Note that we do allow to create an empty
> > +	 * tree from an empty index. Only error when an empty tree is
> > +	 * a result of the i-t-a thing.
> > +	 */
> > +	if (it->entry_count < 0 &&
> > +	    !hashcmp(it->sha1, EMPTY_TREE_SHA1_BIN))
> > +		return error(_("cannot build a tree from just intent-to-add entries"));
> 
> The test would not let you tell between the two possible ways the
> last step "git commit" fails.
> 
> Did it fail due to the protection this change adds (i.e. you should
> be checking if "git write-tree" fails if that is the case we want to
> cover), or did it fail because you recorded an empty tree as the
> root commit without giving the --allow-empty option?
> 
> In any case, I am not sure about the logic in the comment, either.
> "git commit --allow-empty" should be able to create a new commit
> without any files in it, no?

You're right. If an empty index can produce an empty tree, then an index full of i-t-a entries should also be able to produce an empty tree.

git-commit not failing when --allow-empty is not given is another (known) problem, where it miscounts the number of real index entries. It's not right to "fix" it in here.

I'll deal with that separately. Let's focus on cache-tree only this time. So how about this on top?

-- 8< --
diff --git a/cache-tree.c b/cache-tree.c
index 75e73d7..2d50640 100644
--- a/cache-tree.c
+++ b/cache-tree.c
@@ -433,15 +433,6 @@ int cache_tree_update(struct index_state *istate, int flags)
 	i = update_one(it, cache, entries, "", 0, &skip, flags);
 	if (i < 0)
 		return i;
-	/*
-	 * Top dir can become empty if all entries are i-t-a (even
-	 * from subdirs). Note that we do allow to create an empty
-	 * tree from an empty index. Only error when an empty tree is
-	 * a result of the i-t-a thing.
-	 */
-	if (it->entry_count < 0 &&
-	    !hashcmp(it->sha1, EMPTY_TREE_SHA1_BIN))
-		return error(_("cannot build a tree from just intent-to-add entries"));
 	istate->cache_changed |= CACHE_TREE_CHANGED;
 	return 0;
 }
diff --git a/t/t2203-add-intent.sh b/t/t2203-add-intent.sh
index a19f06b..1a01279 100755
--- a/t/t2203-add-intent.sh
+++ b/t/t2203-add-intent.sh
@@ -104,10 +104,17 @@ test_expect_success 'cache-tree does skip dir that becomes empty' '
 	git init ita-in-dir &&
 	(
 		cd ita-in-dir &&
+		echo ground >ground &&
+		git add ground &&
 		mkdir -p 1/2/3 &&
 		echo 4 >1/2/3/4 &&
 		git add -N 1/2/3/4 &&
-		test_must_fail git commit -m committed
+		git commit -m committed &&
+		git ls-tree -r HEAD >actual &&
+		cat >expected <<\EOF &&
+100644 blob b649b43b0708f5604ac912f5a15f7da2bad51a1b	ground
+EOF
+		test_cmp expected actual
 	)
 '
 
-- 8< --

> > +test_expect_success 'cache-tree does skip dir that becomes empty' '
> > +	rm -fr ita-in-dir &&
> > +	git init ita-in-dir &&
> > +	(
> > +		cd ita-in-dir &&
> > +		mkdir -p 1/2/3 &&
> > +		echo 4 >1/2/3/4 &&
> > +		git add -N 1/2/3/4 &&
> > +		test_must_fail git commit -m committed
> > +	)
> > +'
> > +
> >  test_done
Previous: Junio C HamanoNext: Junio C Hamano
Message 10 of 30 in “Git doesn't always add files to a commit (amend)”
  1. Yuri KanivetskyJul 4, 2016
  2. Duy NguyenJul 4, 2016
  3. cache-tree.c: fix i-t-a check skipping directory updates sometimesNguyễn Thái Ngọc Duy, Jul 4, 2016
  4. Junio C HamanoJul 6, 2016
  5. Junio C HamanoJul 6, 2016
  6. 0/2 cache-tree building fix in the presence of ita entriesNguyễn Thái Ngọc Duy, Jul 6, 2016
  7. 1/2 cache-tree.c: fix i-t-a entry skipping directory updates sometimesNguyễn Thái Ngọc Duy, Jul 6, 2016
  8. 2/2 cache-tree: do not generate empty trees as a result of all i-t-a subentriesNguyễn Thái Ngọc Duy, Jul 6, 2016
  9. Junio C HamanoJul 6, 2016
  10. Duy NguyenJul 7, 2016
  11. Junio C HamanoJul 7, 2016
  12. Duy NguyenJul 8, 2016
  13. Junio C HamanoJul 8, 2016
  14. Duy NguyenJul 8, 2016
  15. Junio C HamanoJul 8, 2016
  16. 0/4 cache-tree building fix in the presence of ita entriesNguyễn Thái Ngọc Duy, Jul 9, 2016
  17. 2/4 test-lib.sh: introduce and use $_EMPTY_BLOBNguyễn Thái Ngọc Duy, Jul 9, 2016
  18. 3/4 cache-tree.c: fix i-t-a entry skipping directory updates sometimesNguyễn Thái Ngọc Duy, Jul 9, 2016
  19. 4/4 cache-tree: do not generate empty trees as a result of all i-t-a subentriesNguyễn Thái Ngọc Duy, Jul 9, 2016
  20. Junio C HamanoJul 12, 2016
  21. Duy NguyenJul 13, 2016
  22. 1/4 test-lib.sh: introduce and use $_EMPTY_TREENguyễn Thái Ngọc Duy, Jul 9, 2016
  23. Junio C HamanoJul 12, 2016
  24. Duy NguyenJul 13, 2016
  25. 0/4 cache-tree building fix in the presence of ita entriesNguyễn Thái Ngọc Duy, Jul 16, 2016
  26. 1/4 test-lib.sh: introduce and use $EMPTY_TREENguyễn Thái Ngọc Duy, Jul 16, 2016
  27. 3/4 cache-tree.c: fix i-t-a entry skipping directory updates sometimesNguyễn Thái Ngọc Duy, Jul 16, 2016
  28. 4/4 cache-tree: do not generate empty trees as a result of all i-t-a subentriesNguyễn Thái Ngọc Duy, Jul 16, 2016
  29. 2/4 test-lib.sh: introduce and use $EMPTY_BLOBNguyễn Thái Ngọc Duy, Jul 16, 2016
  30. Junio C HamanoJul 18, 2016

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.