@matheusbsbs / matheus-bagliano / commits / e46bbcf6e8
Fork 0

Move read_cache_unmerged() to read-cache.c

builtin-read-tree has a read_cache_unmerged() which is useful for other builtins, for example builtin-merge uses it as well. Move it to read-cache.c to avoid code duplication. Signed-off-by: Miklos Vajna <vmiklos@frugalware.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Miklos Vajna committed Jun 27, 2008 at 18:21 UTC e46bbcf6e89e4b1d3d8de1d20d836538ab0f0c85
3 files changed +33 -24
builtin-read-tree.c
-24
@@ -29,30 +29,6 @@ static int list_tree(unsigned char *sha1)
29 return 0;
30 }
31
32 -static int read_cache_unmerged(void)
33 -{
34 - int i;
35 - struct cache_entry **dst;
36 - struct cache_entry *last = NULL;
37 -
38 - read_cache();
39 - dst = active_cache;
40 - for (i = 0; i < active_nr; i++) {
41 - struct cache_entry *ce = active_cache[i];
42 - if (ce_stage(ce)) {
43 - remove_name_hash(ce);
44 - if (last && !strcmp(ce->name, last->name))
45 - continue;
46 - cache_tree_invalidate_path(active_cache_tree, ce->name);
47 - last = ce;
48 - continue;
49 - }
50 - *dst++ = ce;
51 - }
52 - active_nr = dst - active_cache;
53 - return !!last;
54 -}
55 -
32 static void prime_cache_tree_rec(struct cache_tree *it, struct tree *tree)
33 {
34 struct tree_desc desc;
cache.h
+2
@@ -254,6 +254,7 @@ static inline void remove_name_hash(struct cache_entry *ce)
254
255 #define read_cache() read_index(&the_index)
256 #define read_cache_from(path) read_index_from(&the_index, (path))
257 +#define read_cache_unmerged() read_index_unmerged(&the_index)
258 #define write_cache(newfd, cache, entries) write_index(&the_index, (newfd))
259 #define discard_cache() discard_index(&the_index)
260 #define unmerged_cache() unmerged_index(&the_index)
@@ -356,6 +357,7 @@ extern int init_db(const char *template_dir, unsigned int flags);
357 /* Initialize and use the cache information */
358 extern int read_index(struct index_state *);
359 extern int read_index_from(struct index_state *, const char *path);
360 +extern int read_index_unmerged(struct index_state *);
361 extern int write_index(const struct index_state *, int newfd);
362 extern int discard_index(struct index_state *);
363 extern int unmerged_index(const struct index_state *);
read-cache.c
+31
@@ -1410,3 +1410,34 @@ int write_index(const struct index_state *istate, int newfd)
1410 }
1411 return ce_flush(&c, newfd);
1412 }
1413 +
1414 +/*
1415 + * Read the index file that is potentially unmerged into given
1416 + * index_state, dropping any unmerged entries. Returns true is
1417 + * the index is unmerged. Callers who want to refuse to work
1418 + * from an unmerged state can call this and check its return value,
1419 + * instead of calling read_cache().
1420 + */
1421 +int read_index_unmerged(struct index_state *istate)
1422 +{
1423 + int i;
1424 + struct cache_entry **dst;
1425 + struct cache_entry *last = NULL;
1426 +
1427 + read_index(istate);
1428 + dst = istate->cache;
1429 + for (i = 0; i < istate->cache_nr; i++) {
1430 + struct cache_entry *ce = istate->cache[i];
1431 + if (ce_stage(ce)) {
1432 + remove_name_hash(ce);
1433 + if (last && !strcmp(ce->name, last->name))
1434 + continue;
1435 + cache_tree_invalidate_path(istate->cache_tree, ce->name);
1436 + last = ce;
1437 + continue;
1438 + }
1439 + *dst++ = ce;
1440 + }
1441 + istate->cache_nr = dst - istate->cache;
1442 + return !!last;
1443 +}