cache-tree: Create/update cache-tree on checkout
PATCH v6 1/4 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.
--- builtin/checkout.c | 8 ++++++++ cache-tree.c | 12 +++++++++++- cache-tree.h | 1 + t/t0090-cache-tree.sh | 19 ++++++++++++++++--- 4 files changed, 36 insertions(+), 4 deletions(-)
builtin/checkout.c +8 -0
@@ -553,6 +553,14 @@ static int merge_working_tree(const struct checkout_opts *opts, } } +if (!active_cache_tree) + active_cache_tree =cache_tree (); + +if (!cache_tree_fully_valid (active_cache_tree)) +cache_tree_update (active_cache_tree, + (const struct cache_entry *const *)active_cache, + active_nr,WRITE_TREE_SILENT |WRITE_TREE_REPAIR ); +if (write_cache (newfd, active_cache, active_nr) ||commit_locked_index (lock_file))die (_ ("unable to write new index file" ));
cache-tree.c +11 -1
@@ -239,9 +239,12 @@ static int update_one(struct cache_tree *it,struct strbuf buffer;int missing_ok = flags &WRITE_TREE_MISSING_OK ;int dryrun = flags &WRITE_TREE_DRY_RUN ; +int repair = flags &WRITE_TREE_REPAIR ;int to_invalidate =0 ;int i; +assert (!(dryrun && repair)); + *skip_count =0 ;if (0 <= it->entry_count &&has_sha1_file (it->sha1))
@@ -374,7 +377,14 @@ static int update_one(struct cache_tree *it,#endif } -if (dryrun) +if (repair) { +unsigned char sha1[20 ]; +hash_sha1_file (buffer.buf, buffer.len, tree_type, sha1); +if (has_sha1_file (sha1)) +hashcpy (it->sha1, sha1); +else + to_invalidate =1 ; + }else if (dryrun)hash_sha1_file (buffer.buf, buffer.len, tree_type, it->sha1);else if (write_sha1_file (buffer.buf, buffer.len, tree_type, it->sha1)) {strbuf_release (&buffer);
cache-tree.h +1 -0
@@ -39,6 +39,7 @@ int update_main_cache_tree(int);#define WRITE_TREE_IGNORE_CACHE_TREE 2#define WRITE_TREE_DRY_RUN 4#define WRITE_TREE_SILENT 8 +#define WRITE_TREE_REPAIR 16/* error return codes */ #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' ' test_expect_success 'git-add invalidates cache-tree' ' test_when_finished "git reset --hard; git read-tree HEAD" && - echo "I changed this file" > foo && + echo "I changed this file" >foo && git add foo && test_invalid_cache_tree ' test_expect_success 'update-index invalidates cache-tree' ' test_when_finished "git reset --hard; git read-tree HEAD" && - echo "I changed this file" > foo && + echo "I changed this file" >foo && git update-index --add foo && test_invalid_cache_tree '
@@ -85,9 +85,22 @@ test_expect_success 'reset --hard without index gives cache-tree' ' test_shallow_cache_tree ' -test_expect_failure 'checkout gives cache-tree' ' +test_expect_success 'checkout gives cache-tree' ' + git tag current && git checkout HEAD^ && test_shallow_cache_tree ' +test_expect_success 'checkout -b gives cache-tree' ' + git checkout current && + git checkout -b prev HEAD^ && + test_shallow_cache_tree +' + +test_expect_success 'checkout -B gives cache-tree' ' + git checkout current && + git checkout -B prev HEAD^ && + test_shallow_cache_tree +' + test_done
Signature · 1 line
2.0.0.390.gcb682f8
PATCH v6 2/4 test-dump-cache-tree: invalid trees are not errors
Do not treat known-invalid trees as errors even when their subtree_nr is incorrect. Because git already knows that these trees are invalid, an incorrect subtree_nr will not cause problems.
Add a couple of comments.
--- test-dump-cache-tree.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
test-dump-cache-tree.c +2 -2
@@ -26,16 +26,16 @@ static int dump_cache_tree(struct cache_tree *it,return 0 ;if (it->entry_count <0 ) { +/* invalid */ dump_one (it, pfx,"" );dump_one (ref, pfx,"#(ref) " ); -if (it->subtree_nr != ref->subtree_nr) - errs =1 ; }else {dump_one (it, pfx,"" );if (hashcmp (it->sha1, ref->sha1) || ref->entry_count != it->entry_count || ref->subtree_nr != it->subtree_nr) { +/* claims to be valid but is lying */ dump_one (ref, pfx,"#(ref) " ); errs =1 ; }
Signature · 1 line
2.0.0.390.gcb682f8
PATCH v6 3/4 cache-tree: subdirectory tests
In reply to David TurnerAdd tests to confirm that invalidation of subdirectories neither over- nor under-invalidates.
--- t/t0090-cache-tree.sh | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-)
t/t0090-cache-tree.sh +23 -3
@@ -22,9 +22,10 @@ test_shallow_cache_tree () { } test_invalid_cache_tree () { - echo "invalid (0 subtrees)" >expect && - printf "SHA #(ref) (%d entries, 0 subtrees)\n" $(git ls-files|wc -l) >>expect && - cmp_cache_tree expect + printf "invalid %s ()\n" "" "$@" >expect && + test-dump-cache-tree | \ + sed -n -e "s/[0-9]* subtrees//" -e '/#(ref)/d' -e '/^invalid /p' >actual && + test_cmp expect actual } test_no_cache_tree () {
@@ -49,6 +50,25 @@ test_expect_success 'git-add invalidates cache-tree' ' test_invalid_cache_tree ' +test_expect_success 'git-add in subdir invalidates cache-tree' ' + test_when_finished "git reset --hard; git read-tree HEAD" && + mkdir dirx && + echo "I changed this file" >dirx/foo && + git add dirx/foo && + test_invalid_cache_tree +' + +test_expect_success 'git-add in subdir does not invalidate sibling cache-tree' ' + git tag no-children && + test_when_finished "git reset --hard no-children; git read-tree HEAD" && + mkdir dir1 dir2 && + test_commit dir1/a && + test_commit dir2/b && + echo "I changed this file" >dir1/a && + git add dir1/a && + test_invalid_cache_tree dir1/ +' + test_expect_success 'update-index invalidates cache-tree' ' test_when_finished "git reset --hard; git read-tree HEAD" && echo "I changed this file" >foo &&
Signature · 1 line
2.0.0.390.gcb682f8
Re: [PATCH 3/4 v6] cache-tree: subdirectory tests
Quoted 13 lines · David Turner
Add tests to confirm that invalidation of subdirectories neither over- nor under-invalidates.
Signed-off-by David Turner <[email protected]>--- t/t0090-cache-tree.sh | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-)t/t0090-cache-tree.sh +0
-0
t/t0090-cache-tree.sh test_shallow_cache_tree () { · line 26
Quoted 8 lines
t/t0090-cache-tree.sh +2
-3@@ -22,9 +22,10 @@ test_shallow_cache_tree () { } test_invalid_cache_tree () { - echo "invalid (0 subtrees)" >expect && - printf "SHA #(ref) (%d entries, 0 subtrees)\n" $(git ls-files|wc -l) >>expect && - cmp_cache_tree expect + printf "invalid %s ()\n" "" "$@" >expect && + test-dump-cache-tree | \
nit: unnecessary backslash
Quoted 33 lines
t/t0090-cache-tree.sh +21
-1+ sed -n -e "s/[0-9]* subtrees//" -e '/#(ref)/d' -e '/^invalid /p' >actual && + test_cmp expect actual } test_no_cache_tree () {@@ -49,6 +50,25 @@ test_expect_success 'git-add invalidates cache-tree' ' test_invalid_cache_tree ' +test_expect_success 'git-add in subdir invalidates cache-tree' ' + test_when_finished "git reset --hard; git read-tree HEAD" && + mkdir dirx && + echo "I changed this file" >dirx/foo && + git add dirx/foo && + test_invalid_cache_tree +' + +test_expect_success 'git-add in subdir does not invalidate sibling cache-tree' ' + git tag no-children && + test_when_finished "git reset --hard no-children; git read-tree HEAD" && + mkdir dir1 dir2 && + test_commit dir1/a && + test_commit dir2/b && + echo "I changed this file" >dir1/a && + git add dir1/a && + test_invalid_cache_tree dir1/ +' + test_expect_success 'update-index invalidates cache-tree' ' test_when_finished "git reset --hard; git read-tree HEAD" && echo "I changed this file" >foo && --2.0.0.390.gcb682f8
Re: [PATCH 3/4 v6] cache-tree: subdirectory tests
Quoted 16 lines · Eric Sunshine
Quoted 15 lines · David Turner
Add tests to confirm that invalidation of subdirectories neither over- nor under-invalidates.
Signed-off-by David Turner <[email protected]>--- t/t0090-cache-tree.sh | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-)t/t0090-cache-tree.sh +0
-0@@ -22,9 +22,10 @@ test_shallow_cache_tree () { }
Quoted 5 lines
t/t0090-cache-tree.sh test_shallow_cache_tree () { · line 25
Quoted 5 lines
t/t0090-cache-tree.sh +1
-3test_invalid_cache_tree () { - echo "invalid (0 subtrees)" >expect && - printf "SHA #(ref) (%d entries, 0 subtrees)\n" $(git ls-files|wc -l) >>expect && - cmp_cache_tree expect + printf "invalid %s ()\n" "" "$@" >expect &&
Hmm. This will always expect that the top-level is invalid, even when $# is 0. It is OK if you never need to use this to test that a cache-tree is fully valid, but is it something we want to check?
Existing tests are mostly about "cache-tree is populated fully at a few strategic, well known and easy places and then it degrades over time", but after all your series is adding more places to that set of "a few places", so we may want to make sure that future breakages to the new code paths that "repair" the cache-tree are caught by these tests.
Quoted 3 lines
t/t0090-cache-tree.sh test_shallow_cache_tree () { · line 26
Quoted 1 line
t/t0090-cache-tree.sh +1
-0+ test-dump-cache-tree | \nit: unnecessary backslash
Good eyes ;-)
Quoted 1 line
t/t0090-cache-tree.sh test_shallow_cache_tree () { · line 27
Quoted 1 line
t/t0090-cache-tree.sh +1
-0+ sed -n -e "s/[0-9]* subtrees//" -e '/#(ref)/d' -e '/^invalid /p' >actual &&
Is the second one to remove "#(ref)", which appears for a good "reference" cache tree entry shown for comparison, necessary? Do they ever begin with "invalid"? If they ever begin with "invalid" that itself may even be a noteworthy breakage to catch, no?
Quoted 32 lines
Quoted 32 lines
t/t0090-cache-tree.sh +20
-1+ test_cmp expect actual } test_no_cache_tree () {@@ -49,6 +50,25 @@ test_expect_success 'git-add invalidates cache-tree' ' test_invalid_cache_tree ' +test_expect_success 'git-add in subdir invalidates cache-tree' ' + test_when_finished "git reset --hard; git read-tree HEAD" && + mkdir dirx && + echo "I changed this file" >dirx/foo && + git add dirx/foo && + test_invalid_cache_tree +' + +test_expect_success 'git-add in subdir does not invalidate sibling cache-tree' ' + git tag no-children && + test_when_finished "git reset --hard no-children; git read-tree HEAD" && + mkdir dir1 dir2 && + test_commit dir1/a && + test_commit dir2/b && + echo "I changed this file" >dir1/a && + git add dir1/a && + test_invalid_cache_tree dir1/ +' + test_expect_success 'update-index invalidates cache-tree' ' test_when_finished "git reset --hard; git read-tree HEAD" && echo "I changed this file" >foo && --2.0.0.390.gcb682f8
Re: [PATCH 3/4 v6] cache-tree: subdirectory tests
Quoted 6 lines · Junio C Hamano
Quoted 1 line
Quoted 1 line
+ sed -n -e "s/[0-9]* subtrees//" -e '/#(ref)/d' -e '/^invalid /p' >actual &&
Is the second one to remove "#(ref)", which appears for a good "reference" cache tree entry shown for comparison, necessary? Do they ever begin with "invalid"? If they ever begin with "invalid" that itself may even be a noteworthy breakage to catch, no?
Answering to myself...
Because test-dump-cache-tree uses DRY_RUN to create only an in-core copy of tree object, and we notice that the reference cache-tree created in the tests contains the object name of a tree that does not yet exist in the object database. We get "invalid #(ref)" for such node.
In the ideal world, I think whoever tries to compare two cache-trees (i.e. test-dump-cache-tree) should *not* care, because we are merely trying to show what the correct tree object name for the node would be, but this is only for testing, so the best way forward would be to:
- Stop using DRY_RUN in test-dump-cache-tree.c;
- Stop the code to support DRY_RUN from cache-tree.c (nobody but the test uses it); and
- Drop the "-e '#(ref)/d'" from the above.
I would think.
Re: [PATCH 3/4 v6] cache-tree: subdirectory tests
Quoted 23 lines · Junio C Hamano
Quoted 6 lines · Junio C Hamano
Quoted 1 line
Quoted 1 line
+ sed -n -e "s/[0-9]* subtrees//" -e '/#(ref)/d' -e '/^invalid /p' >actual &&
Is the second one to remove "#(ref)", which appears for a good "reference" cache tree entry shown for comparison, necessary? Do they ever begin with "invalid"? If they ever begin with "invalid" that itself may even be a noteworthy breakage to catch, no?
Answering to myself...
Because test-dump-cache-tree uses
DRY_RUNto create only an in-core copy of tree object, and we notice that the reference cache-tree created in the tests contains the object name of a tree that does not yet exist in the object database. We get "invalid #(ref)" for such node.In the ideal world, I think whoever tries to compare two cache-trees (i.e. test-dump-cache-tree) should *not* care, because we are merely trying to show what the correct tree object name for the node would be, but this is only for testing, so the best way forward would be to:
Quoted 8 lines
- Stop using
DRY_RUNintest-dump-cache-tree.c;- Stop the code to support
DRY_RUNfromcache-tree.c(nobody but the test uses it); and- Drop the "-e '#(ref)/d'" from the above.
I would think.
Do you mean that I should do this in this patch set, or that it's a good idea for the future?
Also, if we don't use DRY_RUN, won't test-dump-cache-tree add trees to the actual ODB, which would be odd for a test program?
Re: [PATCH 3/4 v6] cache-tree: subdirectory tests
Quoted 13 lines · David Turner
Quoted 11 lines · Junio C Hamano
In the ideal world, I think whoever tries to compare two cache-trees (i.e. test-dump-cache-tree) should *not* care, because we are merely trying to show what the correct tree object name for the node would be, but this is only for testing, so the best way forward would be to:
- Stop using
DRY_RUNintest-dump-cache-tree.c;- Stop the code to support
DRY_RUNfromcache-tree.c(nobody but the test uses it); and
Quoted 6 lines
Quoted 3 lines
- Drop the "-e '#(ref)/d'" from the above.
I would think.
Do you mean that I should do this in this patch set, or that it's a good idea for the future?
I have no strong preference either way. Removing DRY_RUN may simplify things in the code that gets used in the real life (as opposed to the code that is only used during the tests), so I do not mind it if it was done before the series as a preparation step.
Quoted 2 lines
Also, if we don't use
DRY_RUN, won't test-dump-cache-tree add trees to the actual ODB, which would be odd for a test program?
I do not see it as odd at all; after all, nobody in the real-life uses dry-run and as you can see its use is broken, or at least is inconsistent with the rest of the system.
Re: [PATCH 3/4 v6] cache-tree: subdirectory tests
In reply to Junio C HamanoQuoted 18 lines · Junio C Hamano
Quoted 16 lines · Eric Sunshine
Quoted 15 lines · David Turner
Add tests to confirm that invalidation of subdirectories neither over- nor under-invalidates.
Signed-off-by David Turner <[email protected]>--- t/t0090-cache-tree.sh | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-)t/t0090-cache-tree.sh +0
-0@@ -22,9 +22,10 @@ test_shallow_cache_tree () { }
Quoted 9 lines
Quoted 5 lines
t/t0090-cache-tree.sh test_shallow_cache_tree () { · line 25
Quoted 5 lines
t/t0090-cache-tree.sh +1
-3test_invalid_cache_tree () { - echo "invalid (0 subtrees)" >expect && - printf "SHA #(ref) (%d entries, 0 subtrees)\n" $(git ls-files|wc -l) >>expect && - cmp_cache_tree expect + printf "invalid %s ()\n" "" "$@" >expect &&Hmm. This will always expect that the top-level is invalid, even when $# is 0. It is OK if you never need to use this to test that a cache-tree is fully valid, but is it something we want to check?
We have test_cache_tree to check that it's fully valid.
Quoted 6 lines
Existing tests are mostly about "cache-tree is populated fully at a few strategic, well known and easy places and then it degrades over time", but after all your series is adding more places to that set of "a few places", so we may want to make sure that future breakages to the new code paths that "repair" the cache-tree are caught by these tests.
This patchset un-failed "initial commit has cache-tree", and added "commit in child dir has cache-tree" and "partial commit gives cache-tree". I've just added a test for interactive commit; when you take a look at the next patchset, you can let me know if this seems sufficient to you.
PATCH v6 4/4 cache-tree: Write updated cache-tree after commit
In reply to David TurnerDuring the commit process, update the cache-tree. Write this updated cache-tree so that it's ready for subsequent commands.
Add test code which demonstrates that git commit now writes the cache tree. Make all tests test the entire cache-tree, not just the root level.
--- builtin/commit.c | 9 +++++- t/t0090-cache-tree.sh | 87 ++++++++++++++++++++++++++++++++++++++++++--------- 2 files changed, 81 insertions(+), 15 deletions(-)
builtin/commit.c +8 -1
@@ -342,6 +342,8 @@ static char *prepare_index(int argc, const char **argv, const char *prefix,discard_cache ();read_cache_from (index_lock.filename); +if (update_main_cache_tree (WRITE_TREE_SILENT ) >=0 ) +write_cache (fd, active_cache, active_nr); commit_style =COMMIT_NORMAL ;return index_lock.filename;
@@ -383,8 +385,12 @@ static char *prepare_index(int argc, const char **argv, const char *prefix,if (!only && !pathspec.nr) { fd =hold_locked_index (&index_lock,1 );refresh_cache_or_die (refresh_flags); -if (active_cache_changed) { +if (active_cache_changed + || !cache_tree_fully_valid (active_cache_tree)) {update_main_cache_tree (WRITE_TREE_SILENT ); + active_cache_changed =1 ; + } +if (active_cache_changed) {if (write_cache (fd, active_cache, active_nr) ||commit_locked_index (&index_lock))die (_ ("unable to write new_index file" ));
@@ -435,6 +441,7 @@ static char *prepare_index(int argc, const char **argv, const char *prefix, fd =hold_locked_index (&index_lock,1 );add_remove_files (&partial);refresh_cache (REFRESH_QUIET ); +update_main_cache_tree (WRITE_TREE_SILENT );if (write_cache (fd, active_cache, active_nr) ||close_lock_file (&index_lock))die (_ ("unable to write new_index file" ));
t/t0090-cache-tree.sh +73 -14
160 lines not on this page · raw message
Signature · 1 line
2.0.0.390.gcb682f8
Re: [PATCH 4/4 v6] cache-tree: Write updated cache-tree after commit
t/t0090-cache-tree.sh cmp_cache_tree () { · line 18
Quoted 4 lines · David Turner
t/t0090-cache-tree.sh +0
-0@@ -16,8 +16,34 @@ cmp_cache_tree () { # We don't bother with actually checking the SHA1: # test-dump-cache-tree already verifies that all existing data is # correct.
Is this statement now stale and needs to be removed?
Quoted 17 lines
t/t0090-cache-tree.sh +15
-2-test_shallow_cache_tree () { - printf "SHA (%d entries, 0 subtrees)\n" $(git ls-files|wc -l) >expect && +generate_expected_cache_tree_rec () { + dir="$1${1:+/}" && + parent="$2" && + # ls-files might have foo/bar, foo/bar/baz, and foo/bar/quux + # We want to count only foo because it's the only direct child + subtrees=$(git ls-files|grep /|cut -d / -f 1|uniq) && + subtree_count=$(echo "$subtrees"|awk '$1 {++c} END {print c}') && + entries=$(git ls-files|wc -l) && + printf "SHA $dir (%d entries, %d subtrees)\n" "$entries" "$subtree_count" && + for subtree in $subtrees + do + cd "$subtree" + generate_expected_cache_tree_rec "$dir$subtree" "$dir" || return 1 + cd .. + done &&
t/t0090-cache-tree.sh cmp_cache_tree () { · line 43
Quoted 10 lines
t/t0090-cache-tree.sh +10
-0+ dir=$parent +} + +generate_expected_cache_tree () { + cwd=$(pwd) + generate_expected_cache_tree_rec + ret="$?" + cd "$cwd" + return $ret +}
As we always have had trouble between $PWD and $(pwd) on other platforms, I'd prefer something simpler, like:
expected_cache_tree () {
(
# subshell to let it wander around freely
generate_expected_cache_tree_rec
)
}Other than these two, the new tests were good from a cursory look. The change to builtin/commit.c looked good, too.
Thanks.
Re: [PATCH 4/4 v6] cache-tree: Write updated cache-tree after commit
Quoted 8 lines · Junio C Hamano
t/t0090-cache-tree.sh cmp_cache_tree () { · line 18
Quoted 4 lines · David Turner
t/t0090-cache-tree.sh +0
-0@@ -16,8 +16,34 @@ cmp_cache_tree () { # We don't bother with actually checking the SHA1: # test-dump-cache-tree already verifies that all existing data is # correct.Is this statement now stale and needs to be removed?
I think it is still accurate; we still don't bother to check SHAs and test-dump-cache-tree still does the comparison.