From 996277c520641d650dc15ad751cc4ad33318e298 Mon Sep 17 00:00:00 2001 From: Thomas Rast Date: Tue, 6 Dec 2011 18:43:37 +0100 Subject: Refactor cache_tree_update idiom from commit We'll need to safely create or update the cache-tree data of the_index from other places. While at it, give it an argument that lets us silence the messages produced by unmerged entries (which prevent it from working). Signed-off-by: Thomas Rast Signed-off-by: Junio C Hamano --- builtin/commit.c | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) (limited to 'builtin') diff --git a/builtin/commit.c b/builtin/commit.c index 8f2bebecf..4f782a33f 100644 --- a/builtin/commit.c +++ b/builtin/commit.c @@ -862,10 +862,7 @@ static int prepare_to_commit(const char *index_file, const char *prefix, */ discard_cache(); read_cache_from(index_file); - if (!active_cache_tree) - active_cache_tree = cache_tree(); - if (cache_tree_update(active_cache_tree, - active_cache, active_nr, 0, 0) < 0) { + if (update_main_cache_tree(0)) { error(_("Error building trees")); return 0; } -- cgit v1.2.1 From 11c8a74a64a49f236d475514f723123a8928463d Mon Sep 17 00:00:00 2001 From: Thomas Rast Date: Tue, 6 Dec 2011 18:43:38 +0100 Subject: commit: write cache-tree data when writing index anyway In prepare_index(), we refresh the index, and then write it to disk if this changed the index data. After running hooks we re-read the index and compute the root tree sha1 with the cache-tree machinery. This gives us a mostly free opportunity to write up-to-date cache-tree data: we can compute it in prepare_index() immediately before writing the index to disk. If we do this, we were going to write the index anyway, and the later cache-tree update has no further work to do. If we don't do it, we don't do any extra work, though we still don't have have cache-tree data after the commit. The only case that suffers badly is when the pre-commit hook changes many trees in the index. I'm writing this off as highly unusual. Signed-off-by: Thomas Rast Signed-off-by: Junio C Hamano --- builtin/commit.c | 2 ++ 1 file changed, 2 insertions(+) (limited to 'builtin') diff --git a/builtin/commit.c b/builtin/commit.c index 4f782a33f..5125841f9 100644 --- a/builtin/commit.c +++ b/builtin/commit.c @@ -394,6 +394,7 @@ 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(1); if (write_cache(fd, active_cache, active_nr) || close_lock_file(&index_lock)) die(_("unable to write new_index file")); @@ -414,6 +415,7 @@ static char *prepare_index(int argc, const char **argv, const char *prefix, fd = hold_locked_index(&index_lock, 1); refresh_cache_or_die(refresh_flags); if (active_cache_changed) { + update_main_cache_tree(1); if (write_cache(fd, active_cache, active_nr) || commit_locked_index(&index_lock)) die(_("unable to write new_index file")); -- cgit v1.2.1 From 6c52ec8a9ab48b50fc8bf9559467d5a4cf7eee3b Mon Sep 17 00:00:00 2001 From: Thomas Rast Date: Tue, 6 Dec 2011 18:43:39 +0100 Subject: reset: update cache-tree data when appropriate In the case of --mixed and --hard, we throw away the old index and rebuild everything from the tree argument (or HEAD). So we have an opportunity here to fill in the cache-tree data, just as read-tree did. Signed-off-by: Thomas Rast Signed-off-by: Junio C Hamano --- builtin/reset.c | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'builtin') diff --git a/builtin/reset.c b/builtin/reset.c index 811e8e252..8c2c1d52a 100644 --- a/builtin/reset.c +++ b/builtin/reset.c @@ -43,6 +43,7 @@ static int reset_index_file(const unsigned char *sha1, int reset_type, int quiet int nr = 1; int newfd; struct tree_desc desc[2]; + struct tree *tree; struct unpack_trees_options opts; struct lock_file *lock = xcalloc(1, sizeof(struct lock_file)); @@ -84,6 +85,12 @@ static int reset_index_file(const unsigned char *sha1, int reset_type, int quiet return error(_("Failed to find tree of %s."), sha1_to_hex(sha1)); if (unpack_trees(nr, desc, &opts)) return -1; + + if (reset_type == MIXED || reset_type == HARD) { + tree = parse_tree_indirect(sha1); + prime_cache_tree(&active_cache_tree, tree); + } + if (write_cache(newfd, active_cache, active_nr) || commit_locked_index(lock)) return error(_("Could not write new index file.")); -- cgit v1.2.1