1 #include "git-compat-util.h"
7 static struct userdiff_driver
*drivers
;
9 static int drivers_alloc
;
11 #define PATTERNS(lang, rx, wrx) { \
16 .cflags = REG_EXTENDED, \
18 .word_regex = wrx "|[^[:space:]]|[\xc0-\xff][\x80-\xbf]+", \
19 .word_regex_multi_byte = wrx "|[^[:space:]]", \
21 #define IPATTERN(lang, rx, wrx) { \
26 .cflags = REG_EXTENDED | REG_ICASE, \
28 .word_regex = wrx "|[^[:space:]]|[\xc0-\xff][\x80-\xbf]+", \
29 .word_regex_multi_byte = wrx "|[^[:space:]]", \
33 * Built-in drivers for various languages, sorted by their names
34 * (except that the "default" is left at the end).
36 * When writing or updating patterns, assume that the contents these
37 * patterns are applied to are syntactically correct. The patterns
38 * can be simple without implementing all syntactical corner cases, as
39 * long as they are sufficiently permissive.
41 static struct userdiff_driver builtin_drivers
[] = {
43 "!^(.*[ \t])?(is[ \t]+new|renames|is[ \t]+separate)([ \t].*)?$\n"
44 "!^[ \t]*with[ \t].*$\n"
45 "^[ \t]*((procedure|function)[ \t]+.*)$\n"
46 "^[ \t]*((package|protected|task)[ \t]+.*)$",
48 "[a-zA-Z][a-zA-Z0-9_]*"
49 "|[-+]?[0-9][0-9#_.aAbBcCdDeEfF]*([eE][+-]?[0-9_]+)?"
50 "|=>|\\.\\.|\\*\\*|:=|/=|>=|<=|<<|>>|<>"),
52 /* Optional leading indentation */
54 /* Start of captured text */
57 /* POSIX identifier with mandatory parentheses */
58 "[a-zA-Z_][a-zA-Z0-9_]*[ \t]*\\([ \t]*\\))"
60 /* Bashism identifier with optional parentheses */
61 "(function[ \t]+[a-zA-Z_][a-zA-Z0-9_]*(([ \t]*\\([ \t]*\\))|([ \t]+))"
63 /* Optional whitespace */
65 /* Compound command starting with `{`, `(`, `((` or `[[` */
66 "(\\{|\\(\\(?|\\[\\[)"
67 /* End of captured text */
70 /* Characters not in the default $IFS value */
73 "(@[a-zA-Z]{1,}[ \t]*\\{{0,1}[ \t]*[^ \t\"@',\\#}{~%]*).*$",
75 "[={}\"]|[^={}\" \t]+"),
77 /* Jump targets or access declarations */
78 "!^[ \t]*[A-Za-z_][A-Za-z_0-9]*:[[:space:]]*($|/[/*])\n"
79 /* functions/methods, variables, and compounds at top level */
80 "^((::[[:space:]]*)?[A-Za-z_].*)$",
82 /* identifiers and keywords */
83 "[a-zA-Z_][a-zA-Z0-9_]*"
84 /* decimal and octal integers as well as floatingpoint numbers */
85 "|[0-9][0-9.]*([Ee][-+]?[0-9]+)?[fFlLuU]*"
86 /* hexadecimal and binary integers */
87 "|0[xXbB][0-9a-fA-F]+[lLuU]*"
88 /* floatingpoint numbers that begin with a decimal point */
89 "|\\.[0-9][0-9]*([Ee][-+]?[0-9]+)?[fFlL]?"
90 "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->\\*?|\\.\\*|<=>"),
93 "!^[ \t]*(do|while|for|if|else|instanceof|new|return|switch|case|throw|catch|using)\n"
94 /* Methods and constructors */
95 "^[ \t]*(((static|public|internal|private|protected|new|virtual|sealed|override|unsafe|async)[ \t]+)*[][<>@.~_[:alnum:]]+[ \t]+[<>@._[:alnum:]]+[ \t]*\\(.*\\))[ \t]*$\n"
97 "^[ \t]*(((static|public|internal|private|protected|new|virtual|sealed|override|unsafe)[ \t]+)*[][<>@.~_[:alnum:]]+[ \t]+[@._[:alnum:]]+)[ \t]*$\n"
98 /* Type definitions */
99 "^[ \t]*(((static|public|internal|private|protected|new|unsafe|sealed|abstract|partial)[ \t]+)*(class|enum|interface|struct|record)[ \t]+.*)$\n"
101 "^[ \t]*(namespace[ \t]+.*)$",
103 "[a-zA-Z_][a-zA-Z0-9_]*"
104 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?"
105 "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->"),
107 "![:;][[:space:]]*$\n"
108 "^[:[@.#]?[_a-z0-9].*$",
111 * This regex comes from W3C CSS specs. Should theoretically also
112 * allow ISO 10646 characters U+00A0 and higher,
113 * but they are not handled in this regex.
115 "-?[_a-zA-Z][-_a-zA-Z0-9]*" /* identifiers */
116 "|-?[0-9]+|\\#[0-9a-fA-F]+" /* numbers */
121 /* lines beginning with a word optionally preceded by '&' or the root */
122 "^[ \t]*((/[ \t]*\\{|&?[a-zA-Z_]).*)",
124 /* Property names and math operators */
125 "[a-zA-Z0-9,._+?#-]+"
126 "|[-+*/%&^|!~]|>>|<<|&&|\\|\\|"),
128 "^[ \t]*((def(macro|module|impl|protocol|p)?|test)[ \t].*)$",
130 /* Atoms, names, and module attributes */
131 "[@:]?[a-zA-Z0-9@_?!]+"
132 /* Numbers with specific base */
133 "|[-+]?0[xob][0-9a-fA-F]+"
135 "|[-+]?[0-9][0-9_.]*([eE][-+]?[0-9_]+)?"
136 /* Operators and atoms that represent them */
137 "|:?(\\+\\+|--|\\.\\.|~~~|<>|\\^\\^\\^|<?\\|>|<<<?|>?>>|<<?~|~>?>|<~>|<=|>=|===?|!==?|=~|&&&?|\\|\\|\\|?|=>|<-|\\\\\\\\|->)"
138 /* Not real operators, but should be grouped */
139 "|:?%[A-Za-z0-9_.]\\{\\}?"),
141 /* Don't match comment lines */
143 /* Don't match 'module procedure' lines */
144 "!^[ \t]*MODULE[ \t]+PROCEDURE[ \t]\n"
145 /* Program, module, block data */
146 "^[ \t]*((END[ \t]+)?(PROGRAM|MODULE|BLOCK[ \t]+DATA"
147 /* Subroutines and functions */
148 "|([^!'\" \t]+[ \t]+)*(SUBROUTINE|FUNCTION))[ \t]+[A-Z].*)$",
150 "[a-zA-Z][a-zA-Z0-9_]*"
151 "|\\.([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])\\."
152 /* numbers and format statements like 2E14.4, or ES12.6, 9X.
153 * Don't worry about format statements without leading digits since
154 * they would have been matched above as a variable anyway. */
155 "|[-+]?[0-9.]+([AaIiDdEeFfLlTtXx][Ss]?[-+]?[0-9.]*)?(_[a-zA-Z0-9][a-zA-Z0-9_]*)?"
156 "|//|\\*\\*|::|[/<>=]="),
158 "^((\\.[^.]|(int|ext|est|int\\.?/ext|i/e)[. ]).*)$",
163 "^[ \t]*(func[ \t]*.*(\\{[ \t]*)?)\n"
164 /* Structs and interfaces */
165 "^[ \t]*(type[ \t].*(struct|interface)[ \t]*(\\{[ \t]*)?)",
167 "[a-zA-Z_][a-zA-Z0-9_]*"
168 "|[-+0-9.eE]+i?|0[xX]?[0-9a-fA-F]+i?"
169 "|[-+*/<>%&^|=!:]=|--|\\+\\+|<<=?|>>=?|&\\^=?|&&|\\|\\||<-|\\.{3}"),
171 "^[ \t]*(<[Hh][1-6]([ \t].*)?>.*)$",
175 "!^[ \t]*(catch|do|for|if|instanceof|new|return|switch|throw|while)\n"
176 /* Class, enum, interface, and record declarations */
177 "^[ \t]*(([a-z-]+[ \t]+)*(class|enum|interface|record)[ \t]+.*)$\n"
178 /* Method definitions; note that constructor signatures are not */
179 /* matched because they are indistinguishable from method calls. */
180 "^[ \t]*(([A-Za-z_<>&][][?&<>.,A-Za-z_0-9]*[ \t]+)+[A-Za-z_][A-Za-z_0-9]*[ \t]*\\([^;]*)$",
182 "[a-zA-Z_][a-zA-Z0-9_]*"
183 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?"
185 "|--|\\+\\+|<<=?|>>>?=?|&&|\\|\\|"),
187 "^[ \t]*(([a-z]+[ \t]+)*(fun|class|interface)[ \t]+.*)$",
189 "[a-zA-Z_][a-zA-Z0-9_]*"
190 /* hexadecimal and binary numbers */
191 "|0[xXbB][0-9a-fA-F_]+[lLuU]*"
192 /* integers and floats */
193 "|[0-9][0-9_]*([.][0-9_]*)?([Ee][-+]?[0-9]+)?[fFlLuU]*"
194 /* floating point numbers beginning with decimal point */
195 "|[.][0-9][0-9_]*([Ee][-+]?[0-9]+)?[fFlLuU]?"
196 /* unary and binary operators */
197 "|[-+*/<>%&^|=!]==?|--|\\+\\+|<<=|>>=|&&|\\|\\||->|\\.\\*|!!|[?:.][.:]"),
199 "^ {0,3}#{1,6}[ \t].*",
204 * Octave pattern is mostly the same as matlab, except that '%%%' and
205 * '##' can also be used to begin code sections, in addition to '%%'
206 * that is understood by both.
208 "^[[:space:]]*((classdef|function)[[:space:]].*)$|^(%%%?|##)[[:space:]].*$",
210 "[a-zA-Z_][a-zA-Z0-9_]*|[-+0-9.e]+|[=~<>]=|\\.[*/\\^']|\\|\\||&&"),
212 /* Negate C statements that can look like functions */
213 "!^[ \t]*(do|for|if|else|return|switch|while)\n"
214 /* Objective-C methods */
215 "^[ \t]*([-+][ \t]*\\([ \t]*[A-Za-z_][A-Za-z_0-9* \t]*\\)[ \t]*[A-Za-z_].*)$\n"
217 "^[ \t]*(([A-Za-z_][A-Za-z_0-9]*[ \t]+)+[A-Za-z_][A-Za-z_0-9]*[ \t]*\\([^;]*)$\n"
218 /* Objective-C class/protocol definitions */
219 "^(@(implementation|interface|protocol)[ \t].*)$",
221 "[a-zA-Z_][a-zA-Z0-9_]*"
222 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?"
223 "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->"),
225 "^(((class[ \t]+)?(procedure|function)|constructor|destructor|interface"
226 "|implementation|initialization|finalization)[ \t]*.*)$\n"
227 "^(.*=[ \t]*(class|record).*)$",
229 "[a-zA-Z_][a-zA-Z0-9_]*"
230 "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+"
231 "|<>|<=|>=|:=|\\.\\."),
234 "^sub [[:alnum:]_':]+[ \t]*"
235 "(\\([^)]*\\)[ \t]*)?" /* prototype */
237 * Attributes. A regex can't count nested parentheses,
238 * so just slurp up whatever we see, taking care not
239 * to accept lines like "sub foo; # defined elsewhere".
241 * An attribute could contain a semicolon, but at that
242 * point it seems reasonable enough to give up.
245 "(\\{[ \t]*)?" /* brace can come here or on the next line */
246 "(#.*)?$\n" /* comment */
247 "^(BEGIN|END|INIT|CHECK|UNITCHECK|AUTOLOAD|DESTROY)[ \t]*"
248 "(\\{[ \t]*)?" /* brace can come here or on the next line */
250 "^=head[0-9] .*", /* POD */
252 "[[:alpha:]_'][[:alnum:]_']*"
253 "|0[xb]?[0-9a-fA-F_]*"
254 /* taking care not to interpret 3..5 as (3.)(.5) */
255 "|[0-9a-fA-F_]+(\\.[0-9a-fA-F_]+)?([eE][-+]?[0-9_]+)?"
256 "|=>|-[rwxoRWXOezsfdlpSugkbctTBMAC>]|~~|::"
257 "|&&=|\\|\\|=|//=|\\*\\*="
258 "|&&|\\|\\||//|\\+\\+|--|\\*\\*|\\.\\.\\.?"
263 "^[\t ]*(((public|protected|private|static|abstract|final)[\t ]+)*function.*)$\n"
264 "^[\t ]*((((final|abstract)[\t ]+)?class|enum|interface|trait).*)$",
266 "[a-zA-Z_][a-zA-Z0-9_]*"
267 "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+"
268 "|[-+*/<>%&^|=!.]=|--|\\+\\+|<<=?|>>=?|===|&&|\\|\\||::|->"),
270 "^[ \t]*((class|(async[ \t]+)?def)[ \t].*)$",
272 "[a-zA-Z_][a-zA-Z0-9_]*"
273 "|[-+0-9.e]+[jJlL]?|0[xX]?[0-9a-fA-F]+[lL]?"
274 "|[-+*/<>%&^|=!]=|//=?|<<=?|>>=?|\\*\\*=?"),
277 "^[ \t]*((class|module|def)[ \t].*)$",
279 "(@|@@|\\$)?[a-zA-Z_][a-zA-Z0-9_]*"
280 "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+|\\?(\\\\C-)?(\\\\M-)?."
281 "|//=?|[-+*/<>%&^|=!]=|<<=?|>>=?|===|\\.{1,3}|::|[!=]~"),
283 "^[\t ]*((pub(\\([^\\)]+\\))?[\t ]+)?((async|const|unsafe|extern([\t ]+\"[^\"]+\"))[\t ]+)?(struct|enum|union|mod|trait|fn|impl|macro_rules!)[< \t]+[^;]*)$",
285 "[a-zA-Z_][a-zA-Z0-9_]*"
286 "|[0-9][0-9_a-fA-Fiosuxz]*(\\.([0-9]*[eE][+-]?)?[0-9_fF]*)?"
287 "|[-+*\\/<>%&^|=!:]=|<<=?|>>=?|&&|\\|\\||->|=>|\\.{2}=|\\.{3}|::"),
289 "^[\t ]*(\\(((define|def(struct|syntax|class|method|rules|record|proto|alias)?)[-*/ \t]|(library|module|struct|class)[*+ \t]).*)$",
291 * R7RS valid identifiers include any sequence enclosed
292 * within vertical lines having no backslashes
295 /* All other words should be delimited by spaces or parentheses */
296 "|([^][)(}{[ \t])+"),
297 PATTERNS("tex", "^(\\\\((sub)*section|chapter|part)\\*{0,1}\\{.*)$",
298 "\\\\[a-zA-Z@]+|\\\\.|([a-zA-Z0-9]|[^\x01-\x7f])+"),
299 { "default", NULL
, NULL
, -1, { NULL
, 0 } },
304 static struct userdiff_driver driver_true
= {
309 static struct userdiff_driver driver_false
= {
314 struct find_by_namelen_data
{
317 struct userdiff_driver
*driver
;
320 static int userdiff_find_by_namelen_cb(struct userdiff_driver
*driver
,
321 enum userdiff_driver_type type UNUSED
,
324 struct find_by_namelen_data
*cb_data
= priv
;
326 if (!xstrncmpz(driver
->name
, cb_data
->name
, cb_data
->len
)) {
327 cb_data
->driver
= driver
;
328 return 1; /* tell the caller to stop iterating */
333 static int regexec_supports_multi_byte_chars(void)
335 static const char not_space
[] = "[^[:space:]]";
336 static const char utf8_multi_byte_char
[] = "\xc2\xa3";
339 static int result
= -1;
343 if (regcomp(&re
, not_space
, REG_EXTENDED
))
344 BUG("invalid regular expression: %s", not_space
);
345 result
= !regexec(&re
, utf8_multi_byte_char
, 1, &match
, 0) &&
347 match
.rm_eo
== strlen(utf8_multi_byte_char
);
352 static struct userdiff_driver
*userdiff_find_by_namelen(const char *name
, size_t len
)
354 struct find_by_namelen_data udcbdata
= {
358 for_each_userdiff_driver(userdiff_find_by_namelen_cb
, &udcbdata
);
359 return udcbdata
.driver
;
362 static int parse_funcname(struct userdiff_funcname
*f
, const char *k
,
363 const char *v
, int cflags
)
365 if (git_config_string(&f
->pattern
, k
, v
) < 0)
371 static int parse_tristate(int *b
, const char *k
, const char *v
)
373 if (v
&& !strcasecmp(v
, "auto"))
376 *b
= git_config_bool(k
, v
);
380 static int parse_bool(int *b
, const char *k
, const char *v
)
382 *b
= git_config_bool(k
, v
);
386 int userdiff_config(const char *k
, const char *v
)
388 struct userdiff_driver
*drv
;
389 const char *name
, *type
;
392 if (parse_config_key(k
, "diff", &name
, &namelen
, &type
) || !name
)
395 drv
= userdiff_find_by_namelen(name
, namelen
);
397 ALLOC_GROW(drivers
, ndrivers
+1, drivers_alloc
);
398 drv
= &drivers
[ndrivers
++];
399 memset(drv
, 0, sizeof(*drv
));
400 drv
->name
= xmemdupz(name
, namelen
);
404 if (!strcmp(type
, "funcname"))
405 return parse_funcname(&drv
->funcname
, k
, v
, 0);
406 if (!strcmp(type
, "xfuncname"))
407 return parse_funcname(&drv
->funcname
, k
, v
, REG_EXTENDED
);
408 if (!strcmp(type
, "binary"))
409 return parse_tristate(&drv
->binary
, k
, v
);
410 if (!strcmp(type
, "command"))
411 return git_config_string(&drv
->external
, k
, v
);
412 if (!strcmp(type
, "textconv"))
413 return git_config_string(&drv
->textconv
, k
, v
);
414 if (!strcmp(type
, "cachetextconv"))
415 return parse_bool(&drv
->textconv_want_cache
, k
, v
);
416 if (!strcmp(type
, "wordregex"))
417 return git_config_string(&drv
->word_regex
, k
, v
);
418 if (!strcmp(type
, "algorithm"))
419 return git_config_string(&drv
->algorithm
, k
, v
);
424 struct userdiff_driver
*userdiff_find_by_name(const char *name
)
426 int len
= strlen(name
);
427 struct userdiff_driver
*driver
= userdiff_find_by_namelen(name
, len
);
428 if (driver
&& driver
->word_regex_multi_byte
) {
429 if (regexec_supports_multi_byte_chars())
430 driver
->word_regex
= driver
->word_regex_multi_byte
;
431 driver
->word_regex_multi_byte
= NULL
;
436 struct userdiff_driver
*userdiff_find_by_path(struct index_state
*istate
,
439 static struct attr_check
*check
;
442 check
= attr_check_initl("diff", NULL
);
445 git_check_attr(istate
, path
, check
);
447 if (ATTR_TRUE(check
->items
[0].value
))
449 if (ATTR_FALSE(check
->items
[0].value
))
450 return &driver_false
;
451 if (ATTR_UNSET(check
->items
[0].value
))
453 return userdiff_find_by_name(check
->items
[0].value
);
456 struct userdiff_driver
*userdiff_get_textconv(struct repository
*r
,
457 struct userdiff_driver
*driver
)
459 if (!driver
->textconv
)
462 if (driver
->textconv_want_cache
&& !driver
->textconv_cache
) {
463 struct notes_cache
*c
= xmalloc(sizeof(*c
));
464 struct strbuf name
= STRBUF_INIT
;
466 strbuf_addf(&name
, "textconv/%s", driver
->name
);
467 notes_cache_init(r
, c
, name
.buf
, driver
->textconv
);
468 driver
->textconv_cache
= c
;
469 strbuf_release(&name
);
475 static int for_each_userdiff_driver_list(each_userdiff_driver_fn fn
,
476 enum userdiff_driver_type type
, void *cb_data
,
477 struct userdiff_driver
*drv
,
482 for (i
= 0; i
< drv_size
; i
++) {
483 struct userdiff_driver
*item
= drv
+ i
;
484 if ((ret
= fn(item
, type
, cb_data
)))
490 int for_each_userdiff_driver(each_userdiff_driver_fn fn
, void *cb_data
)
494 ret
= for_each_userdiff_driver_list(fn
, USERDIFF_DRIVER_TYPE_CUSTOM
,
495 cb_data
, drivers
, ndrivers
);
499 ret
= for_each_userdiff_driver_list(fn
, USERDIFF_DRIVER_TYPE_BUILTIN
,
500 cb_data
, builtin_drivers
,
501 ARRAY_SIZE(builtin_drivers
));