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>
This commit is contained in:
Brandon Williams 2017-06-12 15:13:57 -07:00 committed by Junio C Hamano
parent a33e0b2a77
commit 85ab50f938
3 changed files with 21 additions and 12 deletions

View File

@ -460,7 +460,7 @@ void overlay_tree_on_cache(const char *tree_name, const char *prefix)
PATHSPEC_PREFER_CWD, prefix, matchbuf);
} else
memset(&pathspec, 0, sizeof(pathspec));
if (read_tree(tree, 1, &pathspec))
if (read_tree(tree, 1, &pathspec, &the_index))
die("unable to read tree entries %s", tree_name);
for (i = 0; i < active_nr; i++) {

28
tree.c
View File

@ -1,3 +1,4 @@
#define NO_THE_INDEX_COMPATIBILITY_MACROS
#include "cache.h"
#include "cache-tree.h"
#include "tree.h"
@ -8,7 +9,11 @@
const char *tree_type = "tree";
static int read_one_entry_opt(const unsigned char *sha1, const char *base, int baselen, const char *pathname, unsigned mode, int stage, int opt)
static int read_one_entry_opt(struct index_state *istate,
const unsigned char *sha1,
const char *base, int baselen,
const char *pathname,
unsigned mode, int stage, int opt)
{
int len;
unsigned int size;
@ -27,14 +32,15 @@ static int read_one_entry_opt(const unsigned char *sha1, const char *base, int b
memcpy(ce->name, base, baselen);
memcpy(ce->name + baselen, pathname, len+1);
hashcpy(ce->oid.hash, sha1);
return add_cache_entry(ce, opt);
return add_index_entry(istate, ce, opt);
}
static int read_one_entry(const unsigned char *sha1, struct strbuf *base,
const char *pathname, unsigned mode, int stage,
void *context)
{
return read_one_entry_opt(sha1, base->buf, base->len, pathname,
struct index_state *istate = context;
return read_one_entry_opt(istate, sha1, base->buf, base->len, pathname,
mode, stage,
ADD_CACHE_OK_TO_ADD|ADD_CACHE_SKIP_DFCHECK);
}
@ -47,7 +53,8 @@ static int read_one_entry_quick(const unsigned char *sha1, struct strbuf *base,
const char *pathname, unsigned mode, int stage,
void *context)
{
return read_one_entry_opt(sha1, base->buf, base->len, pathname,
struct index_state *istate = context;
return read_one_entry_opt(istate, sha1, base->buf, base->len, pathname,
mode, stage,
ADD_CACHE_JUST_APPEND);
}
@ -144,7 +151,8 @@ static int cmp_cache_name_compare(const void *a_, const void *b_)
ce2->name, ce2->ce_namelen, ce_stage(ce2));
}
int read_tree(struct tree *tree, int stage, struct pathspec *match)
int read_tree(struct tree *tree, int stage, struct pathspec *match,
struct index_state *istate)
{
read_tree_fn_t fn = NULL;
int i, err;
@ -164,23 +172,23 @@ int read_tree(struct tree *tree, int stage, struct pathspec *match)
* do it the original slow way, otherwise, append and then
* sort at the end.
*/
for (i = 0; !fn && i < active_nr; i++) {
const struct cache_entry *ce = active_cache[i];
for (i = 0; !fn && i < istate->cache_nr; i++) {
const struct cache_entry *ce = istate->cache[i];
if (ce_stage(ce) == stage)
fn = read_one_entry;
}
if (!fn)
fn = read_one_entry_quick;
err = read_tree_recursive(tree, "", 0, stage, match, fn, NULL);
err = read_tree_recursive(tree, "", 0, stage, match, fn, istate);
if (fn == read_one_entry || err)
return err;
/*
* Sort the cache entry -- we need to nuke the cache tree, though.
*/
cache_tree_free(&active_cache_tree);
QSORT(active_cache, active_nr, cmp_cache_name_compare);
cache_tree_free(&istate->cache_tree);
QSORT(istate->cache, istate->cache_nr, cmp_cache_name_compare);
return 0;
}

3
tree.h
View File

@ -34,6 +34,7 @@ extern int read_tree_recursive(struct tree *tree,
int stage, const struct pathspec *pathspec,
read_tree_fn_t fn, void *context);
extern int read_tree(struct tree *tree, int stage, struct pathspec *pathspec);
extern int read_tree(struct tree *tree, int stage, struct pathspec *pathspec,
struct index_state *istate);
#endif /* TREE_H */