Skip to content

Commit 4ed115e

Browse files
committed
cache-tree: do not try to use an invalidated subtree info to build a tree
We punt from repairing the cache-tree during a branch switching if it involves having to create a new tree object that does not yet exist in the object store. "mkdir dir && >dir/file && git add dir" followed by "git checkout" is one example, when a tree that records the state of such "dir/" is not in the object store. However, after discovering that we do not have a tree object that records the state of "dir/", the caller failed to remember the fact that it noticed the cache-tree entry it received for "dir/" is invalidated, it already knows it should not be populating the level that has "dir/" as its immediate subdirectory, and it is not an error at all for the sublevel cache-tree entry gave it a bogus object name it shouldn't even look at. This led the caller to detect and report a non-existent error. The end result was the same and we avoided stuffing a non-existent tree to the cache-tree, but we shouldn't have issued an alarming error message to the user. Signed-off-by: Junio C Hamano <[email protected]>
1 parent 9c4d6c0 commit 4ed115e

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

cache-tree.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,7 @@ static int update_one(struct cache_tree *it,
316316
int pathlen, entlen;
317317
const unsigned char *sha1;
318318
unsigned mode;
319+
int expected_missing = 0;
319320

320321
path = ce->name;
321322
pathlen = ce_namelen(ce);
@@ -332,8 +333,10 @@ static int update_one(struct cache_tree *it,
332333
i += sub->count;
333334
sha1 = sub->cache_tree->sha1;
334335
mode = S_IFDIR;
335-
if (sub->cache_tree->entry_count < 0)
336+
if (sub->cache_tree->entry_count < 0) {
336337
to_invalidate = 1;
338+
expected_missing = 1;
339+
}
337340
}
338341
else {
339342
sha1 = ce->sha1;
@@ -343,6 +346,8 @@ static int update_one(struct cache_tree *it,
343346
}
344347
if (mode != S_IFGITLINK && !missing_ok && !has_sha1_file(sha1)) {
345348
strbuf_release(&buffer);
349+
if (expected_missing)
350+
return -1;
346351
return error("invalid object %06o %s for '%.*s'",
347352
mode, sha1_to_hex(sha1), entlen+baselen, path);
348353
}

t/t0090-cache-tree.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,4 +210,12 @@ test_expect_success 'partial commit gives cache-tree' '
210210
test_cache_tree
211211
'
212212

213+
test_expect_success 'no phantom error when switching trees' '
214+
mkdir newdir &&
215+
>newdir/one &&
216+
git add newdir/one &&
217+
git checkout 2>errors &&
218+
! test -s errors
219+
'
220+
213221
test_done

0 commit comments

Comments
 (0)