Skip to content

Commit 6d5c16a

Browse files
committed
Merge branch 'tr/cache-tree' into maint-1.7.8
* tr/cache-tree: t0090: be prepared that 'wc -l' writes leading blanks reset: update cache-tree data when appropriate commit: write cache-tree data when writing index anyway Refactor cache_tree_update idiom from commit Test the current state of the cache-tree optimization Add test-scrap-cache-tree
2 parents 00fb2d2 + 4cd6755 commit 6d5c16a

File tree

10 files changed

+142
-11
lines changed

10 files changed

+142
-11
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@
171171
/test-date
172172
/test-delta
173173
/test-dump-cache-tree
174+
/test-scrap-cache-tree
174175
/test-genrandom
175176
/test-index-version
176177
/test-line-buffer

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,7 @@ TEST_PROGRAMS_NEED_X += test-ctype
435435
TEST_PROGRAMS_NEED_X += test-date
436436
TEST_PROGRAMS_NEED_X += test-delta
437437
TEST_PROGRAMS_NEED_X += test-dump-cache-tree
438+
TEST_PROGRAMS_NEED_X += test-scrap-cache-tree
438439
TEST_PROGRAMS_NEED_X += test-genrandom
439440
TEST_PROGRAMS_NEED_X += test-index-version
440441
TEST_PROGRAMS_NEED_X += test-line-buffer

