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