cache-tree: update API to take abitrary flags

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Nguyễn Thái Ngọc Duy committed Jan 16, 2012 at 09:36 UTC e859c69b26528a55bd64f4eee2ed06fad999d862
5 files changed +19 -20
builtin/commit.c
+2 -2
@@ -400,7 +400,7 @@ static char *prepare_index(int argc, const char **argv, const char *prefix,
400 fd = hold_locked_index(&index_lock, 1);
401 add_files_to_cache(also ? prefix : NULL, pathspec, 0);
402 refresh_cache_or_die(refresh_flags);
403 - update_main_cache_tree(1);
403 + update_main_cache_tree(WRITE_TREE_SILENT);
404 if (write_cache(fd, active_cache, active_nr) ||
405 close_lock_file(&index_lock))
406 die(_("unable to write new_index file"));
@@ -421,7 +421,7 @@ static char *prepare_index(int argc, const char **argv, const char *prefix,
421 fd = hold_locked_index(&index_lock, 1);
422 refresh_cache_or_die(refresh_flags);
423 if (active_cache_changed) {
424 - update_main_cache_tree(1);
424 + update_main_cache_tree(WRITE_TREE_SILENT);
425 if (write_cache(fd, active_cache, active_nr) ||
426 commit_locked_index(&index_lock))
427 die(_("unable to write new_index file"));
cache-tree.c
+12 -15
@@ -150,9 +150,10 @@ 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, int silent)
153 + int entries, int flags)
154 {
155 int i, funny;
156 + int silent = flags & WRITE_TREE_SILENT;
157
158 /* Verify that the tree is merged */
159 funny = 0;
@@ -241,10 +242,11 @@ static int update_one(struct cache_tree *it,
242 int entries,
243 const char *base,
244 int baselen,
244 - int missing_ok,
245 - int dryrun)
245 + int flags)
246 {
247 struct strbuf buffer;
248 + int missing_ok = flags & WRITE_TREE_MISSING_OK;
249 + int dryrun = flags & WRITE_TREE_DRY_RUN;
250 int i;
251
252 if (0 <= it->entry_count && has_sha1_file(it->sha1))
@@ -288,8 +290,7 @@ static int update_one(struct cache_tree *it,
290 cache + i, entries - i,
291 path,
292 baselen + sublen + 1,
291 - missing_ok,
292 - dryrun);
293 + flags);
294 if (subcnt < 0)
295 return subcnt;
296 i += subcnt - 1;
@@ -371,15 +372,13 @@ static int update_one(struct cache_tree *it,
372 int cache_tree_update(struct cache_tree *it,
373 struct cache_entry **cache,
374 int entries,
374 - int missing_ok,
375 - int dryrun,
376 - int silent)
375 + int flags)
376 {
377 int i;
379 - i = verify_cache(cache, entries, silent);
378 + i = verify_cache(cache, entries, flags);
379 if (i)
380 return i;
382 - i = update_one(it, cache, entries, "", 0, missing_ok, dryrun);
381 + i = update_one(it, cache, entries, "", 0, flags);
382 if (i < 0)
383 return i;
384 return 0;
@@ -572,11 +571,9 @@ int write_cache_as_tree(unsigned char *sha1, int flags, const char *prefix)
571
572 was_valid = cache_tree_fully_valid(active_cache_tree);
573 if (!was_valid) {
575 - int missing_ok = flags & WRITE_TREE_MISSING_OK;
576 -
574 if (cache_tree_update(active_cache_tree,
575 active_cache, active_nr,
579 - missing_ok, 0, 0) < 0)
576 + flags) < 0)
577 return WRITE_TREE_UNMERGED_INDEX;
578 if (0 <= newfd) {
579 if (!write_cache(newfd, active_cache, active_nr) &&
@@ -672,10 +669,10 @@ int cache_tree_matches_traversal(struct cache_tree *root,
669 return 0;
670 }
671
675 -int update_main_cache_tree (int silent)
672 +int update_main_cache_tree(int flags)
673 {
674 if (!the_index.cache_tree)
675 the_index.cache_tree = cache_tree();
676 return cache_tree_update(the_index.cache_tree,
680 - the_index.cache, the_index.cache_nr, 0, 0, silent);
677 + the_index.cache, the_index.cache_nr, flags);
678 }
cache-tree.h
+3 -1
@@ -29,13 +29,15 @@ 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, int);
32 +int cache_tree_update(struct cache_tree *, struct cache_entry **, 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
38 #define WRITE_TREE_IGNORE_CACHE_TREE 2
39 +#define WRITE_TREE_DRY_RUN 4
40 +#define WRITE_TREE_SILENT 8
41
42 /* error return codes */
43 #define WRITE_TREE_UNREADABLE_INDEX (-1)
merge-recursive.c
+1 -1
@@ -264,7 +264,7 @@ struct tree *write_tree_from_memory(struct merge_options *o)
264
265 if (!cache_tree_fully_valid(active_cache_tree) &&
266 cache_tree_update(active_cache_tree,
267 - active_cache, active_nr, 0, 0, 0) < 0)
267 + active_cache, active_nr, 0) < 0)
268 die("error building trees");
269
270 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, 0);
62 + cache_tree_update(another, active_cache, active_nr, WRITE_TREE_DRY_RUN);
63 return dump_cache_tree(active_cache_tree, another, "");
64 }