userdiff.c: remove implicit dependency on the_index

[jc: squashed in missing forward decl in userdiff.h found by Ramsay] Helped-by: Ramsay Jones <ramsay@ramsayjones.plus.com> 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 acd00ea04998ce469d1775c658134b097e18f5a3
11 files changed +61 -41
archive-zip.c
+9 -5
@@ -264,9 +264,10 @@ static int has_only_ascii(const char *s)
264 }
265 }
266
267 -static int entry_is_binary(const char *path, const void *buffer, size_t size)
267 +static int entry_is_binary(struct index_state *istate, const char *path,
268 + const void *buffer, size_t size)
269 {
269 - struct userdiff_driver *driver = userdiff_find_by_path(path);
270 + struct userdiff_driver *driver = userdiff_find_by_path(istate, path);
271 if (!driver)
272 driver = userdiff_find_by_name("default");
273 if (driver->binary != -1)
@@ -352,7 +353,8 @@ static int write_zip_entry(struct archiver_args *args,
353 return error(_("cannot read %s"),
354 oid_to_hex(oid));
355 crc = crc32(crc, buffer, size);
355 - is_binary = entry_is_binary(path_without_prefix,
356 + is_binary = entry_is_binary(args->repo->index,
357 + path_without_prefix,
358 buffer, size);
359 out = buffer;
360 }
@@ -428,7 +430,8 @@ static int write_zip_entry(struct archiver_args *args,
430 break;
431 crc = crc32(crc, buf, readlen);
432 if (is_binary == -1)
431 - is_binary = entry_is_binary(path_without_prefix,
433 + is_binary = entry_is_binary(args->repo->index,
434 + path_without_prefix,
435 buf, readlen);
436 write_or_die(1, buf, readlen);
437 }
@@ -460,7 +463,8 @@ static int write_zip_entry(struct archiver_args *args,
463 break;
464 crc = crc32(crc, buf, readlen);
465 if (is_binary == -1)
463 - is_binary = entry_is_binary(path_without_prefix,
466 + is_binary = entry_is_binary(args->repo->index,
467 + path_without_prefix,
468 buf, readlen);
469
470 zstream.next_in = buf;
builtin/grep.c
+2 -1
@@ -103,7 +103,8 @@ static void add_work(struct grep_opt *opt, const struct grep_source *gs)
103
104 todo[todo_end].source = *gs;
105 if (opt->binary != GREP_BINARY_TEXT)
106 - grep_source_load_driver(&todo[todo_end].source);
106 + grep_source_load_driver(&todo[todo_end].source,
107 + opt->repo->index);
108 todo[todo_end].done = 0;
109 strbuf_reset(&todo[todo_end].out);
110 todo_end = (todo_end + 1) % ARRAY_SIZE(todo);
combine-diff.c
+1 -1
@@ -987,7 +987,7 @@ static void show_patch_diff(struct combine_diff_path *elem, int num_parent,
987 const char *line_prefix = diff_line_prefix(opt);
988
989 context = opt->context;
990 - userdiff = userdiff_find_by_path(elem->path);
990 + userdiff = userdiff_find_by_path(opt->repo->index, elem->path);
991 if (!userdiff)
992 userdiff = userdiff_find_by_name("default");
993 if (opt->flags.allow_textconv)
diff.c
+23 -17
@@ -2093,23 +2093,25 @@ static void diff_words_flush(struct emit_callback *ecbdata)
2093 }
2094 }
2095
2096 -static void diff_filespec_load_driver(struct diff_filespec *one)
2096 +static void diff_filespec_load_driver(struct diff_filespec *one,
2097 + struct index_state *istate)
2098 {
2099 /* Use already-loaded driver */
2100 if (one->driver)
2101 return;
2102
2103 if (S_ISREG(one->mode))
2103 - one->driver = userdiff_find_by_path(one->path);
2104 + one->driver = userdiff_find_by_path(istate, one->path);
2105
2106 /* Fallback to default settings */
2107 if (!one->driver)
2108 one->driver = userdiff_find_by_name("default");
2109 }
2110
2110 -static const char *userdiff_word_regex(struct diff_filespec *one)
2111 +static const char *userdiff_word_regex(struct diff_filespec *one,
2112 + struct index_state *istate)
2113 {
2112 - diff_filespec_load_driver(one);
2114 + diff_filespec_load_driver(one, istate);
2115 return one->driver->word_regex;
2116 }
2117
@@ -2132,9 +2134,9 @@ static void init_diff_words_data(struct emit_callback *ecbdata,
2134 xcalloc(1, sizeof(struct emitted_diff_symbols));
2135
2136 if (!o->word_regex)
2135 - o->word_regex = userdiff_word_regex(one);
2137 + o->word_regex = userdiff_word_regex(one, o->repo->index);
2138 if (!o->word_regex)
2137 - o->word_regex = userdiff_word_regex(two);
2139 + o->word_regex = userdiff_word_regex(two, o->repo->index);
2140 if (!o->word_regex)
2141 o->word_regex = diff_word_regex_cfg;
2142 if (o->word_regex) {
@@ -3257,7 +3259,7 @@ int diff_filespec_is_binary(struct repository *r,
3259 struct diff_filespec *one)
3260 {
3261 if (one->is_binary == -1) {
3260 - diff_filespec_load_driver(one);
3262 + diff_filespec_load_driver(one, r->index);
3263 if (one->driver->binary != -1)
3264 one->is_binary = one->driver->binary;
3265 else {
@@ -3273,9 +3275,10 @@ int diff_filespec_is_binary(struct repository *r,
3275 return one->is_binary;
3276 }
3277
3276 -static const struct userdiff_funcname *diff_funcname_pattern(struct diff_filespec *one)
3278 +static const struct userdiff_funcname *
3279 +diff_funcname_pattern(struct diff_options *o, struct diff_filespec *one)
3280 {
3278 - diff_filespec_load_driver(one);
3281 + diff_filespec_load_driver(one, o->repo->index);
3282 return one->driver->funcname.pattern ? &one->driver->funcname : NULL;
3283 }
3284
@@ -3287,12 +3290,13 @@ void diff_set_mnemonic_prefix(struct diff_options *options, const char *a, const
3290 options->b_prefix = b;
3291 }
3292
3290 -struct userdiff_driver *get_textconv(struct diff_filespec *one)
3293 +struct userdiff_driver *get_textconv(struct index_state *istate,
3294 + struct diff_filespec *one)
3295 {
3296 if (!DIFF_FILE_VALID(one))
3297 return NULL;
3298
3295 - diff_filespec_load_driver(one);
3299 + diff_filespec_load_driver(one, istate);
3300 return userdiff_get_textconv(one->driver);
3301 }
3302
@@ -3342,8 +3346,8 @@ static void builtin_diff(const char *name_a,
3346 }
3347
3348 if (o->flags.allow_textconv) {
3345 - textconv_one = get_textconv(one);
3346 - textconv_two = get_textconv(two);
3349 + textconv_one = get_textconv(o->repo->index, one);
3350 + textconv_two = get_textconv(o->repo->index, two);
3351 }
3352
3353 /* Never use a non-valid filename anywhere if at all possible */
@@ -3465,9 +3469,9 @@ static void builtin_diff(const char *name_a,
3469 mf1.size = fill_textconv(o->repo, textconv_one, one, &mf1.ptr);
3470 mf2.size = fill_textconv(o->repo, textconv_two, two, &mf2.ptr);
3471
3468 - pe = diff_funcname_pattern(one);
3472 + pe = diff_funcname_pattern(o, one);
3473 if (!pe)
3470 - pe = diff_funcname_pattern(two);
3474 + pe = diff_funcname_pattern(o, two);
3475
3476 memset(&xpp, 0, sizeof(xpp));
3477 memset(&xecfg, 0, sizeof(xecfg));
@@ -4223,7 +4227,9 @@ static void run_diff_cmd(const char *pgm,
4227
4228
4229 if (o->flags.allow_external) {
4226 - struct userdiff_driver *drv = userdiff_find_by_path(attr_path);
4230 + struct userdiff_driver *drv;
4231 +
4232 + drv = userdiff_find_by_path(o->repo->index, attr_path);
4233 if (drv && drv->external)
4234 pgm = drv->external;
4235 }
@@ -6399,7 +6405,7 @@ int textconv_object(struct repository *r,
6405
6406 df = alloc_filespec(path);
6407 fill_filespec(df, oid, oid_valid, mode);
6402 - textconv = get_textconv(df);
6408 + textconv = get_textconv(r->index, df);
6409 if (!textconv) {
6410 free_filespec(df);
6411 return 0;
diff.h
+2 -1
@@ -455,7 +455,8 @@ size_t fill_textconv(struct repository *r,
455 * and only if it has textconv enabled (otherwise return NULL). The result
456 * can be passed to fill_textconv().
457 */
458 -struct userdiff_driver *get_textconv(struct diff_filespec *one);
458 +struct userdiff_driver *get_textconv(struct index_state *istate,
459 + struct diff_filespec *one);
460
461 /*
462 * Prepare diff_filespec and convert it using diff textconv API
diffcore-pickaxe.c
+2 -2
@@ -139,8 +139,8 @@ static int pickaxe_match(struct diff_filepair *p, struct diff_options *o,
139 return 0;
140
141 if (o->flags.allow_textconv) {
142 - textconv_one = get_textconv(p->one);
143 - textconv_two = get_textconv(p->two);
142 + textconv_one = get_textconv(o->repo->index, p->one);
143 + textconv_two = get_textconv(o->repo->index, p->two);
144 }
145
146 /*
grep.c
+12 -9
@@ -11,7 +11,8 @@
11 #include "help.h"
12
13 static int grep_source_load(struct grep_source *gs);
14 -static int grep_source_is_binary(struct grep_source *gs);
14 +static int grep_source_is_binary(struct grep_source *gs,
15 + struct index_state *istate);
16
17 static struct grep_opt grep_defaults;
18
@@ -1547,7 +1548,7 @@ static int match_funcname(struct grep_opt *opt, struct grep_source *gs, char *bo
1548 {
1549 xdemitconf_t *xecfg = opt->priv;
1550 if (xecfg && !xecfg->find_func) {
1550 - grep_source_load_driver(gs);
1551 + grep_source_load_driver(gs, opt->repo->index);
1552 if (gs->driver->funcname.pattern) {
1553 const struct userdiff_funcname *pe = &gs->driver->funcname;
1554 xdiff_set_find_func(xecfg, pe->pattern, pe->cflags);
@@ -1804,7 +1805,7 @@ static int grep_source_1(struct grep_opt *opt, struct grep_source *gs, int colle
1805 opt->last_shown = 0;
1806
1807 if (opt->allow_textconv) {
1807 - grep_source_load_driver(gs);
1808 + grep_source_load_driver(gs, opt->repo->index);
1809 /*
1810 * We might set up the shared textconv cache data here, which
1811 * is not thread-safe.
@@ -1821,11 +1822,11 @@ static int grep_source_1(struct grep_opt *opt, struct grep_source *gs, int colle
1822 if (!textconv) {
1823 switch (opt->binary) {
1824 case GREP_BINARY_DEFAULT:
1824 - if (grep_source_is_binary(gs))
1825 + if (grep_source_is_binary(gs, opt->repo->index))
1826 binary_match_only = 1;
1827 break;
1828 case GREP_BINARY_NOMATCH:
1828 - if (grep_source_is_binary(gs))
1829 + if (grep_source_is_binary(gs, opt->repo->index))
1830 return 0; /* Assume unmatch */
1831 break;
1832 case GREP_BINARY_TEXT:
@@ -2171,22 +2172,24 @@ static int grep_source_load(struct grep_source *gs)
2172 BUG("invalid grep_source type to load");
2173 }
2174
2174 -void grep_source_load_driver(struct grep_source *gs)
2175 +void grep_source_load_driver(struct grep_source *gs,
2176 + struct index_state *istate)
2177 {
2178 if (gs->driver)
2179 return;
2180
2181 grep_attr_lock();
2182 if (gs->path)
2181 - gs->driver = userdiff_find_by_path(gs->path);
2183 + gs->driver = userdiff_find_by_path(istate, gs->path);
2184 if (!gs->driver)
2185 gs->driver = userdiff_find_by_name("default");
2186 grep_attr_unlock();
2187 }
2188
2187 -static int grep_source_is_binary(struct grep_source *gs)
2189 +static int grep_source_is_binary(struct grep_source *gs,
2190 + struct index_state *istate)
2191 {
2189 - grep_source_load_driver(gs);
2192 + grep_source_load_driver(gs, istate);
2193 if (gs->driver->binary != -1)
2194 return gs->driver->binary;
2195
grep.h
+2 -1
@@ -220,7 +220,8 @@ void grep_source_init(struct grep_source *gs, enum grep_source_type type,
220 const void *identifier);
221 void grep_source_clear_data(struct grep_source *gs);
222 void grep_source_clear(struct grep_source *gs);
223 -void grep_source_load_driver(struct grep_source *gs);
223 +void grep_source_load_driver(struct grep_source *gs,
224 + struct index_state *istate);
225
226
227 int grep_source(struct grep_opt *opt, struct grep_source *gs);
line-range.c
+1 -1
@@ -198,7 +198,7 @@ static const char *parse_range_funcname(const char *arg, nth_line_fn_t nth_line_
198 anchor--; /* input is in human terms */
199 start = nth_line_cb(cb_data, anchor);
200
201 - drv = userdiff_find_by_path(path);
201 + drv = userdiff_find_by_path(&the_index, path);
202 if (drv && drv->funcname.pattern) {
203 const struct userdiff_funcname *pe = &drv->funcname;
204 xecfg = xcalloc(1, sizeof(*xecfg));
userdiff.c
+3 -2
@@ -270,7 +270,8 @@ struct userdiff_driver *userdiff_find_by_name(const char *name) {
270 return userdiff_find_by_namelen(name, len);
271 }
272
273 -struct userdiff_driver *userdiff_find_by_path(const char *path)
273 +struct userdiff_driver *userdiff_find_by_path(struct index_state *istate,
274 + const char *path)
275 {
276 static struct attr_check *check;
277
@@ -278,7 +279,7 @@ struct userdiff_driver *userdiff_find_by_path(const char *path)
279 check = attr_check_initl("diff", NULL);
280 if (!path)
281 return NULL;
281 - if (git_check_attr(&the_index, path, check))
282 + if (git_check_attr(istate, path, check))
283 return NULL;
284
285 if (ATTR_TRUE(check->items[0].value))
userdiff.h
+4 -1
@@ -3,6 +3,8 @@
3
4 #include "notes-cache.h"
5
6 +struct index_state;
7 +
8 struct userdiff_funcname {
9 const char *pattern;
10 int cflags;
@@ -21,7 +23,8 @@ struct userdiff_driver {
23
24 int userdiff_config(const char *k, const char *v);
25 struct userdiff_driver *userdiff_find_by_name(const char *name);
24 -struct userdiff_driver *userdiff_find_by_path(const char *path);
26 +struct userdiff_driver *userdiff_find_by_path(struct index_state *istate,
27 + const char *path);
28
29 /*
30 * Initialize any textconv-related fields in the driver and return it, or NULL