The fifth batch
[alt-git.git] / userdiff.c
blob340c4eb4f77ab17798d25383fa4b6135c661b67a
1 #define USE_THE_REPOSITORY_VARIABLE
2 #define DISABLE_SIGN_COMPARE_WARNINGS
4 #include "git-compat-util.h"
5 #include "config.h"
6 #include "userdiff.h"
7 #include "attr.h"
8 #include "strbuf.h"
9 #include "environment.h"
11 static struct userdiff_driver *drivers;
12 static int ndrivers;
13 static int drivers_alloc;
15 #define PATTERNS(lang, rx, wrx) { \
16 .name = lang, \
17 .binary = -1, \
18 .funcname = { \
19 .pattern = rx, \
20 .cflags = REG_EXTENDED, \
21 }, \
22 .word_regex = wrx "|[^[:space:]]|[\xc0-\xff][\x80-\xbf]+", \
23 .word_regex_multi_byte = wrx "|[^[:space:]]", \
25 #define IPATTERN(lang, rx, wrx) { \
26 .name = lang, \
27 .binary = -1, \
28 .funcname = { \
29 .pattern = rx, \
30 .cflags = REG_EXTENDED | REG_ICASE, \
31 }, \
32 .word_regex = wrx "|[^[:space:]]|[\xc0-\xff][\x80-\xbf]+", \
33 .word_regex_multi_byte = wrx "|[^[:space:]]", \
37 * Built-in drivers for various languages, sorted by their names
38 * (except that the "default" is left at the end).
40 * When writing or updating patterns, assume that the contents these
41 * patterns are applied to are syntactically correct. The patterns
42 * can be simple without implementing all syntactical corner cases, as
43 * long as they are sufficiently permissive.
45 static struct userdiff_driver builtin_drivers[] = {
46 IPATTERN("ada",
47 "!^(.*[ \t])?(is[ \t]+new|renames|is[ \t]+separate)([ \t].*)?$\n"
48 "!^[ \t]*with[ \t].*$\n"
49 "^[ \t]*((procedure|function)[ \t]+.*)$\n"
50 "^[ \t]*((package|protected|task)[ \t]+.*)$",
51 /* -- */
52 "[a-zA-Z][a-zA-Z0-9_]*"
53 "|[-+]?[0-9][0-9#_.aAbBcCdDeEfF]*([eE][+-]?[0-9_]+)?"
54 "|=>|\\.\\.|\\*\\*|:=|/=|>=|<=|<<|>>|<>"),
55 PATTERNS("bash",
56 /* Optional leading indentation */
57 "^[ \t]*"
58 /* Start of captured text */
59 "("
60 "("
61 /* POSIX identifier with mandatory parentheses */
62 "[a-zA-Z_][a-zA-Z0-9_]*[ \t]*\\([ \t]*\\))"
63 "|"
64 /* Bashism identifier with optional parentheses */
65 "(function[ \t]+[a-zA-Z_][a-zA-Z0-9_]*(([ \t]*\\([ \t]*\\))|([ \t]+))"
66 ")"
67 /* Optional whitespace */
68 "[ \t]*"
69 /* Compound command starting with `{`, `(`, `((` or `[[` */
70 "(\\{|\\(\\(?|\\[\\[)"
71 /* End of captured text */
72 ")",
73 /* -- */
74 /* Characters not in the default $IFS value */
75 "[^ \t]+"),
76 PATTERNS("bibtex",
77 "(@[a-zA-Z]{1,}[ \t]*\\{{0,1}[ \t]*[^ \t\"@',\\#}{~%]*).*$",
78 /* -- */
79 "[={}\"]|[^={}\" \t]+"),
80 PATTERNS("cpp",
81 /* Jump targets or access declarations */
82 "!^[ \t]*[A-Za-z_][A-Za-z_0-9]*:[[:space:]]*($|/[/*])\n"
83 /* functions/methods, variables, and compounds at top level */
84 "^((::[[:space:]]*)?[A-Za-z_].*)$",
85 /* -- */
86 /* identifiers and keywords */
87 "[a-zA-Z_][a-zA-Z0-9_]*"
88 /* decimal and octal integers as well as floatingpoint numbers */
89 "|[0-9][0-9.]*([Ee][-+]?[0-9]+)?[fFlLuU]*"
90 /* hexadecimal and binary integers */
91 "|0[xXbB][0-9a-fA-F]+[lLuU]*"
92 /* floatingpoint numbers that begin with a decimal point */
93 "|\\.[0-9][0-9]*([Ee][-+]?[0-9]+)?[fFlL]?"
94 "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->\\*?|\\.\\*|<=>"),
95 PATTERNS("csharp",
97 * Jump over reserved keywords which are illegal method names, but which
98 * can be followed by parentheses without special characters in between,
99 * making them look like methods.
101 "!(^|[ \t]+)" /* Start of line or whitespace. */
102 "(do|while|for|foreach|if|else|new|default|return|switch|case|throw"
103 "|catch|using|lock|fixed)"
104 "([ \t(]+|$)\n" /* Whitespace, "(", or end of line. */
106 * Methods/constructors:
107 * The strategy is to identify a minimum of two groups (any combination
108 * of keywords/type/name) before the opening parenthesis, and without
109 * final unexpected characters, normally only used in ordinary statements.
111 "^[ \t]*" /* Remove leading whitespace. */
112 "(" /* Start chunk header capture. */
113 "(" /* First group. */
114 "[][[:alnum:]@_.]" /* Name. */
115 "(<[][[:alnum:]@_, \t<>]+>)?" /* Optional generic parameters. */
116 ")+"
117 "([ \t]+" /* Subsequent groups, prepended with space. */
118 "([][[:alnum:]@_.](<[][[:alnum:]@_, \t<>]+>)?)+"
119 ")+"
120 "[ \t]*" /* Optional space before parameters start. */
121 "\\(" /* Start of method parameters. */
122 "[^;]*" /* Allow complex parameters, but exclude statements (;). */
123 ")$\n" /* Close chunk header capture. */
125 * Properties:
126 * As with methods, expect a minimum of two groups. But, more trivial than
127 * methods, the vast majority of properties long enough to be worth
128 * showing a chunk header for don't include "=:;,()" on the line they are
129 * defined, since they don't have a parameter list.
131 "^[ \t]*("
132 "([][[:alnum:]@_.](<[][[:alnum:]@_, \t<>]+>)?)+"
133 "([ \t]+"
134 "([][[:alnum:]@_.](<[][[:alnum:]@_, \t<>]+>)?)+"
135 ")+" /* Up to here, same as methods regex. */
136 "[^;=:,()]*" /* Compared to methods, no parameter list allowed. */
137 ")$\n"
138 /* Type definitions */
139 "^[ \t]*(((static|public|internal|private|protected|new|unsafe|sealed|abstract|partial)[ \t]+)*(class|enum|interface|struct|record)[ \t]+.*)$\n"
140 /* Namespace */
141 "^[ \t]*(namespace[ \t]+.*)$",
142 /* -- */
143 "[a-zA-Z_][a-zA-Z0-9_]*"
144 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?"
145 "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->"),
146 IPATTERN("css",
147 "![:;][[:space:]]*$\n"
148 "^[:[@.#]?[_a-z0-9].*$",
149 /* -- */
151 * This regex comes from W3C CSS specs. Should theoretically also
152 * allow ISO 10646 characters U+00A0 and higher,
153 * but they are not handled in this regex.
155 "-?[_a-zA-Z][-_a-zA-Z0-9]*" /* identifiers */
156 "|-?[0-9]+|\\#[0-9a-fA-F]+" /* numbers */
158 PATTERNS("dts",
159 "!;\n"
160 "!=\n"
161 /* lines beginning with a word optionally preceded by '&' or the root */
162 "^[ \t]*((/[ \t]*\\{|&?[a-zA-Z_]).*)",
163 /* -- */
164 /* Property names and math operators */
165 "[a-zA-Z0-9,._+?#-]+"
166 "|[-+*/%&^|!~]|>>|<<|&&|\\|\\|"),
167 PATTERNS("elixir",
168 "^[ \t]*((def(macro|module|impl|protocol|p)?|test)[ \t].*)$",
169 /* -- */
170 /* Atoms, names, and module attributes */
171 "[@:]?[a-zA-Z0-9@_?!]+"
172 /* Numbers with specific base */
173 "|[-+]?0[xob][0-9a-fA-F]+"
174 /* Numbers */
175 "|[-+]?[0-9][0-9_.]*([eE][-+]?[0-9_]+)?"
176 /* Operators and atoms that represent them */
177 "|:?(\\+\\+|--|\\.\\.|~~~|<>|\\^\\^\\^|<?\\|>|<<<?|>?>>|<<?~|~>?>|<~>|<=|>=|===?|!==?|=~|&&&?|\\|\\|\\|?|=>|<-|\\\\\\\\|->)"
178 /* Not real operators, but should be grouped */
179 "|:?%[A-Za-z0-9_.]\\{\\}?"),
180 IPATTERN("fortran",
181 /* Don't match comment lines */
182 "!^([C*]|[ \t]*!)\n"
183 /* Don't match 'module procedure' lines */
184 "!^[ \t]*MODULE[ \t]+PROCEDURE[ \t]\n"
185 /* Program, module, block data */
186 "^[ \t]*((END[ \t]+)?(PROGRAM|MODULE|BLOCK[ \t]+DATA"
187 /* Subroutines and functions */
188 "|([^!'\" \t]+[ \t]+)*(SUBROUTINE|FUNCTION))[ \t]+[A-Z].*)$",
189 /* -- */
190 "[a-zA-Z][a-zA-Z0-9_]*"
191 "|\\.([Ee][Qq]|[Nn][Ee]|[Gg][TtEe]|[Ll][TtEe]|[Tt][Rr][Uu][Ee]|[Ff][Aa][Ll][Ss][Ee]|[Aa][Nn][Dd]|[Oo][Rr]|[Nn]?[Ee][Qq][Vv]|[Nn][Oo][Tt])\\."
192 /* numbers and format statements like 2E14.4, or ES12.6, 9X.
193 * Don't worry about format statements without leading digits since
194 * they would have been matched above as a variable anyway. */
195 "|[-+]?[0-9.]+([AaIiDdEeFfLlTtXx][Ss]?[-+]?[0-9.]*)?(_[a-zA-Z0-9][a-zA-Z0-9_]*)?"
196 "|//|\\*\\*|::|[/<>=]="),
197 IPATTERN("fountain",
198 "^((\\.[^.]|(int|ext|est|int\\.?/ext|i/e)[. ]).*)$",
199 /* -- */
200 "[^ \t-]+"),
201 PATTERNS("golang",
202 /* Functions */
203 "^[ \t]*(func[ \t]*.*(\\{[ \t]*)?)\n"
204 /* Structs and interfaces */
205 "^[ \t]*(type[ \t].*(struct|interface)[ \t]*(\\{[ \t]*)?)",
206 /* -- */
207 "[a-zA-Z_][a-zA-Z0-9_]*"
208 "|[-+0-9.eE]+i?|0[xX]?[0-9a-fA-F]+i?"
209 "|[-+*/<>%&^|=!:]=|--|\\+\\+|<<=?|>>=?|&\\^=?|&&|\\|\\||<-|\\.{3}"),
210 PATTERNS("html",
211 "^[ \t]*(<[Hh][1-6]([ \t].*)?>.*)$",
212 /* -- */
213 "[^<>= \t]+"),
214 PATTERNS("java",
215 "!^[ \t]*(catch|do|for|if|instanceof|new|return|switch|throw|while)\n"
216 /* Class, enum, interface, and record declarations */
217 "^[ \t]*(([a-z-]+[ \t]+)*(class|enum|interface|record)[ \t]+.*)$\n"
218 /* Method definitions; note that constructor signatures are not */
219 /* matched because they are indistinguishable from method calls. */
220 "^[ \t]*(([A-Za-z_<>&][][?&<>.,A-Za-z_0-9]*[ \t]+)+[A-Za-z_][A-Za-z_0-9]*[ \t]*\\([^;]*)$",
221 /* -- */
222 "[a-zA-Z_][a-zA-Z0-9_]*"
223 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?"
224 "|[-+*/<>%&^|=!]="
225 "|--|\\+\\+|<<=?|>>>?=?|&&|\\|\\|"),
226 PATTERNS("kotlin",
227 "^[ \t]*(([a-z]+[ \t]+)*(fun|class|interface)[ \t]+.*)$",
228 /* -- */
229 "[a-zA-Z_][a-zA-Z0-9_]*"
230 /* hexadecimal and binary numbers */
231 "|0[xXbB][0-9a-fA-F_]+[lLuU]*"
232 /* integers and floats */
233 "|[0-9][0-9_]*([.][0-9_]*)?([Ee][-+]?[0-9]+)?[fFlLuU]*"
234 /* floating point numbers beginning with decimal point */
235 "|[.][0-9][0-9_]*([Ee][-+]?[0-9]+)?[fFlLuU]?"
236 /* unary and binary operators */
237 "|[-+*/<>%&^|=!]==?|--|\\+\\+|<<=|>>=|&&|\\|\\||->|\\.\\*|!!|[?:.][.:]"),
238 PATTERNS("markdown",
239 "^ {0,3}#{1,6}[ \t].*",
240 /* -- */
241 "[^<>= \t]+"),
242 PATTERNS("matlab",
244 * Octave pattern is mostly the same as matlab, except that '%%%' and
245 * '##' can also be used to begin code sections, in addition to '%%'
246 * that is understood by both.
248 "^[[:space:]]*((classdef|function)[[:space:]].*)$|^(%%%?|##)[[:space:]].*$",
249 /* -- */
250 "[a-zA-Z_][a-zA-Z0-9_]*|[-+0-9.e]+|[=~<>]=|\\.[*/\\^']|\\|\\||&&"),
251 PATTERNS("objc",
252 /* Negate C statements that can look like functions */
253 "!^[ \t]*(do|for|if|else|return|switch|while)\n"
254 /* Objective-C methods */
255 "^[ \t]*([-+][ \t]*\\([ \t]*[A-Za-z_][A-Za-z_0-9* \t]*\\)[ \t]*[A-Za-z_].*)$\n"
256 /* C functions */
257 "^[ \t]*(([A-Za-z_][A-Za-z_0-9]*[ \t]+)+[A-Za-z_][A-Za-z_0-9]*[ \t]*\\([^;]*)$\n"
258 /* Objective-C class/protocol definitions */
259 "^(@(implementation|interface|protocol)[ \t].*)$",
260 /* -- */
261 "[a-zA-Z_][a-zA-Z0-9_]*"
262 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?"
263 "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->"),
264 PATTERNS("pascal",
265 "^(((class[ \t]+)?(procedure|function)|constructor|destructor|interface"
266 "|implementation|initialization|finalization)[ \t]*.*)$\n"
267 "^(.*=[ \t]*(class|record).*)$",
268 /* -- */
269 "[a-zA-Z_][a-zA-Z0-9_]*"
270 "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+"
271 "|<>|<=|>=|:=|\\.\\."),
272 PATTERNS("perl",
273 "^package .*\n"
274 "^sub [[:alnum:]_':]+[ \t]*"
275 "(\\([^)]*\\)[ \t]*)?" /* prototype */
277 * Attributes. A regex can't count nested parentheses,
278 * so just slurp up whatever we see, taking care not
279 * to accept lines like "sub foo; # defined elsewhere".
281 * An attribute could contain a semicolon, but at that
282 * point it seems reasonable enough to give up.
284 "(:[^;#]*)?"
285 "(\\{[ \t]*)?" /* brace can come here or on the next line */
286 "(#.*)?$\n" /* comment */
287 "^(BEGIN|END|INIT|CHECK|UNITCHECK|AUTOLOAD|DESTROY)[ \t]*"
288 "(\\{[ \t]*)?" /* brace can come here or on the next line */
289 "(#.*)?$\n"
290 "^=head[0-9] .*", /* POD */
291 /* -- */
292 "[[:alpha:]_'][[:alnum:]_']*"
293 "|0[xb]?[0-9a-fA-F_]*"
294 /* taking care not to interpret 3..5 as (3.)(.5) */
295 "|[0-9a-fA-F_]+(\\.[0-9a-fA-F_]+)?([eE][-+]?[0-9_]+)?"
296 "|=>|-[rwxoRWXOezsfdlpSugkbctTBMAC>]|~~|::"
297 "|&&=|\\|\\|=|//=|\\*\\*="
298 "|&&|\\|\\||//|\\+\\+|--|\\*\\*|\\.\\.\\.?"
299 "|[-+*/%.^&<>=!|]="
300 "|=~|!~"
301 "|<<|<>|<=>|>>"),
302 PATTERNS("php",
303 "^[\t ]*(((public|protected|private|static|abstract|final)[\t ]+)*function.*)$\n"
304 "^[\t ]*((((final|abstract)[\t ]+)?class|enum|interface|trait).*)$",
305 /* -- */
306 "[a-zA-Z_][a-zA-Z0-9_]*"
307 "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+"
308 "|[-+*/<>%&^|=!.]=|--|\\+\\+|<<=?|>>=?|===|&&|\\|\\||::|->"),
309 PATTERNS("python",
310 "^[ \t]*((class|(async[ \t]+)?def)[ \t].*)$",
311 /* -- */
312 "[a-zA-Z_][a-zA-Z0-9_]*"
313 "|[-+0-9.e]+[jJlL]?|0[xX]?[0-9a-fA-F]+[lL]?"
314 "|[-+*/<>%&^|=!]=|//=?|<<=?|>>=?|\\*\\*=?"),
315 /* -- */
316 PATTERNS("ruby",
317 "^[ \t]*((class|module|def)[ \t].*)$",
318 /* -- */
319 "(@|@@|\\$)?[a-zA-Z_][a-zA-Z0-9_]*"
320 "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+|\\?(\\\\C-)?(\\\\M-)?."
321 "|//=?|[-+*/<>%&^|=!]=|<<=?|>>=?|===|\\.{1,3}|::|[!=]~"),
322 PATTERNS("rust",
323 "^[\t ]*((pub(\\([^\\)]+\\))?[\t ]+)?((async|const|unsafe|extern([\t ]+\"[^\"]+\"))[\t ]+)?(struct|enum|union|mod|trait|fn|impl|macro_rules!)[< \t]+[^;]*)$",
324 /* -- */
325 "[a-zA-Z_][a-zA-Z0-9_]*"
326 "|[0-9][0-9_a-fA-Fiosuxz]*(\\.([0-9]*[eE][+-]?)?[0-9_fF]*)?"
327 "|[-+*\\/<>%&^|=!:]=|<<=?|>>=?|&&|\\|\\||->|=>|\\.{2}=|\\.{3}|::"),
328 PATTERNS("scheme",
329 "^[\t ]*(\\(((define|def(struct|syntax|class|method|rules|record|proto|alias)?)[-*/ \t]|(library|module|struct|class)[*+ \t]).*)$",
331 * R7RS valid identifiers include any sequence enclosed
332 * within vertical lines having no backslashes
334 "\\|([^\\\\]*)\\|"
335 /* All other words should be delimited by spaces or parentheses */
336 "|([^][)(}{[ \t])+"),
337 PATTERNS("tex", "^(\\\\((sub)*section|chapter|part)\\*{0,1}\\{.*)$",
338 "\\\\[a-zA-Z@]+|\\\\.|([a-zA-Z0-9]|[^\x01-\x7f])+"),
339 { .name = "default", .binary = -1 },
341 #undef PATTERNS
342 #undef IPATTERN
344 static struct userdiff_driver driver_true = {
345 .name = "diff=true",
346 .binary = 0,
349 static struct userdiff_driver driver_false = {
350 .name = "!diff",
351 .binary = 1,
354 struct find_by_namelen_data {
355 const char *name;
356 size_t len;
357 struct userdiff_driver *driver;
360 static int userdiff_find_by_namelen_cb(struct userdiff_driver *driver,
361 enum userdiff_driver_type type UNUSED,
362 void *priv)
364 struct find_by_namelen_data *cb_data = priv;
366 if (!xstrncmpz(driver->name, cb_data->name, cb_data->len)) {
367 cb_data->driver = driver;
368 return 1; /* tell the caller to stop iterating */
370 return 0;
373 static int regexec_supports_multi_byte_chars(void)
375 static const char not_space[] = "[^[:space:]]";
376 static const char utf8_multi_byte_char[] = "\xc2\xa3";
377 regex_t re;
378 regmatch_t match;
379 static int result = -1;
381 if (result != -1)
382 return result;
383 if (regcomp(&re, not_space, REG_EXTENDED))
384 BUG("invalid regular expression: %s", not_space);
385 result = !regexec(&re, utf8_multi_byte_char, 1, &match, 0) &&
386 match.rm_so == 0 &&
387 match.rm_eo == strlen(utf8_multi_byte_char);
388 regfree(&re);
389 return result;
392 static struct userdiff_driver *userdiff_find_by_namelen(const char *name, size_t len)
394 struct find_by_namelen_data udcbdata = {
395 .name = name,
396 .len = len,
398 for_each_userdiff_driver(userdiff_find_by_namelen_cb, &udcbdata);
399 return udcbdata.driver;
402 static int parse_funcname(struct userdiff_funcname *f, const char *k,
403 const char *v, int cflags)
405 f->pattern = NULL;
406 FREE_AND_NULL(f->pattern_owned);
407 if (git_config_string(&f->pattern_owned, k, v) < 0)
408 return -1;
409 f->pattern = f->pattern_owned;
410 f->cflags = cflags;
411 return 0;
414 static int parse_tristate(int *b, const char *k, const char *v)
416 if (v && !strcasecmp(v, "auto"))
417 *b = -1;
418 else
419 *b = git_config_bool(k, v);
420 return 0;
423 static int parse_bool(int *b, const char *k, const char *v)
425 *b = git_config_bool(k, v);
426 return 0;
429 int userdiff_config(const char *k, const char *v)
431 struct userdiff_driver *drv;
432 const char *name, *type;
433 size_t namelen;
435 if (parse_config_key(k, "diff", &name, &namelen, &type) || !name)
436 return 0;
438 drv = userdiff_find_by_namelen(name, namelen);
439 if (!drv) {
440 ALLOC_GROW(drivers, ndrivers+1, drivers_alloc);
441 drv = &drivers[ndrivers++];
442 memset(drv, 0, sizeof(*drv));
443 drv->name = xmemdupz(name, namelen);
444 drv->binary = -1;
447 if (!strcmp(type, "funcname"))
448 return parse_funcname(&drv->funcname, k, v, 0);
449 if (!strcmp(type, "xfuncname"))
450 return parse_funcname(&drv->funcname, k, v, REG_EXTENDED);
451 if (!strcmp(type, "binary"))
452 return parse_tristate(&drv->binary, k, v);
453 if (!strcmp(type, "command")) {
454 FREE_AND_NULL(drv->external.cmd);
455 return git_config_string(&drv->external.cmd, k, v);
457 if (!strcmp(type, "trustexitcode")) {
458 drv->external.trust_exit_code = git_config_bool(k, v);
459 return 0;
461 if (!strcmp(type, "textconv")) {
462 int ret;
463 FREE_AND_NULL(drv->textconv_owned);
464 ret = git_config_string(&drv->textconv_owned, k, v);
465 drv->textconv = drv->textconv_owned;
466 return ret;
468 if (!strcmp(type, "cachetextconv"))
469 return parse_bool(&drv->textconv_want_cache, k, v);
470 if (!strcmp(type, "wordregex")) {
471 int ret;
472 FREE_AND_NULL(drv->word_regex_owned);
473 ret = git_config_string(&drv->word_regex_owned, k, v);
474 drv->word_regex = drv->word_regex_owned;
475 return ret;
477 if (!strcmp(type, "algorithm")) {
478 int ret;
479 FREE_AND_NULL(drv->algorithm_owned);
480 ret = git_config_string(&drv->algorithm_owned, k, v);
481 drv->algorithm = drv->algorithm_owned;
482 return ret;
485 return 0;
488 struct userdiff_driver *userdiff_find_by_name(const char *name)
490 int len = strlen(name);
491 struct userdiff_driver *driver = userdiff_find_by_namelen(name, len);
492 if (driver && driver->word_regex_multi_byte) {
493 if (regexec_supports_multi_byte_chars())
494 driver->word_regex = driver->word_regex_multi_byte;
495 driver->word_regex_multi_byte = NULL;
497 return driver;
500 struct userdiff_driver *userdiff_find_by_path(struct index_state *istate,
501 const char *path)
503 static struct attr_check *check;
505 if (!check)
506 check = attr_check_initl("diff", NULL);
507 if (!path)
508 return NULL;
509 git_check_attr(istate, path, check);
511 if (ATTR_TRUE(check->items[0].value))
512 return &driver_true;
513 if (ATTR_FALSE(check->items[0].value))
514 return &driver_false;
515 if (ATTR_UNSET(check->items[0].value))
516 return NULL;
517 return userdiff_find_by_name(check->items[0].value);
520 struct userdiff_driver *userdiff_get_textconv(struct repository *r,
521 struct userdiff_driver *driver)
523 if (!driver->textconv)
524 return NULL;
526 if (driver->textconv_want_cache && !driver->textconv_cache &&
527 have_git_dir()) {
528 struct notes_cache *c = xmalloc(sizeof(*c));
529 struct strbuf name = STRBUF_INIT;
531 strbuf_addf(&name, "textconv/%s", driver->name);
532 notes_cache_init(r, c, name.buf, driver->textconv);
533 driver->textconv_cache = c;
534 strbuf_release(&name);
537 return driver;
540 static int for_each_userdiff_driver_list(each_userdiff_driver_fn fn,
541 enum userdiff_driver_type type, void *cb_data,
542 struct userdiff_driver *drv,
543 int drv_size)
545 int i;
546 int ret;
547 for (i = 0; i < drv_size; i++) {
548 struct userdiff_driver *item = drv + i;
549 if ((ret = fn(item, type, cb_data)))
550 return ret;
552 return 0;
555 int for_each_userdiff_driver(each_userdiff_driver_fn fn, void *cb_data)
557 int ret;
559 ret = for_each_userdiff_driver_list(fn, USERDIFF_DRIVER_TYPE_CUSTOM,
560 cb_data, drivers, ndrivers);
561 if (ret)
562 return ret;
564 ret = for_each_userdiff_driver_list(fn, USERDIFF_DRIVER_TYPE_BUILTIN,
565 cb_data, builtin_drivers,
566 ARRAY_SIZE(builtin_drivers));
567 if (ret)
568 return ret;
570 return 0;