]> git.ipfire.org Git - thirdparty/git.git/blame - userdiff.c
Merge branch 'hs/rebase-not-in-progress' into HEAD
[thirdparty/git.git] / userdiff.c
CommitLineData
36bf1958 1#include "git-compat-util.h"
b2141fc1 2#include "config.h"
be58e70d 3#include "userdiff.h"
be58e70d 4#include "attr.h"
36bf1958 5#include "strbuf.h"
be58e70d
JK
6
7static struct userdiff_driver *drivers;
8static int ndrivers;
9static int drivers_alloc;
10
2dd75f12
ÆAB
11#define PATTERNS(lang, rx, wrx) { \
12 .name = lang, \
13 .binary = -1, \
14 .funcname = { \
15 .pattern = rx, \
16 .cflags = REG_EXTENDED, \
17 }, \
18 .word_regex = wrx "|[^[:space:]]|[\xc0-\xff][\x80-\xbf]+", \
be391449 19 .word_regex_multi_byte = wrx "|[^[:space:]]", \
2dd75f12
ÆAB
20}
21#define IPATTERN(lang, rx, wrx) { \
22 .name = lang, \
23 .binary = -1, \
24 .funcname = { \
25 .pattern = rx, \
26 .cflags = REG_EXTENDED | REG_ICASE, \
27 }, \
28 .word_regex = wrx "|[^[:space:]]|[\xc0-\xff][\x80-\xbf]+", \
be391449 29 .word_regex_multi_byte = wrx "|[^[:space:]]", \
2dd75f12 30}
b6029b32
JH
31
32/*
33 * Built-in drivers for various languages, sorted by their names
34 * (except that the "default" is left at the end).
35 *
36 * When writing or updating patterns, assume that the contents these
37 * patterns are applied to are syntactically correct. The patterns
38 * can be simple without implementing all syntactical corner cases, as
39 * long as they are sufficiently permissive.
40 */
be58e70d 41static struct userdiff_driver builtin_drivers[] = {
e90d065e 42IPATTERN("ada",
39a87a29 43 "!^(.*[ \t])?(is[ \t]+new|renames|is[ \t]+separate)([ \t].*)?$\n"
e90d065e
AJ
44 "!^[ \t]*with[ \t].*$\n"
45 "^[ \t]*((procedure|function)[ \t]+.*)$\n"
46 "^[ \t]*((package|protected|task)[ \t]+.*)$",
47 /* -- */
48 "[a-zA-Z][a-zA-Z0-9_]*"
39a87a29 49 "|[-+]?[0-9][0-9#_.aAbBcCdDeEfF]*([eE][+-]?[0-9_]+)?"
e90d065e 50 "|=>|\\.\\.|\\*\\*|:=|/=|>=|<=|<<|>>|<>"),
2ff6c346
VE
51PATTERNS("bash",
52 /* Optional leading indentation */
53 "^[ \t]*"
54 /* Start of captured text */
55 "("
56 "("
57 /* POSIX identifier with mandatory parentheses */
58 "[a-zA-Z_][a-zA-Z0-9_]*[ \t]*\\([ \t]*\\))"
59 "|"
60 /* Bashism identifier with optional parentheses */
61 "(function[ \t]+[a-zA-Z_][a-zA-Z0-9_]*(([ \t]*\\([ \t]*\\))|([ \t]+))"
62 ")"
63 /* Optional whitespace */
64 "[ \t]*"
65 /* Compound command starting with `{`, `(`, `((` or `[[` */
66 "(\\{|\\(\\(?|\\[\\[)"
67 /* End of captured text */
68 ")",
69 /* -- */
70 /* Characters not in the default $IFS value */
71 "[^ \t]+"),
6d1c9c52
ÆAB
72PATTERNS("bibtex",
73 "(@[a-zA-Z]{1,}[ \t]*\\{{0,1}[ \t]*[^ \t\"@',\\#}{~%]*).*$",
74 /* -- */
ddd164d0
ÆAB
75 "[={}\"]|[^={}\" \t]+"),
76PATTERNS("cpp",
77 /* Jump targets or access declarations */
78 "!^[ \t]*[A-Za-z_][A-Za-z_0-9]*:[[:space:]]*($|/[/*])\n"
79 /* functions/methods, variables, and compounds at top level */
80 "^((::[[:space:]]*)?[A-Za-z_].*)$",
81 /* -- */
350b87cd 82 /* identifiers and keywords */
ddd164d0 83 "[a-zA-Z_][a-zA-Z0-9_]*"
350b87cd 84 /* decimal and octal integers as well as floatingpoint numbers */
386076ec 85 "|[0-9][0-9.]*([Ee][-+]?[0-9]+)?[fFlLuU]*"
350b87cd 86 /* hexadecimal and binary integers */
386076ec 87 "|0[xXbB][0-9a-fA-F]+[lLuU]*"
350b87cd 88 /* floatingpoint numbers that begin with a decimal point */
386076ec 89 "|\\.[0-9][0-9]*([Ee][-+]?[0-9]+)?[fFlL]?"
c4fdba33 90 "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->\\*?|\\.\\*|<=>"),
ddd164d0
ÆAB
91PATTERNS("csharp",
92 /* Keywords */
93 "!^[ \t]*(do|while|for|if|else|instanceof|new|return|switch|case|throw|catch|using)\n"
94 /* Methods and constructors */
95 "^[ \t]*(((static|public|internal|private|protected|new|virtual|sealed|override|unsafe|async)[ \t]+)*[][<>@.~_[:alnum:]]+[ \t]+[<>@._[:alnum:]]+[ \t]*\\(.*\\))[ \t]*$\n"
96 /* Properties */
97 "^[ \t]*(((static|public|internal|private|protected|new|virtual|sealed|override|unsafe)[ \t]+)*[][<>@.~_[:alnum:]]+[ \t]+[@._[:alnum:]]+)[ \t]*$\n"
98 /* Type definitions */
c4e31781 99 "^[ \t]*(((static|public|internal|private|protected|new|unsafe|sealed|abstract|partial)[ \t]+)*(class|enum|interface|struct|record)[ \t]+.*)$\n"
ddd164d0
ÆAB
100 /* Namespace */
101 "^[ \t]*(namespace[ \t]+.*)$",
102 /* -- */
103 "[a-zA-Z_][a-zA-Z0-9_]*"
104 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?"
105 "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->"),
106IPATTERN("css",
107 "![:;][[:space:]]*$\n"
108 "^[:[@.#]?[_a-z0-9].*$",
109 /* -- */
110 /*
111 * This regex comes from W3C CSS specs. Should theoretically also
112 * allow ISO 10646 characters U+00A0 and higher,
113 * but they are not handled in this regex.
114 */
115 "-?[_a-zA-Z][-_a-zA-Z0-9]*" /* identifiers */
116 "|-?[0-9]+|\\#[0-9a-fA-F]+" /* numbers */
117),
3c81760b
SB
118PATTERNS("dts",
119 "!;\n"
8da56a48 120 "!=\n"
3c81760b 121 /* lines beginning with a word optionally preceded by '&' or the root */
8da56a48 122 "^[ \t]*((/[ \t]*\\{|&?[a-zA-Z_]).*)",
3c81760b
SB
123 /* -- */
124 /* Property names and math operators */
125 "[a-zA-Z0-9,._+?#-]+"
126 "|[-+*/%&^|!~]|>>|<<|&&|\\|\\|"),
a807200f
ŁN
127PATTERNS("elixir",
128 "^[ \t]*((def(macro|module|impl|protocol|p)?|test)[ \t].*)$",
d1b1384d 129 /* -- */
a807200f 130 /* Atoms, names, and module attributes */
d1b1384d 131 "[@:]?[a-zA-Z0-9@_?!]+"
a807200f
ŁN
132 /* Numbers with specific base */
133 "|[-+]?0[xob][0-9a-fA-F]+"
134 /* Numbers */
135 "|[-+]?[0-9][0-9_.]*([eE][-+]?[0-9_]+)?"
136 /* Operators and atoms that represent them */
137 "|:?(\\+\\+|--|\\.\\.|~~~|<>|\\^\\^\\^|<?\\|>|<<<?|>?>>|<<?~|~>?>|<~>|<=|>=|===?|!==?|=~|&&&?|\\|\\|\\|?|=>|<-|\\\\\\\\|->)"
138 /* Not real operators, but should be grouped */
139 "|:?%[A-Za-z0-9_.]\\{\\}?"),
909a5494 140IPATTERN("fortran",
b79e6925 141 /* Don't match comment lines */
909a5494 142 "!^([C*]|[ \t]*!)\n"
b79e6925 143 /* Don't match 'module procedure' lines */
909a5494 144 "!^[ \t]*MODULE[ \t]+PROCEDURE[ \t]\n"
b79e6925 145 /* Program, module, block data */
909a5494 146 "^[ \t]*((END[ \t]+)?(PROGRAM|MODULE|BLOCK[ \t]+DATA"
b79e6925 147 /* Subroutines and functions */
75c3b6b2 148 "|([^!'\" \t]+[ \t]+)*(SUBROUTINE|FUNCTION))[ \t]+[A-Z].*)$",
909a5494
BC
149 /* -- */
150 "[a-zA-Z][a-zA-Z0-9_]*"
151 "|\\.([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])\\."
152 /* numbers and format statements like 2E14.4, or ES12.6, 9X.
153 * Don't worry about format statements without leading digits since
154 * they would have been matched above as a variable anyway. */
155 "|[-+]?[0-9.]+([AaIiDdEeFfLlTtXx][Ss]?[-+]?[0-9.]*)?(_[a-zA-Z0-9][a-zA-Z0-9_]*)?"
664d44ee 156 "|//|\\*\\*|::|[/<>=]="),
6d1c9c52
ÆAB
157IPATTERN("fountain",
158 "^((\\.[^.]|(int|ext|est|int\\.?/ext|i/e)[. ]).*)$",
159 /* -- */
69f9c87d 160 "[^ \t-]+"),
1dbf0c0a
AG
161PATTERNS("golang",
162 /* Functions */
163 "^[ \t]*(func[ \t]*.*(\\{[ \t]*)?)\n"
164 /* Structs and interfaces */
165 "^[ \t]*(type[ \t].*(struct|interface)[ \t]*(\\{[ \t]*)?)",
166 /* -- */
167 "[a-zA-Z_][a-zA-Z0-9_]*"
168 "|[-+0-9.eE]+i?|0[xX]?[0-9a-fA-F]+i?"
169 "|[-+*/<>%&^|=!:]=|--|\\+\\+|<<=?|>>=?|&\\^=?|&&|\\|\\||<-|\\.{3}"),
6d1c9c52
ÆAB
170PATTERNS("html",
171 "^[ \t]*(<[Hh][1-6]([ \t].*)?>.*)$",
172 /* -- */
664d44ee 173 "[^<>= \t]+"),
80c49c3d 174PATTERNS("java",
be58e70d 175 "!^[ \t]*(catch|do|for|if|instanceof|new|return|switch|throw|while)\n"
575e6fcf 176 /* Class, enum, interface, and record declarations */
93d52ed0 177 "^[ \t]*(([a-z-]+[ \t]+)*(class|enum|interface|record)[ \t]+.*)$\n"
a8cbc895
TH
178 /* Method definitions; note that constructor signatures are not */
179 /* matched because they are indistinguishable from method calls. */
180 "^[ \t]*(([A-Za-z_<>&][][?&<>.,A-Za-z_0-9]*[ \t]+)+[A-Za-z_][A-Za-z_0-9]*[ \t]*\\([^;]*)$",
959e2e64 181 /* -- */
80c49c3d
TR
182 "[a-zA-Z_][a-zA-Z0-9_]*"
183 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?"
184 "|[-+*/<>%&^|=!]="
664d44ee 185 "|--|\\+\\+|<<=?|>>>?=?|&&|\\|\\|"),
09188ed9
JD
186PATTERNS("kotlin",
187 "^[ \t]*(([a-z]+[ \t]+)*(fun|class|interface)[ \t]+.*)$",
188 /* -- */
189 "[a-zA-Z_][a-zA-Z0-9_]*"
190 /* hexadecimal and binary numbers */
191 "|0[xXbB][0-9a-fA-F_]+[lLuU]*"
192 /* integers and floats */
193 "|[0-9][0-9_]*([.][0-9_]*)?([Ee][-+]?[0-9]+)?[fFlLuU]*"
194 /* floating point numbers beginning with decimal point */
195 "|[.][0-9][0-9_]*([Ee][-+]?[0-9]+)?[fFlLuU]?"
196 /* unary and binary operators */
197 "|[-+*/<>%&^|=!]==?|--|\\+\\+|<<=|>>=|&&|\\|\\||->|\\.\\*|!!|[?:.][.:]"),
09dad925
AH
198PATTERNS("markdown",
199 "^ {0,3}#{1,6}[ \t].*",
6d1c9c52 200 /* -- */
09dad925 201 "[^<>= \t]+"),
53b10a14 202PATTERNS("matlab",
2731a784
BL
203 /*
204 * Octave pattern is mostly the same as matlab, except that '%%%' and
91bf382f 205 * '##' can also be used to begin code sections, in addition to '%%'
2731a784
BL
206 * that is understood by both.
207 */
91bf382f 208 "^[[:space:]]*((classdef|function)[[:space:]].*)$|^(%%%?|##)[[:space:]].*$",
6d1c9c52 209 /* -- */
53b10a14 210 "[a-zA-Z_][a-zA-Z0-9_]*|[-+0-9.e]+|[=~<>]=|\\.[*/\\^']|\\|\\||&&"),
80c49c3d 211PATTERNS("objc",
be58e70d
JK
212 /* Negate C statements that can look like functions */
213 "!^[ \t]*(do|for|if|else|return|switch|while)\n"
214 /* Objective-C methods */
215 "^[ \t]*([-+][ \t]*\\([ \t]*[A-Za-z_][A-Za-z_0-9* \t]*\\)[ \t]*[A-Za-z_].*)$\n"
216 /* C functions */
959e2e64 217 "^[ \t]*(([A-Za-z_][A-Za-z_0-9]*[ \t]+)+[A-Za-z_][A-Za-z_0-9]*[ \t]*\\([^;]*)$\n"
be58e70d 218 /* Objective-C class/protocol definitions */
80c49c3d
TR
219 "^(@(implementation|interface|protocol)[ \t].*)$",
220 /* -- */
221 "[a-zA-Z_][a-zA-Z0-9_]*"
222 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?"
664d44ee 223 "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->"),
80c49c3d 224PATTERNS("pascal",
82512e00
ÆAB
225 "^(((class[ \t]+)?(procedure|function)|constructor|destructor|interface"
226 "|implementation|initialization|finalization)[ \t]*.*)$\n"
80c49c3d
TR
227 "^(.*=[ \t]*(class|record).*)$",
228 /* -- */
229 "[a-zA-Z_][a-zA-Z0-9_]*"
230 "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+"
664d44ee 231 "|<>|<=|>=|:=|\\.\\."),
71a5d4bc 232PATTERNS("perl",
ea2ca449
JN
233 "^package .*\n"
234 "^sub [[:alnum:]_':]+[ \t]*"
235 "(\\([^)]*\\)[ \t]*)?" /* prototype */
236 /*
237 * Attributes. A regex can't count nested parentheses,
238 * so just slurp up whatever we see, taking care not
239 * to accept lines like "sub foo; # defined elsewhere".
240 *
241 * An attribute could contain a semicolon, but at that
242 * point it seems reasonable enough to give up.
243 */
244 "(:[^;#]*)?"
245 "(\\{[ \t]*)?" /* brace can come here or on the next line */
246 "(#.*)?$\n" /* comment */
f143d9c6 247 "^(BEGIN|END|INIT|CHECK|UNITCHECK|AUTOLOAD|DESTROY)[ \t]*"
ea2ca449
JN
248 "(\\{[ \t]*)?" /* brace can come here or on the next line */
249 "(#.*)?$\n"
12f0967a 250 "^=head[0-9] .*", /* POD */
71a5d4bc
JN
251 /* -- */
252 "[[:alpha:]_'][[:alnum:]_']*"
253 "|0[xb]?[0-9a-fA-F_]*"
254 /* taking care not to interpret 3..5 as (3.)(.5) */
255 "|[0-9a-fA-F_]+(\\.[0-9a-fA-F_]+)?([eE][-+]?[0-9_]+)?"
256 "|=>|-[rwxoRWXOezsfdlpSugkbctTBMAC>]|~~|::"
257 "|&&=|\\|\\|=|//=|\\*\\*="
258 "|&&|\\|\\||//|\\+\\+|--|\\*\\*|\\.\\.\\.?"
259 "|[-+*/%.^&<>=!|]="
260 "|=~|!~"
664d44ee 261 "|<<|<>|<=>|>>"),
6d2f208c 262PATTERNS("php",
aff92827 263 "^[\t ]*(((public|protected|private|static|abstract|final)[\t ]+)*function.*)$\n"
2c7f3aac 264 "^[\t ]*((((final|abstract)[\t ]+)?class|enum|interface|trait).*)$",
80c49c3d
TR
265 /* -- */
266 "[a-zA-Z_][a-zA-Z0-9_]*"
267 "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+"
664d44ee 268 "|[-+*/<>%&^|=!.]=|--|\\+\\+|<<=?|>>=?|===|&&|\\|\\||::|->"),
6d1c9c52
ÆAB
269PATTERNS("python",
270 "^[ \t]*((class|(async[ \t]+)?def)[ \t].*)$",
80c49c3d
TR
271 /* -- */
272 "[a-zA-Z_][a-zA-Z0-9_]*"
273 "|[-+0-9.e]+[jJlL]?|0[xX]?[0-9a-fA-F]+[lL]?"
664d44ee 274 "|[-+*/<>%&^|=!]=|//=?|<<=?|>>=?|\\*\\*=?"),
80c49c3d 275 /* -- */
6d1c9c52
ÆAB
276PATTERNS("ruby",
277 "^[ \t]*((class|module|def)[ \t].*)$",
80c49c3d
TR
278 /* -- */
279 "(@|@@|\\$)?[a-zA-Z_][a-zA-Z0-9_]*"
280 "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+|\\?(\\\\C-)?(\\\\M-)?."
664d44ee 281 "|//=?|[-+*/<>%&^|=!]=|<<=?|>>=?|===|\\.{1,3}|::|[!=]~"),
d74e7860 282PATTERNS("rust",
a04c7e0f 283 "^[\t ]*((pub(\\([^\\)]+\\))?[\t ]+)?((async|const|unsafe|extern([\t ]+\"[^\"]+\"))[\t ]+)?(struct|enum|union|mod|trait|fn|impl|macro_rules!)[< \t]+[^;]*)$",
d74e7860
MAL
284 /* -- */
285 "[a-zA-Z_][a-zA-Z0-9_]*"
33be7b38 286 "|[0-9][0-9_a-fA-Fiosuxz]*(\\.([0-9]*[eE][+-]?)?[0-9_fF]*)?"
d74e7860 287 "|[-+*\\/<>%&^|=!:]=|<<=?|>>=?|&&|\\|\\||->|=>|\\.{2}=|\\.{3}|::"),
a4373903
AR
288PATTERNS("scheme",
289 "^[\t ]*(\\(((define|def(struct|syntax|class|method|rules|record|proto|alias)?)[-*/ \t]|(library|module|struct|class)[*+ \t]).*)$",
290 /*
291 * R7RS valid identifiers include any sequence enclosed
292 * within vertical lines having no backslashes
293 */
294 "\\|([^\\\\]*)\\|"
295 /* All other words should be delimited by spaces or parentheses */
296 "|([^][)(}{[ \t])+"),
80c49c3d 297PATTERNS("tex", "^(\\\\((sub)*section|chapter|part)\\*{0,1}\\{.*)$",
be391449 298 "\\\\[a-zA-Z@]+|\\\\.|([a-zA-Z0-9]|[^\x01-\x7f])+"),
a4cf900e 299{ "default", NULL, NULL, -1, { NULL, 0 } },
be58e70d 300};
80c49c3d 301#undef PATTERNS
909a5494 302#undef IPATTERN
be58e70d
JK
303
304static struct userdiff_driver driver_true = {
2dd75f12
ÆAB
305 .name = "diff=true",
306 .binary = 0,
be58e70d 307};
be58e70d
JK
308
309static struct userdiff_driver driver_false = {
2dd75f12
ÆAB
310 .name = "!diff",
311 .binary = 1,
be58e70d 312};
be58e70d 313
f12fa9ee
ÆAB
314struct find_by_namelen_data {
315 const char *name;
316 size_t len;
317 struct userdiff_driver *driver;
318};
319
320static int userdiff_find_by_namelen_cb(struct userdiff_driver *driver,
c25d9e52
JK
321 enum userdiff_driver_type type UNUSED,
322 void *priv)
be58e70d 323{
f12fa9ee
ÆAB
324 struct find_by_namelen_data *cb_data = priv;
325
f0e578c6 326 if (!xstrncmpz(driver->name, cb_data->name, cb_data->len)) {
f12fa9ee
ÆAB
327 cb_data->driver = driver;
328 return 1; /* tell the caller to stop iterating */
be58e70d 329 }
f12fa9ee
ÆAB
330 return 0;
331}
332
be391449
RS
333static int regexec_supports_multi_byte_chars(void)
334{
335 static const char not_space[] = "[^[:space:]]";
336 static const char utf8_multi_byte_char[] = "\xc2\xa3";
337 regex_t re;
338 regmatch_t match;
339 static int result = -1;
340
341 if (result != -1)
342 return result;
343 if (regcomp(&re, not_space, REG_EXTENDED))
344 BUG("invalid regular expression: %s", not_space);
345 result = !regexec(&re, utf8_multi_byte_char, 1, &match, 0) &&
346 match.rm_so == 0 &&
347 match.rm_eo == strlen(utf8_multi_byte_char);
348 regfree(&re);
349 return result;
350}
351
f12fa9ee
ÆAB
352static struct userdiff_driver *userdiff_find_by_namelen(const char *name, size_t len)
353{
354 struct find_by_namelen_data udcbdata = {
355 .name = name,
356 .len = len,
357 };
358 for_each_userdiff_driver(userdiff_find_by_namelen_cb, &udcbdata);
359 return udcbdata.driver;
be58e70d
JK
360}
361
be58e70d
JK
362static int parse_funcname(struct userdiff_funcname *f, const char *k,
363 const char *v, int cflags)
364{
365 if (git_config_string(&f->pattern, k, v) < 0)
366 return -1;
367 f->cflags = cflags;
6680a087 368 return 0;
be58e70d
JK
369}
370
122aa6f9
JK
371static int parse_tristate(int *b, const char *k, const char *v)
372{
373 if (v && !strcasecmp(v, "auto"))
374 *b = -1;
375 else
376 *b = git_config_bool(k, v);
6680a087 377 return 0;
122aa6f9
JK
378}
379
d9bae1a1
JK
380static int parse_bool(int *b, const char *k, const char *v)
381{
382 *b = git_config_bool(k, v);
6680a087 383 return 0;
d9bae1a1
JK
384}
385
c7534ef4 386int userdiff_config(const char *k, const char *v)
be58e70d
JK
387{
388 struct userdiff_driver *drv;
0a5987fe 389 const char *name, *type;
f5914f4b 390 size_t namelen;
0a5987fe
JK
391
392 if (parse_config_key(k, "diff", &name, &namelen, &type) || !name)
393 return 0;
394
395 drv = userdiff_find_by_namelen(name, namelen);
396 if (!drv) {
397 ALLOC_GROW(drivers, ndrivers+1, drivers_alloc);
398 drv = &drivers[ndrivers++];
399 memset(drv, 0, sizeof(*drv));
400 drv->name = xmemdupz(name, namelen);
401 drv->binary = -1;
402 }
be58e70d 403
0a5987fe 404 if (!strcmp(type, "funcname"))
be58e70d 405 return parse_funcname(&drv->funcname, k, v, 0);
0a5987fe 406 if (!strcmp(type, "xfuncname"))
be58e70d 407 return parse_funcname(&drv->funcname, k, v, REG_EXTENDED);
0a5987fe 408 if (!strcmp(type, "binary"))
122aa6f9 409 return parse_tristate(&drv->binary, k, v);
0a5987fe 410 if (!strcmp(type, "command"))
6680a087 411 return git_config_string(&drv->external, k, v);
0a5987fe 412 if (!strcmp(type, "textconv"))
6680a087 413 return git_config_string(&drv->textconv, k, v);
0a5987fe 414 if (!strcmp(type, "cachetextconv"))
d9bae1a1 415 return parse_bool(&drv->textconv_want_cache, k, v);
0a5987fe 416 if (!strcmp(type, "wordregex"))
6680a087 417 return git_config_string(&drv->word_regex, k, v);
a4cf900e
JC
418 if (!strcmp(type, "algorithm"))
419 return git_config_string(&drv->algorithm, k, v);
be58e70d
JK
420
421 return 0;
422}
423
3b335762
NTND
424struct userdiff_driver *userdiff_find_by_name(const char *name)
425{
be58e70d 426 int len = strlen(name);
be391449
RS
427 struct userdiff_driver *driver = userdiff_find_by_namelen(name, len);
428 if (driver && driver->word_regex_multi_byte) {
429 if (regexec_supports_multi_byte_chars())
430 driver->word_regex = driver->word_regex_multi_byte;
431 driver->word_regex_multi_byte = NULL;
432 }
433 return driver;
be58e70d
JK
434}
435
acd00ea0
NTND
436struct userdiff_driver *userdiff_find_by_path(struct index_state *istate,
437 const char *path)
be58e70d 438{
2aef63d3 439 static struct attr_check *check;
be58e70d 440
2aef63d3
JH
441 if (!check)
442 check = attr_check_initl("diff", NULL);
be58e70d
JK
443 if (!path)
444 return NULL;
44451a2e 445 git_check_attr(istate, path, check);
be58e70d 446
2aef63d3 447 if (ATTR_TRUE(check->items[0].value))
be58e70d 448 return &driver_true;
2aef63d3 449 if (ATTR_FALSE(check->items[0].value))
be58e70d 450 return &driver_false;
2aef63d3 451 if (ATTR_UNSET(check->items[0].value))
be58e70d 452 return NULL;
2aef63d3 453 return userdiff_find_by_name(check->items[0].value);
be58e70d 454}
3813e690 455
bd7ad45b
NTND
456struct userdiff_driver *userdiff_get_textconv(struct repository *r,
457 struct userdiff_driver *driver)
3813e690
JK
458{
459 if (!driver->textconv)
460 return NULL;
461
462 if (driver->textconv_want_cache && !driver->textconv_cache) {
463 struct notes_cache *c = xmalloc(sizeof(*c));
464 struct strbuf name = STRBUF_INIT;
465
466 strbuf_addf(&name, "textconv/%s", driver->name);
bd7ad45b 467 notes_cache_init(r, c, name.buf, driver->textconv);
3813e690 468 driver->textconv_cache = c;
460c7eb2 469 strbuf_release(&name);
3813e690
JK
470 }
471
472 return driver;
473}
f12fa9ee
ÆAB
474
475static int for_each_userdiff_driver_list(each_userdiff_driver_fn fn,
476 enum userdiff_driver_type type, void *cb_data,
477 struct userdiff_driver *drv,
478 int drv_size)
479{
480 int i;
481 int ret;
482 for (i = 0; i < drv_size; i++) {
483 struct userdiff_driver *item = drv + i;
484 if ((ret = fn(item, type, cb_data)))
485 return ret;
486 }
487 return 0;
488}
489
490int for_each_userdiff_driver(each_userdiff_driver_fn fn, void *cb_data)
491{
492 int ret;
493
494 ret = for_each_userdiff_driver_list(fn, USERDIFF_DRIVER_TYPE_CUSTOM,
495 cb_data, drivers, ndrivers);
496 if (ret)
497 return ret;
498
499 ret = for_each_userdiff_driver_list(fn, USERDIFF_DRIVER_TYPE_BUILTIN,
500 cb_data, builtin_drivers,
501 ARRAY_SIZE(builtin_drivers));
502 if (ret)
503 return ret;
504
505 return 0;
506}