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 /* Optional whitespace */
69 /* Compound command starting with `{`, `(`, `((` or `[[` */
70 "(\\{|\\(\\(?|\\[\\[)"
71 /* End of captured text */
74 /* Characters not in the default $IFS value */
77 "(@[a-zA-Z]{1,}[ \t]*\\{{0,1}[ \t]*[^ \t\"@',\\#}{~%]*).*$",
79 "[={}\"]|[^={}\" \t]+"),
81 /* Jump targets or access declarations */
82 "!^[ \t]*[A-Za-z_][A-Za-z_0-9]*:[[:space:]]*($|/[/*])\n"
83 /* functions/methods, variables, and compounds at top level */
84 "^((::[[:space:]]*)?[A-Za-z_].*)$",
86 /* identifiers and keywords */
87 "[a-zA-Z_][a-zA-Z0-9_]*"
88 /* decimal and octal integers as well as floatingpoint numbers */
89 "|[0-9][0-9.]*([Ee][-+]?[0-9]+)?[fFlLuU]*"
90 /* hexadecimal and binary integers */
91 "|0[xXbB][0-9a-fA-F]+[lLuU]*"
92 /* floatingpoint numbers that begin with a decimal point */
93 "|\\.[0-9][0-9]*([Ee][-+]?[0-9]+)?[fFlL]?"
94 "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->\\*?|\\.\\*|<=>"),
97 * Jump over reserved keywords which are illegal method names, but which
98 * can be followed by parentheses without special characters in between,
99 * making them look like methods.
101 "!(^|[ \t]+)" /* Start of line or whitespace. */
102 "(do|while|for|foreach|if|else|new|default|return|switch|case|throw"
103 "|catch|using|lock|fixed)"
104 "([ \t(]+|$)\n" /* Whitespace, "(", or end of line. */
106 * Methods/constructors:
107 * The strategy is to identify a minimum of two groups (any combination
108 * of keywords/type/name) before the opening parenthesis, and without
109 * final unexpected characters, normally only used in ordinary statements.
111 "^[ \t]*" /* Remove leading whitespace. */
112 "(" /* Start chunk header capture. */
113 "(" /* First group. */
114 "[][[:alnum:]@_.]" /* Name. */
115 "(<[][[:alnum:]@_, \t<>]+>)?" /* Optional generic parameters. */
117 "([ \t]+" /* Subsequent groups, prepended with space. */
118 "([][[:alnum:]@_.](<[][[:alnum:]@_, \t<>]+>)?)+"
120 "[ \t]*" /* Optional space before parameters start. */
121 "\\(" /* Start of method parameters. */
122 "[^;]*" /* Allow complex parameters, but exclude statements (;). */
123 ")$\n" /* Close chunk header capture. */
126 * As with methods, expect a minimum of two groups. But, more trivial than
127 * methods, the vast majority of properties long enough to be worth
128 * showing a chunk header for don't include "=:;,()" on the line they are
129 * defined, since they don't have a parameter list.
132 "([][[:alnum:]@_.](<[][[:alnum:]@_, \t<>]+>)?)+"
134 "([][[:alnum:]@_.](<[][[:alnum:]@_, \t<>]+>)?)+"
135 ")+" /* Up to here, same as methods regex. */
136 "[^;=:,()]*" /* Compared to methods, no parameter list allowed. */
138 /* Type definitions */
139 "^[ \t]*(((static|public|internal|private|protected|new|unsafe|sealed|abstract|partial)[ \t]+)*(class|enum|interface|struct|record)[ \t]+.*)$\n"
141 "^[ \t]*(namespace[ \t]+.*)$",
143 "[a-zA-Z_][a-zA-Z0-9_]*"
144 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?"
145 "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->"),
147 "![:;][[:space:]]*$\n"
148 "^[:[@.#]?[_a-z0-9].*$",
151 * This regex comes from W3C CSS specs. Should theoretically also
152 * allow ISO 10646 characters U+00A0 and higher,
153 * but they are not handled in this regex.
155 "-?[_a-zA-Z][-_a-zA-Z0-9]*" /* identifiers */
156 "|-?[0-9]+|\\#[0-9a-fA-F]+" /* numbers */
161 /* lines beginning with a word optionally preceded by '&' or the root */
162 "^[ \t]*((/[ \t]*\\{|&?[a-zA-Z_]).*)",
164 /* Property names and math operators */
165 "[a-zA-Z0-9,._+?#-]+"
166 "|[-+*/%&^|!~]|>>|<<|&&|\\|\\|"),
168 "^[ \t]*((def(macro|module|impl|protocol|p)?|test)[ \t].*)$",
170 /* Atoms, names, and module attributes */
171 "[@:]?[a-zA-Z0-9@_?!]+"
172 /* Numbers with specific base */
173 "|[-+]?0[xob][0-9a-fA-F]+"
175 "|[-+]?[0-9][0-9_.]*([eE][-+]?[0-9_]+)?"
176 /* Operators and atoms that represent them */
177 "|:?(\\+\\+|--|\\.\\.|~~~|<>|\\^\\^\\^|<?\\|>|<<<?|>?>>|<<?~|~>?>|<~>|<=|>=|===?|!==?|=~|&&&?|\\|\\|\\|?|=>|<-|\\\\\\\\|->)"
178 /* Not real operators, but should be grouped */
179 "|:?%[A-Za-z0-9_.]\\{\\}?"),
181 /* Don't match comment lines */
183 /* Don't match 'module procedure' lines */
184 "!^[ \t]*MODULE[ \t]+PROCEDURE[ \t]\n"
185 /* Program, module, block data */
186 "^[ \t]*((END[ \t]+)?(PROGRAM|MODULE|BLOCK[ \t]+DATA"
187 /* Subroutines and functions */
188 "|([^!'\" \t]+[ \t]+)*(SUBROUTINE|FUNCTION))[ \t]+[A-Z].*)$",
190 "[a-zA-Z][a-zA-Z0-9_]*"
191 "|\\.([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])\\."
192 /* numbers and format statements like 2E14.4, or ES12.6, 9X.
193 * Don't worry about format statements without leading digits since
194 * they would have been matched above as a variable anyway. */
195 "|[-+]?[0-9.]+([AaIiDdEeFfLlTtXx][Ss]?[-+]?[0-9.]*)?(_[a-zA-Z0-9][a-zA-Z0-9_]*)?"
196 "|//|\\*\\*|::|[/<>=]="),
198 "^((\\.[^.]|(int|ext|est|int\\.?/ext|i/e)[. ]).*)$",
203 "^[ \t]*(func[ \t]*.*(\\{[ \t]*)?)\n"
204 /* Structs and interfaces */
205 "^[ \t]*(type[ \t].*(struct|interface)[ \t]*(\\{[ \t]*)?)",
207 "[a-zA-Z_][a-zA-Z0-9_]*"
208 "|[-+0-9.eE]+i?|0[xX]?[0-9a-fA-F]+i?"
209 "|[-+*/<>%&^|=!:]=|--|\\+\\+|<<=?|>>=?|&\\^=?|&&|\\|\\||<-|\\.{3}"),
211 "^[ \t]*(<[Hh][1-6]([ \t].*)?>.*)$",
215 "!^[ \t]*(catch|do|for|if|instanceof|new|return|switch|throw|while)\n"
216 /* Class, enum, interface, and record declarations */
217 "^[ \t]*(([a-z-]+[ \t]+)*(class|enum|interface|record)[ \t]+.*)$\n"
218 /* Method definitions; note that constructor signatures are not */
219 /* matched because they are indistinguishable from method calls. */
220 "^[ \t]*(([A-Za-z_<>&][][?&<>.,A-Za-z_0-9]*[ \t]+)+[A-Za-z_][A-Za-z_0-9]*[ \t]*\\([^;]*)$",
222 "[a-zA-Z_][a-zA-Z0-9_]*"
223 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?"
225 "|--|\\+\\+|<<=?|>>>?=?|&&|\\|\\|"),
227 "^[ \t]*(([a-z]+[ \t]+)*(fun|class|interface)[ \t]+.*)$",
229 "[a-zA-Z_][a-zA-Z0-9_]*"
230 /* hexadecimal and binary numbers */
231 "|0[xXbB][0-9a-fA-F_]+[lLuU]*"
232 /* integers and floats */
233 "|[0-9][0-9_]*([.][0-9_]*)?([Ee][-+]?[0-9]+)?[fFlLuU]*"
234 /* floating point numbers beginning with decimal point */
235 "|[.][0-9][0-9_]*([Ee][-+]?[0-9]+)?[fFlLuU]?"
236 /* unary and binary operators */
237 "|[-+*/<>%&^|=!]==?|--|\\+\\+|<<=|>>=|&&|\\|\\||->|\\.\\*|!!|[?:.][.:]"),
239 "^ {0,3}#{1,6}[ \t].*",
244 * Octave pattern is mostly the same as matlab, except that '%%%' and
245 * '##' can also be used to begin code sections, in addition to '%%'
246 * that is understood by both.
248 "^[[:space:]]*((classdef|function)[[:space:]].*)$|^(%%%?|##)[[:space:]].*$",
250 "[a-zA-Z_][a-zA-Z0-9_]*|[-+0-9.e]+|[=~<>]=|\\.[*/\\^']|\\|\\||&&"),
252 /* Negate C statements that can look like functions */
253 "!^[ \t]*(do|for|if|else|return|switch|while)\n"
254 /* Objective-C methods */
255 "^[ \t]*([-+][ \t]*\\([ \t]*[A-Za-z_][A-Za-z_0-9* \t]*\\)[ \t]*[A-Za-z_].*)$\n"
257 "^[ \t]*(([A-Za-z_][A-Za-z_0-9]*[ \t]+)+[A-Za-z_][A-Za-z_0-9]*[ \t]*\\([^;]*)$\n"
258 /* Objective-C class/protocol definitions */
259 "^(@(implementation|interface|protocol)[ \t].*)$",
261 "[a-zA-Z_][a-zA-Z0-9_]*"
262 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?"
263 "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->"),
265 "^(((class[ \t]+)?(procedure|function)|constructor|destructor|interface"
266 "|implementation|initialization|finalization)[ \t]*.*)$\n"
267 "^(.*=[ \t]*(class|record).*)$",
269 "[a-zA-Z_][a-zA-Z0-9_]*"
270 "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+"
271 "|<>|<=|>=|:=|\\.\\."),
274 "^sub [[:alnum:]_':]+[ \t]*"
275 "(\\([^)]*\\)[ \t]*)?" /* prototype */
277 * Attributes. A regex can't count nested parentheses,
278 * so just slurp up whatever we see, taking care not
279 * to accept lines like "sub foo; # defined elsewhere".
281 * An attribute could contain a semicolon, but at that
282 * point it seems reasonable enough to give up.
285 "(\\{[ \t]*)?" /* brace can come here or on the next line */
286 "(#.*)?$\n" /* comment */
287 "^(BEGIN|END|INIT|CHECK|UNITCHECK|AUTOLOAD|DESTROY)[ \t]*"
288 "(\\{[ \t]*)?" /* brace can come here or on the next line */
290 "^=head[0-9] .*", /* POD */
292 "[[:alpha:]_'][[:alnum:]_']*"
293 "|0[xb]?[0-9a-fA-F_]*"
294 /* taking care not to interpret 3..5 as (3.)(.5) */
295 "|[0-9a-fA-F_]+(\\.[0-9a-fA-F_]+)?([eE][-+]?[0-9_]+)?"
296 "|=>|-[rwxoRWXOezsfdlpSugkbctTBMAC>]|~~|::"
297 "|&&=|\\|\\|=|//=|\\*\\*="
298 "|&&|\\|\\||//|\\+\\+|--|\\*\\*|\\.\\.\\.?"
303 "^[\t ]*(((public|protected|private|static|abstract|final)[\t ]+)*function.*)$\n"
304 "^[\t ]*((((final|abstract)[\t ]+)?class|enum|interface|trait).*)$",
306 "[a-zA-Z_][a-zA-Z0-9_]*"
307 "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+"
308 "|[-+*/<>%&^|=!.]=|--|\\+\\+|<<=?|>>=?|===|&&|\\|\\||::|->"),
310 "^[ \t]*((class|(async[ \t]+)?def)[ \t].*)$",
312 "[a-zA-Z_][a-zA-Z0-9_]*"
313 "|[-+0-9.e]+[jJlL]?|0[xX]?[0-9a-fA-F]+[lL]?"
314 "|[-+*/<>%&^|=!]=|//=?|<<=?|>>=?|\\*\\*=?"),
317 "^[ \t]*((class|module|def)[ \t].*)$",
319 "(@|@@|\\$)?[a-zA-Z_][a-zA-Z0-9_]*"
320 "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+|\\?(\\\\C-)?(\\\\M-)?."
321 "|//=?|[-+*/<>%&^|=!]=|<<=?|>>=?|===|\\.{1,3}|::|[!=]~"),
323 "^[\t ]*((pub(\\([^\\)]+\\))?[\t ]+)?((async|const|unsafe|extern([\t ]+\"[^\"]+\"))[\t ]+)?(struct|enum|union|mod|trait|fn|impl|macro_rules!)[< \t]+[^;]*)$",
325 "[a-zA-Z_][a-zA-Z0-9_]*"
326 "|[0-9][0-9_a-fA-Fiosuxz]*(\\.([0-9]*[eE][+-]?)?[0-9_fF]*)?"
327 "|[-+*\\/<>%&^|=!:]=|<<=?|>>=?|&&|\\|\\||->|=>|\\.{2}=|\\.{3}|::"),
329 "^[\t ]*(\\(((define|def(struct|syntax|class|method|rules|record|proto|alias)?)[-*/ \t]|(library|module|struct|class)[*+ \t]).*)$",
331 * R7RS valid identifiers include any sequence enclosed
332 * within vertical lines having no backslashes
335 /* All other words should be delimited by spaces or parentheses */
336 "|([^][)(}{[ \t])+"),
337 PATTERNS("tex", "^(\\\\((sub)*section|chapter|part)\\*{0,1}\\{.*)$",
338 "\\\\[a-zA-Z@]+|\\\\.|([a-zA-Z0-9]|[^\x01-\x7f])+"),
339 { .name
= "default", .binary
= -1 },
344 static struct userdiff_driver driver_true
= {
349 static struct userdiff_driver driver_false
= {
354 struct find_by_namelen_data
{
357 struct userdiff_driver
*driver
;
360 static int userdiff_find_by_namelen_cb(struct userdiff_driver
*driver
,
361 enum userdiff_driver_type type UNUSED
,
364 struct find_by_namelen_data
*cb_data
= priv
;
366 if (!xstrncmpz(driver
->name
, cb_data
->name
, cb_data
->len
)) {
367 cb_data
->driver
= driver
;
368 return 1; /* tell the caller to stop iterating */
373 static int regexec_supports_multi_byte_chars(void)
375 static const char not_space
[] = "[^[:space:]]";
376 static const char utf8_multi_byte_char
[] = "\xc2\xa3";
379 static int result
= -1;
383 if (regcomp(&re
, not_space
, REG_EXTENDED
))
384 BUG("invalid regular expression: %s", not_space
);
385 result
= !regexec(&re
, utf8_multi_byte_char
, 1, &match
, 0) &&
387 match
.rm_eo
== strlen(utf8_multi_byte_char
);
392 static struct userdiff_driver
*userdiff_find_by_namelen(const char *name
, size_t len
)
394 struct find_by_namelen_data udcbdata
= {
398 for_each_userdiff_driver(userdiff_find_by_namelen_cb
, &udcbdata
);
399 return udcbdata
.driver
;
402 static int parse_funcname(struct userdiff_funcname
*f
, const char *k
,
403 const char *v
, int cflags
)
406 FREE_AND_NULL(f
->pattern_owned
);
407 if (git_config_string(&f
->pattern_owned
, k
, v
) < 0)
409 f
->pattern
= f
->pattern_owned
;
414 static int parse_tristate(int *b
, const char *k
, const char *v
)
416 if (v
&& !strcasecmp(v
, "auto"))
419 *b
= git_config_bool(k
, v
);
423 static int parse_bool(int *b
, const char *k
, const char *v
)
425 *b
= git_config_bool(k
, v
);
429 int userdiff_config(const char *k
, const char *v
)
431 struct userdiff_driver
*drv
;
432 const char *name
, *type
;
435 if (parse_config_key(k
, "diff", &name
, &namelen
, &type
) || !name
)
438 drv
= userdiff_find_by_namelen(name
, namelen
);
440 ALLOC_GROW(drivers
, ndrivers
+1, drivers_alloc
);
441 drv
= &drivers
[ndrivers
++];
442 memset(drv
, 0, sizeof(*drv
));
443 drv
->name
= xmemdupz(name
, namelen
);
447 if (!strcmp(type
, "funcname"))
448 return parse_funcname(&drv
->funcname
, k
, v
, 0);
449 if (!strcmp(type
, "xfuncname"))
450 return parse_funcname(&drv
->funcname
, k
, v
, REG_EXTENDED
);
451 if (!strcmp(type
, "binary"))
452 return parse_tristate(&drv
->binary
, k
, v
);
453 if (!strcmp(type
, "command")) {
454 FREE_AND_NULL(drv
->external
.cmd
);
455 return git_config_string(&drv
->external
.cmd
, k
, v
);
457 if (!strcmp(type
, "trustexitcode")) {
458 drv
->external
.trust_exit_code
= git_config_bool(k
, v
);
461 if (!strcmp(type
, "textconv")) {
463 FREE_AND_NULL(drv
->textconv_owned
);
464 ret
= git_config_string(&drv
->textconv_owned
, k
, v
);
465 drv
->textconv
= drv
->textconv_owned
;
468 if (!strcmp(type
, "cachetextconv"))
469 return parse_bool(&drv
->textconv_want_cache
, k
, v
);
470 if (!strcmp(type
, "wordregex")) {
472 FREE_AND_NULL(drv
->word_regex_owned
);
473 ret
= git_config_string(&drv
->word_regex_owned
, k
, v
);
474 drv
->word_regex
= drv
->word_regex_owned
;
477 if (!strcmp(type
, "algorithm")) {
479 FREE_AND_NULL(drv
->algorithm_owned
);
480 ret
= git_config_string(&drv
->algorithm_owned
, k
, v
);
481 drv
->algorithm
= drv
->algorithm_owned
;
488 struct userdiff_driver
*userdiff_find_by_name(const char *name
)
490 int len
= strlen(name
);
491 struct userdiff_driver
*driver
= userdiff_find_by_namelen(name
, len
);
492 if (driver
&& driver
->word_regex_multi_byte
) {
493 if (regexec_supports_multi_byte_chars())
494 driver
->word_regex
= driver
->word_regex_multi_byte
;
495 driver
->word_regex_multi_byte
= NULL
;
500 struct userdiff_driver
*userdiff_find_by_path(struct index_state
*istate
,
503 static struct attr_check
*check
;
506 check
= attr_check_initl("diff", NULL
);
509 git_check_attr(istate
, path
, check
);
511 if (ATTR_TRUE(check
->items
[0].value
))
513 if (ATTR_FALSE(check
->items
[0].value
))
514 return &driver_false
;
515 if (ATTR_UNSET(check
->items
[0].value
))
517 return userdiff_find_by_name(check
->items
[0].value
);
520 struct userdiff_driver
*userdiff_get_textconv(struct repository
*r
,
521 struct userdiff_driver
*driver
)
523 if (!driver
->textconv
)
526 if (driver
->textconv_want_cache
&& !driver
->textconv_cache
&&
528 struct notes_cache
*c
= xmalloc(sizeof(*c
));
529 struct strbuf name
= STRBUF_INIT
;
531 strbuf_addf(&name
, "textconv/%s", driver
->name
);
532 notes_cache_init(r
, c
, name
.buf
, driver
->textconv
);
533 driver
->textconv_cache
= c
;
534 strbuf_release(&name
);
540 static int for_each_userdiff_driver_list(each_userdiff_driver_fn fn
,
541 enum userdiff_driver_type type
, void *cb_data
,
542 struct userdiff_driver
*drv
,
547 for (i
= 0; i
< drv_size
; i
++) {
548 struct userdiff_driver
*item
= drv
+ i
;
549 if ((ret
= fn(item
, type
, cb_data
)))
555 int for_each_userdiff_driver(each_userdiff_driver_fn fn
, void *cb_data
)
559 ret
= for_each_userdiff_driver_list(fn
, USERDIFF_DRIVER_TYPE_CUSTOM
,
560 cb_data
, drivers
, ndrivers
);
564 ret
= for_each_userdiff_driver_list(fn
, USERDIFF_DRIVER_TYPE_BUILTIN
,
565 cb_data
, builtin_drivers
,
566 ARRAY_SIZE(builtin_drivers
));