Raw
1 #define USE_THE_REPOSITORY_VARIABLE
2 #define DISABLE_SIGN_COMPARE_WARNINGS
3
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"
10
11 static struct userdiff_driver *drivers;
12 static int ndrivers;
13 static int drivers_alloc;
14
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:]]", \
24 }
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:]]", \
34 }
35
36 /*
37 * Built-in drivers for various languages, sorted by their names
38 * (except that the "default" is left at the end).
39 *
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.
44 */
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 /* Everything after the function header is captured */
68 ".*$"
69 /* End of captured text */
70 ")",
71 /* -- */
72 /* Identifiers: variable and function names */
73 "[a-zA-Z_][a-zA-Z0-9_]*"
74 /* Shell variables: $VAR, ${VAR} */
75 "|\\$[a-zA-Z0-9_]+|\\$\\{"
76 /*Command list separators and redirection operators */
77 "|\\|\\||&&|<<|>>"
78 /* Operators ending in '=' (comparison + compound assignment) */
79 "|==|!=|<=|>=|[-+*/%&|^]="
80 /* Additional parameter expansion operators */
81 "|:=|:-|:\\+|:\\?|##|%%|\\^\\^|,,"
82 /* Command-line options (to avoid splitting -option) */
83 "|[-a-zA-Z0-9_]+"
84 /* Brackets and grouping symbols */
85 "|\\(|\\)|\\{|\\}|\\[|\\]"),
86 PATTERNS("bibtex",
87 "(@[a-zA-Z]{1,}[ \t]*\\{{0,1}[ \t]*[^ \t\"@',\\#}{~%]*).*$",
88 /* -- */
89 "[={}\"]|[^={}\" \t]+"),
90 PATTERNS("cpp",
91 /* Jump targets or access declarations */
92 "!^[ \t]*[A-Za-z_][A-Za-z_0-9]*:[[:space:]]*($|/[/*])\n"
93 /* functions/methods, variables, and compounds at top level */
94 "^((::[[:space:]]*)?[A-Za-z_].*)$",
95 /* -- */
96 /* identifiers and keywords */
97 "[a-zA-Z_][a-zA-Z0-9_]*"
98 /* decimal and octal integers as well as floatingpoint numbers */
99 "|[0-9][0-9.]*([Ee][-+]?[0-9]+)?[fFlLuU]*"
100 /* hexadecimal and binary integers */
101 "|0[xXbB][0-9a-fA-F]+[lLuU]*"
102 /* floatingpoint numbers that begin with a decimal point */
103 "|\\.[0-9][0-9]*([Ee][-+]?[0-9]+)?[fFlL]?"
104 "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->\\*?|\\.\\*|<=>"),
105 PATTERNS("csharp",
106 /*
107 * Jump over reserved keywords which are illegal method names, but which
108 * can be followed by parentheses without special characters in between,
109 * making them look like methods.
110 */
111 "!(^|[ \t]+)" /* Start of line or whitespace. */
112 "(do|while|for|foreach|if|else|new|default|return|switch|case|throw"
113 "|catch|using|lock|fixed)"
114 "([ \t(]+|$)\n" /* Whitespace, "(", or end of line. */
115 /*
116 * Methods/constructors:
117 * The strategy is to identify a minimum of two groups (any combination
118 * of keywords/type/name) before the opening parenthesis, and without
119 * final unexpected characters, normally only used in ordinary statements.
120 */
121 "^[ \t]*" /* Remove leading whitespace. */
122 "(" /* Start chunk header capture. */
123 "(" /* First group. */
124 "[][[:alnum:]@_.]" /* Name. */
125 "(<[][[:alnum:]@_, \t<>]+>)?" /* Optional generic parameters. */
126 ")+"
127 "([ \t]+" /* Subsequent groups, prepended with space. */
128 "([][[:alnum:]@_.](<[][[:alnum:]@_, \t<>]+>)?)+"
129 ")+"
130 "[ \t]*" /* Optional space before parameters start. */
131 "\\(" /* Start of method parameters. */
132 "[^;]*" /* Allow complex parameters, but exclude statements (;). */
133 ")$\n" /* Close chunk header capture. */
134 /*
135 * Properties:
136 * As with methods, expect a minimum of two groups. But, more trivial than
137 * methods, the vast majority of properties long enough to be worth
138 * showing a chunk header for don't include "=:;,()" on the line they are
139 * defined, since they don't have a parameter list.
140 */
141 "^[ \t]*("
142 "([][[:alnum:]@_.](<[][[:alnum:]@_, \t<>]+>)?)+"
143 "([ \t]+"
144 "([][[:alnum:]@_.](<[][[:alnum:]@_, \t<>]+>)?)+"
145 ")+" /* Up to here, same as methods regex. */
146 "[^;=:,()]*" /* Compared to methods, no parameter list allowed. */
147 ")$\n"
148 /* Type definitions */
149 "^[ \t]*(((static|public|internal|private|protected|new|unsafe|sealed|abstract|partial)[ \t]+)*(class|enum|interface|struct|record)[ \t]+.*)$\n"
150 /* Namespace */
151 "^[ \t]*(namespace[ \t]+.*)$",
152 /* -- */
153 "[a-zA-Z_][a-zA-Z0-9_]*"
154 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?"
155 "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->"),
156 IPATTERN("css",
157 "![:;][[:space:]]*$\n"
158 "^[:[@.#]?[_a-z0-9].*$",
159 /* -- */
160 /*
161 * This regex comes from W3C CSS specs. Should theoretically also
162 * allow ISO 10646 characters U+00A0 and higher,
163 * but they are not handled in this regex.
164 */
165 "-?[_a-zA-Z][-_a-zA-Z0-9]*" /* identifiers */
166 "|-?[0-9]+|\\#[0-9a-fA-F]+" /* numbers */
167 ),
168 PATTERNS("dts",
169 "!;\n"
170 "!=\n"
171 /* lines beginning with a word optionally preceded by '&' or the root */
172 "^[ \t]*((/[ \t]*\\{|&?[a-zA-Z_]).*)",
173 /* -- */
174 /* Property names and math operators */
175 "[a-zA-Z0-9,._+?#-]+"
176 "|[-+*/%&^|!~]|>>|<<|&&|\\|\\|"),
177 PATTERNS("elixir",
178 "^[ \t]*((def(macro|module|impl|protocol|p)?|test)[ \t].*)$",
179 /* -- */
180 /* Atoms, names, and module attributes */
181 "[@:]?[a-zA-Z0-9@_?!]+"
182 /* Numbers with specific base */
183 "|[-+]?0[xob][0-9a-fA-F]+"
184 /* Numbers */
185 "|[-+]?[0-9][0-9_.]*([eE][-+]?[0-9_]+)?"
186 /* Operators and atoms that represent them */
187 "|:?(\\+\\+|--|\\.\\.|~~~|<>|\\^\\^\\^|<?\\|>|<<<?|>?>>|<<?~|~>?>|<~>|<=|>=|===?|!==?|=~|&&&?|\\|\\|\\|?|=>|<-|\\\\\\\\|->)"
188 /* Not real operators, but should be grouped */
189 "|:?%[A-Za-z0-9_.]\\{\\}?"),
190 IPATTERN("fortran",
191 /* Don't match comment lines */
192 "!^([C*]|[ \t]*!)\n"
193 /* Don't match 'module procedure' lines */
194 "!^[ \t]*MODULE[ \t]+PROCEDURE[ \t]\n"
195 /* Program, module, block data */
196 "^[ \t]*((END[ \t]+)?(PROGRAM|MODULE|BLOCK[ \t]+DATA"
197 /* Subroutines and functions */
198 "|([^!'\" \t]+[ \t]+)*(SUBROUTINE|FUNCTION))[ \t]+[A-Z].*)$",
199 /* -- */
200 "[a-zA-Z][a-zA-Z0-9_]*"
201 "|\\.([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])\\."
202 /* numbers and format statements like 2E14.4, or ES12.6, 9X.
203 * Don't worry about format statements without leading digits since
204 * they would have been matched above as a variable anyway. */
205 "|[-+]?[0-9.]+([AaIiDdEeFfLlTtXx][Ss]?[-+]?[0-9.]*)?(_[a-zA-Z0-9][a-zA-Z0-9_]*)?"
206 "|//|\\*\\*|::|[/<>=]="),
207 IPATTERN("fountain",
208 "^((\\.[^.]|(int|ext|est|int\\.?/ext|i/e)[. ]).*)$",
209 /* -- */
210 "[^ \t-]+"),
211 PATTERNS("golang",
212 /* Functions */
213 "^[ \t]*(func[ \t]*.*(\\{[ \t]*)?)\n"
214 /* Structs and interfaces */
215 "^[ \t]*(type[ \t].*(struct|interface)[ \t]*(\\{[ \t]*)?)",
216 /* -- */
217 "[a-zA-Z_][a-zA-Z0-9_]*"
218 "|[-+0-9.eE]+i?|0[xX]?[0-9a-fA-F]+i?"
219 "|[-+*/<>%&^|=!:]=|--|\\+\\+|<<=?|>>=?|&\\^=?|&&|\\|\\||<-|\\.{3}"),
220 PATTERNS("html",
221 "^[ \t]*(<[Hh][1-6]([ \t].*)?>.*)$",
222 /* -- */
223 "[^<>= \t]+"),
224 PATTERNS("ini",
225 "^[ \t]*\\[[^]]+\\]",
226 /* -- */
227 "[^ \t]+"),
228 PATTERNS("java",
229 "!^[ \t]*(catch|do|for|if|instanceof|new|return|switch|throw|while)\n"
230 /* Class, enum, interface, and record declarations */
231 "^[ \t]*(([a-z-]+[ \t]+)*(class|enum|interface|record)[ \t]+.*)$\n"
232 /* Method definitions; note that constructor signatures are not */
233 /* matched because they are indistinguishable from method calls. */
234 "^[ \t]*(([A-Za-z_<>&][][?&<>.,A-Za-z_0-9]*[ \t]+)+[A-Za-z_][A-Za-z_0-9]*[ \t]*\\([^;]*)$",
235 /* -- */
236 "[a-zA-Z_][a-zA-Z0-9_]*"
237 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?"
238 "|[-+*/<>%&^|=!]="
239 "|--|\\+\\+|<<=?|>>>?=?|&&|\\|\\|"),
240 PATTERNS("kotlin",
241 "^[ \t]*(([a-z]+[ \t]+)*(fun|class|interface)[ \t]+.*)$",
242 /* -- */
243 "[a-zA-Z_][a-zA-Z0-9_]*"
244 /* hexadecimal and binary numbers */
245 "|0[xXbB][0-9a-fA-F_]+[lLuU]*"
246 /* integers and floats */
247 "|[0-9][0-9_]*([.][0-9_]*)?([Ee][-+]?[0-9]+)?[fFlLuU]*"
248 /* floating point numbers beginning with decimal point */
249 "|[.][0-9][0-9_]*([Ee][-+]?[0-9]+)?[fFlLuU]?"
250 /* unary and binary operators */
251 "|[-+*/<>%&^|=!]==?|--|\\+\\+|<<=|>>=|&&|\\|\\||->|\\.\\*|!!|[?:.][.:]"),
252 PATTERNS("markdown",
253 "^ {0,3}#{1,6}[ \t].*",
254 /* -- */
255 "[^<>= \t]+"),
256 PATTERNS("matlab",
257 /*
258 * Octave pattern is mostly the same as matlab, except that '%%%' and
259 * '##' can also be used to begin code sections, in addition to '%%'
260 * that is understood by both.
261 */
262 "^[[:space:]]*((classdef|function)[[:space:]].*)$|^(%%%?|##)[[:space:]].*$",
263 /* -- */
264 "[a-zA-Z_][a-zA-Z0-9_]*|[-+0-9.e]+|[=~<>]=|\\.[*/\\^']|\\|\\||&&"),
265 PATTERNS("objc",
266 /* Negate C statements that can look like functions */
267 "!^[ \t]*(do|for|if|else|return|switch|while)\n"
268 /* Objective-C methods */
269 "^[ \t]*([-+][ \t]*\\([ \t]*[A-Za-z_][A-Za-z_0-9* \t]*\\)[ \t]*[A-Za-z_].*)$\n"
270 /* C functions */
271 "^[ \t]*(([A-Za-z_][A-Za-z_0-9]*[ \t]+)+[A-Za-z_][A-Za-z_0-9]*[ \t]*\\([^;]*)$\n"
272 /* Objective-C class/protocol definitions */
273 "^(@(implementation|interface|protocol)[ \t].*)$",
274 /* -- */
275 "[a-zA-Z_][a-zA-Z0-9_]*"
276 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?"
277 "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->"),
278 PATTERNS("pascal",
279 "^(((class[ \t]+)?(procedure|function)|constructor|destructor|interface"
280 "|implementation|initialization|finalization)[ \t]*.*)$\n"
281 "^(.*=[ \t]*(class|record).*)$",
282 /* -- */
283 "[a-zA-Z_][a-zA-Z0-9_]*"
284 "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+"
285 "|<>|<=|>=|:=|\\.\\."),
286 PATTERNS("perl",
287 "^package .*\n"
288 "^sub [[:alnum:]_':]+[ \t]*"
289 "(\\([^)]*\\)[ \t]*)?" /* prototype */
290 /*
291 * Attributes. A regex can't count nested parentheses,
292 * so just slurp up whatever we see, taking care not
293 * to accept lines like "sub foo; # defined elsewhere".
294 *
295 * An attribute could contain a semicolon, but at that
296 * point it seems reasonable enough to give up.
297 */
298 "(:[^;#]*)?"
299 "(\\{[ \t]*)?" /* brace can come here or on the next line */
300 "(#.*)?$\n" /* comment */
301 "^(BEGIN|END|INIT|CHECK|UNITCHECK|AUTOLOAD|DESTROY)[ \t]*"
302 "(\\{[ \t]*)?" /* brace can come here or on the next line */
303 "(#.*)?$\n"
304 "^=head[0-9] .*", /* POD */
305 /* -- */
306 "[[:alpha:]_'][[:alnum:]_']*"
307 "|0[xb]?[0-9a-fA-F_]*"
308 /* taking care not to interpret 3..5 as (3.)(.5) */
309 "|[0-9a-fA-F_]+(\\.[0-9a-fA-F_]+)?([eE][-+]?[0-9_]+)?"
310 "|=>|-[rwxoRWXOezsfdlpSugkbctTBMAC>]|~~|::"
311 "|&&=|\\|\\|=|//=|\\*\\*="
312 "|&&|\\|\\||//|\\+\\+|--|\\*\\*|\\.\\.\\.?"
313 "|[-+*/%.^&<>=!|]="
314 "|=~|!~"
315 "|<<|<>|<=>|>>"),
316 PATTERNS("php",
317 "^[\t ]*(((public|protected|private|static|abstract|final)[\t ]+)*function.*)$\n"
318 "^[\t ]*((((final|abstract)[\t ]+)?class|enum|interface|trait).*)$",
319 /* -- */
320 "[a-zA-Z_][a-zA-Z0-9_]*"
321 "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+"
322 "|[-+*/<>%&^|=!.]=|--|\\+\\+|<<=?|>>=?|===|&&|\\|\\||::|->"),
323 PATTERNS("python",
324 "^[ \t]*((class|(async[ \t]+)?def)[ \t].*)$",
325 /* -- */
326 "[a-zA-Z_][a-zA-Z0-9_]*"
327 "|[-+0-9.e]+[jJlL]?|0[xX]?[0-9a-fA-F]+[lL]?"
328 "|[-+*/<>%&^|=!]=|//=?|<<=?|>>=?|\\*\\*=?"),
329 /* -- */
330 PATTERNS("r",
331 "^[ \t]*([a-zA-z][a-zA-Z0-9_.]*[ \t]*(<-|=)[ \t]*function.*)$",
332 /* -- */
333 "[^ \t]+"),
334 PATTERNS("ruby",
335 "^[ \t]*((class|module|def)[ \t].*)$",
336 /* -- */
337 "(@|@@|\\$)?[a-zA-Z_][a-zA-Z0-9_]*"
338 "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+|\\?(\\\\C-)?(\\\\M-)?."
339 "|//=?|[-+*/<>%&^|=!]=|<<=?|>>=?|===|\\.{1,3}|::|[!=]~"),
340 PATTERNS("rust",
341 "^[\t ]*((pub(\\([^\\)]+\\))?[\t ]+)?((async|const|unsafe|extern([\t ]+\"[^\"]+\"))[\t ]+)?(struct|enum|union|mod|trait|fn|impl|macro_rules!)[< \t]+[^;]*)$",
342 /* -- */
343 "[a-zA-Z_][a-zA-Z0-9_]*"
344 "|[0-9][0-9_a-fA-Fiosuxz]*(\\.([0-9]*[eE][+-]?)?[0-9_fF]*)?"
345 "|[-+*\\/<>%&^|=!:]=|<<=?|>>=?|&&|\\|\\||->|=>|\\.{2}=|\\.{3}|::"),
346 PATTERNS("scheme",
347 /*
348 * An unindented opening parenthesis identifies a top-level
349 * expression in all Lisp dialects.
350 */
351 "^(\\(.*)$\n"
352 /* For Scheme: a possibly indented left paren followed by a keyword. */
353 "^[\t ]*(\\(((define|def(struct|syntax|class|method|rules|record|proto|alias)?)[-*/ \t]|(library|module|struct|class)[*+ \t]).*)$\n"
354 /*
355 * For all Lisp dialects: a slightly indented line starting with "(def".
356 */
357 "^ ?(\\([Dd][Ee][Ff].*)$",
358 /*
359 * The union of R7RS and Common Lisp symbol syntax: allows arbitrary
360 * strings between vertical bars, including any escaped characters.
361 */
362 "\\|([^|\\\\]|\\\\.)*\\|"
363 /* All other words should be delimited by spaces or parentheses. */
364 "|([^][)(}{ \t])+"),
365 PATTERNS("tex", "^(\\\\((sub)*section|chapter|part)\\*{0,1}\\{.*)$",
366 "\\\\[a-zA-Z@]+|\\\\.|([a-zA-Z0-9]|[^\x01-\x7f])+"),
367 { .name = "default", .binary = -1 },
368 };
369 #undef PATTERNS
370 #undef IPATTERN
371
372 static struct userdiff_driver driver_true = {
373 .name = "diff=true",
374 .binary = 0,
375 };
376
377 static struct userdiff_driver driver_false = {
378 .name = "!diff",
379 .binary = 1,
380 };
381
382 struct find_by_namelen_data {
383 const char *name;
384 size_t len;
385 struct userdiff_driver *driver;
386 };
387
388 static int userdiff_find_by_namelen_cb(struct userdiff_driver *driver,
389 enum userdiff_driver_type type UNUSED,
390 void *priv)
391 {
392 struct find_by_namelen_data *cb_data = priv;
393
394 if (!xstrncmpz(driver->name, cb_data->name, cb_data->len)) {
395 cb_data->driver = driver;
396 return 1; /* tell the caller to stop iterating */
397 }
398 return 0;
399 }
400
401 static int regexec_supports_multi_byte_chars(void)
402 {
403 static const char not_space[] = "[^[:space:]]";
404 static const char utf8_multi_byte_char[] = "\xc2\xa3";
405 regex_t re;
406 regmatch_t match;
407 static int result = -1;
408
409 if (result != -1)
410 return result;
411 if (regcomp(&re, not_space, REG_EXTENDED))
412 BUG("invalid regular expression: %s", not_space);
413 result = !regexec(&re, utf8_multi_byte_char, 1, &match, 0) &&
414 match.rm_so == 0 &&
415 match.rm_eo == strlen(utf8_multi_byte_char);
416 regfree(&re);
417 return result;
418 }
419
420 static struct userdiff_driver *userdiff_find_by_namelen(const char *name, size_t len)
421 {
422 struct find_by_namelen_data udcbdata = {
423 .name = name,
424 .len = len,
425 };
426 for_each_userdiff_driver(userdiff_find_by_namelen_cb, &udcbdata);
427 return udcbdata.driver;
428 }
429
430 static int parse_funcname(struct userdiff_funcname *f, const char *k,
431 const char *v, int cflags)
432 {
433 f->pattern = NULL;
434 FREE_AND_NULL(f->pattern_owned);
435 if (git_config_string(&f->pattern_owned, k, v) < 0)
436 return -1;
437 f->pattern = f->pattern_owned;
438 f->cflags = cflags;
439 return 0;
440 }
441
442 static int parse_tristate(int *b, const char *k, const char *v)
443 {
444 if (v && !strcasecmp(v, "auto"))
445 *b = -1;
446 else
447 *b = git_config_bool(k, v);
448 return 0;
449 }
450
451 static int parse_bool(int *b, const char *k, const char *v)
452 {
453 *b = git_config_bool(k, v);
454 return 0;
455 }
456
457 int userdiff_config(const char *k, const char *v)
458 {
459 struct userdiff_driver *drv;
460 const char *name, *type;
461 size_t namelen;
462
463 if (parse_config_key(k, "diff", &name, &namelen, &type) || !name)
464 return 0;
465
466 drv = userdiff_find_by_namelen(name, namelen);
467 if (!drv) {
468 ALLOC_GROW(drivers, ndrivers+1, drivers_alloc);
469 drv = &drivers[ndrivers++];
470 memset(drv, 0, sizeof(*drv));
471 drv->name = xmemdupz(name, namelen);
472 drv->binary = -1;
473 }
474
475 if (!strcmp(type, "funcname"))
476 return parse_funcname(&drv->funcname, k, v, 0);
477 if (!strcmp(type, "xfuncname"))
478 return parse_funcname(&drv->funcname, k, v, REG_EXTENDED);
479 if (!strcmp(type, "binary"))
480 return parse_tristate(&drv->binary, k, v);
481 if (!strcmp(type, "command")) {
482 FREE_AND_NULL(drv->external.cmd);
483 return git_config_string(&drv->external.cmd, k, v);
484 }
485 if (!strcmp(type, "trustexitcode")) {
486 drv->external.trust_exit_code = git_config_bool(k, v);
487 return 0;
488 }
489 if (!strcmp(type, "textconv")) {
490 int ret;
491 FREE_AND_NULL(drv->textconv_owned);
492 ret = git_config_string(&drv->textconv_owned, k, v);
493 drv->textconv = drv->textconv_owned;
494 return ret;
495 }
496 if (!strcmp(type, "cachetextconv"))
497 return parse_bool(&drv->textconv_want_cache, k, v);
498 if (!strcmp(type, "wordregex")) {
499 int ret;
500 FREE_AND_NULL(drv->word_regex_owned);
501 ret = git_config_string(&drv->word_regex_owned, k, v);
502 drv->word_regex = drv->word_regex_owned;
503 return ret;
504 }
505 if (!strcmp(type, "algorithm")) {
506 int ret;
507 FREE_AND_NULL(drv->algorithm_owned);
508 ret = git_config_string(&drv->algorithm_owned, k, v);
509 drv->algorithm = drv->algorithm_owned;
510 return ret;
511 }
512
513 return 0;
514 }
515
516 struct userdiff_driver *userdiff_find_by_name(const char *name)
517 {
518 int len = strlen(name);
519 struct userdiff_driver *driver = userdiff_find_by_namelen(name, len);
520 if (driver && driver->word_regex_multi_byte) {
521 if (regexec_supports_multi_byte_chars())
522 driver->word_regex = driver->word_regex_multi_byte;
523 driver->word_regex_multi_byte = NULL;
524 }
525 return driver;
526 }
527
528 struct userdiff_driver *userdiff_find_by_path(struct index_state *istate,
529 const char *path)
530 {
531 static struct attr_check *check;
532
533 if (!check)
534 check = attr_check_initl("diff", NULL);
535 if (!path)
536 return NULL;
537 git_check_attr(istate, path, check);
538
539 if (ATTR_TRUE(check->items[0].value))
540 return &driver_true;
541 if (ATTR_FALSE(check->items[0].value))
542 return &driver_false;
543 if (ATTR_UNSET(check->items[0].value))
544 return NULL;
545 return userdiff_find_by_name(check->items[0].value);
546 }
547
548 struct userdiff_driver *userdiff_get_textconv(struct repository *r,
549 struct userdiff_driver *driver)
550 {
551 if (!driver->textconv)
552 return NULL;
553
554 if (driver->textconv_want_cache && !driver->textconv_cache &&
555 have_git_dir()) {
556 struct notes_cache *c = xmalloc(sizeof(*c));
557 struct strbuf name = STRBUF_INIT;
558
559 strbuf_addf(&name, "textconv/%s", driver->name);
560 notes_cache_init(r, c, name.buf, driver->textconv);
561 driver->textconv_cache = c;
562 strbuf_release(&name);
563 }
564
565 return driver;
566 }
567
568 static int for_each_userdiff_driver_list(each_userdiff_driver_fn fn,
569 enum userdiff_driver_type type, void *cb_data,
570 struct userdiff_driver *drv,
571 int drv_size)
572 {
573 int i;
574 int ret;
575 for (i = 0; i < drv_size; i++) {
576 struct userdiff_driver *item = drv + i;
577 if ((ret = fn(item, type, cb_data)))
578 return ret;
579 }
580 return 0;
581 }
582
583 int for_each_userdiff_driver(each_userdiff_driver_fn fn, void *cb_data)
584 {
585 int ret;
586
587 ret = for_each_userdiff_driver_list(fn, USERDIFF_DRIVER_TYPE_CUSTOM,
588 cb_data, drivers, ndrivers);
589 if (ret)
590 return ret;
591
592 ret = for_each_userdiff_driver_list(fn, USERDIFF_DRIVER_TYPE_BUILTIN,
593 cb_data, builtin_drivers,
594 ARRAY_SIZE(builtin_drivers));
595 if (ret)
596 return ret;
597
598 return 0;
599 }