diff.c: reduce implicit dependency on the_index

diff and textconv code has so widespread use that it's hard to simply update their api and all call sites at once because it would result in a big patch. For now reduce the_index references to two places: diff_setup() and fill_textconv(). Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Nguyễn Thái Ngọc Duy committed Sep 21, 2018 at 17:57 UTC b78ea5fc3574a2ce262cfb37a7ea890caddd0cf5
8 files changed +161 -112
builtin/reset.c
+1
@@ -159,6 +159,7 @@ static int read_from_tree(const struct pathspec *pathspec,
159 opt.format_callback = update_index_from_diff;
160 opt.format_callback_data = &intent_to_add;
161 opt.flags.override_submodule_config = 1;
162 + opt.repo = the_repository;
163
164 if (do_diff_cache(tree_oid, &opt))
165 return 1;
diff.c
+102 -74
@@ -554,14 +554,15 @@ static int count_lines(const char *data, int size)
554 return count;
555 }
556
557 -static int fill_mmfile(mmfile_t *mf, struct diff_filespec *one)
557 +static int fill_mmfile(struct repository *r, mmfile_t *mf,
558 + struct diff_filespec *one)
559 {
560 if (!DIFF_FILE_VALID(one)) {
561 mf->ptr = (char *)""; /* does not matter */
562 mf->size = 0;
563 return 0;
564 }
564 - else if (diff_populate_filespec(one, 0))
565 + else if (diff_populate_filespec(r, one, 0))
566 return -1;
567
568 mf->ptr = one->data;
@@ -570,11 +571,12 @@ static int fill_mmfile(mmfile_t *mf, struct diff_filespec *one)
571 }
572
573 /* like fill_mmfile, but only for size, so we can avoid retrieving blob */
573 -static unsigned long diff_filespec_size(struct diff_filespec *one)
574 +static unsigned long diff_filespec_size(struct repository *r,
575 + struct diff_filespec *one)
576 {
577 if (!DIFF_FILE_VALID(one))
578 return 0;
577 - diff_populate_filespec(one, CHECK_SIZE_ONLY);
579 + diff_populate_filespec(r, one, CHECK_SIZE_ONLY);
580 return one->size;
581 }
582
@@ -2965,18 +2967,19 @@ static void show_dirstat(struct diff_options *options)
2967 }
2968
2969 if (DIFF_FILE_VALID(p->one) && DIFF_FILE_VALID(p->two)) {
2968 - diff_populate_filespec(p->one, 0);
2969 - diff_populate_filespec(p->two, 0);
2970 - diffcore_count_changes(p->one, p->two, NULL, NULL,
2970 + diff_populate_filespec(options->repo, p->one, 0);
2971 + diff_populate_filespec(options->repo, p->two, 0);
2972 + diffcore_count_changes(options->repo,
2973 + p->one, p->two, NULL, NULL,
2974 &copied, &added);
2975 diff_free_filespec_data(p->one);
2976 diff_free_filespec_data(p->two);
2977 } else if (DIFF_FILE_VALID(p->one)) {
2975 - diff_populate_filespec(p->one, CHECK_SIZE_ONLY);
2978 + diff_populate_filespec(options->repo, p->one, CHECK_SIZE_ONLY);
2979 copied = added = 0;
2980 diff_free_filespec_data(p->one);
2981 } else if (DIFF_FILE_VALID(p->two)) {
2979 - diff_populate_filespec(p->two, CHECK_SIZE_ONLY);
2982 + diff_populate_filespec(options->repo, p->two, CHECK_SIZE_ONLY);
2983 copied = 0;
2984 added = p->two->size;
2985 diff_free_filespec_data(p->two);
@@ -3250,7 +3253,8 @@ static void emit_binary_diff(struct diff_options *o,
3253 emit_binary_diff_body(o, two, one);
3254 }
3255
3253 -int diff_filespec_is_binary(struct diff_filespec *one)
3256 +int diff_filespec_is_binary(struct repository *r,
3257 + struct diff_filespec *one)
3258 {
3259 if (one->is_binary == -1) {
3260 diff_filespec_load_driver(one);
@@ -3258,7 +3262,7 @@ int diff_filespec_is_binary(struct diff_filespec *one)
3262 one->is_binary = one->driver->binary;
3263 else {
3264 if (!one->data && DIFF_FILE_VALID(one))
3261 - diff_populate_filespec(one, CHECK_BINARY);
3265 + diff_populate_filespec(r, one, CHECK_BINARY);
3266 if (one->is_binary == -1 && one->data)
3267 one->is_binary = buffer_is_binary(one->data,
3268 one->size);
@@ -3380,13 +3384,13 @@ static void builtin_diff(const char *name_a,
3384 if ((one->mode ^ two->mode) & S_IFMT)
3385 goto free_ab_and_return;
3386 if (complete_rewrite &&
3383 - (textconv_one || !diff_filespec_is_binary(one)) &&
3384 - (textconv_two || !diff_filespec_is_binary(two))) {
3387 + (textconv_one || !diff_filespec_is_binary(o->repo, one)) &&
3388 + (textconv_two || !diff_filespec_is_binary(o->repo, two))) {
3389 emit_diff_symbol(o, DIFF_SYMBOL_HEADER,
3390 header.buf, header.len, 0);
3391 strbuf_reset(&header);
3392 emit_rewrite_diff(name_a, name_b, one, two,
3389 - textconv_one, textconv_two, o);
3393 + textconv_one, textconv_two, o);
3394 o->found_changes = 1;
3395 goto free_ab_and_return;
3396 }
@@ -3398,8 +3402,8 @@ static void builtin_diff(const char *name_a,
3402 strbuf_reset(&header);
3403 goto free_ab_and_return;
3404 } else if (!o->flags.text &&
3401 - ( (!textconv_one && diff_filespec_is_binary(one)) ||
3402 - (!textconv_two && diff_filespec_is_binary(two)) )) {
3405 + ( (!textconv_one && diff_filespec_is_binary(o->repo, one)) ||
3406 + (!textconv_two && diff_filespec_is_binary(o->repo, two)) )) {
3407 struct strbuf sb = STRBUF_INIT;
3408 if (!one->data && !two->data &&
3409 S_ISREG(one->mode) && S_ISREG(two->mode) &&
@@ -3420,7 +3424,8 @@ static void builtin_diff(const char *name_a,
3424 strbuf_release(&sb);
3425 goto free_ab_and_return;
3426 }
3423 - if (fill_mmfile(&mf1, one) < 0 || fill_mmfile(&mf2, two) < 0)
3427 + if (fill_mmfile(o->repo, &mf1, one) < 0 ||
3428 + fill_mmfile(o->repo, &mf2, two) < 0)
3429 die("unable to read files to diff");
3430 /* Quite common confusing case */
3431 if (mf1.size == mf2.size &&
@@ -3571,20 +3576,21 @@ static void builtin_diffstat(const char *name_a, const char *name_b,
3576
3577 same_contents = !oidcmp(&one->oid, &two->oid);
3578
3574 - if (diff_filespec_is_binary(one) || diff_filespec_is_binary(two)) {
3579 + if (diff_filespec_is_binary(o->repo, one) ||
3580 + diff_filespec_is_binary(o->repo, two)) {
3581 data->is_binary = 1;
3582 if (same_contents) {
3583 data->added = 0;
3584 data->deleted = 0;
3585 } else {
3580 - data->added = diff_filespec_size(two);
3581 - data->deleted = diff_filespec_size(one);
3586 + data->added = diff_filespec_size(o->repo, two);
3587 + data->deleted = diff_filespec_size(o->repo, one);
3588 }
3589 }
3590
3591 else if (complete_rewrite) {
3586 - diff_populate_filespec(one, 0);
3587 - diff_populate_filespec(two, 0);
3592 + diff_populate_filespec(o->repo, one, 0);
3593 + diff_populate_filespec(o->repo, two, 0);
3594 data->deleted = count_lines(one->data, one->size);
3595 data->added = count_lines(two->data, two->size);
3596 }
@@ -3594,7 +3600,8 @@ static void builtin_diffstat(const char *name_a, const char *name_b,
3600 xpparam_t xpp;
3601 xdemitconf_t xecfg;
3602
3597 - if (fill_mmfile(&mf1, one) < 0 || fill_mmfile(&mf2, two) < 0)
3603 + if (fill_mmfile(o->repo, &mf1, one) < 0 ||
3604 + fill_mmfile(o->repo, &mf2, two) < 0)
3605 die("unable to read files to diff");
3606
3607 memset(&xpp, 0, sizeof(xpp));
@@ -3632,7 +3639,8 @@ static void builtin_checkdiff(const char *name_a, const char *name_b,
3639 data.ws_rule = whitespace_rule(attr_path);
3640 data.conflict_marker_size = ll_merge_marker_size(attr_path);
3641
3635 - if (fill_mmfile(&mf1, one) < 0 || fill_mmfile(&mf2, two) < 0)
3642 + if (fill_mmfile(o->repo, &mf1, one) < 0 ||
3643 + fill_mmfile(o->repo, &mf2, two) < 0)
3644 die("unable to read files to diff");
3645
3646 /*
@@ -3641,7 +3649,7 @@ static void builtin_checkdiff(const char *name_a, const char *name_b,
3649 * introduced changes, and as long as the "new" side is text, we
3650 * can and should check what it introduces.
3651 */
3644 - if (diff_filespec_is_binary(two))
3652 + if (diff_filespec_is_binary(o->repo, two))
3653 goto free_and_return;
3654 else {
3655 /* Crazy xdl interfaces.. */
@@ -3714,7 +3722,10 @@ void fill_filespec(struct diff_filespec *spec, const struct object_id *oid,
3722 * the work tree has that object contents, return true, so that
3723 * prepare_temp_file() does not have to inflate and extract.
3724 */
3717 -static int reuse_worktree_file(const char *name, const struct object_id *oid, int want_file)
3725 +static int reuse_worktree_file(struct index_state *istate,
3726 + const char *name,
3727 + const struct object_id *oid,
3728 + int want_file)
3729 {
3730 const struct cache_entry *ce;
3731 struct stat st;
@@ -3733,7 +3744,7 @@ static int reuse_worktree_file(const char *name, const struct object_id *oid, in
3744 * by diff-cache --cached, which does read the cache before
3745 * calling us.
3746 */
3736 - if (!active_cache)
3747 + if (!istate->cache)
3748 return 0;
3749
3750 /* We want to avoid the working directory if our caller
@@ -3752,14 +3763,14 @@ static int reuse_worktree_file(const char *name, const struct object_id *oid, in
3763 * Similarly, if we'd have to convert the file contents anyway, that
3764 * makes the optimization not worthwhile.
3765 */
3755 - if (!want_file && would_convert_to_git(&the_index, name))
3766 + if (!want_file && would_convert_to_git(istate, name))
3767 return 0;
3768
3769 len = strlen(name);
3759 - pos = cache_name_pos(name, len);
3770 + pos = index_name_pos(istate, name, len);
3771 if (pos < 0)
3772 return 0;
3762 - ce = active_cache[pos];
3773 + ce = istate->cache[pos];
3774
3775 /*
3776 * This is not the sha1 we are looking for, or
@@ -3779,7 +3790,7 @@ static int reuse_worktree_file(const char *name, const struct object_id *oid, in
3790 * If ce matches the file in the work tree, we can reuse it.
3791 */
3792 if (ce_uptodate(ce) ||
3782 - (!lstat(name, &st) && !ce_match_stat(ce, &st, 0)))
3793 + (!lstat(name, &st) && !ie_match_stat(istate, ce, &st, 0)))
3794 return 1;
3795
3796 return 0;
@@ -3812,7 +3823,9 @@ static int diff_populate_gitlink(struct diff_filespec *s, int size_only)
3823 * grab the data for the blob (or file) for our own in-core comparison.
3824 * diff_filespec has data and size fields for this purpose.
3825 */
3815 -int diff_populate_filespec(struct diff_filespec *s, unsigned int flags)
3826 +int diff_populate_filespec(struct repository *r,
3827 + struct diff_filespec *s,
3828 + unsigned int flags)
3829 {
3830 int size_only = flags & CHECK_SIZE_ONLY;
3831 int err = 0;
@@ -3839,7 +3852,7 @@ int diff_populate_filespec(struct diff_filespec *s, unsigned int flags)
3852 return diff_populate_gitlink(s, size_only);
3853
3854 if (!s->oid_valid ||
3842 - reuse_worktree_file(s->path, &s->oid, 0)) {
3855 + reuse_worktree_file(r->index, s->path, &s->oid, 0)) {
3856 struct strbuf buf = STRBUF_INIT;
3857 struct stat st;
3858 int fd;
@@ -3872,7 +3885,7 @@ int diff_populate_filespec(struct diff_filespec *s, unsigned int flags)
3885 * point if the path requires us to run the content
3886 * conversion.
3887 */
3875 - if (size_only && !would_convert_to_git(&the_index, s->path))
3888 + if (size_only && !would_convert_to_git(r->index, s->path))
3889 return 0;
3890
3891 /*
@@ -3899,7 +3912,7 @@ int diff_populate_filespec(struct diff_filespec *s, unsigned int flags)
3912 /*
3913 * Convert from working tree format to canonical git format
3914 */
3902 - if (convert_to_git(&the_index, s->path, s->data, s->size, &buf, conv_flags)) {
3915 + if (convert_to_git(r->index, s->path, s->data, s->size, &buf, conv_flags)) {
3916 size_t size = 0;
3917 munmap(s->data, s->size);
3918 s->should_munmap = 0;
@@ -3911,8 +3924,7 @@ int diff_populate_filespec(struct diff_filespec *s, unsigned int flags)
3924 else {
3925 enum object_type type;
3926 if (size_only || (flags & CHECK_BINARY)) {
3914 - type = oid_object_info(the_repository, &s->oid,
3915 - &s->size);
3927 + type = oid_object_info(r, &s->oid, &s->size);
3928 if (type < 0)
3929 die("unable to read %s",
3930 oid_to_hex(&s->oid));
@@ -3950,7 +3962,8 @@ void diff_free_filespec_data(struct diff_filespec *s)
3962 FREE_AND_NULL(s->cnt_data);
3963 }
3964
3953 -static void prep_temp_blob(const char *path, struct diff_tempfile *temp,
3965 +static void prep_temp_blob(struct index_state *istate,
3966 + const char *path, struct diff_tempfile *temp,
3967 void *blob,
3968 unsigned long size,
3969 const struct object_id *oid,
@@ -3968,7 +3981,7 @@ static void prep_temp_blob(const char *path, struct diff_tempfile *temp,
3981 temp->tempfile = mks_tempfile_ts(tempfile.buf, strlen(base) + 1);
3982 if (!temp->tempfile)
3983 die_errno("unable to create temp-file");
3971 - if (convert_to_working_tree(&the_index, path,
3984 + if (convert_to_working_tree(istate, path,
3985 (const char *)blob, (size_t)size, &buf)) {
3986 blob = buf.buf;
3987 size = buf.len;
@@ -3984,8 +3997,9 @@ static void prep_temp_blob(const char *path, struct diff_tempfile *temp,
3997 free(path_dup);
3998 }
3999
3987 -static struct diff_tempfile *prepare_temp_file(const char *name,
3988 - struct diff_filespec *one)
4000 +static struct diff_tempfile *prepare_temp_file(struct repository *r,
4001 + const char *name,
4002 + struct diff_filespec *one)
4003 {
4004 struct diff_tempfile *temp = claim_diff_tempfile();
4005
@@ -4002,7 +4016,7 @@ static struct diff_tempfile *prepare_temp_file(const char *name,
4016
4017 if (!S_ISGITLINK(one->mode) &&
4018 (!one->oid_valid ||
4005 - reuse_worktree_file(name, &one->oid, 1))) {
4019 + reuse_worktree_file(r->index, name, &one->oid, 1))) {
4020 struct stat st;
4021 if (lstat(name, &st) < 0) {
4022 if (errno == ENOENT)
@@ -4013,7 +4027,7 @@ static struct diff_tempfile *prepare_temp_file(const char *name,
4027 struct strbuf sb = STRBUF_INIT;
4028 if (strbuf_readlink(&sb, name, st.st_size) < 0)
4029 die_errno("readlink(%s)", name);
4016 - prep_temp_blob(name, temp, sb.buf, sb.len,
4030 + prep_temp_blob(r->index, name, temp, sb.buf, sb.len,
4031 (one->oid_valid ?
4032 &one->oid : &null_oid),
4033 (one->oid_valid ?
@@ -4038,19 +4052,21 @@ static struct diff_tempfile *prepare_temp_file(const char *name,
4052 return temp;
4053 }
4054 else {
4041 - if (diff_populate_filespec(one, 0))
4055 + if (diff_populate_filespec(r, one, 0))
4056 die("cannot read data blob for %s", one->path);
4043 - prep_temp_blob(name, temp, one->data, one->size,
4057 + prep_temp_blob(r->index, name, temp,
4058 + one->data, one->size,
4059 &one->oid, one->mode);
4060 }
4061 return temp;
4062 }
4063
4049 -static void add_external_diff_name(struct argv_array *argv,
4064 +static void add_external_diff_name(struct repository *r,
4065 + struct argv_array *argv,
4066 const char *name,
4067 struct diff_filespec *df)
4068 {
4053 - struct diff_tempfile *temp = prepare_temp_file(name, df);
4069 + struct diff_tempfile *temp = prepare_temp_file(r, name, df);
4070 argv_array_push(argv, temp->name);
4071 argv_array_push(argv, temp->hex);
4072 argv_array_push(argv, temp->mode);
@@ -4079,11 +4095,11 @@ static void run_external_diff(const char *pgm,
4095 argv_array_push(&argv, name);
4096
4097 if (one && two) {
4082 - add_external_diff_name(&argv, name, one);
4098 + add_external_diff_name(o->repo, &argv, name, one);
4099 if (!other)
4084 - add_external_diff_name(&argv, name, two);
4100 + add_external_diff_name(o->repo, &argv, name, two);
4101 else {
4086 - add_external_diff_name(&argv, other, two);
4102 + add_external_diff_name(o->repo, &argv, other, two);
4103 argv_array_push(&argv, other);
4104 argv_array_push(&argv, xfrm_msg);
4105 }
@@ -4176,8 +4192,10 @@ static void fill_metainfo(struct strbuf *msg,
4192
4193 if (o->flags.binary) {
4194 mmfile_t mf;
4179 - if ((!fill_mmfile(&mf, one) && diff_filespec_is_binary(one)) ||
4180 - (!fill_mmfile(&mf, two) && diff_filespec_is_binary(two)))
4195 + if ((!fill_mmfile(o->repo, &mf, one) &&
4196 + diff_filespec_is_binary(o->repo, one)) ||
4197 + (!fill_mmfile(o->repo, &mf, two) &&
4198 + diff_filespec_is_binary(o->repo, two)))
4199 abbrev = hexsz;
4200 }
4201 strbuf_addf(msg, "%s%sindex %s..%s", line_prefix, set,
@@ -4305,7 +4323,8 @@ static void run_diff(struct diff_filepair *p, struct diff_options *o)
4323 */
4324 struct diff_filespec *null = alloc_filespec(two->path);
4325 run_diff_cmd(NULL, name, other, attr_path,
4308 - one, null, &msg, o, p);
4326 + one, null, &msg,
4327 + o, p);
4328 free(null);
4329 strbuf_release(&msg);
4330
@@ -4329,7 +4348,8 @@ static void run_diffstat(struct diff_filepair *p, struct diff_options *o,
4348
4349 if (DIFF_PAIR_UNMERGED(p)) {
4350 /* unmerged */
4332 - builtin_diffstat(p->one->path, NULL, NULL, NULL, diffstat, o, p);
4351 + builtin_diffstat(p->one->path, NULL, NULL, NULL,
4352 + diffstat, o, p);
4353 return;
4354 }
4355
@@ -4342,7 +4362,8 @@ static void run_diffstat(struct diff_filepair *p, struct diff_options *o,
4362 diff_fill_oid_info(p->one);
4363 diff_fill_oid_info(p->two);
4364
4345 - builtin_diffstat(name, other, p->one, p->two, diffstat, o, p);
4365 + builtin_diffstat(name, other, p->one, p->two,
4366 + diffstat, o, p);
4367 }
4368
4369 static void run_checkdiff(struct diff_filepair *p, struct diff_options *o)
@@ -4374,6 +4395,7 @@ void diff_setup(struct diff_options *options)
4395 memcpy(options, &default_diff_options, sizeof(*options));
4396
4397 options->file = stdout;
4398 + options->repo->index = &the_index;
4399
4400 options->abbrev = DEFAULT_ABBREV;
4401 options->line_termination = '\n';
@@ -5696,12 +5718,12 @@ static int diff_get_patch_id(struct diff_options *options, struct object_id *oid
5718 if (diff_header_only)
5719 continue;
5720
5699 - if (fill_mmfile(&mf1, p->one) < 0 ||
5700 - fill_mmfile(&mf2, p->two) < 0)
5721 + if (fill_mmfile(options->repo, &mf1, p->one) < 0 ||
5722 + fill_mmfile(options->repo, &mf2, p->two) < 0)
5723 return error("unable to read files to diff");
5724
5703 - if (diff_filespec_is_binary(p->one) ||
5704 - diff_filespec_is_binary(p->two)) {
5725 + if (diff_filespec_is_binary(options->repo, p->one) ||
5726 + diff_filespec_is_binary(options->repo, p->two)) {
5727 git_SHA1_Update(&ctx, oid_to_hex(&p->one->oid),
5728 GIT_SHA1_HEXSZ);
5729 git_SHA1_Update(&ctx, oid_to_hex(&p->two->oid),
@@ -6004,19 +6026,21 @@ static void diffcore_apply_filter(struct diff_options *options)
6026 }
6027
6028 /* Check whether two filespecs with the same mode and size are identical */
6007 -static int diff_filespec_is_identical(struct diff_filespec *one,
6029 +static int diff_filespec_is_identical(struct repository *r,
6030 + struct diff_filespec *one,
6031 struct diff_filespec *two)
6032 {
6033 if (S_ISGITLINK(one->mode))
6034 return 0;
6012 - if (diff_populate_filespec(one, 0))
6035 + if (diff_populate_filespec(r, one, 0))
6036 return 0;
6014 - if (diff_populate_filespec(two, 0))
6037 + if (diff_populate_filespec(r, two, 0))
6038 return 0;
6039 return !memcmp(one->data, two->data, one->size);
6040 }
6041
6019 -static int diff_filespec_check_stat_unmatch(struct diff_filepair *p)
6042 +static int diff_filespec_check_stat_unmatch(struct repository *r,
6043 + struct diff_filepair *p)
6044 {
6045 if (p->done_skip_stat_unmatch)
6046 return p->skip_stat_unmatch_result;
@@ -6040,10 +6064,10 @@ static int diff_filespec_check_stat_unmatch(struct diff_filepair *p)
6064 !DIFF_FILE_VALID(p->two) ||
6065 (p->one->oid_valid && p->two->oid_valid) ||
6066 (p->one->mode != p->two->mode) ||
6043 - diff_populate_filespec(p->one, CHECK_SIZE_ONLY) ||
6044 - diff_populate_filespec(p->two, CHECK_SIZE_ONLY) ||
6067 + diff_populate_filespec(r, p->one, CHECK_SIZE_ONLY) ||
6068 + diff_populate_filespec(r, p->two, CHECK_SIZE_ONLY) ||
6069 (p->one->size != p->two->size) ||
6046 - !diff_filespec_is_identical(p->one, p->two)) /* (2) */
6070 + !diff_filespec_is_identical(r, p->one, p->two)) /* (2) */
6071 p->skip_stat_unmatch_result = 1;
6072 return p->skip_stat_unmatch_result;
6073 }
@@ -6058,7 +6082,7 @@ static void diffcore_skip_stat_unmatch(struct diff_options *diffopt)
6082 for (i = 0; i < q->nr; i++) {
6083 struct diff_filepair *p = q->queue[i];
6084
6061 - if (diff_filespec_check_stat_unmatch(p))
6085 + if (diff_filespec_check_stat_unmatch(diffopt->repo, p))
6086 diff_q(&outq, p);
6087 else {
6088 /*
@@ -6100,7 +6124,8 @@ void diffcore_std(struct diff_options *options)
6124 if (!options->found_follow) {
6125 /* See try_to_follow_renames() in tree-diff.c */
6126 if (options->break_opt != -1)
6103 - diffcore_break(options->break_opt);
6127 + diffcore_break(options->repo,
6128 + options->break_opt);
6129 if (options->detect_rename)
6130 diffcore_rename(options);
6131 if (options->break_opt != -1)
@@ -6251,7 +6276,7 @@ void diff_change(struct diff_options *options,
6276 return;
6277
6278 if (options->flags.quick && options->skip_stat_unmatch &&
6254 - !diff_filespec_check_stat_unmatch(p))
6279 + !diff_filespec_check_stat_unmatch(options->repo, p))
6280 return;
6281
6282 options->flags.has_changes = 1;
@@ -6273,8 +6298,10 @@ struct diff_filepair *diff_unmerge(struct diff_options *options, const char *pat
6298 return pair;
6299 }
6300
6276 -static char *run_textconv(const char *pgm, struct diff_filespec *spec,
6277 - size_t *outsize)
6301 +static char *run_textconv(struct repository *r,
6302 + const char *pgm,
6303 + struct diff_filespec *spec,
6304 + size_t *outsize)
6305 {
6306 struct diff_tempfile *temp;
6307 const char *argv[3];
@@ -6283,7 +6310,7 @@ static char *run_textconv(const char *pgm, struct diff_filespec *spec,
6310 struct strbuf buf = STRBUF_INIT;
6311 int err = 0;
6312
6286 - temp = prepare_temp_file(spec->path, spec);
6313 + temp = prepare_temp_file(r, spec->path, spec);
6314 *arg++ = pgm;
6315 *arg++ = temp->name;
6316 *arg = NULL;
@@ -6314,6 +6341,7 @@ size_t fill_textconv(struct userdiff_driver *driver,
6341 struct diff_filespec *df,
6342 char **outbuf)
6343 {
6344 + struct repository *r = the_repository;
6345 size_t size;
6346
6347 if (!driver) {
@@ -6321,7 +6349,7 @@ size_t fill_textconv(struct userdiff_driver *driver,
6349 *outbuf = "";
6350 return 0;
6351 }
6324 - if (diff_populate_filespec(df, 0))
6352 + if (diff_populate_filespec(r, df, 0))
6353 die("unable to read files to diff");
6354 *outbuf = df->data;
6355 return df->size;
@@ -6338,7 +6366,7 @@ size_t fill_textconv(struct userdiff_driver *driver,
6366 return size;
6367 }
6368
6341 - *outbuf = run_textconv(driver->textconv, df, &size);
6369 + *outbuf = run_textconv(r, driver->textconv, df, &size);
6370 if (!*outbuf)
6371 die("unable to read files to diff");
6372
diff.h
+3
@@ -18,6 +18,7 @@ struct userdiff_driver;
18 struct oid_array;
19 struct commit;
20 struct combine_diff_path;
21 +struct repository;
22
23 typedef int (*pathchange_fn_t)(struct diff_options *options,
24 struct combine_diff_path *path);
@@ -220,6 +221,8 @@ struct diff_options {
221 /* XDF_WHITESPACE_FLAGS regarding block detection are set at 2, 3, 4 */
222 #define COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE (1<<5)
223 int color_moved_ws_handling;
224 +
225 + struct repository *repo;
226 };
227
228 void diff_emit_submodule_del(struct diff_options *o, const char *line);
diffcore-break.c
+7 -5
@@ -5,7 +5,8 @@
5 #include "diff.h"
6 #include "diffcore.h"
7
8 -static int should_break(struct diff_filespec *src,
8 +static int should_break(struct repository *r,
9 + struct diff_filespec *src,
10 struct diff_filespec *dst,
11 int break_score,
12 int *merge_score_p)
@@ -61,7 +62,8 @@ static int should_break(struct diff_filespec *src,
62 !oidcmp(&src->oid, &dst->oid))
63 return 0; /* they are the same */
64
64 - if (diff_populate_filespec(src, 0) || diff_populate_filespec(dst, 0))
65 + if (diff_populate_filespec(r, src, 0) ||
66 + diff_populate_filespec(r, dst, 0))
67 return 0; /* error but caught downstream */
68
69 max_size = ((src->size > dst->size) ? src->size : dst->size);
@@ -71,7 +73,7 @@ static int should_break(struct diff_filespec *src,
73 if (!src->size)
74 return 0; /* we do not let empty files get renamed */
75
74 - if (diffcore_count_changes(src, dst,
76 + if (diffcore_count_changes(r, src, dst,
77 &src->cnt_data, &dst->cnt_data,
78 &src_copied, &literal_added))
79 return 0;
@@ -114,7 +116,7 @@ static int should_break(struct diff_filespec *src,
116 return 1;
117 }
118
117 -void diffcore_break(int break_score)
119 +void diffcore_break(struct repository *r, int break_score)
120 {
121 struct diff_queue_struct *q = &diff_queued_diff;
122 struct diff_queue_struct outq;
@@ -178,7 +180,7 @@ void diffcore_break(int break_score)
180 object_type(p->one->mode) == OBJ_BLOB &&
181 object_type(p->two->mode) == OBJ_BLOB &&
182 !strcmp(p->one->path, p->two->path)) {
181 - if (should_break(p->one, p->two,
183 + if (should_break(r, p->one, p->two,
184 break_score, &score)) {
185 /* Split this into delete and create */
186 struct diff_filespec *null_one, *null_two;
diffcore-delta.c
+7 -5
@@ -121,14 +121,15 @@ static int spanhash_cmp(const void *a_, const void *b_)
121 a->hashval > b->hashval ? 1 : 0;
122 }
123
124 -static struct spanhash_top *hash_chars(struct diff_filespec *one)
124 +static struct spanhash_top *hash_chars(struct repository *r,
125 + struct diff_filespec *one)
126 {
127 int i, n;
128 unsigned int accum1, accum2, hashval;
129 struct spanhash_top *hash;
130 unsigned char *buf = one->data;
131 unsigned int sz = one->size;
131 - int is_text = !diff_filespec_is_binary(one);
132 + int is_text = !diff_filespec_is_binary(r, one);
133
134 i = INITIAL_HASH_SIZE;
135 hash = xmalloc(st_add(sizeof(*hash),
@@ -162,7 +163,8 @@ static struct spanhash_top *hash_chars(struct diff_filespec *one)
163 return hash;
164 }
165
165 -int diffcore_count_changes(struct diff_filespec *src,
166 +int diffcore_count_changes(struct repository *r,
167 + struct diff_filespec *src,
168 struct diff_filespec *dst,
169 void **src_count_p,
170 void **dst_count_p,
@@ -177,14 +179,14 @@ int diffcore_count_changes(struct diff_filespec *src,
179 if (src_count_p)
180 src_count = *src_count_p;
181 if (!src_count) {
180 - src_count = hash_chars(src);
182 + src_count = hash_chars(r, src);
183 if (src_count_p)
184 *src_count_p = src_count;
185 }
186 if (dst_count_p)
187 dst_count = *dst_count_p;
188 if (!dst_count) {
187 - dst_count = hash_chars(dst);
189 + dst_count = hash_chars(r, dst);
190 if (dst_count_p)
191 *dst_count_p = dst_count;
192 }
diffcore-rename.c
+22 -13
@@ -128,7 +128,8 @@ struct diff_score {
128 short name_score;
129 };
130
131 -static int estimate_similarity(struct diff_filespec *src,
131 +static int estimate_similarity(struct repository *r,
132 + struct diff_filespec *src,
133 struct diff_filespec *dst,
134 int minimum_score)
135 {
@@ -165,10 +166,10 @@ static int estimate_similarity(struct diff_filespec *src,
166 * say whether the size is valid or not!)
167 */
168 if (!src->cnt_data &&
168 - diff_populate_filespec(src, CHECK_SIZE_ONLY))
169 + diff_populate_filespec(r, src, CHECK_SIZE_ONLY))
170 return 0;
171 if (!dst->cnt_data &&
171 - diff_populate_filespec(dst, CHECK_SIZE_ONLY))
172 + diff_populate_filespec(r, dst, CHECK_SIZE_ONLY))
173 return 0;
174
175 max_size = ((src->size > dst->size) ? src->size : dst->size);
@@ -186,12 +187,12 @@ static int estimate_similarity(struct diff_filespec *src,
187 if (max_size * (MAX_SCORE-minimum_score) < delta_size * MAX_SCORE)
188 return 0;
189
189 - if (!src->cnt_data && diff_populate_filespec(src, 0))
190 + if (!src->cnt_data && diff_populate_filespec(r, src, 0))
191 return 0;
191 - if (!dst->cnt_data && diff_populate_filespec(dst, 0))
192 + if (!dst->cnt_data && diff_populate_filespec(r, dst, 0))
193 return 0;
194
194 - if (diffcore_count_changes(src, dst,
195 + if (diffcore_count_changes(r, src, dst,
196 &src->cnt_data, &dst->cnt_data,
197 &src_copied, &literal_added))
198 return 0;
@@ -256,10 +257,11 @@ struct file_similarity {
257 struct diff_filespec *filespec;
258 };
259
259 -static unsigned int hash_filespec(struct diff_filespec *filespec)
260 +static unsigned int hash_filespec(struct repository *r,
261 + struct diff_filespec *filespec)
262 {
263 if (!filespec->oid_valid) {
262 - if (diff_populate_filespec(filespec, 0))
264 + if (diff_populate_filespec(r, filespec, 0))
265 return 0;
266 hash_object_file(filespec->data, filespec->size, "blob",
267 &filespec->oid);
@@ -280,7 +282,9 @@ static int find_identical_files(struct hashmap *srcs,
282 /*
283 * Find the best source match for specified destination.
284 */
283 - p = hashmap_get_from_hash(srcs, hash_filespec(target), NULL);
285 + p = hashmap_get_from_hash(srcs,
286 + hash_filespec(options->repo, target),
287 + NULL);
288 for (; p; p = hashmap_get_next(srcs, p)) {
289 int score;
290 struct diff_filespec *source = p->filespec;
@@ -316,14 +320,16 @@ static int find_identical_files(struct hashmap *srcs,
320 return renames;
321 }
322
319 -static void insert_file_table(struct hashmap *table, int index, struct diff_filespec *filespec)
323 +static void insert_file_table(struct repository *r,
324 + struct hashmap *table, int index,
325 + struct diff_filespec *filespec)
326 {
327 struct file_similarity *entry = xmalloc(sizeof(*entry));
328
329 entry->index = index;
330 entry->filespec = filespec;
331
326 - hashmap_entry_init(entry, hash_filespec(filespec));
332 + hashmap_entry_init(entry, hash_filespec(r, filespec));
333 hashmap_add(table, entry);
334 }
335
@@ -344,7 +350,9 @@ static int find_exact_renames(struct diff_options *options)
350 */
351 hashmap_init(&file_table, NULL, NULL, rename_src_nr);
352 for (i = rename_src_nr-1; i >= 0; i--)
347 - insert_file_table(&file_table, i, rename_src[i].p->one);
353 + insert_file_table(options->repo,
354 + &file_table, i,
355 + rename_src[i].p->one);
356
357 /* Walk the destinations and find best source match */
358 for (i = 0; i < rename_dst_nr; i++)
@@ -557,7 +565,8 @@ void diffcore_rename(struct diff_options *options)
565 diff_unmodified_pair(rename_src[j].p))
566 continue;
567
560 - this_src.score = estimate_similarity(one, two,
568 + this_src.score = estimate_similarity(options->repo,
569 + one, two,
570 minimum_score);
571 this_src.name_score = basename_same(one, two);
572 this_src.dst = i;
diffcore.h
+7 -6
@@ -7,6 +7,8 @@
7 #include "cache.h"
8
9 struct diff_options;
10 +struct repository;
11 +struct userdiff_driver;
12
13 /* This header file is internal between diff.c and its diff transformers
14 * (e.g. diffcore-rename, diffcore-pickaxe). Never include this header
@@ -26,8 +28,6 @@ struct diff_options;
28
29 #define MINIMUM_BREAK_SIZE 400 /* do not break a file smaller than this */
30
29 -struct userdiff_driver;
30 -
31 struct diff_filespec {
32 struct object_id oid;
33 char *path;
@@ -61,10 +61,10 @@ void fill_filespec(struct diff_filespec *, const struct object_id *,
61
62 #define CHECK_SIZE_ONLY 1
63 #define CHECK_BINARY 2
64 -int diff_populate_filespec(struct diff_filespec *, unsigned int);
64 +int diff_populate_filespec(struct repository *, struct diff_filespec *, unsigned int);
65 void diff_free_filespec_data(struct diff_filespec *);
66 void diff_free_filespec_blob(struct diff_filespec *);
67 -int diff_filespec_is_binary(struct diff_filespec *);
67 +int diff_filespec_is_binary(struct repository *, struct diff_filespec *);
68
69 struct diff_filepair {
70 struct diff_filespec *one;
@@ -111,7 +111,7 @@ struct diff_filepair *diff_queue(struct diff_queue_struct *,
111 struct diff_filespec *);
112 void diff_q(struct diff_queue_struct *, struct diff_filepair *);
113
114 -void diffcore_break(int);
114 +void diffcore_break(struct repository *, int);
115 void diffcore_rename(struct diff_options *);
116 void diffcore_merge_broken(void);
117 void diffcore_pickaxe(struct diff_options *);
@@ -142,7 +142,8 @@ void diff_debug_queue(const char *, struct diff_queue_struct *);
142 #define diff_debug_queue(a,b) do { /* nothing */ } while (0)
143 #endif
144
145 -int diffcore_count_changes(struct diff_filespec *src,
145 +int diffcore_count_changes(struct repository *r,
146 + struct diff_filespec *src,
147 struct diff_filespec *dst,
148 void **src_count_p,
149 void **dst_count_p,
line-log.c
+12 -9
@@ -508,7 +508,9 @@ static void fill_blob_sha1(struct commit *commit, struct diff_filespec *spec)
508 return;
509 }
510
511 -static void fill_line_ends(struct diff_filespec *spec, long *lines,
511 +static void fill_line_ends(struct repository *r,
512 + struct diff_filespec *spec,
513 + long *lines,
514 unsigned long **line_ends)
515 {
516 int num = 0, size = 50;
@@ -516,7 +518,7 @@ static void fill_line_ends(struct diff_filespec *spec, long *lines,
518 unsigned long *ends = NULL;
519 char *data = NULL;
520
519 - if (diff_populate_filespec(spec, 0))
521 + if (diff_populate_filespec(r, spec, 0))
522 die("Cannot read blob %s", oid_to_hex(&spec->oid));
523
524 ALLOC_ARRAY(ends, size);
@@ -555,7 +557,8 @@ static const char *nth_line(void *data, long line)
557 }
558
559 static struct line_log_data *
558 -parse_lines(struct commit *commit, const char *prefix, struct string_list *args)
560 +parse_lines(struct repository *r, struct commit *commit,
561 + const char *prefix, struct string_list *args)
562 {
563 long lines = 0;
564 unsigned long *ends = NULL;
@@ -583,7 +586,7 @@ parse_lines(struct commit *commit, const char *prefix, struct string_list *args)
586
587 spec = alloc_filespec(full_name);
588 fill_blob_sha1(commit, spec);
586 - fill_line_ends(spec, &lines, &ends);
589 + fill_line_ends(r, spec, &lines, &ends);
590 cb_data.spec = spec;
591 cb_data.lines = lines;
592 cb_data.line_ends = ends;
@@ -739,7 +742,7 @@ void line_log_init(struct rev_info *rev, const char *prefix, struct string_list
742 struct line_log_data *range;
743
744 commit = check_single_commit(rev);
742 - range = parse_lines(commit, prefix, args);
745 + range = parse_lines(rev->diffopt.repo, commit, prefix, args);
746 add_line_range(rev, commit, range);
747
748 if (!rev->diffopt.detect_rename) {
@@ -891,8 +894,8 @@ static void dump_diff_hacky_one(struct rev_info *rev, struct line_log_data *rang
894 return;
895
896 if (pair->one->oid_valid)
894 - fill_line_ends(pair->one, &p_lines, &p_ends);
895 - fill_line_ends(pair->two, &t_lines, &t_ends);
897 + fill_line_ends(rev->diffopt.repo, pair->one, &p_lines, &p_ends);
898 + fill_line_ends(rev->diffopt.repo, pair->two, &t_lines, &t_ends);
899
900 fprintf(opt->file, "%s%sdiff --git a/%s b/%s%s\n", prefix, c_meta, pair->one->path, pair->two->path, c_reset);
901 fprintf(opt->file, "%s%s--- %s%s%s\n", prefix, c_meta,
@@ -1008,12 +1011,12 @@ static int process_diff_filepair(struct rev_info *rev,
1011 return 0;
1012
1013 assert(pair->two->oid_valid);
1011 - diff_populate_filespec(pair->two, 0);
1014 + diff_populate_filespec(rev->diffopt.repo, pair->two, 0);
1015 file_target.ptr = pair->two->data;
1016 file_target.size = pair->two->size;
1017
1018 if (pair->one->oid_valid) {
1016 - diff_populate_filespec(pair->one, 0);
1019 + diff_populate_filespec(rev->diffopt.repo, pair->one, 0);
1020 file_parent.ptr = pair->one->data;
1021 file_parent.size = pair->one->size;
1022 } else {