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 <dturner@twitter.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

David Turner committed Jul 5, 2014 at 21:06 UTC aecf567cbfb6ab46e82f7f5df36fb6a2dd5bee69
4 files changed +36 -4
builtin/checkout.c
+8
@@ -553,6 +553,14 @@ static int merge_working_tree(const struct checkout_opts *opts,
553 }
554 }
555
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 +
564 if (write_cache(newfd, active_cache, active_nr) ||
565 commit_locked_index(lock_file))
566 die(_("unable to write new index file"));
cache-tree.c
+11 -1
@@ -239,9 +239,12 @@ static int update_one(struct cache_tree *it,
239 struct strbuf buffer;
240 int missing_ok = flags & WRITE_TREE_MISSING_OK;
241 int dryrun = flags & WRITE_TREE_DRY_RUN;
242 + int repair = flags & WRITE_TREE_REPAIR;
243 int to_invalidate = 0;
244 int i;
245
246 + assert(!(dryrun && repair));
247 +
248 *skip_count = 0;
249
250 if (0 <= it->entry_count && has_sha1_file(it->sha1))
@@ -374,7 +377,14 @@ static int update_one(struct cache_tree *it,
377 #endif
378 }
379
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)
388 hash_sha1_file(buffer.buf, buffer.len, tree_type, it->sha1);
389 else if (write_sha1_file(buffer.buf, buffer.len, tree_type, it->sha1)) {
390 strbuf_release(&buffer);
cache-tree.h
+1
@@ -39,6 +39,7 @@ int update_main_cache_tree(int);
39 #define WRITE_TREE_IGNORE_CACHE_TREE 2
40 #define WRITE_TREE_DRY_RUN 4
41 #define WRITE_TREE_SILENT 8
42 +#define WRITE_TREE_REPAIR 16
43
44 /* error return codes */
45 #define WRITE_TREE_UNREADABLE_INDEX (-1)
t/t0090-cache-tree.sh
+16 -3
@@ -44,14 +44,14 @@ test_expect_success 'read-tree HEAD establishes cache-tree' '
44
45 test_expect_success 'git-add invalidates cache-tree' '
46 test_when_finished "git reset --hard; git read-tree HEAD" &&
47 - echo "I changed this file" > foo &&
47 + echo "I changed this file" >foo &&
48 git add foo &&
49 test_invalid_cache_tree
50 '
51
52 test_expect_success 'update-index invalidates cache-tree' '
53 test_when_finished "git reset --hard; git read-tree HEAD" &&
54 - echo "I changed this file" > foo &&
54 + echo "I changed this file" >foo &&
55 git update-index --add foo &&
56 test_invalid_cache_tree
57 '
@@ -85,9 +85,22 @@ test_expect_success 'reset --hard without index gives cache-tree' '
85 test_shallow_cache_tree
86 '
87
88 -test_expect_failure 'checkout gives cache-tree' '
88 +test_expect_success 'checkout gives cache-tree' '
89 + git tag current &&
90 git checkout HEAD^ &&
91 test_shallow_cache_tree
92 '
93
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 +
106 test_done