]> git.ipfire.org Git - thirdparty/git.git/commitdiff
cache-tree: do not generate empty trees as a result of all i-t-a subentries
authorNguyễn Thái Ngọc Duy <pclouds@gmail.com>
Sat, 16 Jul 2016 05:06:27 +0000 (07:06 +0200)
committerJunio C Hamano <gitster@pobox.com>
Mon, 18 Jul 2016 20:45:33 +0000 (13:45 -0700)
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>
cache-tree.c
t/t2203-add-intent.sh

index c2676e8a310a46b64fbaf607ee83daeac8367f2c..f28b1f45a49b842c2f7a372b0c0c595bebc56b5d 100644 (file)
@@ -325,6 +325,7 @@ static int update_one(struct cache_tree *it,
                const unsigned char *sha1;
                unsigned mode;
                int expected_missing = 0;
+               int contains_ita = 0;
 
                path = ce->name;
                pathlen = ce_namelen(ce);
@@ -341,7 +342,8 @@ static int update_one(struct cache_tree *it,
                        i += sub->count;
                        sha1 = sub->cache_tree->sha1;
                        mode = S_IFDIR;
-                       if (sub->cache_tree->entry_count < 0) {
+                       contains_ita = sub->cache_tree->entry_count < 0;
+                       if (contains_ita) {
                                to_invalidate = 1;
                                expected_missing = 1;
                        }
@@ -380,6 +382,12 @@ static int update_one(struct cache_tree *it,
                        continue;
                }
 
+               /*
+                * "sub" can be an empty tree if all subentries are i-t-a.
+                */
+               if (contains_ita && !hashcmp(sha1, EMPTY_TREE_SHA1_BIN))
+                       continue;
+
                strbuf_grow(&buffer, entlen + 100);
                strbuf_addf(&buffer, "%o %.*s%c", mode, entlen, path + baselen, '\0');
                strbuf_add(&buffer, sha1, 20);
index 24aed2e5410d00a10498dfa0da023d998b4f6005..8f22c43e245cb6fb68449a64ba45222e187f56a9 100755 (executable)
@@ -99,5 +99,19 @@ test_expect_success 'cache-tree does not ignore dir that has i-t-a entries' '
        )
 '
 
+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 &&
+               git write-tree >actual &&
+               echo $EMPTY_TREE >expected &&
+               test_cmp expected actual
+       )
+'
+
 test_done