Skip to content

Commit 6d6a782

Browse files
pcloudsgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Nguyễn Thái Ngọc Duy <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent c041d54 commit 6d6a782

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

cache-tree.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,7 @@ static int update_one(struct cache_tree *it,
325325
const unsigned char *sha1;
326326
unsigned mode;
327327
int expected_missing = 0;
328+
int contains_ita = 0;
328329

329330
path = ce->name;
330331
pathlen = ce_namelen(ce);
@@ -341,7 +342,8 @@ static int update_one(struct cache_tree *it,
341342
i += sub->count;
342343
sha1 = sub->cache_tree->sha1;
343344
mode = S_IFDIR;
344-
if (sub->cache_tree->entry_count < 0) {
345+
contains_ita = sub->cache_tree->entry_count < 0;
346+
if (contains_ita) {
345347
to_invalidate = 1;
346348
expected_missing = 1;
347349
}
@@ -380,6 +382,12 @@ static int update_one(struct cache_tree *it,
380382
continue;
381383
}
382384

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+
383391
strbuf_grow(&buffer, entlen + 100);
384392
strbuf_addf(&buffer, "%o %.*s%c", mode, entlen, path + baselen, '\0');
385393
strbuf_add(&buffer, sha1, 20);

t/t2203-add-intent.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,5 +99,19 @@ test_expect_success 'cache-tree does not ignore dir that has i-t-a entries' '
9999
)
100100
'
101101

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+
102116
test_done
103117

0 commit comments

Comments
 (0)