cache-tree: Create/update cache-tree on checkout
PATCH 1/3 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.
--- builtin/checkout.c | 4 ++++ cache-tree.c | 22 ++++++++++++---------- cache-tree.h | 1 + t/t0090-cache-tree.sh | 15 ++++++++++++++- 4 files changed, 31 insertions(+), 11 deletions(-)
builtin/checkout.c +4 -0
@@ -553,6 +553,10 @@ static int merge_working_tree(const struct checkout_opts *opts, } } +if (write_cache_as_tree (NULL ,WRITE_TREE_DO_NOT_WRITE ,"" )) { +warn ("Unable to write cache_tree" ); + } +if (write_cache (newfd, active_cache, active_nr) ||commit_locked_index (lock_file))die (_ ("unable to write new index file" ));
cache-tree.c +12 -10
@@ -568,17 +568,18 @@ static struct cache_tree *cache_tree_find(struct cache_tree *it, const char *patint write_cache_as_tree (unsigned char *sha1,int flags,const char *prefix) { -int entries, was_valid, newfd; +int entries, was_valid, newfd =-1 ;struct lock_file *lock_file; -/* -* We can't free this memory, it becomes part of a linked list -* parsed atexit() -*/ - lock_file =xcalloc (1 ,sizeof (struct lock_file )); - - newfd =hold_locked_index (lock_file,1 ); +if (!(flags &WRITE_TREE_DO_NOT_WRITE )) { +/* +* We can't free this memory, it becomes part of a linked list +* parsed atexit() +*/ + lock_file =xcalloc (1 ,sizeof (struct lock_file )); + newfd =hold_locked_index (lock_file,1 ); + } entries =read_cache ();if (entries <0 )return WRITE_TREE_UNREADABLE_INDEX ;
@@ -612,9 +613,10 @@ int write_cache_as_tree(unsigned char *sha1, int flags, const char *prefix)cache_tree_find (active_cache_tree, prefix);if (!subtree)return WRITE_TREE_PREFIX_ERROR ; -hashcpy (sha1, subtree->sha1); +if (sha1) +hashcpy (sha1, subtree->sha1); } -else +else if (sha1)hashcpy (sha1, active_cache_tree->sha1);if (0 <= newfd)
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_DO_NOT_WRITE 16/* error return codes */ #define WRITE_TREE_UNREADABLE_INDEX (-1)
t/t0090-cache-tree.sh +14 -1
@@ -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 2/3 test-dump-cache-tree: Improve output format and exit code
Make test-dump-cache-tree more useful for testing. Do not treat known invalid trees as errors (and do not produce non-zero exit code), because we can fall back to the non-cache-tree codepath.
--- t/t0090-cache-tree.sh | 28 +++++++++++++++++++++++++--- test-dump-cache-tree.c | 24 ++++++++++++------------ 2 files changed, 37 insertions(+), 15 deletions(-)
t/t0090-cache-tree.sh +25 -3
@@ -21,10 +21,13 @@ test_shallow_cache_tree () { cmp_cache_tree expect } +# Test that the cache-tree for a given directory is invalid. +# If no directory is given, check that the root is invalid 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 + test-dump-cache-tree >actual && + sed -e "s/$_x40/SHA/" -e "s/[0-9]* subtrees//g" <actual >filtered && + expect=$(printf "invalid $1 ()\n") && + fgrep "$expect" filtered } test_no_cache_tree () {
@@ -49,6 +52,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 &&
test-dump-cache-tree.c +12 -12
@@ -6,12 +6,12 @@static void dump_one (struct cache_tree *it,const char *pfx,const char *x) {if (it->entry_count <0 ) -printf ("%-40s %s%s (%d subtrees)\n" , -"invalid" , x, pfx, it->subtree_nr); +printf ("%-40s %s (%d subtrees)%s\n" , +"invalid" , pfx, it->subtree_nr, x);else -printf ("%s %s%s (%d entries, %d subtrees)\n" , -sha1_to_hex (it->sha1), x, pfx, - it->entry_count, it->subtree_nr); +printf ("%s %s (%d entries, %d subtrees)%s\n" , +sha1_to_hex (it->sha1), pfx, + it->entry_count, it->subtree_nr, x); } static int dump_cache_tree(struct cache_tree *it,
@@ -25,19 +25,19 @@ static int dump_cache_tree(struct cache_tree *it,/* missing in either */ return 0 ; -if (it->entry_count <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) { -dump_one (ref, pfx,"#(ref) " ); +/* claims to be valid but is lying */ +dump_one (ref, pfx," #(error)" ); errs =1 ; + }else { +/* is actually valid */ +dump_one (it, pfx,"" ); } }
Signature · 1 line
2.0.0.390.gcb682f8
PATCH 3/3 cache-tree: Write index with updated cache-tree after commit
In reply to David TurnerDuring the commit process, the cache-tree is updated. We need to 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. Also demonstrate that cache-tree invalidation is correct.
--- builtin/commit.c | 6 ++++++ t/t0090-cache-tree.sh | 50 +++++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 53 insertions(+), 3 deletions(-)
builtin/commit.c +6 -0
@@ -607,6 +607,8 @@ static int prepare_to_commit(const char *index_file, const char *prefix,const char *hook_arg2 =NULL ;int clean_message_contents = (cleanup_mode !=CLEANUP_NONE );int old_display_comment_prefix; +static struct lock_file index_lock; +int index_fd;/* This checks and barfs if author is badly specified */ determine_author_info (author_ident);
@@ -872,6 +874,10 @@ static int prepare_to_commit(const char *index_file, const char *prefix,error (_ ("Error building trees" ));return 0 ; } +/* After updating the cache-tree, rewrite the index */ + index_fd =hold_locked_index (&index_lock,0 ); +if (index_fd >=0 &&write_index (&the_index, index_fd) >=0 ) +commit_locked_index (&index_lock);if (run_commit_hook (use_editor, index_file,"prepare-commit-msg" ,git_path (commit_editmsg), hook_arg1, hook_arg2,NULL ))
t/t0090-cache-tree.sh +47 -3
83 lines not on this page · raw message
Signature · 1 line
2.0.0.390.gcb682f8
Quoted 16 lines · David Turner
When git checkout checks out a branch, create or update the cache-tree so that subsequent operations are faster.
Signed-off-by David Turner <[email protected]>--- builtin/checkout.c | 4 ++++ cache-tree.c | 22 ++++++++++++---------- cache-tree.h | 1 + t/t0090-cache-tree.sh | 15 ++++++++++++++- 4 files changed, 31 insertions(+), 11 deletions(-)builtin/checkout.c +0
-0
builtin/checkout.c static int merge_working_tree(const struct checkout_opts *opts, · line 559
Quoted 7 lines
builtin/checkout.c +4
-0@@ -553,6 +553,10 @@ static int merge_working_tree(const struct checkout_opts *opts, } } +if (write_cache_as_tree (NULL ,WRITE_TREE_DO_NOT_WRITE ,"" )) { +warn ("Unable to write cache_tree" ); + } +
I wonder if we should do this in !opts->force code path only. In the opts->force code path we could use prime_cache_tree() (like read-tree), which is supposedly faster (but may need some tests to be sure). prime_cache_tree() could be made a bit faster by doing it during tree traversal in unpack_trees() so we don't have to unpack any tree objects twice.
Signature · 1 line
Duy
Quoted 16 lines · David Turner
When git checkout checks out a branch, create or update the cache-tree so that subsequent operations are faster.
Signed-off-by David Turner <[email protected]>--- builtin/checkout.c | 4 ++++ cache-tree.c | 22 ++++++++++++---------- cache-tree.h | 1 + t/t0090-cache-tree.sh | 15 ++++++++++++++- 4 files changed, 31 insertions(+), 11 deletions(-)builtin/checkout.c +0
-0
builtin/checkout.c static int merge_working_tree(const struct checkout_opts *opts, · line 559
Quoted 7 lines
builtin/checkout.c +4
-0@@ -553,6 +553,10 @@ static int merge_working_tree(const struct checkout_opts *opts, } } +if (write_cache_as_tree (NULL ,WRITE_TREE_DO_NOT_WRITE ,"" )) { +warn ("Unable to write cache_tree" ); + } +
This is not even warn-worthy event, isn't it? "checkout -m other" would attempt a three-way merge to replay your local changes relative to your current HEAD in the context of "other" branch and will leave conflicts in the index and in the working tree, and it is perfectly a normal thing that you cannot write the index in such a state as a tree object. Perhaps you should check if the index is unmerged before even attempting to compute the cache tree.
Also it looks very strange that write-cache-as-tree, whose *PRIMARY* function is to write a tree, receives a "DO NOT WRITE" option here. I understand that you want a new interface into the cache-tree subsystem that only updates the cache-tree but it somehow smells like a sloppy/lazy refactoring that is not done quite right.
Re: [PATCH 3/3] cache-tree: Write index with updated cache-tree after commit
In reply to David TurnerQuoted 26 lines · David Turner
During the commit process, the cache-tree is updated. We need to 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. Also demonstrate that cache-tree invalidation is correct.
Signed-off-by David Turner <[email protected]>--- builtin/commit.c | 6 ++++++ t/t0090-cache-tree.sh | 50 +++++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 53 insertions(+), 3 deletions(-)builtin/commit.c +2
-0@@ -607,6 +607,8 @@ static int prepare_to_commit(const char *index_file, const char *prefix,const char *hook_arg2 =NULL ;int clean_message_contents = (cleanup_mode !=CLEANUP_NONE );int old_display_comment_prefix; +static struct lock_file index_lock; +int index_fd;/* This checks and barfs if author is badly specified */ determine_author_info (author_ident);
builtin/commit.c static int prepare_to_commit(const char *index_file, const char *prefix, · line 880
Quoted 7 lines
builtin/commit.c +4
-0@@ -872,6 +874,10 @@ static int prepare_to_commit(const char *index_file, const char *prefix,error (_ ("Error building trees" ));return 0 ; } +/* After updating the cache-tree, rewrite the index */ + index_fd =hold_locked_index (&index_lock,0 ); +if (index_fd >=0 &&write_index (&the_index, index_fd) >=0 ) +commit_locked_index (&index_lock);
Is this run unconditionally even when we are making a partial commit out of a temporary index file (which will be discarded after we create this commit)?
I have a feeling that a better place to populate the cache-tree may be prepare-index which knows the distinction between various modes of commit, but I haven't looked at the code path for a while...
Quoted 3 lines · Duy Nguyen
I wonder if we should do this in !opts->force code path only. In the
opts->forcecode path we could useprime_cache_tree()(like read-tree), which is supposedly faster...
Nobody sane should be constantly running "checkout -f", so even if priming from existing tree objects were faster, it would be adding complexity to the code to optimize for a wrong case.