1 #define USE_THE_REPOSITORY_VARIABLE
3 #include "git-compat-util.h"
8 #include "environment.h"
10 static struct userdiff_driver
*drivers
;
12 static int drivers_alloc
;
14 #define PATTERNS(lang, rx, wrx) { \
19 .cflags = REG_EXTENDED, \
21 .word_regex = wrx "|[^[:space:]]|[\xc0-\xff][\x80-\xbf]+", \
22 .word_regex_multi_byte = wrx "|[^[:space:]]", \
24 #define IPATTERN(lang, rx, wrx) { \
29 .cflags = REG_EXTENDED | REG_ICASE, \
31 .word_regex = wrx "|[^[:space:]]|[\xc0-\xff][\x80-\xbf]+", \
32 .word_regex_multi_byte = wrx "|[^[:space:]]", \
36 * Built-in drivers for various languages, sorted by their names
37 * (except that the "default" is left at the end).
39 * When writing or updating patterns, assume that the contents these
40 * patterns are applied to are syntactically correct. The patterns
41 * can be simple without implementing all syntactical corner cases, as
42 * long as they are sufficiently permissive.
44 static struct userdiff_driver builtin_drivers
[] = {
46 "!^(.*[ \t])?(is[ \t]+new|renames|is[ \t]+separate)([ \t].*)?$\n"
47 "!^[ \t]*with[ \t].*$\n"
48 "^[ \t]*((procedure|function)[ \t]+.*)$\n"
49 "^[ \t]*((package|protected|task)[ \t]+.*)$",
51 "[a-zA-Z][a-zA-Z0-9_]*"
52 "|[-+]?[0-9][0-9#_.aAbBcCdDeEfF]*([eE][+-]?[0-9_]+)?"
53 "|=>|\\.\\.|\\*\\*|:=|/=|>=|<=|<<|>>|<>"),
55 /* Optional leading indentation */
57 /* Start of captured text */
60 /* POSIX identifier with mandatory parentheses */
61 "[a-zA-Z_][a-zA-Z0-9_]*[ \t]*\\([ \t]*\\))"
63 /* Bashism identifier with optional parentheses */
64 "(function[ \t]+[a-zA-Z_][a-zA-Z0-9_]*(([ \t]*\\([ \t]*\\))|([ \t]+))"
66 /* Optional whitespace */
68 /* Compound command starting with `{`, `(`, `((` or `[[` */
69 "(\\{|\\(\\(?|\\[\\[)"
70 /* End of captured text */
73 /* Characters not in the default $IFS value */
76 "(@[a-zA-Z]{1,}[ \t]*\\{{0,1}[ \t]*[^ \t\"@',\\#}{~%]*).*$",
78 "[={}\"]|[^={}\" \t]+"),
80 /* Jump targets or access declarations */
81 "!^[ \t]*[A-Za-z_][A-Za-z_0-9]*:[[:space:]]*($|/[/*])\n"
82 /* functions/methods, variables, and compounds at top level */
83 "^((::[[:space:]]*)?[A-Za-z_].*)$",
85 /* identifiers and keywords */
86 "[a-zA-Z_][a-zA-Z0-9_]*"
87 /* decimal and octal integers as well as floatingpoint numbers */
88 "|[0-9][0-9.]*([Ee][-+]?[0-9]+)?[fFlLuU]*"
89 /* hexadecimal and binary integers */
90 "|0[xXbB][0-9a-fA-F]+[lLuU]*"
91 /* floatingpoint numbers that begin with a decimal point */
92 "|\\.[0-9][0-9]*([Ee][-+]?[0-9]+)?[fFlL]?"
93 "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->\\*?|\\.\\*|<=>"),
96 * Jump over reserved keywords which are illegal method names, but which
97 * can be followed by parentheses without special characters in between,
98 * making them look like methods.
100 "!(^|[ \t]+)" /* Start of line or whitespace. */
101 "(do|while|for|foreach|if|else|new|default|return|switch|case|throw"
102 "|catch|using|lock|fixed)"
103 "([ \t(]+|$)\n" /* Whitespace, "(", or end of line. */
105 * Methods/constructors:
106 * The strategy is to identify a minimum of two groups (any combination
107 * of keywords/type/name) before the opening parenthesis, and without
108 * final unexpected characters, normally only used in ordinary statements.
110 "^[ \t]*" /* Remove leading whitespace. */
111 "(" /* Start chunk header capture. */
112 "(" /* First group. */
113 "[][[:alnum:]@_.]" /* Name. */
114 "(<[][[:alnum:]@_, \t<>]+>)?" /* Optional generic parameters. */
116 "([ \t]+" /* Subsequent groups, prepended with space. */
117 "([][[:alnum:]@_.](<[][[:alnum:]@_, \t<>]+>)?)+"
119 "[ \t]*" /* Optional space before parameters start. */
120 "\\(" /* Start of method parameters. */
121 "[^;]*" /* Allow complex parameters, but exclude statements (;). */
122 ")$\n" /* Close chunk header capture. */
125 * As with methods, expect a minimum of two groups. But, more trivial than
126 * methods, the vast majority of properties long enough to be worth
127 * showing a chunk header for don't include "=:;,()" on the line they are
128 * defined, since they don't have a parameter list.
131 "([][[:alnum:]@_.](<[][[:alnum:]@_, \t<>]+>)?)+"
133 "([][[:alnum:]@_.](<[][[:alnum:]@_, \t<>]+>)?)+"
134 ")+" /* Up to here, same as methods regex. */
135 "[^;=:,()]*" /* Compared to methods, no parameter list allowed. */
137 /* Type definitions */
138 "^[ \t]*(((static|public|internal|private|protected|new|unsafe|sealed|abstract|partial)[ \t]+)*(class|enum|interface|struct|record)[ \t]+.*)$\n"
140 "^[ \t]*(namespace[ \t]+.*)$",
142 "[a-zA-Z_][a-zA-Z0-9_]*"
143 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?"
144 "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->"),
146 "![:;][[:space:]]*$\n"
147 "^[:[@.#]?[_a-z0-9].*$",
150 * This regex comes from W3C CSS specs. Should theoretically also
151 * allow ISO 10646 characters U+00A0 and higher,
152 * but they are not handled in this regex.
154 "-?[_a-zA-Z][-_a-zA-Z0-9]*" /* identifiers */
155 "|-?[0-9]+|\\#[0-9a-fA-F]+" /* numbers */
160 /* lines beginning with a word optionally preceded by '&' or the root */
161 "^[ \t]*((/[ \t]*\\{|&?[a-zA-Z_]).*)",
163 /* Property names and math operators */
164 "[a-zA-Z0-9,._+?#-]+"
165 "|[-+*/%&^|!~]|>>|<<|&&|\\|\\|"),
167 "^[ \t]*((def(macro|module|impl|protocol|p)?|test)[ \t].*)$",
169 /* Atoms, names, and module attributes */
170 "[@:]?[a-zA-Z0-9@_?!]+"
171 /* Numbers with specific base */
172 "|[-+]?0[xob][0-9a-fA-F]+"
174 "|[-+]?[0-9][0-9_.]*([eE][-+]?[0-9_]+)?"
175 /* Operators and atoms that represent them */
176 "|:?(\\+\\+|--|\\.\\.|~~~|<>|\\^\\^\\^|<?\\|>|<<<?|>?>>|<<?~|~>?>|<~>|<=|>=|===?|!==?|=~|&&&?|\\|\\|\\|?|=>|<-|\\\\\\\\|->)"
177 /* Not real operators, but should be grouped */
178 "|:?%[A-Za-z0-9_.]\\{\\}?"),
180 /* Don't match comment lines */
182 /* Don't match 'module procedure' lines */
183 "!^[ \t]*MODULE[ \t]+PROCEDURE[ \t]\n"
184 /* Program, module, block data */
185 "^[ \t]*((END[ \t]+)?(PROGRAM|MODULE|BLOCK[ \t]+DATA"
186 /* Subroutines and functions */
187 "|([^!'\" \t]+[ \t]+)*(SUBROUTINE|FUNCTION))[ \t]+[A-Z].*)$",
189 "[a-zA-Z][a-zA-Z0-9_]*"
190 "|\\.([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])\\."
191 /* numbers and format statements like 2E14.4, or ES12.6, 9X.
192 * Don't worry about format statements without leading digits since
193 * they would have been matched above as a variable anyway. */
194 "|[-+]?[0-9.]+([AaIiDdEeFfLlTtXx][Ss]?[-+]?[0-9.]*)?(_[a-zA-Z0-9][a-zA-Z0-9_]*)?"
195 "|//|\\*\\*|::|[/<>=]="),
197 "^((\\.[^.]|(int|ext|est|int\\.?/ext|i/e)[. ]).*)$",
202 "^[ \t]*(func[ \t]*.*(\\{[ \t]*)?)\n"
203 /* Structs and interfaces */
204 "^[ \t]*(type[ \t].*(struct|interface)[ \t]*(\\{[ \t]*)?)",
206 "[a-zA-Z_][a-zA-Z0-9_]*"
207 "|[-+0-9.eE]+i?|0[xX]?[0-9a-fA-F]+i?"
208 "|[-+*/<>%&^|=!:]=|--|\\+\\+|<<=?|>>=?|&\\^=?|&&|\\|\\||<-|\\.{3}"),
210 "^[ \t]*(<[Hh][1-6]([ \t].*)?>.*)$",
214 "!^[ \t]*(catch|do|for|if|instanceof|new|return|switch|throw|while)\n"
215 /* Class, enum, interface, and record declarations */
216 "^[ \t]*(([a-z-]+[ \t]+)*(class|enum|interface|record)[ \t]+.*)$\n"
217 /* Method definitions; note that constructor signatures are not */
218 /* matched because they are indistinguishable from method calls. */
219 "^[ \t]*(([A-Za-z_<>&][][?&<>.,A-Za-z_0-9]*[ \t]+)+[A-Za-z_][A-Za-z_0-9]*[ \t]*\\([^;]*)$",
221 "[a-zA-Z_][a-zA-Z0-9_]*"
222 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?"
224 "|--|\\+\\+|<<=?|>>>?=?|&&|\\|\\|"),
226 "^[ \t]*(([a-z]+[ \t]+)*(fun|class|interface)[ \t]+.*)$",
228 "[a-zA-Z_][a-zA-Z0-9_]*"
229 /* hexadecimal and binary numbers */
230 "|0[xXbB][0-9a-fA-F_]+[lLuU]*"
231 /* integers and floats */
232 "|[0-9][0-9_]*([.][0-9_]*)?([Ee][-+]?[0-9]+)?[fFlLuU]*"
233 /* floating point numbers beginning with decimal point */
234 "|[.][0-9][0-9_]*([Ee][-+]?[0-9]+)?[fFlLuU]?"
235 /* unary and binary operators */
236 "|[-+*/<>%&^|=!]==?|--|\\+\\+|<<=|>>=|&&|\\|\\||->|\\.\\*|!!|[?:.][.:]"),
238 "^ {0,3}#{1,6}[ \t].*",
243 * Octave pattern is mostly the same as matlab, except that '%%%' and
244 * '##' can also be used to begin code sections, in addition to '%%'
245 * that is understood by both.
247 "^[[:space:]]*((classdef|function)[[:space:]].*)$|^(%%%?|##)[[:space:]].*$",
249 "[a-zA-Z_][a-zA-Z0-9_]*|[-+0-9.e]+|[=~<>]=|\\.[*/\\^']|\\|\\||&&"),
251 /* Negate C statements that can look like functions */
252 "!^[ \t]*(do|for|if|else|return|switch|while)\n"
253 /* Objective-C methods */
254 "^[ \t]*([-+][ \t]*\\([ \t]*[A-Za-z_][A-Za-z_0-9* \t]*\\)[ \t]*[A-Za-z_].*)$\n"
256 "^[ \t]*(([A-Za-z_][A-Za-z_0-9]*[ \t]+)+[A-Za-z_][A-Za-z_0-9]*[ \t]*\\([^;]*)$\n"
257 /* Objective-C class/protocol definitions */
258 "^(@(implementation|interface|protocol)[ \t].*)$",
260 "[a-zA-Z_][a-zA-Z0-9_]*"
261 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?"
262 "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->"),
264 "^(((class[ \t]+)?(procedure|function)|constructor|destructor|interface"
265 "|implementation|initialization|finalization)[ \t]*.*)$\n"
266 "^(.*=[ \t]*(class|record).*)$",
268 "[a-zA-Z_][a-zA-Z0-9_]*"
269 "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+"
270 "|<>|<=|>=|:=|\\.\\."),
273 "^sub [[:alnum:]_':]+[ \t]*"
274 "(\\([^)]*\\)[ \t]*)?" /* prototype */
276 * Attributes. A regex can't count nested parentheses,
277 * so just slurp up whatever we see, taking care not
278 * to accept lines like "sub foo; # defined elsewhere".
280 * An attribute could contain a semicolon, but at that
281 * point it seems reasonable enough to give up.
284 "(\\{[ \t]*)?" /* brace can come here or on the next line */
285 "(#.*)?$\n" /* comment */
286 "^(BEGIN|END|INIT|CHECK|UNITCHECK|AUTOLOAD|DESTROY)[ \t]*"
287 "(\\{[ \t]*)?" /* brace can come here or on the next line */
289 "^=head[0-9] .*", /* POD */
291 "[[:alpha:]_'][[:alnum:]_']*"
292 "|0[xb]?[0-9a-fA-F_]*"
293 /* taking care not to interpret 3..5 as (3.)(.5) */
294 "|[0-9a-fA-F_]+(\\.[0-9a-fA-F_]+)?([eE][-+]?[0-9_]+)?"
295 "|=>|-[rwxoRWXOezsfdlpSugkbctTBMAC>]|~~|::"
296 "|&&=|\\|\\|=|//=|\\*\\*="
297 "|&&|\\|\\||//|\\+\\+|--|\\*\\*|\\.\\.\\.?"
302 "^[\t ]*(((public|protected|private|static|abstract|final)[\t ]+)*function.*)$\n"
303 "^[\t ]*((((final|abstract)[\t ]+)?class|enum|interface|trait).*)$",
305 "[a-zA-Z_][a-zA-Z0-9_]*"
306 "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+"
307 "|[-+*/<>%&^|=!.]=|--|\\+\\+|<<=?|>>=?|===|&&|\\|\\||::|->"),
309 "^[ \t]*((class|(async[ \t]+)?def)[ \t].*)$",
311 "[a-zA-Z_][a-zA-Z0-9_]*"
312 "|[-+0-9.e]+[jJlL]?|0[xX]?[0-9a-fA-F]+[lL]?"
313 "|[-+*/<>%&^|=!]=|//=?|<<=?|>>=?|\\*\\*=?"),
316 "^[ \t]*((class|module|def)[ \t].*)$",
318 "(@|@@|\\$)?[a-zA-Z_][a-zA-Z0-9_]*"
319 "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+|\\?(\\\\C-)?(\\\\M-)?."
320 "|//=?|[-+*/<>%&^|=!]=|<<=?|>>=?|===|\\.{1,3}|::|[!=]~"),
322 "^[\t ]*((pub(\\([^\\)]+\\))?[\t ]+)?((async|const|unsafe|extern([\t ]+\"[^\"]+\"))[\t ]+)?(struct|enum|union|mod|trait|fn|impl|macro_rules!)[< \t]+[^;]*)$",
324 "[a-zA-Z_][a-zA-Z0-9_]*"
325 "|[0-9][0-9_a-fA-Fiosuxz]*(\\.([0-9]*[eE][+-]?)?[0-9_fF]*)?"
326 "|[-+*\\/<>%&^|=!:]=|<<=?|>>=?|&&|\\|\\||->|=>|\\.{2}=|\\.{3}|::"),
328 "^[\t ]*(\\(((define|def(struct|syntax|class|method|rules|record|proto|alias)?)[-*/ \t]|(library|module|struct|class)[*+ \t]).*)$",
330 * R7RS valid identifiers include any sequence enclosed
331 * within vertical lines having no backslashes
334 /* All other words should be delimited by spaces or parentheses */
335 "|([^][)(}{[ \t])+"),
336 PATTERNS("tex", "^(\\\\((sub)*section|chapter|part)\\*{0,1}\\{.*)$",
337 "\\\\[a-zA-Z@]+|\\\\.|([a-zA-Z0-9]|[^\x01-\x7f])+"),
338 { .name
= "default", .binary
= -1 },
343 static struct userdiff_driver driver_true
= {
348 static struct userdiff_driver driver_false
= {
353 struct find_by_namelen_data
{
356 struct userdiff_driver
*driver
;
359 static int userdiff_find_by_namelen_cb(struct userdiff_driver
*driver
,
360 enum userdiff_driver_type type UNUSED
,
363 struct find_by_namelen_data
*cb_data
= priv
;
365 if (!xstrncmpz(driver
->name
, cb_data
->name
, cb_data
->len
)) {
366 cb_data
->driver
= driver
;
367 return 1; /* tell the caller to stop iterating */
372 static int regexec_supports_multi_byte_chars(void)
374 static const char not_space
[] = "[^[:space:]]";
375 static const char utf8_multi_byte_char
[] = "\xc2\xa3";
378 static int result
= -1;
382 if (regcomp(&re
, not_space
, REG_EXTENDED
))
383 BUG("invalid regular expression: %s", not_space
);
384 result
= !regexec(&re
, utf8_multi_byte_char
, 1, &match
, 0) &&
386 match
.rm_eo
== strlen(utf8_multi_byte_char
);
391 static struct userdiff_driver
*userdiff_find_by_namelen(const char *name
, size_t len
)
393 struct find_by_namelen_data udcbdata
= {
397 for_each_userdiff_driver(userdiff_find_by_namelen_cb
, &udcbdata
);
398 return udcbdata
.driver
;
401 static int parse_funcname(struct userdiff_funcname
*f
, const char *k
,
402 const char *v
, int cflags
)
405 FREE_AND_NULL(f
->pattern_owned
);
406 if (git_config_string(&f
->pattern_owned
, k
, v
) < 0)
408 f
->pattern
= f
->pattern_owned
;
413 static int parse_tristate(int *b
, const char *k
, const char *v
)
415 if (v
&& !strcasecmp(v
, "auto"))
418 *b
= git_config_bool(k
, v
);
422 static int parse_bool(int *b
, const char *k
, const char *v
)
424 *b
= git_config_bool(k
, v
);
428 int userdiff_config(const char *k
, const char *v
)
430 struct userdiff_driver
*drv
;
431 const char *name
, *type
;
434 if (parse_config_key(k
, "diff", &name
, &namelen
, &type
) || !name
)
437 drv
= userdiff_find_by_namelen(name
, namelen
);
439 ALLOC_GROW(drivers
, ndrivers
+1, drivers_alloc
);
440 drv
= &drivers
[ndrivers
++];
441 memset(drv
, 0, sizeof(*drv
));
442 drv
->name
= xmemdupz(name
, namelen
);
446 if (!strcmp(type
, "funcname"))
447 return parse_funcname(&drv
->funcname
, k
, v
, 0);
448 if (!strcmp(type
, "xfuncname"))
449 return parse_funcname(&drv
->funcname
, k
, v
, REG_EXTENDED
);
450 if (!strcmp(type
, "binary"))
451 return parse_tristate(&drv
->binary
, k
, v
);
452 if (!strcmp(type
, "command")) {
453 FREE_AND_NULL(drv
->external
.cmd
);
454 return git_config_string(&drv
->external
.cmd
, k
, v
);
456 if (!strcmp(type
, "trustexitcode")) {
457 drv
->external
.trust_exit_code
= git_config_bool(k
, v
);
460 if (!strcmp(type
, "textconv")) {
462 FREE_AND_NULL(drv
->textconv_owned
);
463 ret
= git_config_string(&drv
->textconv_owned
, k
, v
);
464 drv
->textconv
= drv
->textconv_owned
;
467 if (!strcmp(type
, "cachetextconv"))
468 return parse_bool(&drv
->textconv_want_cache
, k
, v
);
469 if (!strcmp(type
, "wordregex")) {
471 FREE_AND_NULL(drv
->word_regex_owned
);
472 ret
= git_config_string(&drv
->word_regex_owned
, k
, v
);
473 drv
->word_regex
= drv
->word_regex_owned
;
476 if (!strcmp(type
, "algorithm")) {
478 FREE_AND_NULL(drv
->algorithm_owned
);
479 ret
= git_config_string(&drv
->algorithm_owned
, k
, v
);
480 drv
->algorithm
= drv
->algorithm_owned
;
487 struct userdiff_driver
*userdiff_find_by_name(const char *name
)
489 int len
= strlen(name
);
490 struct userdiff_driver
*driver
= userdiff_find_by_namelen(name
, len
);
491 if (driver
&& driver
->word_regex_multi_byte
) {
492 if (regexec_supports_multi_byte_chars())
493 driver
->word_regex
= driver
->word_regex_multi_byte
;
494 driver
->word_regex_multi_byte
= NULL
;
499 struct userdiff_driver
*userdiff_find_by_path(struct index_state
*istate
,
502 static struct attr_check
*check
;
505 check
= attr_check_initl("diff", NULL
);
508 git_check_attr(istate
, path
, check
);
510 if (ATTR_TRUE(check
->items
[0].value
))
512 if (ATTR_FALSE(check
->items
[0].value
))
513 return &driver_false
;
514 if (ATTR_UNSET(check
->items
[0].value
))
516 return userdiff_find_by_name(check
->items
[0].value
);
519 struct userdiff_driver
*userdiff_get_textconv(struct repository
*r
,
520 struct userdiff_driver
*driver
)
522 if (!driver
->textconv
)
525 if (driver
->textconv_want_cache
&& !driver
->textconv_cache
&&
527 struct notes_cache
*c
= xmalloc(sizeof(*c
));
528 struct strbuf name
= STRBUF_INIT
;
530 strbuf_addf(&name
, "textconv/%s", driver
->name
);
531 notes_cache_init(r
, c
, name
.buf
, driver
->textconv
);
532 driver
->textconv_cache
= c
;
533 strbuf_release(&name
);
539 static int for_each_userdiff_driver_list(each_userdiff_driver_fn fn
,
540 enum userdiff_driver_type type
, void *cb_data
,
541 struct userdiff_driver
*drv
,
546 for (i
= 0; i
< drv_size
; i
++) {
547 struct userdiff_driver
*item
= drv
+ i
;
548 if ((ret
= fn(item
, type
, cb_data
)))
554 int for_each_userdiff_driver(each_userdiff_driver_fn fn
, void *cb_data
)
558 ret
= for_each_userdiff_driver_list(fn
, USERDIFF_DRIVER_TYPE_CUSTOM
,
559 cb_data
, drivers
, ndrivers
);
563 ret
= for_each_userdiff_driver_list(fn
, USERDIFF_DRIVER_TYPE_BUILTIN
,
564 cb_data
, builtin_drivers
,
565 ARRAY_SIZE(builtin_drivers
));