tree: convert read_tree to take an index parameter

Signed-off-by: Brandon Williams <bmwill@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Brandon Williams committed Jun 12, 2017 at 15:13 UTC 85ab50f938601cf874c841cee4c56f1d1dc8199e
3 files changed +21 -12
builtin/ls-files.c
+1 -1
@@ -460,7 +460,7 @@ void overlay_tree_on_cache(const char *tree_name, const char *prefix)
460 PATHSPEC_PREFER_CWD, prefix, matchbuf);
461 } else
462 memset(&pathspec, 0, sizeof(pathspec));
463 - if (read_tree(tree, 1, &pathspec))
463 + if (read_tree(tree, 1, &pathspec, &the_index))
464 die("unable to read tree entries %s", tree_name);
465
466 for (i = 0; i < active_nr; i++) {
tree.c
+18 -10
@@ -1,3 +1,4 @@
1 +#define NO_THE_INDEX_COMPATIBILITY_MACROS
2 #include "cache.h"
3 #include "cache-tree.h"
4 #include "tree.h"
@@ -8,7 +9,11 @@
9
10 const char *tree_type = "tree";
11
11 -static int read_one_entry_opt(const unsigned char *sha1, const char *base, int baselen, const char *pathname, unsigned mode, int stage, int opt)
12 +static int read_one_entry_opt(struct index_state *istate,
13 + const unsigned char *sha1,
14 + const char *base, int baselen,
15 + const char *pathname,
16 + unsigned mode, int stage, int opt)
17 {
18 int len;
19 unsigned int size;
@@ -27,14 +32,15 @@ static int read_one_entry_opt(const unsigned char *sha1, const char *base, int b
32 memcpy(ce->name, base, baselen);
33 memcpy(ce->name + baselen, pathname, len+1);
34 hashcpy(ce->oid.hash, sha1);
30 - return add_cache_entry(ce, opt);
35 + return add_index_entry(istate, ce, opt);
36 }
37
38 static int read_one_entry(const unsigned char *sha1, struct strbuf *base,
39 const char *pathname, unsigned mode, int stage,
40 void *context)
41 {
37 - return read_one_entry_opt(sha1, base->buf, base->len, pathname,
42 + struct index_state *istate = context;
43 + return read_one_entry_opt(istate, sha1, base->buf, base->len, pathname,
44 mode, stage,
45 ADD_CACHE_OK_TO_ADD|ADD_CACHE_SKIP_DFCHECK);
46 }
@@ -47,7 +53,8 @@ static int read_one_entry_quick(const unsigned char *sha1, struct strbuf *base,
53 const char *pathname, unsigned mode, int stage,
54 void *context)
55 {
50 - return read_one_entry_opt(sha1, base->buf, base->len, pathname,
56 + struct index_state *istate = context;
57 + return read_one_entry_opt(istate, sha1, base->buf, base->len, pathname,
58 mode, stage,
59 ADD_CACHE_JUST_APPEND);
60 }
@@ -144,7 +151,8 @@ static int cmp_cache_name_compare(const void *a_, const void *b_)
151 ce2->name, ce2->ce_namelen, ce_stage(ce2));
152 }
153
147 -int read_tree(struct tree *tree, int stage, struct pathspec *match)
154 +int read_tree(struct tree *tree, int stage, struct pathspec *match,
155 + struct index_state *istate)
156 {
157 read_tree_fn_t fn = NULL;
158 int i, err;
@@ -164,23 +172,23 @@ int read_tree(struct tree *tree, int stage, struct pathspec *match)
172 * do it the original slow way, otherwise, append and then
173 * sort at the end.
174 */
167 - for (i = 0; !fn && i < active_nr; i++) {
168 - const struct cache_entry *ce = active_cache[i];
175 + for (i = 0; !fn && i < istate->cache_nr; i++) {
176 + const struct cache_entry *ce = istate->cache[i];
177 if (ce_stage(ce) == stage)
178 fn = read_one_entry;
179 }
180
181 if (!fn)
182 fn = read_one_entry_quick;
175 - err = read_tree_recursive(tree, "", 0, stage, match, fn, NULL);
183 + err = read_tree_recursive(tree, "", 0, stage, match, fn, istate);
184 if (fn == read_one_entry || err)
185 return err;
186
187 /*
188 * Sort the cache entry -- we need to nuke the cache tree, though.
189 */
182 - cache_tree_free(&active_cache_tree);
183 - QSORT(active_cache, active_nr, cmp_cache_name_compare);
190 + cache_tree_free(&istate->cache_tree);
191 + QSORT(istate->cache, istate->cache_nr, cmp_cache_name_compare);
192 return 0;
193 }
194
tree.h
+2 -1
@@ -34,6 +34,7 @@ extern int read_tree_recursive(struct tree *tree,
34 int stage, const struct pathspec *pathspec,
35 read_tree_fn_t fn, void *context);
36
37 -extern int read_tree(struct tree *tree, int stage, struct pathspec *pathspec);
37 +extern int read_tree(struct tree *tree, int stage, struct pathspec *pathspec,
38 + struct index_state *istate);
39
40 #endif /* TREE_H */