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>
Junio C Hamano committed
Jul 13, 2011 at 18:57 UTC
bf979c07c728b9d4f2691a023b3f358610808789
1 file changed
+19
-28
diff-lib.c
+19
-28
@@ -440,20 +440,19 @@ static int oneway_diff(struct cache_entry **src, struct unpack_trees_options *o)
440
return 0;
441
}
442
443
-int run_diff_index(struct rev_info *revs, int cached)
443
+static int diff_cache(struct rev_info *revs,
444
+ const unsigned char *tree_sha1,
445
+ const char *tree_name,
446
+ int cached)
447
{
445
- struct object *ent;
448
struct tree *tree;
447
- const char *tree_name;
448
- struct unpack_trees_options opts;
449
struct tree_desc t;
450
+ struct unpack_trees_options opts;
451
451
- ent = revs->pending.objects[0].item;
452
- tree_name = revs->pending.objects[0].name;
453
- tree = parse_tree_indirect(ent->sha1);
452
+ tree = parse_tree_indirect(tree_sha1);
453
if (!tree)
455
- return error("bad tree object %s", tree_name);
456
-
454
+ return error("bad tree object %s",
455
+ tree_name ? tree_name : sha1_to_hex(tree_sha1));
456
memset(&opts, 0, sizeof(opts));
457
opts.head_idx = 1;
458
opts.index_only = cached;
@@ -466,7 +465,15 @@ int run_diff_index(struct rev_info *revs, int cached)
465
opts.dst_index = NULL;
466
467
init_tree_desc(&t, tree->buffer, tree->size);
469
- if (unpack_trees(1, &t, &opts))
468
+ return unpack_trees(1, &t, &opts);
469
+}
470
+
471
+int run_diff_index(struct rev_info *revs, int cached)
472
+{
473
+ struct object_array_entry *ent;
474
+
475
+ ent = revs->pending.objects;
476
+ if (diff_cache(revs, ent->item->sha1, ent->name, cached))
477
exit(128);
478
479
diff_set_mnemonic_prefix(&revs->diffopt, "c/", cached ? "i/" : "w/");
@@ -478,29 +485,13 @@ int run_diff_index(struct rev_info *revs, int cached)
485
486
int do_diff_cache(const unsigned char *tree_sha1, struct diff_options *opt)
487
{
481
- struct tree *tree;
488
struct rev_info revs;
483
- struct unpack_trees_options opts;
484
- struct tree_desc t;
489
490
init_revisions(&revs, NULL);
491
init_pathspec(&revs.prune_data, opt->pathspec.raw);
488
- tree = parse_tree_indirect(tree_sha1);
489
- if (!tree)
490
- die("bad tree object %s", sha1_to_hex(tree_sha1));
492
+ revs.diffopt = *opt;
493
492
- memset(&opts, 0, sizeof(opts));
493
- opts.head_idx = 1;
494
- opts.index_only = 1;
495
- opts.diff_index_cached = !DIFF_OPT_TST(opt, FIND_COPIES_HARDER);
496
- opts.merge = 1;
497
- opts.fn = oneway_diff;
498
- opts.unpack_data = &revs;
499
- opts.src_index = &the_index;
500
- opts.dst_index = NULL;
501
-
502
- init_tree_desc(&t, tree->buffer, tree->size);
503
- if (unpack_trees(1, &t, &opts))
494
+ if (diff_cache(&revs, tree_sha1, NULL, 1))
495
exit(128);
496
return 0;
497
}