Skip to content

Commit d70a246

Browse files
committed
Merge branch 'jc/cache-tree-wip' into pu
* jc/cache-tree-wip: cache-tree: freshen the tree object at the top level cache-tree: retire cache_tree_fully_valid() API function commit: remove redundant check for active_cache_changed freshen_object(): factor out a helper function
2 parents 2f661de + e38e680 commit d70a246

File tree

9 files changed

+34
-48
lines changed

9 files changed

+34
-48
lines changed

builtin/checkout.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -581,8 +581,7 @@ static int merge_working_tree(const struct checkout_opts *opts,
581581
if (!active_cache_tree)
582582
active_cache_tree = cache_tree();
583583

584-
if (!cache_tree_fully_valid(active_cache_tree))
585-
cache_tree_update(&the_index, WRITE_TREE_SILENT | WRITE_TREE_REPAIR);
584+
cache_tree_update(&the_index, WRITE_TREE_SILENT | WRITE_TREE_REPAIR);
586585

587586
if (write_locked_index(&the_index, lock_file, COMMIT_LOCK))
588587
die(_("unable to write new index file"));

builtin/commit.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -418,9 +418,7 @@ static const char *prepare_index(int argc, const char **argv, const char *prefix
418418
if (!only && !pathspec.nr) {
419419
hold_locked_index(&index_lock, 1);
420420
refresh_cache_or_die(refresh_flags);
421-
if (active_cache_changed
422-
|| !cache_tree_fully_valid(active_cache_tree))
423-
update_main_cache_tree(WRITE_TREE_SILENT);
421+
update_main_cache_tree(WRITE_TREE_SILENT);
424422
if (active_cache_changed) {
425423
if (write_locked_index(&the_index, &index_lock,
426424
COMMIT_LOCK))

cache-tree.c

Lines changed: 18 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -220,20 +220,6 @@ static void discard_unused_subtrees(struct cache_tree *it)
220220
}
221221
}
222222

223-
int cache_tree_fully_valid(struct cache_tree *it)
224-
{
225-
int i;
226-
if (!it)
227-
return 0;
228-
if (it->entry_count < 0 || !has_sha1_file(it->sha1))
229-
return 0;
230-
for (i = 0; i < it->subtree_nr; i++) {
231-
if (!cache_tree_fully_valid(it->down[i]->cache_tree))
232-
return 0;
233-
}
234-
return 1;
235-
}
236-
237223
static int update_one(struct cache_tree *it,
238224
struct cache_entry **cache,
239225
int entries,
@@ -246,14 +232,17 @@ static int update_one(struct cache_tree *it,
246232
int missing_ok = flags & WRITE_TREE_MISSING_OK;
247233
int dryrun = flags & WRITE_TREE_DRY_RUN;
248234
int repair = flags & WRITE_TREE_REPAIR;
235+
int toplevel = flags & WRITE_TREE_TOPLEVEL;
249236
int to_invalidate = 0;
250237
int i;
251238

252239
assert(!(dryrun && repair));
253240

241+
flags &= ~WRITE_TREE_TOPLEVEL;
254242
*skip_count = 0;
255243

256-
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))
257246
return it->entry_count;
258247

259248
/*
@@ -431,7 +420,8 @@ int cache_tree_update(struct index_state *istate, int flags)
431420

432421
if (i)
433422
return i;
434-
i = update_one(it, cache, entries, "", 0, &skip, flags);
423+
i = update_one(it, cache, entries, "", 0, &skip,
424+
flags | WRITE_TREE_TOPLEVEL);
435425
if (i < 0)
436426
return i;
437427
istate->cache_changed |= CACHE_TREE_CHANGED;
@@ -600,7 +590,7 @@ static struct cache_tree *cache_tree_find(struct cache_tree *it, const char *pat
600590

601591
int write_index_as_tree(unsigned char *sha1, struct index_state *index_state, const char *index_path, int flags, const char *prefix)
602592
{
603-
int entries, was_valid, newfd;
593+
int entries, newfd;
604594
struct lock_file *lock_file;
605595

606596
/*
@@ -620,21 +610,18 @@ int write_index_as_tree(unsigned char *sha1, struct index_state *index_state, co
620610
if (!index_state->cache_tree)
621611
index_state->cache_tree = cache_tree();
622612

623-
was_valid = cache_tree_fully_valid(index_state->cache_tree);
624-
if (!was_valid) {
625-
if (cache_tree_update(index_state, flags) < 0)
626-
return WRITE_TREE_UNMERGED_INDEX;
627-
if (0 <= newfd) {
628-
if (!write_locked_index(index_state, lock_file, COMMIT_LOCK))
629-
newfd = -1;
630-
}
631-
/* Not being able to write is fine -- we are only interested
632-
* in updating the cache-tree part, and if the next caller
633-
* ends up using the old index with unupdated cache-tree part
634-
* it misses the work we did here, but that is just a
635-
* performance penalty and not a big deal.
636-
*/
613+
if (cache_tree_update(index_state, flags) < 0)
614+
return WRITE_TREE_UNMERGED_INDEX;
615+
if (0 <= newfd) {
616+
if (!write_locked_index(index_state, lock_file, COMMIT_LOCK))
617+
newfd = -1;
637618
}
619+
/* Not being able to write is fine -- we are only interested
620+
* in updating the cache-tree part, and if the next caller
621+
* ends up using the old index with unupdated cache-tree part
622+
* it misses the work we did here, but that is just a
623+
* performance penalty and not a big deal.
624+
*/
638625

639626
if (prefix) {
640627
struct cache_tree *subtree;

cache-tree.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ struct cache_tree_sub *cache_tree_sub(struct cache_tree *, const char *);
2929
void cache_tree_write(struct strbuf *, struct cache_tree *root);
3030
struct cache_tree *cache_tree_read(const char *buffer, unsigned long size);
3131

32-
int cache_tree_fully_valid(struct cache_tree *);
3332
int cache_tree_update(struct index_state *, int);
3433

3534
int update_main_cache_tree(int);
@@ -40,6 +39,7 @@ int update_main_cache_tree(int);
4039
#define WRITE_TREE_DRY_RUN 4
4140
#define WRITE_TREE_SILENT 8
4241
#define WRITE_TREE_REPAIR 16
42+
#define WRITE_TREE_TOPLEVEL 32
4343

4444
/* error return codes */
4545
#define WRITE_TREE_UNREADABLE_INDEX (-1)

cache.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1126,6 +1126,7 @@ extern int sha1_object_info(const unsigned char *, unsigned long *);
11261126
extern int hash_sha1_file(const void *buf, unsigned long len, const char *type, unsigned char *sha1);
11271127
extern int write_sha1_file(const void *buf, unsigned long len, const char *type, unsigned char *return_sha1);
11281128
extern int hash_sha1_file_literally(const void *buf, unsigned long len, const char *type, unsigned char *sha1, unsigned flags);
1129+
extern int freshen_object(const unsigned char *);
11291130
extern int pretend_sha1_file(void *, unsigned long, enum object_type, unsigned char *);
11301131
extern int force_object_loose(const unsigned char *sha1, time_t mtime);
11311132
extern int git_open_cloexec(const char *name, int flags);

merge-recursive.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -298,8 +298,7 @@ struct tree *write_tree_from_memory(struct merge_options *o)
298298
if (!active_cache_tree)
299299
active_cache_tree = cache_tree();
300300

301-
if (!cache_tree_fully_valid(active_cache_tree) &&
302-
cache_tree_update(&the_index, 0) < 0) {
301+
if (cache_tree_update(&the_index, 0) < 0) {
303302
err(o, _("error building trees"));
304303
return NULL;
305304
}

sequencer.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -386,9 +386,8 @@ static int is_index_unchanged(void)
386386
if (!active_cache_tree)
387387
active_cache_tree = cache_tree();
388388

389-
if (!cache_tree_fully_valid(active_cache_tree))
390-
if (cache_tree_update(&the_index, 0))
391-
return error(_("unable to update cache tree\n"));
389+
if (cache_tree_update(&the_index, 0))
390+
return error(_("unable to update cache tree\n"));
392391

393392
return !hashcmp(active_cache_tree->sha1, head_commit->tree->object.oid.hash);
394393
}

sha1_file.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3275,6 +3275,11 @@ static int freshen_packed_object(const unsigned char *sha1)
32753275
return 1;
32763276
}
32773277

3278+
int freshen_object(const unsigned char *sha1)
3279+
{
3280+
return freshen_packed_object(sha1) || freshen_loose_object(sha1);
3281+
}
3282+
32783283
int write_sha1_file(const void *buf, unsigned long len, const char *type, unsigned char *sha1)
32793284
{
32803285
char hdr[32];
@@ -3284,7 +3289,7 @@ int write_sha1_file(const void *buf, unsigned long len, const char *type, unsign
32843289
* it out into .git/objects/??/?{38} file.
32853290
*/
32863291
write_sha1_file_prepare(buf, len, type, sha1, hdr, &hdrlen);
3287-
if (freshen_packed_object(sha1) || freshen_loose_object(sha1))
3292+
if (freshen_object(sha1))
32883293
return 0;
32893294
return write_loose_object(sha1, hdr, hdrlen, buf, len, 0);
32903295
}
@@ -3302,7 +3307,7 @@ int hash_sha1_file_literally(const void *buf, unsigned long len, const char *typ
33023307

33033308
if (!(flags & HASH_WRITE_OBJECT))
33043309
goto cleanup;
3305-
if (freshen_packed_object(sha1) || freshen_loose_object(sha1))
3310+
if (freshen_object(sha1))
33063311
goto cleanup;
33073312
status = write_loose_object(sha1, header, hdrlen, buf, len, 0);
33083313

unpack-trees.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1243,10 +1243,8 @@ int unpack_trees(unsigned len, struct tree_desc *t, struct unpack_trees_options
12431243
if (!ret) {
12441244
if (!o->result.cache_tree)
12451245
o->result.cache_tree = cache_tree();
1246-
if (!cache_tree_fully_valid(o->result.cache_tree))
1247-
cache_tree_update(&o->result,
1248-
WRITE_TREE_SILENT |
1249-
WRITE_TREE_REPAIR);
1246+
cache_tree_update(&o->result,
1247+
WRITE_TREE_SILENT | WRITE_TREE_REPAIR);
12501248
}
12511249
discard_index(o->dst_index);
12521250
*o->dst_index = o->result;

0 commit comments

Comments
 (0)