Skip to content

Commit aecf567

Browse files
dturner-twgitster
authored andcommitted
cache-tree: create/update cache-tree on checkout
When git checkout checks out a branch, create or update the cache-tree so that subsequent operations are faster. update_main_cache_tree learned a new flag, WRITE_TREE_REPAIR. When WRITE_TREE_REPAIR is set, portions of the cache-tree which do not correspond to existing tree objects are invalidated (and portions which do are marked as valid). No new tree objects are created. Signed-off-by: David Turner <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent c2f7b10 commit aecf567

File tree

4 files changed

+36
-4
lines changed

4 files changed

+36
-4
lines changed

builtin/checkout.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -553,6 +553,14 @@ static int merge_working_tree(const struct checkout_opts *opts,
553553
}
554554
}
555555

556+
if (!active_cache_tree)
557+
active_cache_tree = cache_tree();
558+
559+
if (!cache_tree_fully_valid(active_cache_tree))
560+
cache_tree_update(active_cache_tree,
561+
(const struct cache_entry * const *)active_cache,
562+
active_nr, WRITE_TREE_SILENT | WRITE_TREE_REPAIR);
563+
556564
if (write_cache(newfd, active_cache, active_nr) ||
557565
commit_locked_index(lock_file))
558566
die(_("unable to write new index file"));

cache-tree.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,9 +239,12 @@ static int update_one(struct cache_tree *it,
239239
struct strbuf buffer;
240240
int missing_ok = flags & WRITE_TREE_MISSING_OK;
241241
int dryrun = flags & WRITE_TREE_DRY_RUN;
242+
int repair = flags & WRITE_TREE_REPAIR;
242243
int to_invalidate = 0;
243244
int i;
244245

246+
assert(!(dryrun && repair));
247+
245248
*skip_count = 0;
246249

247250
if (0 <= it->entry_count && has_sha1_file(it->sha1))
@@ -374,7 +377,14 @@ static int update_one(struct cache_tree *it,
374377
#endif
375378
}
376379

377-
if (dryrun)
380+
if (repair) {
381+
unsigned char sha1[20];
382+
hash_sha1_file(buffer.buf, buffer.len, tree_type, sha1);
383+
if (has_sha1_file(sha1))
384+
hashcpy(it->sha1, sha1);
385+
else
386+
to_invalidate = 1;
387+
} else if (dryrun)
378388
hash_sha1_file(buffer.buf, buffer.len, tree_type, it->sha1);
379389
else if (write_sha1_file(buffer.buf, buffer.len, tree_type, it->sha1)) {
380390
strbuf_release(&buffer);

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_IGNORE_CACHE_TREE 2
4040
#define WRITE_TREE_DRY_RUN 4
4141
#define WRITE_TREE_SILENT 8
42+
#define WRITE_TREE_REPAIR 16
4243

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

t/t0090-cache-tree.sh

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,14 @@ test_expect_success 'read-tree HEAD establishes cache-tree' '
4444

4545
test_expect_success 'git-add invalidates cache-tree' '
4646
test_when_finished "git reset --hard; git read-tree HEAD" &&
47-
echo "I changed this file" > foo &&
47+
echo "I changed this file" >foo &&
4848
git add foo &&
4949
test_invalid_cache_tree
5050
'
5151

5252
test_expect_success 'update-index invalidates cache-tree' '
5353
test_when_finished "git reset --hard; git read-tree HEAD" &&
54-
echo "I changed this file" > foo &&
54+
echo "I changed this file" >foo &&
5555
git update-index --add foo &&
5656
test_invalid_cache_tree
5757
'
@@ -85,9 +85,22 @@ test_expect_success 'reset --hard without index gives cache-tree' '
8585
test_shallow_cache_tree
8686
'
8787

88-
test_expect_failure 'checkout gives cache-tree' '
88+
test_expect_success 'checkout gives cache-tree' '
89+
git tag current &&
8990
git checkout HEAD^ &&
9091
test_shallow_cache_tree
9192
'
9293

94+
test_expect_success 'checkout -b gives cache-tree' '
95+
git checkout current &&
96+
git checkout -b prev HEAD^ &&
97+
test_shallow_cache_tree
98+
'
99+
100+
test_expect_success 'checkout -B gives cache-tree' '
101+
git checkout current &&
102+
git checkout -B prev HEAD^ &&
103+
test_shallow_cache_tree
104+
'
105+
93106
test_done

0 commit comments

Comments
 (0)