cache-tree: Create/update cache-tree on checkout
PATCH v4 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 v4 2/4 test-dump-cache-tree: invalid trees are not errors
Do not treat known-invalid trees as errors even when their count is incorrect. Because git already knows that these trees are invalid, nothing depends on the count field.
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 v4 3/4 cache-tree: subdirectory tests
In reply to David TurnerAdd tests to confirm that invalidation of subdirectories nether over- nor under-invalidates.
--- t/t0090-cache-tree.sh | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 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 &&
Signature · 1 line
2.0.0.390.gcb682f8
Re: [PATCH v4 3/4] cache-tree: subdirectory tests
Quoted 1 line · David Turner
Add tests to confirm that invalidation of subdirectories nether over-
s/nether/neither/
Quoted 4 lines
nor under-invalidates.
Signed-off-by David Turner <[email protected]>---
PATCH v4 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 | 31 +++++++++++------- t/t0090-cache-tree.sh | 87 ++++++++++++++++++++++++++++++++++++++++++--------- 2 files changed, 91 insertions(+), 27 deletions(-)
builtin/commit.c +19 -12
@@ -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;
@@ -363,10 +365,18 @@ static char *prepare_index(int argc, const char **argv, const char *prefix, fd =hold_locked_index (&index_lock,1 );add_files_to_cache (also ? prefix :NULL , &pathspec,0 );refresh_cache_or_die (refresh_flags); -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" )); +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) || +close_lock_file (&index_lock)) +die (_ ("unable to write new_index file" )); + }else { +rollback_lock_file (&index_lock); + } commit_style =COMMIT_NORMAL ;return index_lock.filename; }
@@ -383,14 +393,10 @@ 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) { -update_main_cache_tree (WRITE_TREE_SILENT ); -if (write_cache (fd, active_cache, active_nr) || -commit_locked_index (&index_lock)) -die (_ ("unable to write new_index file" )); - }else { -rollback_lock_file (&index_lock); - } +update_main_cache_tree (WRITE_TREE_SILENT ); +if (write_cache (fd, active_cache, active_nr) || +commit_locked_index (&index_lock)) +die (_ ("unable to write new_index file" )); commit_style =COMMIT_AS_IS ;return get_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 +72 -15
167 lines not on this page · raw message
Signature · 1 line
2.0.0.390.gcb682f8
Re: [PATCH v4 2/4] test-dump-cache-tree: invalid trees are not errors
In reply to David TurnerQuoted 3 lines · David Turner
Do not treat known-invalid trees as errors even when their count is incorrect. Because git already knows that these trees are invalid, nothing depends on the count field.
s/count/subtree_nr/; they are different.
"nothing depends on" is not quite correct. The field keeps track of the number of subdirectories in the directory recorded in the cache-tree, and it *must* be maintained correctly (we iterate over the it->down[] array up to that number).
The number of subdirectories the directory actually has, which we can discover by counting entries in the main part of the index, may be different from subtree_nr, if we haven't run update_one() on it, e.g. we may have added a path in a new subdirectory, at which time we would have invalidated the directory and its it->down[] array does not know the new subdirectory.
While reading 3/4, I wondered if it makes sense to show the (N subtrees) for invalidated node, but as a debugging measure it helped me often while developping the framework, so we may not want to drop the subtree_nr from the output for invalidated entries.
The change itself looks good.
Thanks.
Re: [PATCH v4 3/4] cache-tree: subdirectory tests
In reply to David TurnerQuoted 15 lines · David Turner
Add tests to confirm that invalidation of subdirectories nether over- nor under-invalidates.
Signed-off-by David Turner <[email protected]>--- t/t0090-cache-tree.sh | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-)t/t0090-cache-tree.sh +0
-0@@ -21,10 +21,13 @@ test_shallow_cache_tree () { cmp_cache_tree expect }
t/t0090-cache-tree.sh test_shallow_cache_tree () { · line 29
Quoted 10 lines
t/t0090-cache-tree.sh +5
-3+# 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") &&
It would be saner to do 'printf "string %s more string" "$1"' than embedding caller-supplied "$1" inside the format specifier.
t/t0090-cache-tree.sh test_shallow_cache_tree () { · line 30
Quoted 1 line
t/t0090-cache-tree.sh +1
-0+ fgrep "$expect" filtered
We'd actually want to see fewer uses of 'fgrep' in the tests for two reasons.
Is having an entry that is invalidated the only thing we care about in this test? Shouldn't the caller expect "These subtrees and nothing else must be invalidated", in which case the helper should check not just the expected "invalid dir1/" appears in the output but no other unexpected "invalid somethingelse/" appears (and this "no other unexpected output" makes use of grep family in tests like this less desirable).
In other words, wouldn't it be better to do the helper along the lines of:
test_invalidated_cache_tree () {
if test $# != 0
then
printf "invalid %s ()\n" "" "$@"
fi >expect &&
test-dump-cache-tree |
sed -n -e '/^invalid /p' >actual &&
test_cmp expect actual
}and use
test_invalidated_cache_tree dir1
when we expect only dir1 and dir2 (but not dir2 or anything else) is invalidated?
Thanks.
Re: [PATCH v4 4/4] cache-tree: Write updated cache-tree after commit
In reply to David TurnerQuoted 58 lines · David Turner
During 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.
Signed-off-by David Turner <[email protected]>--- builtin/commit.c | 31 +++++++++++------- t/t0090-cache-tree.sh | 87 ++++++++++++++++++++++++++++++++++++++++++--------- 2 files changed, 91 insertions(+), 27 deletions(-)builtin/commit.c +14
-9@@ -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;@@ -363,10 +365,18 @@ static char *prepare_index(int argc, const char **argv, const char *prefix, fd =hold_locked_index (&index_lock,1 );add_files_to_cache (also ? prefix :NULL , &pathspec,0 );refresh_cache_or_die (refresh_flags); -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" )); +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) || +close_lock_file (&index_lock)) +die (_ ("unable to write new_index file" )); + }else { +rollback_lock_file (&index_lock); + } commit_style =COMMIT_NORMAL ;return index_lock.filename; }@@ -383,14 +393,10 @@ 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) { -update_main_cache_tree (WRITE_TREE_SILENT ); -if (write_cache (fd, active_cache, active_nr) || -commit_locked_index (&index_lock)) -die (_ ("unable to write new_index file" ));
builtin/commit.c static char *prepare_index(int argc, const char **argv, const char *prefix, · line 402
Quoted 10 lines
builtin/commit.c +4
-3- }else { -rollback_lock_file (&index_lock); - } +update_main_cache_tree (WRITE_TREE_SILENT ); +if (write_cache (fd, active_cache, active_nr) || +commit_locked_index (&index_lock)) +die (_ ("unable to write new_index file" )); commit_style =COMMIT_AS_IS ;return get_index_file (); }
Hmph. The above two hunks somehow feel the other way around. When doing a non-partial, non as-is commit, i.e. "git commit $paths", the original used to call 'write-cache' unconditionally, because it is very unlikely for us to see !active_cache_changed after calling the add-files-to-cache with user-supplied pathspec (if there is nothing to change, the user is being silly to say "git commit $paths" in the first place). I would have expected that the patch would have left that code path alone (it seems to be doing the right thing already).
On the other hand, "git commit" to commit the contents of the index as-is is being cautious not to write things out unnecessarily, but as you found out, it would be a good idea to fully populate the cache-tree in this code path and write the otherwise already-good index file out, even if we see !active_cache_changed after we called refresh_cache_or_die(). So I would have expected that the patch would have kept the "write only necessary" carefulness instead of calling write-cache unconditionally.
That is, something like:
fd = hold_locked_index();
refresh_cache_or_die();
+ if (!cache_tree_fully_valid())
+ active_cache_changed = 1;
if (active_cache_changed) {
update_main_cache_tree();t/t0090-cache-tree.sh cache-tree extension. · line 11
Quoted 10 lines
t/t0090-cache-tree.sh +2
-0@@ -7,8 +7,14 @@ cache-tree extension. " . ./test-lib.sh +grep_nonmatch_ok () { + grep $@
dq around it, i.e. "$@".
t/t0090-cache-tree.sh cache-tree extension. · line 12
Quoted 1 line
t/t0090-cache-tree.sh +1
-0+ test "$?" = "2" && return 1
POSIX.1 only specifies >1 not necessarily 2 as an error status.
t/t0090-cache-tree.sh cache-tree extension. · line 14
Quoted 2 lines
t/t0090-cache-tree.sh +2
-0+ return 0 +}
Having said all that I do not think the helper is unnecessary. Just use something like
sed -e '/#(ref)/d'
Quoted 3 lines
t/t0090-cache-tree.sh +1
-1cmp_cache_tree () { - test-dump-cache-tree >actual && + test-dump-cache-tree | grep_nonmatch_ok -v \#\(ref\) >actual &&
Re: [PATCH v4 4/4] cache-tree: Write updated cache-tree after commit
Quoted 6 lines · Junio C Hamano
Quoted 6 lines
builtin/commit.c +0
-0@@ -342,6 +342,8 @@ static char *prepare_index(int argc, const char **argv, const char *prefix,
Quoted 7 lines
builtin/commit.c static char *prepare_index(int argc, const char **argv, const char *prefix, · line 349
Quoted 7 lines
builtin/commit.c +2
-0discard_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;
I'll push out a tentative result of merging this series (with some proposed fix-ups) sometime later today, but this part interacts with Duy's split-index topic in a funny way, so I'd exclude it from the merge result for now.
Thanks.
Re: [PATCH v4 4/4] cache-tree: Write updated cache-tree after commit
Quoted 8 lines · Junio C Hamano
Quoted 6 lines · Junio C Hamano
Quoted 6 lines
builtin/commit.c +0
-0@@ -342,6 +342,8 @@ static char *prepare_index(int argc, const char **argv, const char *prefix,
Quoted 7 lines
Quoted 7 lines
builtin/commit.c static char *prepare_index(int argc, const char **argv, const char *prefix, · line 349
Quoted 7 lines
builtin/commit.c +2
-0discard_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;
I wonder if we need to update_main_cache_tree() so many times in this function. If I read the code correctly, all roads must lead to update_main_cache_tree(0) in prepare_to_commit(). If we find out that we have incomplete cache-tree before that call, we could write the index one more time after that call, instead of spreading update_main_cache_tree() all over prepare_index(). I know the "index_file" in prepare_to_commit() is probably "index.lock" or something, but that does not stop us from locking again ("index.lock.lock") if we want to update it.
Writing cache tree early in prepare_index() does help hooks, but I would say hooks are uncommon case and we could add an option to update-index to explicitly rebuild cache-tree, then hooks that do diff a lot (or other operations that use cache-tree) could rebuild cache-tree by themselves. When the index file is large, every write pays high penalty so I think trying to write less often is good.
Signature · 1 line
Duy
Re: [PATCH v4 4/4] cache-tree: Write updated cache-tree after commit
Quoted 5 lines · Duy Nguyen
Writing cache tree early in
prepare_index()does help hooks, but I would say hooks are uncommon case and we could add an option to update-index to explicitly rebuild cache-tree, then hooks that do diff a lot (or other operations that use cache-tree) could rebuild cache-tree by themselves.
Yes, "update-index --update-cache-tree" would be a good addition for completeness; scripts working with plumbing should be able to do what built-in Porcelains can. They can of course do "write-tree" in the meantime so I do not see it as a very high priority, though.
This should apply on top of 'master', and if the series under discussion turns out to be a good idea, the new call to update-main-cache-tree I added to this code path should use the option added by the series that only repairs parts of cache-trees that can be repaird without writing out new trees, so it is just to give hints to future people (iow I am not going to apply this patch myself right now).
builtin/update-index.c | 8 ++++++++ 1 file changed, 8 insertions(+)
builtin/update-index.c +8 -0
@@ -26,6 +26,7 @@ static int allow_remove;static int allow_replace;static int info_only;static int force_remove; +static int update_cache_tree;static int verbose;static int mark_valid_only;static int mark_skip_worktree_only;
@@ -762,6 +763,8 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)OPT_BIT (0 ,"unmerged" , &refresh_args.flags,N_ ("refresh even if index contains unmerged entries" ),REFRESH_UNMERGED ), +OPT_BOOL (0 ,"update-cache-tree" , &update_cache_tree, +N_ ("update cache-tree before writing the result out" )), {OPTION_CALLBACK, 0, "refresh", &refresh_args, NULL,N_ ("refresh stat information" ), PARSE_OPT_NOARG | PARSE_OPT_NONEG,
@@ -918,6 +921,11 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)strbuf_release (&buf); } +if (update_cache_tree && !unmerged_cache ()) { +update_main_cache_tree (0 ); + active_cache_changed =1 ;/* force write-out */ + } +if (active_cache_changed) {if (newfd <0 ) {if (refresh_args.flags &REFRESH_QUIET )
Re: [PATCH v4 4/4] cache-tree: Write updated cache-tree after commit
In reply to Duy NguyenQuoted 4 lines · Duy Nguyen
... I know the
"index_file" inprepare_to_commit()is probably "index.lock" or something, but that does not stop us from locking again ("index.lock.lock") if we want to update it.
We grabbed the lock on the real index and we have written out the result of "update-index --refresh" to it (and closed), but we still want to and do keep the lock while "add -i" works on it. And then after "add -i" returns, we still have the lock on the real index and the patch wants to write to it again to store the refreshed cache-tree under that lock.
It may be the case that the API suite currently lacks a way to allow the caller to reopen the same "index.lock" file after calling write-locked-index(CLOSE_LOCK), and taking a lock on "index.lock" to write into "index.lock.lock" and renaming it to "index.lock" could be a workaround for it, but doesn't that sound a wrong workaround?
Re: [PATCH v4 4/4] cache-tree: Write updated cache-tree after commit
In reply to Duy NguyenQuoted 3 lines · Duy Nguyen
I wonder if we need to
update_main_cache_tree()so many times in this function. If I read the code correctly, all roads must lead toupdate_main_cache_tree(0)inprepare_to_commit().
I think prepare-to-commit is too late; it does not want to know if the index it was given to create a tree out of is the one that the user will keep using after this invocation of "git commit" or just a temporary one used for partial commit. The cache-tree rebuilt there is what is recorded with commit_tree_extended() in cmd_commit(), but if you are doing a partial commit, these generic code paths are given a temporary index file on the filesystem to work on, and cache-tree in that will not help user's later operation.
For three main uses of "git commit", prepare_index() does these:
- "git commit -a" and "git commit -i $paths" update the index with the new contents from the working tree, fully builds cache-tree in-core to write out the tree objects, and writes the index file to the filesystem. Because this index is the one used after this invocation of "git commit" returns, we have a fully populated cache-tree after this happens. This code path is perfect and there is no need to change.
- "git commit" commits the contents of the index as-is, so technically there is no reason for it to update the index on the filesystem at all, but it refreshes the cached stat data to help the "status" part of the command, and if it finds that stat data was stale, updates the index on the filesystem because it is wasteful not to do so. As we would be spending I/O cycles to update the index file in that case anyway, we also rebuild the cache-tree and include that in the updated index.
When the cached stat data was already up-to-date, however, we do not update the index on the filesystem, so the series under discussion will change the trade-off by doing one more I/O to write out a new index to the filesystem only to update cache-tree.
- "git commit $paths" updates the "real" index with given $paths and writes it out to the filesystem first. This is the index the user will use after "git commit" finishes; traditionally our trade-off was "populate cache-tree only when we do not have to spend any cycle only to do so (i.e. when we are writing trees anyway, or when we read from a tree), and let it degrade as paths are added, removed and modified" and we avoided populating cache-tree in this codepath. The series under discussion will change the trade-off here, too.
After it updates this "real" index, it builds another temporary index that represents the tree state to be committed (starting from HEAD and updates with the given $paths), but that will be discarded and we do not want to spend any extra cycle to do anything only to make its later use more efficient (like writing updated cache-tree to it).
Quoted 3 lines
If we find out that
we have incomplete cache-tree before that call, we could write the index one more time after that call,
and that will make an extra I/O only to write out cache-tree to the temporary index that we will discard immediately after for a partial commit.
Re: [PATCH v4 4/4] cache-tree: Write updated cache-tree after commit
Quoted 6 lines · Junio C Hamano
Quoted 3 lines · Duy Nguyen
I wonder if we need to
update_main_cache_tree()so many times in this function. If I read the code correctly, all roads must lead toupdate_main_cache_tree(0)inprepare_to_commit().
Quoted 8 lines
I think prepare-to-commit is too late; it does not want to know if the index it was given to create a tree out of is the one that the user will keep using after this invocation of "git commit" or just a temporary one used for partial commit. The cache-tree rebuilt there is what is recorded with
commit_tree_extended()incmd_commit(), but if you are doing a partial commit, these generic code paths are given a temporary index file on the filesystem to work on, and cache-tree in that will not help user's later operation.
Right. Thanks for checking.
Signature · 1 line
Duy
Quoted 10 lines · David Turner
When git checkout checks out a branch, create or update the cache-tree so that subsequent operations are faster.
update_main_cache_treelearned a new flag,WRITE_TREE_REPAIR. WhenWRITE_TREE_REPAIRis 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]>---
Looks safe, if a bit wasteful, to me.
Thanks; will queue.