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