cache-tree: do not generate empty trees as a result of all i-t-a subentries

If a subdirectory contains nothing but i-t-a entries, we generate an empty tree object and add it to its parent tree. Which is wrong. Such a subdirectory should not be added. Note that this has a cascading effect. If subdir 'a/b/c' contains nothing but i-t-a entries, we ignore it. But then if 'a/b' contains only (the non-existing) 'a/b/c', then we should ignore 'a/b' while building 'a' too. And it goes all the way up to top directory. Noticed-by: Junio C Hamano <gitster@pobox.com> Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Nguyễn Thái Ngọc Duy committed Jul 16, 2016 at 07:06 UTC 6d6a782fbf83927212f348f91823d886c5cd6d85
2 files changed +23 -1
cache-tree.c
+9 -1
@@ -325,6 +325,7 @@ static int update_one(struct cache_tree *it,
325 const unsigned char *sha1;
326 unsigned mode;
327 int expected_missing = 0;
328 + int contains_ita = 0;
329
330 path = ce->name;
331 pathlen = ce_namelen(ce);
@@ -341,7 +342,8 @@ static int update_one(struct cache_tree *it,
342 i += sub->count;
343 sha1 = sub->cache_tree->sha1;
344 mode = S_IFDIR;
344 - if (sub->cache_tree->entry_count < 0) {
345 + contains_ita = sub->cache_tree->entry_count < 0;
346 + if (contains_ita) {
347 to_invalidate = 1;
348 expected_missing = 1;
349 }
@@ -380,6 +382,12 @@ static int update_one(struct cache_tree *it,
382 continue;
383 }
384
385 + /*
386 + * "sub" can be an empty tree if all subentries are i-t-a.
387 + */
388 + if (contains_ita && !hashcmp(sha1, EMPTY_TREE_SHA1_BIN))
389 + continue;
390 +
391 strbuf_grow(&buffer, entlen + 100);
392 strbuf_addf(&buffer, "%o %.*s%c", mode, entlen, path + baselen, '\0');
393 strbuf_add(&buffer, sha1, 20);
t/t2203-add-intent.sh
+14
@@ -99,5 +99,19 @@ test_expect_success 'cache-tree does not ignore dir that has i-t-a entries' '
99 )
100 '
101
102 +test_expect_success 'cache-tree does skip dir that becomes empty' '
103 + rm -fr ita-in-dir &&
104 + git init ita-in-dir &&
105 + (
106 + cd ita-in-dir &&
107 + mkdir -p 1/2/3 &&
108 + echo 4 >1/2/3/4 &&
109 + git add -N 1/2/3/4 &&
110 + git write-tree >actual &&
111 + echo $EMPTY_TREE >expected &&
112 + test_cmp expected actual
113 + )
114 +'
115 +
116 test_done
117