diff-lib: refactor run_diff_index() and do_diff_cache()
The latter is meant to be an API for internal callers that want to inspect the resulting diff-queue, while the former is an implementation of "git diff-index" command. Extract the common logic into a single helper function and make them thin wrappers around it. Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
47
diff-lib.c
47
diff-lib.c
@ -440,20 +440,19 @@ static int oneway_diff(struct cache_entry **src, struct unpack_trees_options *o)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int run_diff_index(struct rev_info *revs, int cached)
|
static int diff_cache(struct rev_info *revs,
|
||||||
|
const unsigned char *tree_sha1,
|
||||||
|
const char *tree_name,
|
||||||
|
int cached)
|
||||||
{
|
{
|
||||||
struct object *ent;
|
|
||||||
struct tree *tree;
|
struct tree *tree;
|
||||||
const char *tree_name;
|
|
||||||
struct unpack_trees_options opts;
|
|
||||||
struct tree_desc t;
|
struct tree_desc t;
|
||||||
|
struct unpack_trees_options opts;
|
||||||
|
|
||||||
ent = revs->pending.objects[0].item;
|
tree = parse_tree_indirect(tree_sha1);
|
||||||
tree_name = revs->pending.objects[0].name;
|
|
||||||
tree = parse_tree_indirect(ent->sha1);
|
|
||||||
if (!tree)
|
if (!tree)
|
||||||
return error("bad tree object %s", tree_name);
|
return error("bad tree object %s",
|
||||||
|
tree_name ? tree_name : sha1_to_hex(tree_sha1));
|
||||||
memset(&opts, 0, sizeof(opts));
|
memset(&opts, 0, sizeof(opts));
|
||||||
opts.head_idx = 1;
|
opts.head_idx = 1;
|
||||||
opts.index_only = cached;
|
opts.index_only = cached;
|
||||||
@ -466,7 +465,15 @@ int run_diff_index(struct rev_info *revs, int cached)
|
|||||||
opts.dst_index = NULL;
|
opts.dst_index = NULL;
|
||||||
|
|
||||||
init_tree_desc(&t, tree->buffer, tree->size);
|
init_tree_desc(&t, tree->buffer, tree->size);
|
||||||
if (unpack_trees(1, &t, &opts))
|
return unpack_trees(1, &t, &opts);
|
||||||
|
}
|
||||||
|
|
||||||
|
int run_diff_index(struct rev_info *revs, int cached)
|
||||||
|
{
|
||||||
|
struct object_array_entry *ent;
|
||||||
|
|
||||||
|
ent = revs->pending.objects;
|
||||||
|
if (diff_cache(revs, ent->item->sha1, ent->name, cached))
|
||||||
exit(128);
|
exit(128);
|
||||||
|
|
||||||
diff_set_mnemonic_prefix(&revs->diffopt, "c/", cached ? "i/" : "w/");
|
diff_set_mnemonic_prefix(&revs->diffopt, "c/", cached ? "i/" : "w/");
|
||||||
@ -478,29 +485,13 @@ int run_diff_index(struct rev_info *revs, int cached)
|
|||||||
|
|
||||||
int do_diff_cache(const unsigned char *tree_sha1, struct diff_options *opt)
|
int do_diff_cache(const unsigned char *tree_sha1, struct diff_options *opt)
|
||||||
{
|
{
|
||||||
struct tree *tree;
|
|
||||||
struct rev_info revs;
|
struct rev_info revs;
|
||||||
struct unpack_trees_options opts;
|
|
||||||
struct tree_desc t;
|
|
||||||
|
|
||||||
init_revisions(&revs, NULL);
|
init_revisions(&revs, NULL);
|
||||||
init_pathspec(&revs.prune_data, opt->pathspec.raw);
|
init_pathspec(&revs.prune_data, opt->pathspec.raw);
|
||||||
tree = parse_tree_indirect(tree_sha1);
|
revs.diffopt = *opt;
|
||||||
if (!tree)
|
|
||||||
die("bad tree object %s", sha1_to_hex(tree_sha1));
|
|
||||||
|
|
||||||
memset(&opts, 0, sizeof(opts));
|
if (diff_cache(&revs, tree_sha1, NULL, 1))
|
||||||
opts.head_idx = 1;
|
|
||||||
opts.index_only = 1;
|
|
||||||
opts.diff_index_cached = !DIFF_OPT_TST(opt, FIND_COPIES_HARDER);
|
|
||||||
opts.merge = 1;
|
|
||||||
opts.fn = oneway_diff;
|
|
||||||
opts.unpack_data = &revs;
|
|
||||||
opts.src_index = &the_index;
|
|
||||||
opts.dst_index = NULL;
|
|
||||||
|
|
||||||
init_tree_desc(&t, tree->buffer, tree->size);
|
|
||||||
if (unpack_trees(1, &t, &opts))
|
|
||||||
exit(128);
|
exit(128);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user