]> git.ipfire.org Git - thirdparty/git.git/blame - userdiff.c
The fifth batch
[thirdparty/git.git] / userdiff.c
CommitLineData
673af418 1#define USE_THE_REPOSITORY_VARIABLE
41f43b82 2#define DISABLE_SIGN_COMPARE_WARNINGS
673af418 3
36bf1958 4#include "git-compat-util.h"
b2141fc1 5#include "config.h"
be58e70d 6#include "userdiff.h"
be58e70d 7#include "attr.h"
36bf1958 8#include "strbuf.h"
affe355f 9#include "environment.h"
be58e70d
JK
10
11static struct userdiff_driver *drivers;
12static int ndrivers;
13static int drivers_alloc;
14
2dd75f12
ÆAB
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]+", \
be391449 23 .word_regex_multi_byte = wrx "|[^[:space:]]", \
2dd75f12
ÆAB
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]+", \
be391449 33 .word_regex_multi_byte = wrx "|[^[:space:]]", \
2dd75f12 34}
b6029b32
JH
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 */
be58e70d 45static struct userdiff_driver builtin_drivers[] = {
e90d065e 46IPATTERN("ada",
39a87a29 47 "!^(.*[ \t])?(is[ \t]+new|renames|is[ \t]+separate)([ \t].*)?$\n"
e90d065e
AJ
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_]*"
39a87a29 53 "|[-+]?[0-9][0-9#_.aAbBcCdDeEfF]*([eE][+-]?[0-9_]+)?"
e90d065e 54 "|=>|\\.\\.|\\*\\*|:=|/=|>=|<=|<<|>>|<>"),
2ff6c346
VE
55PATTERNS("bash",
56 /* Optional leading indentation */
57 "^[ \t]*"
58 /* Start of captured text */
59 "("
60 "("
61 /* POSIX identifier with mandatory parentheses */
ea8a71b4 62 "([a-zA-Z_][a-zA-Z0-9_]*[ \t]*\\([ \t]*\\))"
2ff6c346
VE
63 "|"
64 /* Bashism identifier with optional parentheses */
ea8a71b4 65 "(function[ \t]+[a-zA-Z_][a-zA-Z0-9_]*(([ \t]*\\([ \t]*\\))|([ \t]+)))"
2ff6c346 66 ")"
ea8a71b4
MD
67 /* Everything after the function header is captured */
68 ".*$"
2ff6c346
VE
69 /* End of captured text */
70 ")",
71 /* -- */
ea8a71b4
MD
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 "|\\(|\\)|\\{|\\}|\\[|\\]"),
6d1c9c52
ÆAB
86PATTERNS("bibtex",
87 "(@[a-zA-Z]{1,}[ \t]*\\{{0,1}[ \t]*[^ \t\"@',\\#}{~%]*).*$",
88 /* -- */
ddd164d0
ÆAB
89 "[={}\"]|[^={}\" \t]+"),
90PATTERNS("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 /* -- */
350b87cd 96 /* identifiers and keywords */
ddd164d0 97 "[a-zA-Z_][a-zA-Z0-9_]*"
350b87cd 98 /* decimal and octal integers as well as floatingpoint numbers */
386076ec 99 "|[0-9][0-9.]*([Ee][-+]?[0-9]+)?[fFlLuU]*"
350b87cd 100 /* hexadecimal and binary integers */
386076ec 101 "|0[xXbB][0-9a-fA-F]+[lLuU]*"
350b87cd 102 /* floatingpoint numbers that begin with a decimal point */
386076ec 103 "|\\.[0-9][0-9]*([Ee][-+]?[0-9]+)?[fFlL]?"
c4fdba33 104 "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->\\*?|\\.\\*|<=>"),
ddd164d0 105PATTERNS("csharp",
ec0e3075
SJ
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"
ddd164d0 148 /* Type definitions */
c4e31781 149 "^[ \t]*(((static|public|internal|private|protected|new|unsafe|sealed|abstract|partial)[ \t]+)*(class|enum|interface|struct|record)[ \t]+.*)$\n"
ddd164d0
ÆAB
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 "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->"),
156IPATTERN("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),
3c81760b
SB
168PATTERNS("dts",
169 "!;\n"
8da56a48 170 "!=\n"
3c81760b 171 /* lines beginning with a word optionally preceded by '&' or the root */
8da56a48 172 "^[ \t]*((/[ \t]*\\{|&?[a-zA-Z_]).*)",
3c81760b
SB
173 /* -- */
174 /* Property names and math operators */
175 "[a-zA-Z0-9,._+?#-]+"
176 "|[-+*/%&^|!~]|>>|<<|&&|\\|\\|"),
a807200f
ŁN
177PATTERNS("elixir",
178 "^[ \t]*((def(macro|module|impl|protocol|p)?|test)[ \t].*)$",
d1b1384d 179 /* -- */
a807200f 180 /* Atoms, names, and module attributes */
d1b1384d 181 "[@:]?[a-zA-Z0-9@_?!]+"
a807200f
ŁN
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_.]\\{\\}?"),
909a5494 190IPATTERN("fortran",
b79e6925 191 /* Don't match comment lines */
909a5494 192 "!^([C*]|[ \t]*!)\n"
b79e6925 193 /* Don't match 'module procedure' lines */
909a5494 194 "!^[ \t]*MODULE[ \t]+PROCEDURE[ \t]\n"
b79e6925 195 /* Program, module, block data */
909a5494 196 "^[ \t]*((END[ \t]+)?(PROGRAM|MODULE|BLOCK[ \t]+DATA"
b79e6925 197 /* Subroutines and functions */
75c3b6b2 198 "|([^!'\" \t]+[ \t]+)*(SUBROUTINE|FUNCTION))[ \t]+[A-Z].*)$",
909a5494
BC
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_]*)?"
664d44ee 206 "|//|\\*\\*|::|[/<>=]="),
6d1c9c52
ÆAB
207IPATTERN("fountain",
208 "^((\\.[^.]|(int|ext|est|int\\.?/ext|i/e)[. ]).*)$",
209 /* -- */
69f9c87d 210 "[^ \t-]+"),
1dbf0c0a
AG
211PATTERNS("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}"),
6d1c9c52
ÆAB
220PATTERNS("html",
221 "^[ \t]*(<[Hh][1-6]([ \t].*)?>.*)$",
222 /* -- */
664d44ee 223 "[^<>= \t]+"),
43380056
LSO
224PATTERNS("ini",
225 "^[ \t]*\\[[^]]+\\]",
226 /* -- */
227 "[^ \t]+"),
80c49c3d 228PATTERNS("java",
be58e70d 229 "!^[ \t]*(catch|do|for|if|instanceof|new|return|switch|throw|while)\n"
575e6fcf 230 /* Class, enum, interface, and record declarations */
93d52ed0 231 "^[ \t]*(([a-z-]+[ \t]+)*(class|enum|interface|record)[ \t]+.*)$\n"
a8cbc895
TH
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]*\\([^;]*)$",
959e2e64 235 /* -- */
80c49c3d
TR
236 "[a-zA-Z_][a-zA-Z0-9_]*"
237 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?"
238 "|[-+*/<>%&^|=!]="
664d44ee 239 "|--|\\+\\+|<<=?|>>>?=?|&&|\\|\\|"),
09188ed9
JD
240PATTERNS("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 "|[-+*/<>%&^|=!]==?|--|\\+\\+|<<=|>>=|&&|\\|\\||->|\\.\\*|!!|[?:.][.:]"),
09dad925
AH
252PATTERNS("markdown",
253 "^ {0,3}#{1,6}[ \t].*",
6d1c9c52 254 /* -- */
09dad925 255 "[^<>= \t]+"),
53b10a14 256PATTERNS("matlab",
2731a784
BL
257 /*
258 * Octave pattern is mostly the same as matlab, except that '%%%' and
91bf382f 259 * '##' can also be used to begin code sections, in addition to '%%'
2731a784
BL
260 * that is understood by both.
261 */
91bf382f 262 "^[[:space:]]*((classdef|function)[[:space:]].*)$|^(%%%?|##)[[:space:]].*$",
6d1c9c52 263 /* -- */
53b10a14 264 "[a-zA-Z_][a-zA-Z0-9_]*|[-+0-9.e]+|[=~<>]=|\\.[*/\\^']|\\|\\||&&"),
80c49c3d 265PATTERNS("objc",
be58e70d
JK
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 */
959e2e64 271 "^[ \t]*(([A-Za-z_][A-Za-z_0-9]*[ \t]+)+[A-Za-z_][A-Za-z_0-9]*[ \t]*\\([^;]*)$\n"
be58e70d 272 /* Objective-C class/protocol definitions */
80c49c3d
TR
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]?"
664d44ee 277 "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->"),
80c49c3d 278PATTERNS("pascal",
82512e00
ÆAB
279 "^(((class[ \t]+)?(procedure|function)|constructor|destructor|interface"
280 "|implementation|initialization|finalization)[ \t]*.*)$\n"
80c49c3d
TR
281 "^(.*=[ \t]*(class|record).*)$",
282 /* -- */
283 "[a-zA-Z_][a-zA-Z0-9_]*"
284 "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+"
664d44ee 285 "|<>|<=|>=|:=|\\.\\."),
71a5d4bc 286PATTERNS("perl",
ea2ca449
JN
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 */
f143d9c6 301 "^(BEGIN|END|INIT|CHECK|UNITCHECK|AUTOLOAD|DESTROY)[ \t]*"
ea2ca449
JN
302 "(\\{[ \t]*)?" /* brace can come here or on the next line */
303 "(#.*)?$\n"
12f0967a 304 "^=head[0-9] .*", /* POD */
71a5d4bc
JN
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 "|=~|!~"
664d44ee 315 "|<<|<>|<=>|>>"),
6d2f208c 316PATTERNS("php",
aff92827 317 "^[\t ]*(((public|protected|private|static|abstract|final)[\t ]+)*function.*)$\n"
2c7f3aac 318 "^[\t ]*((((final|abstract)[\t ]+)?class|enum|interface|trait).*)$",
80c49c3d
TR
319 /* -- */
320 "[a-zA-Z_][a-zA-Z0-9_]*"
321 "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+"
664d44ee 322 "|[-+*/<>%&^|=!.]=|--|\\+\\+|<<=?|>>=?|===|&&|\\|\\||::|->"),
6d1c9c52
ÆAB
323PATTERNS("python",
324 "^[ \t]*((class|(async[ \t]+)?def)[ \t].*)$",
80c49c3d
TR
325 /* -- */
326 "[a-zA-Z_][a-zA-Z0-9_]*"
327 "|[-+0-9.e]+[jJlL]?|0[xX]?[0-9a-fA-F]+[lL]?"
664d44ee 328 "|[-+*/<>%&^|=!]=|//=?|<<=?|>>=?|\\*\\*=?"),
80c49c3d 329 /* -- */
1d9526df
RC
330PATTERNS("r",
331 "^[ \t]*([a-zA-z][a-zA-Z0-9_.]*[ \t]*(<-|=)[ \t]*function.*)$",
332 /* -- */
333 "[^ \t]+"),
6d1c9c52
ÆAB
334PATTERNS("ruby",
335 "^[ \t]*((class|module|def)[ \t].*)$",
80c49c3d
TR
336 /* -- */
337 "(@|@@|\\$)?[a-zA-Z_][a-zA-Z0-9_]*"
338 "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+|\\?(\\\\C-)?(\\\\M-)?."
664d44ee 339 "|//=?|[-+*/<>%&^|=!]=|<<=?|>>=?|===|\\.{1,3}|::|[!=]~"),
d74e7860 340PATTERNS("rust",
a04c7e0f 341 "^[\t ]*((pub(\\([^\\)]+\\))?[\t ]+)?((async|const|unsafe|extern([\t ]+\"[^\"]+\"))[\t ]+)?(struct|enum|union|mod|trait|fn|impl|macro_rules!)[< \t]+[^;]*)$",
d74e7860
MAL
342 /* -- */
343 "[a-zA-Z_][a-zA-Z0-9_]*"
33be7b38 344 "|[0-9][0-9_a-fA-Fiosuxz]*(\\.([0-9]*[eE][+-]?)?[0-9_fF]*)?"
d74e7860 345 "|[-+*\\/<>%&^|=!:]=|<<=?|>>=?|&&|\\|\\||->|=>|\\.{2}=|\\.{3}|::"),
a4373903
AR
346PATTERNS("scheme",
347 "^[\t ]*(\\(((define|def(struct|syntax|class|method|rules|record|proto|alias)?)[-*/ \t]|(library|module|struct|class)[*+ \t]).*)$",
348 /*
349 * R7RS valid identifiers include any sequence enclosed
350 * within vertical lines having no backslashes
351 */
352 "\\|([^\\\\]*)\\|"
353 /* All other words should be delimited by spaces or parentheses */
354 "|([^][)(}{[ \t])+"),
80c49c3d 355PATTERNS("tex", "^(\\\\((sub)*section|chapter|part)\\*{0,1}\\{.*)$",
be391449 356 "\\\\[a-zA-Z@]+|\\\\.|([a-zA-Z0-9]|[^\x01-\x7f])+"),
54443bbf 357{ .name = "default", .binary = -1 },
be58e70d 358};
80c49c3d 359#undef PATTERNS
909a5494 360#undef IPATTERN
be58e70d
JK
361
362static struct userdiff_driver driver_true = {
2dd75f12
ÆAB
363 .name = "diff=true",
364 .binary = 0,
be58e70d 365};
be58e70d
JK
366
367static struct userdiff_driver driver_false = {
2dd75f12
ÆAB
368 .name = "!diff",
369 .binary = 1,
be58e70d 370};
be58e70d 371
f12fa9ee
ÆAB
372struct find_by_namelen_data {
373 const char *name;
374 size_t len;
375 struct userdiff_driver *driver;
376};
377
378static int userdiff_find_by_namelen_cb(struct userdiff_driver *driver,
c25d9e52
JK
379 enum userdiff_driver_type type UNUSED,
380 void *priv)
be58e70d 381{
f12fa9ee
ÆAB
382 struct find_by_namelen_data *cb_data = priv;
383
f0e578c6 384 if (!xstrncmpz(driver->name, cb_data->name, cb_data->len)) {
f12fa9ee
ÆAB
385 cb_data->driver = driver;
386 return 1; /* tell the caller to stop iterating */
be58e70d 387 }
f12fa9ee
ÆAB
388 return 0;
389}
390
be391449
RS
391static int regexec_supports_multi_byte_chars(void)
392{
393 static const char not_space[] = "[^[:space:]]";
394 static const char utf8_multi_byte_char[] = "\xc2\xa3";
395 regex_t re;
396 regmatch_t match;
397 static int result = -1;
398
399 if (result != -1)
400 return result;
401 if (regcomp(&re, not_space, REG_EXTENDED))
402 BUG("invalid regular expression: %s", not_space);
403 result = !regexec(&re, utf8_multi_byte_char, 1, &match, 0) &&
404 match.rm_so == 0 &&
405 match.rm_eo == strlen(utf8_multi_byte_char);
406 regfree(&re);
407 return result;
408}
409
f12fa9ee
ÆAB
410static struct userdiff_driver *userdiff_find_by_namelen(const char *name, size_t len)
411{
412 struct find_by_namelen_data udcbdata = {
413 .name = name,
414 .len = len,
415 };
416 for_each_userdiff_driver(userdiff_find_by_namelen_cb, &udcbdata);
417 return udcbdata.driver;
be58e70d
JK
418}
419
be58e70d
JK
420static int parse_funcname(struct userdiff_funcname *f, const char *k,
421 const char *v, int cflags)
422{
38678e5d
PS
423 f->pattern = NULL;
424 FREE_AND_NULL(f->pattern_owned);
425 if (git_config_string(&f->pattern_owned, k, v) < 0)
be58e70d 426 return -1;
38678e5d 427 f->pattern = f->pattern_owned;
be58e70d 428 f->cflags = cflags;
6680a087 429 return 0;
be58e70d
JK
430}
431
122aa6f9
JK
432static int parse_tristate(int *b, const char *k, const char *v)
433{
434 if (v && !strcasecmp(v, "auto"))
435 *b = -1;
436 else
437 *b = git_config_bool(k, v);
6680a087 438 return 0;
122aa6f9
JK
439}
440
d9bae1a1
JK
441static int parse_bool(int *b, const char *k, const char *v)
442{
443 *b = git_config_bool(k, v);
6680a087 444 return 0;
d9bae1a1
JK
445}
446
c7534ef4 447int userdiff_config(const char *k, const char *v)
be58e70d
JK
448{
449 struct userdiff_driver *drv;
0a5987fe 450 const char *name, *type;
f5914f4b 451 size_t namelen;
0a5987fe
JK
452
453 if (parse_config_key(k, "diff", &name, &namelen, &type) || !name)
454 return 0;
455
456 drv = userdiff_find_by_namelen(name, namelen);
457 if (!drv) {
458 ALLOC_GROW(drivers, ndrivers+1, drivers_alloc);
459 drv = &drivers[ndrivers++];
460 memset(drv, 0, sizeof(*drv));
461 drv->name = xmemdupz(name, namelen);
462 drv->binary = -1;
463 }
be58e70d 464
0a5987fe 465 if (!strcmp(type, "funcname"))
be58e70d 466 return parse_funcname(&drv->funcname, k, v, 0);
0a5987fe 467 if (!strcmp(type, "xfuncname"))
be58e70d 468 return parse_funcname(&drv->funcname, k, v, REG_EXTENDED);
0a5987fe 469 if (!strcmp(type, "binary"))
122aa6f9 470 return parse_tristate(&drv->binary, k, v);
38678e5d
PS
471 if (!strcmp(type, "command")) {
472 FREE_AND_NULL(drv->external.cmd);
473 return git_config_string(&drv->external.cmd, k, v);
474 }
d7b97b71
RS
475 if (!strcmp(type, "trustexitcode")) {
476 drv->external.trust_exit_code = git_config_bool(k, v);
477 return 0;
478 }
38678e5d
PS
479 if (!strcmp(type, "textconv")) {
480 int ret;
481 FREE_AND_NULL(drv->textconv_owned);
482 ret = git_config_string(&drv->textconv_owned, k, v);
483 drv->textconv = drv->textconv_owned;
484 return ret;
485 }
0a5987fe 486 if (!strcmp(type, "cachetextconv"))
d9bae1a1 487 return parse_bool(&drv->textconv_want_cache, k, v);
38678e5d
PS
488 if (!strcmp(type, "wordregex")) {
489 int ret;
490 FREE_AND_NULL(drv->word_regex_owned);
491 ret = git_config_string(&drv->word_regex_owned, k, v);
492 drv->word_regex = drv->word_regex_owned;
493 return ret;
494 }
495 if (!strcmp(type, "algorithm")) {
496 int ret;
497 FREE_AND_NULL(drv->algorithm_owned);
498 ret = git_config_string(&drv->algorithm_owned, k, v);
499 drv->algorithm = drv->algorithm_owned;
500 return ret;
501 }
be58e70d
JK
502
503 return 0;
504}
505
3b335762
NTND
506struct userdiff_driver *userdiff_find_by_name(const char *name)
507{
be58e70d 508 int len = strlen(name);
be391449
RS
509 struct userdiff_driver *driver = userdiff_find_by_namelen(name, len);
510 if (driver && driver->word_regex_multi_byte) {
511 if (regexec_supports_multi_byte_chars())
512 driver->word_regex = driver->word_regex_multi_byte;
513 driver->word_regex_multi_byte = NULL;
514 }
515 return driver;
be58e70d
JK
516}
517
acd00ea0
NTND
518struct userdiff_driver *userdiff_find_by_path(struct index_state *istate,
519 const char *path)
be58e70d 520{
2aef63d3 521 static struct attr_check *check;
be58e70d 522
2aef63d3
JH
523 if (!check)
524 check = attr_check_initl("diff", NULL);
be58e70d
JK
525 if (!path)
526 return NULL;
44451a2e 527 git_check_attr(istate, path, check);
be58e70d 528
2aef63d3 529 if (ATTR_TRUE(check->items[0].value))
be58e70d 530 return &driver_true;
2aef63d3 531 if (ATTR_FALSE(check->items[0].value))
be58e70d 532 return &driver_false;
2aef63d3 533 if (ATTR_UNSET(check->items[0].value))
be58e70d 534 return NULL;
2aef63d3 535 return userdiff_find_by_name(check->items[0].value);
be58e70d 536}
3813e690 537
bd7ad45b
NTND
538struct userdiff_driver *userdiff_get_textconv(struct repository *r,
539 struct userdiff_driver *driver)
3813e690
JK
540{
541 if (!driver->textconv)
542 return NULL;
543
affe355f
JK
544 if (driver->textconv_want_cache && !driver->textconv_cache &&
545 have_git_dir()) {
3813e690
JK
546 struct notes_cache *c = xmalloc(sizeof(*c));
547 struct strbuf name = STRBUF_INIT;
548
549 strbuf_addf(&name, "textconv/%s", driver->name);
bd7ad45b 550 notes_cache_init(r, c, name.buf, driver->textconv);
3813e690 551 driver->textconv_cache = c;
460c7eb2 552 strbuf_release(&name);
3813e690
JK
553 }
554
555 return driver;
556}
f12fa9ee
ÆAB
557
558static int for_each_userdiff_driver_list(each_userdiff_driver_fn fn,
559 enum userdiff_driver_type type, void *cb_data,
560 struct userdiff_driver *drv,
561 int drv_size)
562{
563 int i;
564 int ret;
565 for (i = 0; i < drv_size; i++) {
566 struct userdiff_driver *item = drv + i;
567 if ((ret = fn(item, type, cb_data)))
568 return ret;
569 }
570 return 0;
571}
572
573int for_each_userdiff_driver(each_userdiff_driver_fn fn, void *cb_data)
574{
575 int ret;
576
577 ret = for_each_userdiff_driver_list(fn, USERDIFF_DRIVER_TYPE_CUSTOM,
578 cb_data, drivers, ndrivers);
579 if (ret)
580 return ret;
581
582 ret = for_each_userdiff_driver_list(fn, USERDIFF_DRIVER_TYPE_BUILTIN,
583 cb_data, builtin_drivers,
584 ARRAY_SIZE(builtin_drivers));
585 if (ret)
586 return ret;
587
588 return 0;
589}