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>
This commit is contained in:
Nguyễn Thái Ngọc Duy 2018-09-21 17:57:33 +02:00 committed by Junio C Hamano
parent 35843b1123
commit acd00ea049
11 changed files with 61 additions and 41 deletions

View File

@ -264,9 +264,10 @@ static int has_only_ascii(const char *s)
} }
} }
static int entry_is_binary(const char *path, const void *buffer, size_t size) static int entry_is_binary(struct index_state *istate, const char *path,
const void *buffer, size_t size)
{ {
struct userdiff_driver *driver = userdiff_find_by_path(path); struct userdiff_driver *driver = userdiff_find_by_path(istate, path);
if (!driver) if (!driver)
driver = userdiff_find_by_name("default"); driver = userdiff_find_by_name("default");
if (driver->binary != -1) if (driver->binary != -1)
@ -352,7 +353,8 @@ static int write_zip_entry(struct archiver_args *args,
return error(_("cannot read %s"), return error(_("cannot read %s"),
oid_to_hex(oid)); oid_to_hex(oid));
crc = crc32(crc, buffer, size); crc = crc32(crc, buffer, size);
is_binary = entry_is_binary(path_without_prefix, is_binary = entry_is_binary(args->repo->index,
path_without_prefix,
buffer, size); buffer, size);
out = buffer; out = buffer;
} }
@ -428,7 +430,8 @@ static int write_zip_entry(struct archiver_args *args,
break; break;
crc = crc32(crc, buf, readlen); crc = crc32(crc, buf, readlen);
if (is_binary == -1) if (is_binary == -1)
is_binary = entry_is_binary(path_without_prefix, is_binary = entry_is_binary(args->repo->index,
path_without_prefix,
buf, readlen); buf, readlen);
write_or_die(1, buf, readlen); write_or_die(1, buf, readlen);
} }
@ -460,7 +463,8 @@ static int write_zip_entry(struct archiver_args *args,
break; break;
crc = crc32(crc, buf, readlen); crc = crc32(crc, buf, readlen);
if (is_binary == -1) if (is_binary == -1)
is_binary = entry_is_binary(path_without_prefix, is_binary = entry_is_binary(args->repo->index,
path_without_prefix,
buf, readlen); buf, readlen);
zstream.next_in = buf; zstream.next_in = buf;

View File

@ -103,7 +103,8 @@ static void add_work(struct grep_opt *opt, const struct grep_source *gs)
todo[todo_end].source = *gs; todo[todo_end].source = *gs;
if (opt->binary != GREP_BINARY_TEXT) if (opt->binary != GREP_BINARY_TEXT)
grep_source_load_driver(&todo[todo_end].source); grep_source_load_driver(&todo[todo_end].source,
opt->repo->index);
todo[todo_end].done = 0; todo[todo_end].done = 0;
strbuf_reset(&todo[todo_end].out); strbuf_reset(&todo[todo_end].out);
todo_end = (todo_end + 1) % ARRAY_SIZE(todo); todo_end = (todo_end + 1) % ARRAY_SIZE(todo);

View File

@ -987,7 +987,7 @@ static void show_patch_diff(struct combine_diff_path *elem, int num_parent,
const char *line_prefix = diff_line_prefix(opt); const char *line_prefix = diff_line_prefix(opt);
context = opt->context; context = opt->context;
userdiff = userdiff_find_by_path(elem->path); userdiff = userdiff_find_by_path(opt->repo->index, elem->path);
if (!userdiff) if (!userdiff)
userdiff = userdiff_find_by_name("default"); userdiff = userdiff_find_by_name("default");
if (opt->flags.allow_textconv) if (opt->flags.allow_textconv)

40
diff.c
View File

@ -2093,23 +2093,25 @@ static void diff_words_flush(struct emit_callback *ecbdata)
} }
} }
static void diff_filespec_load_driver(struct diff_filespec *one) static void diff_filespec_load_driver(struct diff_filespec *one,
struct index_state *istate)
{ {
/* Use already-loaded driver */ /* Use already-loaded driver */
if (one->driver) if (one->driver)
return; return;
if (S_ISREG(one->mode)) if (S_ISREG(one->mode))
one->driver = userdiff_find_by_path(one->path); one->driver = userdiff_find_by_path(istate, one->path);
/* Fallback to default settings */ /* Fallback to default settings */
if (!one->driver) if (!one->driver)
one->driver = userdiff_find_by_name("default"); one->driver = userdiff_find_by_name("default");
} }
static const char *userdiff_word_regex(struct diff_filespec *one) static const char *userdiff_word_regex(struct diff_filespec *one,
struct index_state *istate)
{ {
diff_filespec_load_driver(one); diff_filespec_load_driver(one, istate);
return one->driver->word_regex; return one->driver->word_regex;
} }
@ -2132,9 +2134,9 @@ static void init_diff_words_data(struct emit_callback *ecbdata,
xcalloc(1, sizeof(struct emitted_diff_symbols)); xcalloc(1, sizeof(struct emitted_diff_symbols));
if (!o->word_regex) if (!o->word_regex)
o->word_regex = userdiff_word_regex(one); o->word_regex = userdiff_word_regex(one, o->repo->index);
if (!o->word_regex) if (!o->word_regex)
o->word_regex = userdiff_word_regex(two); o->word_regex = userdiff_word_regex(two, o->repo->index);
if (!o->word_regex) if (!o->word_regex)
o->word_regex = diff_word_regex_cfg; o->word_regex = diff_word_regex_cfg;
if (o->word_regex) { if (o->word_regex) {
@ -3257,7 +3259,7 @@ int diff_filespec_is_binary(struct repository *r,
struct diff_filespec *one) struct diff_filespec *one)
{ {
if (one->is_binary == -1) { if (one->is_binary == -1) {
diff_filespec_load_driver(one); diff_filespec_load_driver(one, r->index);
if (one->driver->binary != -1) if (one->driver->binary != -1)
one->is_binary = one->driver->binary; one->is_binary = one->driver->binary;
else { else {
@ -3273,9 +3275,10 @@ int diff_filespec_is_binary(struct repository *r,
return one->is_binary; return one->is_binary;
} }
static const struct userdiff_funcname *diff_funcname_pattern(struct diff_filespec *one) static const struct userdiff_funcname *
diff_funcname_pattern(struct diff_options *o, struct diff_filespec *one)
{ {
diff_filespec_load_driver(one); diff_filespec_load_driver(one, o->repo->index);
return one->driver->funcname.pattern ? &one->driver->funcname : NULL; return one->driver->funcname.pattern ? &one->driver->funcname : NULL;
} }
@ -3287,12 +3290,13 @@ void diff_set_mnemonic_prefix(struct diff_options *options, const char *a, const
options->b_prefix = b; options->b_prefix = b;
} }
struct userdiff_driver *get_textconv(struct diff_filespec *one) struct userdiff_driver *get_textconv(struct index_state *istate,
struct diff_filespec *one)
{ {
if (!DIFF_FILE_VALID(one)) if (!DIFF_FILE_VALID(one))
return NULL; return NULL;
diff_filespec_load_driver(one); diff_filespec_load_driver(one, istate);
return userdiff_get_textconv(one->driver); return userdiff_get_textconv(one->driver);
} }
@ -3342,8 +3346,8 @@ static void builtin_diff(const char *name_a,
} }
if (o->flags.allow_textconv) { if (o->flags.allow_textconv) {
textconv_one = get_textconv(one); textconv_one = get_textconv(o->repo->index, one);
textconv_two = get_textconv(two); textconv_two = get_textconv(o->repo->index, two);
} }
/* Never use a non-valid filename anywhere if at all possible */ /* Never use a non-valid filename anywhere if at all possible */
@ -3465,9 +3469,9 @@ static void builtin_diff(const char *name_a,
mf1.size = fill_textconv(o->repo, textconv_one, one, &mf1.ptr); mf1.size = fill_textconv(o->repo, textconv_one, one, &mf1.ptr);
mf2.size = fill_textconv(o->repo, textconv_two, two, &mf2.ptr); mf2.size = fill_textconv(o->repo, textconv_two, two, &mf2.ptr);
pe = diff_funcname_pattern(one); pe = diff_funcname_pattern(o, one);
if (!pe) if (!pe)
pe = diff_funcname_pattern(two); pe = diff_funcname_pattern(o, two);
memset(&xpp, 0, sizeof(xpp)); memset(&xpp, 0, sizeof(xpp));
memset(&xecfg, 0, sizeof(xecfg)); memset(&xecfg, 0, sizeof(xecfg));
@ -4223,7 +4227,9 @@ static void run_diff_cmd(const char *pgm,
if (o->flags.allow_external) { if (o->flags.allow_external) {
struct userdiff_driver *drv = userdiff_find_by_path(attr_path); struct userdiff_driver *drv;
drv = userdiff_find_by_path(o->repo->index, attr_path);
if (drv && drv->external) if (drv && drv->external)
pgm = drv->external; pgm = drv->external;
} }
@ -6399,7 +6405,7 @@ int textconv_object(struct repository *r,
df = alloc_filespec(path); df = alloc_filespec(path);
fill_filespec(df, oid, oid_valid, mode); fill_filespec(df, oid, oid_valid, mode);
textconv = get_textconv(df); textconv = get_textconv(r->index, df);
if (!textconv) { if (!textconv) {
free_filespec(df); free_filespec(df);
return 0; return 0;

3
diff.h
View File

@ -455,7 +455,8 @@ size_t fill_textconv(struct repository *r,
* and only if it has textconv enabled (otherwise return NULL). The result * and only if it has textconv enabled (otherwise return NULL). The result
* can be passed to fill_textconv(). * can be passed to fill_textconv().
*/ */
struct userdiff_driver *get_textconv(struct diff_filespec *one); struct userdiff_driver *get_textconv(struct index_state *istate,
struct diff_filespec *one);
/* /*
* Prepare diff_filespec and convert it using diff textconv API * Prepare diff_filespec and convert it using diff textconv API

View File

@ -139,8 +139,8 @@ static int pickaxe_match(struct diff_filepair *p, struct diff_options *o,
return 0; return 0;
if (o->flags.allow_textconv) { if (o->flags.allow_textconv) {
textconv_one = get_textconv(p->one); textconv_one = get_textconv(o->repo->index, p->one);
textconv_two = get_textconv(p->two); textconv_two = get_textconv(o->repo->index, p->two);
} }
/* /*

21
grep.c
View File

@ -11,7 +11,8 @@
#include "help.h" #include "help.h"
static int grep_source_load(struct grep_source *gs); static int grep_source_load(struct grep_source *gs);
static int grep_source_is_binary(struct grep_source *gs); static int grep_source_is_binary(struct grep_source *gs,
struct index_state *istate);
static struct grep_opt grep_defaults; static struct grep_opt grep_defaults;
@ -1547,7 +1548,7 @@ static int match_funcname(struct grep_opt *opt, struct grep_source *gs, char *bo
{ {
xdemitconf_t *xecfg = opt->priv; xdemitconf_t *xecfg = opt->priv;
if (xecfg && !xecfg->find_func) { if (xecfg && !xecfg->find_func) {
grep_source_load_driver(gs); grep_source_load_driver(gs, opt->repo->index);
if (gs->driver->funcname.pattern) { if (gs->driver->funcname.pattern) {
const struct userdiff_funcname *pe = &gs->driver->funcname; const struct userdiff_funcname *pe = &gs->driver->funcname;
xdiff_set_find_func(xecfg, pe->pattern, pe->cflags); 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
opt->last_shown = 0; opt->last_shown = 0;
if (opt->allow_textconv) { if (opt->allow_textconv) {
grep_source_load_driver(gs); grep_source_load_driver(gs, opt->repo->index);
/* /*
* We might set up the shared textconv cache data here, which * We might set up the shared textconv cache data here, which
* is not thread-safe. * is not thread-safe.
@ -1821,11 +1822,11 @@ static int grep_source_1(struct grep_opt *opt, struct grep_source *gs, int colle
if (!textconv) { if (!textconv) {
switch (opt->binary) { switch (opt->binary) {
case GREP_BINARY_DEFAULT: case GREP_BINARY_DEFAULT:
if (grep_source_is_binary(gs)) if (grep_source_is_binary(gs, opt->repo->index))
binary_match_only = 1; binary_match_only = 1;
break; break;
case GREP_BINARY_NOMATCH: case GREP_BINARY_NOMATCH:
if (grep_source_is_binary(gs)) if (grep_source_is_binary(gs, opt->repo->index))
return 0; /* Assume unmatch */ return 0; /* Assume unmatch */
break; break;
case GREP_BINARY_TEXT: case GREP_BINARY_TEXT:
@ -2171,22 +2172,24 @@ static int grep_source_load(struct grep_source *gs)
BUG("invalid grep_source type to load"); BUG("invalid grep_source type to load");
} }
void grep_source_load_driver(struct grep_source *gs) void grep_source_load_driver(struct grep_source *gs,
struct index_state *istate)
{ {
if (gs->driver) if (gs->driver)
return; return;
grep_attr_lock(); grep_attr_lock();
if (gs->path) if (gs->path)
gs->driver = userdiff_find_by_path(gs->path); gs->driver = userdiff_find_by_path(istate, gs->path);
if (!gs->driver) if (!gs->driver)
gs->driver = userdiff_find_by_name("default"); gs->driver = userdiff_find_by_name("default");
grep_attr_unlock(); grep_attr_unlock();
} }
static int grep_source_is_binary(struct grep_source *gs) static int grep_source_is_binary(struct grep_source *gs,
struct index_state *istate)
{ {
grep_source_load_driver(gs); grep_source_load_driver(gs, istate);
if (gs->driver->binary != -1) if (gs->driver->binary != -1)
return gs->driver->binary; return gs->driver->binary;

3
grep.h
View File

@ -220,7 +220,8 @@ void grep_source_init(struct grep_source *gs, enum grep_source_type type,
const void *identifier); const void *identifier);
void grep_source_clear_data(struct grep_source *gs); void grep_source_clear_data(struct grep_source *gs);
void grep_source_clear(struct grep_source *gs); void grep_source_clear(struct grep_source *gs);
void grep_source_load_driver(struct grep_source *gs); void grep_source_load_driver(struct grep_source *gs,
struct index_state *istate);
int grep_source(struct grep_opt *opt, struct grep_source *gs); int grep_source(struct grep_opt *opt, struct grep_source *gs);

View File

@ -198,7 +198,7 @@ static const char *parse_range_funcname(const char *arg, nth_line_fn_t nth_line_
anchor--; /* input is in human terms */ anchor--; /* input is in human terms */
start = nth_line_cb(cb_data, anchor); start = nth_line_cb(cb_data, anchor);
drv = userdiff_find_by_path(path); drv = userdiff_find_by_path(&the_index, path);
if (drv && drv->funcname.pattern) { if (drv && drv->funcname.pattern) {
const struct userdiff_funcname *pe = &drv->funcname; const struct userdiff_funcname *pe = &drv->funcname;
xecfg = xcalloc(1, sizeof(*xecfg)); xecfg = xcalloc(1, sizeof(*xecfg));

View File

@ -270,7 +270,8 @@ struct userdiff_driver *userdiff_find_by_name(const char *name) {
return userdiff_find_by_namelen(name, len); return userdiff_find_by_namelen(name, len);
} }
struct userdiff_driver *userdiff_find_by_path(const char *path) struct userdiff_driver *userdiff_find_by_path(struct index_state *istate,
const char *path)
{ {
static struct attr_check *check; static struct attr_check *check;
@ -278,7 +279,7 @@ struct userdiff_driver *userdiff_find_by_path(const char *path)
check = attr_check_initl("diff", NULL); check = attr_check_initl("diff", NULL);
if (!path) if (!path)
return NULL; return NULL;
if (git_check_attr(&the_index, path, check)) if (git_check_attr(istate, path, check))
return NULL; return NULL;
if (ATTR_TRUE(check->items[0].value)) if (ATTR_TRUE(check->items[0].value))

View File

@ -3,6 +3,8 @@
#include "notes-cache.h" #include "notes-cache.h"
struct index_state;
struct userdiff_funcname { struct userdiff_funcname {
const char *pattern; const char *pattern;
int cflags; int cflags;
@ -21,7 +23,8 @@ struct userdiff_driver {
int userdiff_config(const char *k, const char *v); int userdiff_config(const char *k, const char *v);
struct userdiff_driver *userdiff_find_by_name(const char *name); struct userdiff_driver *userdiff_find_by_name(const char *name);
struct userdiff_driver *userdiff_find_by_path(const char *path); struct userdiff_driver *userdiff_find_by_path(struct index_state *istate,
const char *path);
/* /*
* Initialize any textconv-related fields in the driver and return it, or NULL * Initialize any textconv-related fields in the driver and return it, or NULL