builtin/commit.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,7 @@ static char *prepare_index(int argc, const char **argv, const char *prefix,
394394
fd = hold_locked_index(&index_lock, 1);
395395
add_files_to_cache(also ? prefix : NULL, pathspec, 0);
396396
refresh_cache_or_die(refresh_flags);
397+
update_main_cache_tree(1);
397398
if (write_cache(fd, active_cache, active_nr) ||
398399
close_lock_file(&index_lock))
399400
die(_("unable to write new_index file"));
@@ -414,6 +415,7 @@ static char *prepare_index(int argc, const char **argv, const char *prefix,
414415
fd = hold_locked_index(&index_lock, 1);
415416
refresh_cache_or_die(refresh_flags);
416417
if (active_cache_changed) {
418+
update_main_cache_tree(1);
417419
if (write_cache(fd, active_cache, active_nr) ||
418420
commit_locked_index(&index_lock))
419421
die(_("unable to write new_index file"));
@@ -862,10 +864,7 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
862864
*/
863865
discard_cache();
864866
read_cache_from(index_file);
865-
if (!active_cache_tree)
866-
active_cache_tree = cache_tree();
867-
if (cache_tree_update(active_cache_tree,
868-
active_cache, active_nr, 0, 0) < 0) {
867+
if (update_main_cache_tree(0)) {
869868
error(_("Error building trees"));
870869
return 0;
871870
}

builtin/reset.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ static int reset_index_file(const unsigned char *sha1, int reset_type, int quiet
4343
int nr = 1;
4444
int newfd;
4545
struct tree_desc desc[2];
46+
struct tree *tree;
4647
struct unpack_trees_options opts;
4748
struct lock_file *lock = xcalloc(1, sizeof(struct lock_file));
4849

@@ -84,6 +85,12 @@ static int reset_index_file(const unsigned char *sha1, int reset_type, int quiet
8485
return error(_("Failed to find tree of %s."), sha1_to_hex(sha1));
8586
if (unpack_trees(nr, desc, &opts))
8687
return -1;
88+
89+
if (reset_type == MIXED || reset_type == HARD) {
90+
tree = parse_tree_indirect(sha1);
91+
prime_cache_tree(&active_cache_tree, tree);
92+
}
93+
8794
if (write_cache(newfd, active_cache, active_nr) ||
8895
commit_locked_index(lock))
8996
return error(_("Could not write new index file."));

cache-tree.c

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ void cache_tree_invalidate_path(struct cache_tree *it, const char *path)
150150
}
151151

152152
static int verify_cache(struct cache_entry **cache,
153-
int entries)
153+
int entries, int silent)
154154
{
155155
int i, funny;
156156

@@ -159,6 +159,8 @@ static int verify_cache(struct cache_entry **cache,
159159
for (i = 0; i < entries; i++) {
160160
struct cache_entry *ce = cache[i];
161161
if (ce_stage(ce) || (ce->ce_flags & CE_INTENT_TO_ADD)) {
162+
if (silent)
163+
return -1;
162164
if (10 < ++funny) {
163165
fprintf(stderr, "...\n");
164166
break;
@@ -370,10 +372,11 @@ int cache_tree_update(struct cache_tree *it,
370372
struct cache_entry **cache,
371373
int entries,
372374
int missing_ok,
373-
int dryrun)
375+
int dryrun,
376+
int silent)
374377
{
375378
int i;
376-
i = verify_cache(cache, entries);
379+
i = verify_cache(cache, entries, silent);
377380
if (i)
378381
return i;
379382
i = update_one(it, cache, entries, "", 0, missing_ok, dryrun);
@@ -573,7 +576,7 @@ int write_cache_as_tree(unsigned char *sha1, int flags, const char *prefix)
573576

574577
if (cache_tree_update(active_cache_tree,
575578
active_cache, active_nr,
576-
missing_ok, 0) < 0)
579+
missing_ok, 0, 0) < 0)
577580
return WRITE_TREE_UNMERGED_INDEX;
578581
if (0 <= newfd) {
579582
if (!write_cache(newfd, active_cache, active_nr) &&
@@ -668,3 +671,11 @@ int cache_tree_matches_traversal(struct cache_tree *root,
668671
return it->entry_count;
669672
return 0;
670673
}
674+
675+
int update_main_cache_tree (int silent)
676+
{
677+
if (!the_index.cache_tree)
678+
the_index.cache_tree = cache_tree();
679+
return cache_tree_update(the_index.cache_tree,
680+
the_index.cache, the_index.cache_nr, 0, 0, silent);
681+
}

cache-tree.h

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

3131
int cache_tree_fully_valid(struct cache_tree *);
32-
int cache_tree_update(struct cache_tree *, struct cache_entry **, int, int, int);
32+
int cache_tree_update(struct cache_tree *, struct cache_entry **, int, int, int, int);
33+
34+
int update_main_cache_tree(int);
3335

3436
/* bitmasks to write_cache_as_tree flags */
3537
#define WRITE_TREE_MISSING_OK 1

merge-recursive.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ struct tree *write_tree_from_memory(struct merge_options *o)
265265

266266
if (!cache_tree_fully_valid(active_cache_tree) &&
267267
cache_tree_update(active_cache_tree,
268-
active_cache, active_nr, 0, 0) < 0)
268+
active_cache, active_nr, 0, 0, 0) < 0)
269269
die("error building trees");
270270

271271
result = lookup_tree(active_cache_tree->sha1);

t/t0090-cache-tree.sh

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
#!/bin/sh
2+
3+
test_description="Test whether cache-tree is properly updated
4+
5+
Tests whether various commands properly update and/or rewrite the
6+
cache-tree extension.
7+
"
8+
. ./test-lib.sh
9+
10+
cmp_cache_tree () {
11+
test-dump-cache-tree >actual &&
12+
sed "s/$_x40/SHA/" <actual >filtered &&
13+
test_cmp "$1" filtered
14+
}
15+
16+
# We don't bother with actually checking the SHA1:
17+
# test-dump-cache-tree already verifies that all existing data is
18+
# correct.
19+
test_shallow_cache_tree () {
20+
printf "SHA (%d entries, 0 subtrees)\n" $(git ls-files|wc -l) >expect &&
21+
cmp_cache_tree expect
22+
}
23+
24+
test_invalid_cache_tree () {
25+
echo "invalid (0 subtrees)" >expect &&
26+
printf "SHA #(ref) (%d entries, 0 subtrees)\n" $(git ls-files|wc -l) >>expect &&
27+
cmp_cache_tree expect
28+
}
29+
30+
test_no_cache_tree () {
31+
: >expect &&
32+
cmp_cache_tree expect
33+
}
34+
35+
test_expect_failure 'initial commit has cache-tree' '
36+
test_commit foo &&
37+
test_shallow_cache_tree
38+
'
39+
40+
test_expect_success 'read-tree HEAD establishes cache-tree' '
41+
git read-tree HEAD &&
42+
test_shallow_cache_tree
43+
'
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 &&
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 &&
55+
git update-index --add foo &&
56+
test_invalid_cache_tree
57+
'
58+
59+
test_expect_success 'write-tree establishes cache-tree' '
60+
test-scrap-cache-tree &&
61+
git write-tree &&
62+
test_shallow_cache_tree
63+
'
64+
65+
test_expect_success 'test-scrap-cache-tree works' '
66+
git read-tree HEAD &&
67+
test-scrap-cache-tree &&
68+
test_no_cache_tree
69+
'
70+
71+
test_expect_success 'second commit has cache-tree' '
72+
test_commit bar &&
73+
test_shallow_cache_tree
74+
'
75+
76+
test_expect_success 'reset --hard gives cache-tree' '
77+
test-scrap-cache-tree &&
78+
git reset --hard &&
79+
test_shallow_cache_tree
80+
'
81+
82+
test_expect_success 'reset --hard without index gives cache-tree' '
83+
rm -f .git/index &&
84+
git reset --hard &&
85+
test_shallow_cache_tree
86+
'
87+
88+
test_expect_failure 'checkout gives cache-tree' '
89+
git checkout HEAD^ &&
90+
test_shallow_cache_tree
91+
'
92+
93+
test_done

test-dump-cache-tree.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,6 @@ int main(int ac, char **av)
5959
struct cache_tree *another = cache_tree();
6060
if (read_cache() < 0)
6161
die("unable to read index file");
62-
cache_tree_update(another, active_cache, active_nr, 0, 1);
62+
cache_tree_update(another, active_cache, active_nr, 0, 1, 0);
6363
return dump_cache_tree(active_cache_tree, another, "");
6464
}

test-scrap-cache-tree.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#include "cache.h"
2+
#include "tree.h"
3+
#include "cache-tree.h"
4+
5+
static struct lock_file index_lock;
6+
7+
int main(int ac, char **av)
8+
{
9+
int fd = hold_locked_index(&index_lock, 1);
10+
if (read_cache() < 0)
11+
die("unable to read index file");
12+
active_cache_tree = NULL;
13+
if (write_cache(fd, active_cache, active_nr)
14+
|| commit_lock_file(&index_lock))
15+
die("unable to write index file");
16+
return 0;
17+
}

0 commit comments

Comments
 (0)