1 #define USE_THE_REPOSITORY_VARIABLE
2 #define DISABLE_SIGN_COMPARE_WARNINGS
4 #include "git-compat-util.h"
9 #include "environment.h"
11 static struct userdiff_driver
*drivers
;
13 static int drivers_alloc
;
15 #define PATTERNS(lang, rx, wrx) { \
20 .cflags = REG_EXTENDED, \
22 .word_regex = wrx "|[^[:space:]]|[\xc0-\xff][\x80-\xbf]+", \
23 .word_regex_multi_byte = wrx "|[^[:space:]]", \
25 #define IPATTERN(lang, rx, wrx) { \
30 .cflags = REG_EXTENDED | REG_ICASE, \
32 .word_regex = wrx "|[^[:space:]]|[\xc0-\xff][\x80-\xbf]+", \
33 .word_regex_multi_byte = wrx "|[^[:space:]]", \
37 * Built-in drivers for various languages, sorted by their names
38 * (except that the "default" is left at the end).
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.
45 static struct userdiff_driver builtin_drivers
[] = {
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]+.*)$",
52 "[a-zA-Z][a-zA-Z0-9_]*"
53 "|[-+]?[0-9][0-9#_.aAbBcCdDeEfF]*([eE][+-]?[0-9_]+)?"
54 "|=>|\\.\\.|\\*\\*|:=|/=|>=|<=|<<|>>|<>"),
56 /* Optional leading indentation */
58 /* Start of captured text */
61 /* POSIX identifier with mandatory parentheses */
62 "([a-zA-Z_][a-zA-Z0-9_]*[ \t]*\\([ \t]*\\))"
64 /* Bashism identifier with optional parentheses */
65 "(function[ \t]+[a-zA-Z_][a-zA-Z0-9_]*(([ \t]*\\([ \t]*\\))|([ \t]+)))"
67 /* Everything after the function header is captured */
69 /* End of captured text */
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 */
78 /* Operators ending in '=' (comparison + compound assignment) */
79 "|==|!=|<=|>=|[-+*/%&|^]="
80 /* Additional parameter expansion operators */
81 "|:=|:-|:\\+|:\\?|##|%%|\\^\\^|,,"
82 /* Command-line options (to avoid splitting -option) */
84 /* Brackets and grouping symbols */
85 "|\\(|\\)|\\{|\\}|\\[|\\]"),
87 "(@[a-zA-Z]{1,}[ \t]*\\{{0,1}[ \t]*[^ \t\"@',\\#}{~%]*).*$",
89 "[={}\"]|[^={}\" \t]+"),
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_].*)$",
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 "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->\\*?|\\.\\*|<=>"),
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.
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. */
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.
121 "^[ \t]*" /* Remove leading whitespace. */
122 "(" /* Start chunk header capture. */
123 "(" /* First group. */
124 "[][[:alnum:]@_.]" /* Name. */
125 "(<[][[:alnum:]@_, \t<>]+>)?" /* Optional generic parameters. */
127 "([ \t]+" /* Subsequent groups, prepended with space. */
128 "([][[:alnum:]@_.](<[][[:alnum:]@_, \t<>]+>)?)+"
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. */
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.
142 "([][[:alnum:]@_.](<[][[:alnum:]@_, \t<>]+>)?)+"
144 "([][[:alnum:]@_.](<[][[:alnum:]@_, \t<>]+>)?)+"
145 ")+" /* Up to here, same as methods regex. */
146 "[^;=:,()]*" /* Compared to methods, no parameter list allowed. */
148 /* Type definitions */
149 "^[ \t]*(((static|public|internal|private|protected|new|unsafe|sealed|abstract|partial)[ \t]+)*(class|enum|interface|struct|record)[ \t]+.*)$\n"
151 "^[ \t]*(namespace[ \t]+.*)$",
153 "[a-zA-Z_][a-zA-Z0-9_]*"
154 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?"
155 "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->"),
157 "![:;][[:space:]]*$\n"
158 "^[:[@.#]?[_a-z0-9].*$",
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.
165 "-?[_a-zA-Z][-_a-zA-Z0-9]*" /* identifiers */
166 "|-?[0-9]+|\\#[0-9a-fA-F]+" /* numbers */
171 /* lines beginning with a word optionally preceded by '&' or the root */
172 "^[ \t]*((/[ \t]*\\{|&?[a-zA-Z_]).*)",
174 /* Property names and math operators */
175 "[a-zA-Z0-9,._+?#-]+"
176 "|[-+*/%&^|!~]|>>|<<|&&|\\|\\|"),
178 "^[ \t]*((def(macro|module|impl|protocol|p)?|test)[ \t].*)$",
180 /* Atoms, names, and module attributes */
181 "[@:]?[a-zA-Z0-9@_?!]+"
182 /* Numbers with specific base */
183 "|[-+]?0[xob][0-9a-fA-F]+"
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_.]\\{\\}?"),
191 /* Don't match comment lines */
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].*)$",
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 "|//|\\*\\*|::|[/<>=]="),
208 "^((\\.[^.]|(int|ext|est|int\\.?/ext|i/e)[. ]).*)$",
213 "^[ \t]*(func[ \t]*.*(\\{[ \t]*)?)\n"
214 /* Structs and interfaces */
215 "^[ \t]*(type[ \t].*(struct|interface)[ \t]*(\\{[ \t]*)?)",
217 "[a-zA-Z_][a-zA-Z0-9_]*"
218 "|[-+0-9.eE]+i?|0[xX]?[0-9a-fA-F]+i?"
219 "|[-+*/<>%&^|=!:]=|--|\\+\\+|<<=?|>>=?|&\\^=?|&&|\\|\\||<-|\\.{3}"),
221 "^[ \t]*(<[Hh][1-6]([ \t].*)?>.*)$",
225 "^[ \t]*\\[[^]]+\\]",
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]*\\([^;]*)$",
236 "[a-zA-Z_][a-zA-Z0-9_]*"
237 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?"
239 "|--|\\+\\+|<<=?|>>>?=?|&&|\\|\\|"),
241 "^[ \t]*(([a-z]+[ \t]+)*(fun|class|interface)[ \t]+.*)$",
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 "|[-+*/<>%&^|=!]==?|--|\\+\\+|<<=|>>=|&&|\\|\\||->|\\.\\*|!!|[?:.][.:]"),
253 "^ {0,3}#{1,6}[ \t].*",
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.
262 "^[[:space:]]*((classdef|function)[[:space:]].*)$|^(%%%?|##)[[:space:]].*$",
264 "[a-zA-Z_][a-zA-Z0-9_]*|[-+0-9.e]+|[=~<>]=|\\.[*/\\^']|\\|\\||&&"),
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"
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].*)$",
275 "[a-zA-Z_][a-zA-Z0-9_]*"
276 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?"
277 "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->"),
279 "^(((class[ \t]+)?(procedure|function)|constructor|destructor|interface"
280 "|implementation|initialization|finalization)[ \t]*.*)$\n"
281 "^(.*=[ \t]*(class|record).*)$",
283 "[a-zA-Z_][a-zA-Z0-9_]*"
284 "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+"
285 "|<>|<=|>=|:=|\\.\\."),
288 "^sub [[:alnum:]_':]+[ \t]*"
289 "(\\([^)]*\\)[ \t]*)?" /* prototype */
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".
295 * An attribute could contain a semicolon, but at that
296 * point it seems reasonable enough to give up.
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 */
304 "^=head[0-9] .*", /* POD */
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 "|&&|\\|\\||//|\\+\\+|--|\\*\\*|\\.\\.\\.?"
317 "^[\t ]*(((public|protected|private|static|abstract|final)[\t ]+)*function.*)$\n"
318 "^[\t ]*((((final|abstract)[\t ]+)?class|enum|interface|trait).*)$",
320 "[a-zA-Z_][a-zA-Z0-9_]*"
321 "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+"
322 "|[-+*/<>%&^|=!.]=|--|\\+\\+|<<=?|>>=?|===|&&|\\|\\||::|->"),
324 "^[ \t]*((class|(async[ \t]+)?def)[ \t].*)$",
326 "[a-zA-Z_][a-zA-Z0-9_]*"
327 "|[-+0-9.e]+[jJlL]?|0[xX]?[0-9a-fA-F]+[lL]?"
328 "|[-+*/<>%&^|=!]=|//=?|<<=?|>>=?|\\*\\*=?"),
331 "^[ \t]*([a-zA-z][a-zA-Z0-9_.]*[ \t]*(<-|=)[ \t]*function.*)$",
335 "^[ \t]*((class|module|def)[ \t].*)$",
337 "(@|@@|\\$)?[a-zA-Z_][a-zA-Z0-9_]*"
338 "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+|\\?(\\\\C-)?(\\\\M-)?."
339 "|//=?|[-+*/<>%&^|=!]=|<<=?|>>=?|===|\\.{1,3}|::|[!=]~"),
341 "^[\t ]*((pub(\\([^\\)]+\\))?[\t ]+)?((async|const|unsafe|extern([\t ]+\"[^\"]+\"))[\t ]+)?(struct|enum|union|mod|trait|fn|impl|macro_rules!)[< \t]+[^;]*)$",
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}|::"),
347 "^[\t ]*(\\(((define|def(struct|syntax|class|method|rules|record|proto|alias)?)[-*/ \t]|(library|module|struct|class)[*+ \t]).*)$",
349 * R7RS valid identifiers include any sequence enclosed
350 * within vertical lines having no backslashes
353 /* All other words should be delimited by spaces or parentheses */
354 "|([^][)(}{[ \t])+"),
355 PATTERNS("tex", "^(\\\\((sub)*section|chapter|part)\\*{0,1}\\{.*)$",
356 "\\\\[a-zA-Z@]+|\\\\.|([a-zA-Z0-9]|[^\x01-\x7f])+"),
357 { .name
= "default", .binary
= -1 },
362 static struct userdiff_driver driver_true
= {
367 static struct userdiff_driver driver_false
= {
372 struct find_by_namelen_data
{
375 struct userdiff_driver
*driver
;
378 static int userdiff_find_by_namelen_cb(struct userdiff_driver
*driver
,
379 enum userdiff_driver_type type UNUSED
,
382 struct find_by_namelen_data
*cb_data
= priv
;
384 if (!xstrncmpz(driver
->name
, cb_data
->name
, cb_data
->len
)) {
385 cb_data
->driver
= driver
;
386 return 1; /* tell the caller to stop iterating */
391 static int regexec_supports_multi_byte_chars(void)
393 static const char not_space
[] = "[^[:space:]]";
394 static const char utf8_multi_byte_char
[] = "\xc2\xa3";
397 static int result
= -1;
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) &&
405 match
.rm_eo
== strlen(utf8_multi_byte_char
);
410 static struct userdiff_driver
*userdiff_find_by_namelen(const char *name
, size_t len
)
412 struct find_by_namelen_data udcbdata
= {
416 for_each_userdiff_driver(userdiff_find_by_namelen_cb
, &udcbdata
);
417 return udcbdata
.driver
;
420 static int parse_funcname(struct userdiff_funcname
*f
, const char *k
,
421 const char *v
, int cflags
)
424 FREE_AND_NULL(f
->pattern_owned
);
425 if (git_config_string(&f
->pattern_owned
, k
, v
) < 0)
427 f
->pattern
= f
->pattern_owned
;
432 static int parse_tristate(int *b
, const char *k
, const char *v
)
434 if (v
&& !strcasecmp(v
, "auto"))
437 *b
= git_config_bool(k
, v
);
441 static int parse_bool(int *b
, const char *k
, const char *v
)
443 *b
= git_config_bool(k
, v
);
447 int userdiff_config(const char *k
, const char *v
)
449 struct userdiff_driver
*drv
;
450 const char *name
, *type
;
453 if (parse_config_key(k
, "diff", &name
, &namelen
, &type
) || !name
)
456 drv
= userdiff_find_by_namelen(name
, namelen
);
458 ALLOC_GROW(drivers
, ndrivers
+1, drivers_alloc
);
459 drv
= &drivers
[ndrivers
++];
460 memset(drv
, 0, sizeof(*drv
));
461 drv
->name
= xmemdupz(name
, namelen
);
465 if (!strcmp(type
, "funcname"))
466 return parse_funcname(&drv
->funcname
, k
, v
, 0);
467 if (!strcmp(type
, "xfuncname"))
468 return parse_funcname(&drv
->funcname
, k
, v
, REG_EXTENDED
);
469 if (!strcmp(type
, "binary"))
470 return parse_tristate(&drv
->binary
, k
, v
);
471 if (!strcmp(type
, "command")) {
472 FREE_AND_NULL(drv
->external
.cmd
);
473 return git_config_string(&drv
->external
.cmd
, k
, v
);
475 if (!strcmp(type
, "trustexitcode")) {
476 drv
->external
.trust_exit_code
= git_config_bool(k
, v
);
479 if (!strcmp(type
, "textconv")) {
481 FREE_AND_NULL(drv
->textconv_owned
);
482 ret
= git_config_string(&drv
->textconv_owned
, k
, v
);
483 drv
->textconv
= drv
->textconv_owned
;
486 if (!strcmp(type
, "cachetextconv"))
487 return parse_bool(&drv
->textconv_want_cache
, k
, v
);
488 if (!strcmp(type
, "wordregex")) {
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
;
495 if (!strcmp(type
, "algorithm")) {
497 FREE_AND_NULL(drv
->algorithm_owned
);
498 ret
= git_config_string(&drv
->algorithm_owned
, k
, v
);
499 drv
->algorithm
= drv
->algorithm_owned
;
506 struct userdiff_driver
*userdiff_find_by_name(const char *name
)
508 int len
= strlen(name
);
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
;
518 struct userdiff_driver
*userdiff_find_by_path(struct index_state
*istate
,
521 static struct attr_check
*check
;
524 check
= attr_check_initl("diff", NULL
);
527 git_check_attr(istate
, path
, check
);
529 if (ATTR_TRUE(check
->items
[0].value
))
531 if (ATTR_FALSE(check
->items
[0].value
))
532 return &driver_false
;
533 if (ATTR_UNSET(check
->items
[0].value
))
535 return userdiff_find_by_name(check
->items
[0].value
);
538 struct userdiff_driver
*userdiff_get_textconv(struct repository
*r
,
539 struct userdiff_driver
*driver
)
541 if (!driver
->textconv
)
544 if (driver
->textconv_want_cache
&& !driver
->textconv_cache
&&
546 struct notes_cache
*c
= xmalloc(sizeof(*c
));
547 struct strbuf name
= STRBUF_INIT
;
549 strbuf_addf(&name
, "textconv/%s", driver
->name
);
550 notes_cache_init(r
, c
, name
.buf
, driver
->textconv
);
551 driver
->textconv_cache
= c
;
552 strbuf_release(&name
);
558 static 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
,
565 for (i
= 0; i
< drv_size
; i
++) {
566 struct userdiff_driver
*item
= drv
+ i
;
567 if ((ret
= fn(item
, type
, cb_data
)))
573 int for_each_userdiff_driver(each_userdiff_driver_fn fn
, void *cb_data
)
577 ret
= for_each_userdiff_driver_list(fn
, USERDIFF_DRIVER_TYPE_CUSTOM
,
578 cb_data
, drivers
, ndrivers
);
582 ret
= for_each_userdiff_driver_list(fn
, USERDIFF_DRIVER_TYPE_BUILTIN
,
583 cb_data
, builtin_drivers
,
584 ARRAY_SIZE(builtin_drivers
));