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 <trast@student.ethz.ch> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Thomas Rast committed Dec 6, 2011 at 18:43 UTC 996277c520641d650dc15ad751cc4ad33318e298
5 files changed +21 -11
builtin/commit.c
+1 -4
@@ -862,10 +862,7 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
862 */
863 discard_cache();
864 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) {
865 + if (update_main_cache_tree(0)) {
866 error(_("Error building trees"));
867 return 0;
868 }
cache-tree.c
+15 -4
@@ -150,7 +150,7 @@ void cache_tree_invalidate_path(struct cache_tree *it, const char *path)
150 }
151
152 static int verify_cache(struct cache_entry **cache,
153 - int entries)
153 + int entries, int silent)
154 {
155 int i, funny;
156
@@ -159,6 +159,8 @@ static int verify_cache(struct cache_entry **cache,
159 for (i = 0; i < entries; i++) {
160 struct cache_entry *ce = cache[i];
161 if (ce_stage(ce) || (ce->ce_flags & CE_INTENT_TO_ADD)) {
162 + if (silent)
163 + return -1;
164 if (10 < ++funny) {
165 fprintf(stderr, "...\n");
166 break;
@@ -370,10 +372,11 @@ int cache_tree_update(struct cache_tree *it,
372 struct cache_entry **cache,
373 int entries,
374 int missing_ok,
373 - int dryrun)
375 + int dryrun,
376 + int silent)
377 {
378 int i;
376 - i = verify_cache(cache, entries);
379 + i = verify_cache(cache, entries, silent);
380 if (i)
381 return i;
382 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)
576
577 if (cache_tree_update(active_cache_tree,
578 active_cache, active_nr,
576 - missing_ok, 0) < 0)
579 + missing_ok, 0, 0) < 0)
580 return WRITE_TREE_UNMERGED_INDEX;
581 if (0 <= newfd) {
582 if (!write_cache(newfd, active_cache, active_nr) &&
@@ -668,3 +671,11 @@ int cache_tree_matches_traversal(struct cache_tree *root,
671 return it->entry_count;
672 return 0;
673 }
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
+3 -1
@@ -29,7 +29,9 @@ void cache_tree_write(struct strbuf *, struct cache_tree *root);
29 struct cache_tree *cache_tree_read(const char *buffer, unsigned long size);
30
31 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);
35
36 /* bitmasks to write_cache_as_tree flags */
37 #define WRITE_TREE_MISSING_OK 1
merge-recursive.c
+1 -1
@@ -265,7 +265,7 @@ struct tree *write_tree_from_memory(struct merge_options *o)
265
266 if (!cache_tree_fully_valid(active_cache_tree) &&
267 cache_tree_update(active_cache_tree,
268 - active_cache, active_nr, 0, 0) < 0)
268 + active_cache, active_nr, 0, 0, 0) < 0)
269 die("error building trees");
270
271 result = lookup_tree(active_cache_tree->sha1);
test-dump-cache-tree.c
+1 -1
@@ -59,6 +59,6 @@ int main(int ac, char **av)
59 struct cache_tree *another = cache_tree();
60 if (read_cache() < 0)
61 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);
63 return dump_cache_tree(active_cache_tree, another, "");
64 }