Skip to content

Commit e38e680

Browse files
committed
cache-tree: freshen the tree object at the top level
"git write-tree" that is asked to write out a tree object out of the index may not actually write the tree object anew, when there already is the same tree object in the object store. The actual writing of the tree object is done as a side effect of updating the cache tree entries fully in the index, and the optimization to skip writing the tree out is done via "has_sha1_file()", i.e. "does the tree already exist in the object store?" After asking "git write-tree" to write a tree out, the caller may not make it reachable from any ref or other anchoring point, which makes the tree object subject to pruning. Call freshen_object() on the top-level tree object to make sure we mark the tree "young" to protect it. Signed-off-by: Junio C Hamano <[email protected]>
1 parent e382638 commit e38e680

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

cache-tree.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,14 +232,17 @@ static int update_one(struct cache_tree *it,
232232
int missing_ok = flags & WRITE_TREE_MISSING_OK;
233233
int dryrun = flags & WRITE_TREE_DRY_RUN;
234234
int repair = flags & WRITE_TREE_REPAIR;
235+
int toplevel = flags & WRITE_TREE_TOPLEVEL;
235236
int to_invalidate = 0;
236237
int i;
237238

238239
assert(!(dryrun && repair));
239240

241+
flags &= ~WRITE_TREE_TOPLEVEL;
240242
*skip_count = 0;
241243

242-
if (0 <= it->entry_count && has_sha1_file(it->sha1))
244+
if (0 <= it->entry_count &&
245+
(toplevel ? freshen_object : has_sha1_file)(it->sha1))
243246
return it->entry_count;
244247

245248
/*
@@ -409,7 +412,8 @@ int cache_tree_update(struct index_state *istate, int flags)
409412

410413
if (i)
411414
return i;
412-
i = update_one(it, cache, entries, "", 0, &skip, flags);
415+
i = update_one(it, cache, entries, "", 0, &skip,
416+
flags | WRITE_TREE_TOPLEVEL);
413417
if (i < 0)
414418
return i;
415419
istate->cache_changed |= CACHE_TREE_CHANGED;

cache-tree.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ int update_main_cache_tree(int);
3939
#define WRITE_TREE_DRY_RUN 4
4040
#define WRITE_TREE_SILENT 8
4141
#define WRITE_TREE_REPAIR 16
42+
#define WRITE_TREE_TOPLEVEL 32
4243

4344
/* error return codes */
4445
#define WRITE_TREE_UNREADABLE_INDEX (-1)

0 commit comments

Comments
 (0)