Skip to content

Commit 996277c

Browse files
trastgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 4eb0346 commit 996277c

File tree

5 files changed

+21
-11
lines changed

5 files changed

+21
-11
lines changed

builtin/commit.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -862,10 +862,7 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
862862
*/
863863
discard_cache();
864864
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)) {
869866
error(_("Error building trees"));
870867
return 0;
871868
}

cache-tree.c

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ void cache_tree_invalidate_path(struct cache_tree *it, const char *path)
150150
}
151151

152152
static int verify_cache(struct cache_entry **cache,
153-
int entries)
153+
int entries, int silent)
154154
{
155155
int i, funny;
156156

@@ -159,6 +159,8 @@ static int verify_cache(struct cache_entry **cache,
159159
for (i = 0; i < entries; i++) {
160160
struct cache_entry *ce = cache[i];
161161
if (ce_stage(ce) || (ce->ce_flags & CE_INTENT_TO_ADD)) {
162+
if (silent)
163+
return -1;
162164
if (10 < ++funny) {
163165
fprintf(stderr, "...\n");
164166
break;
@@ -370,10 +372,11 @@ int cache_tree_update(struct cache_tree *it,
370372
struct cache_entry **cache,
371373
int entries,
372374
int missing_ok,
373-
int dryrun)
375+
int dryrun,
376+
int silent)
374377
{
375378
int i;
376-
i = verify_cache(cache, entries);
379+
i = verify_cache(cache, entries, silent);
377380
if (i)
378381
return i;
379382
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)
573576

574577
if (cache_tree_update(active_cache_tree,
575578
active_cache, active_nr,
576-
missing_ok, 0) < 0)
579+
missing_ok, 0, 0) < 0)
577580
return WRITE_TREE_UNMERGED_INDEX;
578581
if (0 <= newfd) {
579582
if (!write_cache(newfd, active_cache, active_nr) &&
@@ -668,3 +671,11 @@ int cache_tree_matches_traversal(struct cache_tree *root,
668671
return it->entry_count;
669672
return 0;
670673
}
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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ void cache_tree_write(struct strbuf *, struct cache_tree *root);
2929
struct cache_tree *cache_tree_read(const char *buffer, unsigned long size);
3030

3131
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);
3335

3436
/* bitmasks to write_cache_as_tree flags */
3537
#define WRITE_TREE_MISSING_OK 1

merge-recursive.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ struct tree *write_tree_from_memory(struct merge_options *o)
265265

266266
if (!cache_tree_fully_valid(active_cache_tree) &&
267267
cache_tree_update(active_cache_tree,
268-
active_cache, active_nr, 0, 0) < 0)
268+
active_cache, active_nr, 0, 0, 0) < 0)
269269
die("error building trees");
270270

271271
result = lookup_tree(active_cache_tree->sha1);

test-dump-cache-tree.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,6 @@ int main(int ac, char **av)
5959
struct cache_tree *another = cache_tree();
6060
if (read_cache() < 0)
6161
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);
6363
return dump_cache_tree(active_cache_tree, another, "");
6464
}

0 commit comments

Comments
 (0)