1 #include "git-compat-util.h"
8 static struct userdiff_driver
*drivers
;
10 static int drivers_alloc
;
12 #define PATTERNS(lang, rx, wrx) { \
17 .cflags = REG_EXTENDED, \
19 .word_regex = wrx "|[^[:space:]]|[\xc0-\xff][\x80-\xbf]+", \
20 .word_regex_multi_byte = wrx "|[^[:space:]]", \
22 #define IPATTERN(lang, rx, wrx) { \
27 .cflags = REG_EXTENDED | REG_ICASE, \
29 .word_regex = wrx "|[^[:space:]]|[\xc0-\xff][\x80-\xbf]+", \
30 .word_regex_multi_byte = wrx "|[^[:space:]]", \
34 * Built-in drivers for various languages, sorted by their names
35 * (except that the "default" is left at the end).
37 * When writing or updating patterns, assume that the contents these
38 * patterns are applied to are syntactically correct. The patterns
39 * can be simple without implementing all syntactical corner cases, as
40 * long as they are sufficiently permissive.
42 static struct userdiff_driver builtin_drivers
[] = {
44 "!^(.*[ \t])?(is[ \t]+new|renames|is[ \t]+separate)([ \t].*)?$\n"
45 "!^[ \t]*with[ \t].*$\n"
46 "^[ \t]*((procedure|function)[ \t]+.*)$\n"
47 "^[ \t]*((package|protected|task)[ \t]+.*)$",
49 "[a-zA-Z][a-zA-Z0-9_]*"
50 "|[-+]?[0-9][0-9#_.aAbBcCdDeEfF]*([eE][+-]?[0-9_]+)?"
51 "|=>|\\.\\.|\\*\\*|:=|/=|>=|<=|<<|>>|<>"),
53 /* Optional leading indentation */
55 /* Start of captured text */
58 /* POSIX identifier with mandatory parentheses */
59 "[a-zA-Z_][a-zA-Z0-9_]*[ \t]*\\([ \t]*\\))"
61 /* Bashism identifier with optional parentheses */
62 "(function[ \t]+[a-zA-Z_][a-zA-Z0-9_]*(([ \t]*\\([ \t]*\\))|([ \t]+))"
64 /* Optional whitespace */
66 /* Compound command starting with `{`, `(`, `((` or `[[` */
67 "(\\{|\\(\\(?|\\[\\[)"
68 /* End of captured text */
71 /* Characters not in the default $IFS value */
74 "(@[a-zA-Z]{1,}[ \t]*\\{{0,1}[ \t]*[^ \t\"@',\\#}{~%]*).*$",
76 "[={}\"]|[^={}\" \t]+"),
78 /* Jump targets or access declarations */
79 "!^[ \t]*[A-Za-z_][A-Za-z_0-9]*:[[:space:]]*($|/[/*])\n"
80 /* functions/methods, variables, and compounds at top level */
81 "^((::[[:space:]]*)?[A-Za-z_].*)$",
83 /* identifiers and keywords */
84 "[a-zA-Z_][a-zA-Z0-9_]*"
85 /* decimal and octal integers as well as floatingpoint numbers */
86 "|[0-9][0-9.]*([Ee][-+]?[0-9]+)?[fFlLuU]*"
87 /* hexadecimal and binary integers */
88 "|0[xXbB][0-9a-fA-F]+[lLuU]*"
89 /* floatingpoint numbers that begin with a decimal point */
90 "|\\.[0-9][0-9]*([Ee][-+]?[0-9]+)?[fFlL]?"
91 "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->\\*?|\\.\\*|<=>"),
94 "!^[ \t]*(do|while|for|if|else|instanceof|new|return|switch|case|throw|catch|using)\n"
95 /* Methods and constructors */
96 "^[ \t]*(((static|public|internal|private|protected|new|virtual|sealed|override|unsafe|async)[ \t]+)*[][<>@.~_[:alnum:]]+[ \t]+[<>@._[:alnum:]]+[ \t]*\\(.*\\))[ \t]*$\n"
98 "^[ \t]*(((static|public|internal|private|protected|new|virtual|sealed|override|unsafe)[ \t]+)*[][<>@.~_[:alnum:]]+[ \t]+[@._[:alnum:]]+)[ \t]*$\n"
99 /* Type definitions */
100 "^[ \t]*(((static|public|internal|private|protected|new|unsafe|sealed|abstract|partial)[ \t]+)*(class|enum|interface|struct|record)[ \t]+.*)$\n"
102 "^[ \t]*(namespace[ \t]+.*)$",
104 "[a-zA-Z_][a-zA-Z0-9_]*"
105 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?"
106 "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->"),
108 "![:;][[:space:]]*$\n"
109 "^[:[@.#]?[_a-z0-9].*$",
112 * This regex comes from W3C CSS specs. Should theoretically also
113 * allow ISO 10646 characters U+00A0 and higher,
114 * but they are not handled in this regex.
116 "-?[_a-zA-Z][-_a-zA-Z0-9]*" /* identifiers */
117 "|-?[0-9]+|\\#[0-9a-fA-F]+" /* numbers */
122 /* lines beginning with a word optionally preceded by '&' or the root */
123 "^[ \t]*((/[ \t]*\\{|&?[a-zA-Z_]).*)",
125 /* Property names and math operators */
126 "[a-zA-Z0-9,._+?#-]+"
127 "|[-+*/%&^|!~]|>>|<<|&&|\\|\\|"),
129 "^[ \t]*((def(macro|module|impl|protocol|p)?|test)[ \t].*)$",
131 /* Atoms, names, and module attributes */
132 "[@:]?[a-zA-Z0-9@_?!]+"
133 /* Numbers with specific base */
134 "|[-+]?0[xob][0-9a-fA-F]+"
136 "|[-+]?[0-9][0-9_.]*([eE][-+]?[0-9_]+)?"
137 /* Operators and atoms that represent them */
138 "|:?(\\+\\+|--|\\.\\.|~~~|<>|\\^\\^\\^|<?\\|>|<<<?|>?>>|<<?~|~>?>|<~>|<=|>=|===?|!==?|=~|&&&?|\\|\\|\\|?|=>|<-|\\\\\\\\|->)"
139 /* Not real operators, but should be grouped */
140 "|:?%[A-Za-z0-9_.]\\{\\}?"),
142 /* Don't match comment lines */
144 /* Don't match 'module procedure' lines */
145 "!^[ \t]*MODULE[ \t]+PROCEDURE[ \t]\n"
146 /* Program, module, block data */
147 "^[ \t]*((END[ \t]+)?(PROGRAM|MODULE|BLOCK[ \t]+DATA"
148 /* Subroutines and functions */
149 "|([^!'\" \t]+[ \t]+)*(SUBROUTINE|FUNCTION))[ \t]+[A-Z].*)$",
151 "[a-zA-Z][a-zA-Z0-9_]*"
152 "|\\.([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])\\."
153 /* numbers and format statements like 2E14.4, or ES12.6, 9X.
154 * Don't worry about format statements without leading digits since
155 * they would have been matched above as a variable anyway. */
156 "|[-+]?[0-9.]+([AaIiDdEeFfLlTtXx][Ss]?[-+]?[0-9.]*)?(_[a-zA-Z0-9][a-zA-Z0-9_]*)?"
157 "|//|\\*\\*|::|[/<>=]="),
159 "^((\\.[^.]|(int|ext|est|int\\.?/ext|i/e)[. ]).*)$",
164 "^[ \t]*(func[ \t]*.*(\\{[ \t]*)?)\n"
165 /* Structs and interfaces */
166 "^[ \t]*(type[ \t].*(struct|interface)[ \t]*(\\{[ \t]*)?)",
168 "[a-zA-Z_][a-zA-Z0-9_]*"
169 "|[-+0-9.eE]+i?|0[xX]?[0-9a-fA-F]+i?"
170 "|[-+*/<>%&^|=!:]=|--|\\+\\+|<<=?|>>=?|&\\^=?|&&|\\|\\||<-|\\.{3}"),
172 "^[ \t]*(<[Hh][1-6]([ \t].*)?>.*)$",
176 "!^[ \t]*(catch|do|for|if|instanceof|new|return|switch|throw|while)\n"
177 /* Class, enum, interface, and record declarations */
178 "^[ \t]*(([a-z-]+[ \t]+)*(class|enum|interface|record)[ \t]+.*)$\n"
179 /* Method definitions; note that constructor signatures are not */
180 /* matched because they are indistinguishable from method calls. */
181 "^[ \t]*(([A-Za-z_<>&][][?&<>.,A-Za-z_0-9]*[ \t]+)+[A-Za-z_][A-Za-z_0-9]*[ \t]*\\([^;]*)$",
183 "[a-zA-Z_][a-zA-Z0-9_]*"
184 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?"
186 "|--|\\+\\+|<<=?|>>>?=?|&&|\\|\\|"),
188 "^[ \t]*(([a-z]+[ \t]+)*(fun|class|interface)[ \t]+.*)$",
190 "[a-zA-Z_][a-zA-Z0-9_]*"
191 /* hexadecimal and binary numbers */
192 "|0[xXbB][0-9a-fA-F_]+[lLuU]*"
193 /* integers and floats */
194 "|[0-9][0-9_]*([.][0-9_]*)?([Ee][-+]?[0-9]+)?[fFlLuU]*"
195 /* floating point numbers beginning with decimal point */
196 "|[.][0-9][0-9_]*([Ee][-+]?[0-9]+)?[fFlLuU]?"
197 /* unary and binary operators */
198 "|[-+*/<>%&^|=!]==?|--|\\+\\+|<<=|>>=|&&|\\|\\||->|\\.\\*|!!|[?:.][.:]"),
200 "^ {0,3}#{1,6}[ \t].*",
205 * Octave pattern is mostly the same as matlab, except that '%%%' and
206 * '##' can also be used to begin code sections, in addition to '%%'
207 * that is understood by both.
209 "^[[:space:]]*((classdef|function)[[:space:]].*)$|^(%%%?|##)[[:space:]].*$",
211 "[a-zA-Z_][a-zA-Z0-9_]*|[-+0-9.e]+|[=~<>]=|\\.[*/\\^']|\\|\\||&&"),
213 /* Negate C statements that can look like functions */
214 "!^[ \t]*(do|for|if|else|return|switch|while)\n"
215 /* Objective-C methods */
216 "^[ \t]*([-+][ \t]*\\([ \t]*[A-Za-z_][A-Za-z_0-9* \t]*\\)[ \t]*[A-Za-z_].*)$\n"
218 "^[ \t]*(([A-Za-z_][A-Za-z_0-9]*[ \t]+)+[A-Za-z_][A-Za-z_0-9]*[ \t]*\\([^;]*)$\n"
219 /* Objective-C class/protocol definitions */
220 "^(@(implementation|interface|protocol)[ \t].*)$",
222 "[a-zA-Z_][a-zA-Z0-9_]*"
223 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?"
224 "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->"),
226 "^(((class[ \t]+)?(procedure|function)|constructor|destructor|interface"
227 "|implementation|initialization|finalization)[ \t]*.*)$\n"
228 "^(.*=[ \t]*(class|record).*)$",
230 "[a-zA-Z_][a-zA-Z0-9_]*"
231 "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+"
232 "|<>|<=|>=|:=|\\.\\."),
235 "^sub [[:alnum:]_':]+[ \t]*"
236 "(\\([^)]*\\)[ \t]*)?" /* prototype */
238 * Attributes. A regex can't count nested parentheses,
239 * so just slurp up whatever we see, taking care not
240 * to accept lines like "sub foo; # defined elsewhere".
242 * An attribute could contain a semicolon, but at that
243 * point it seems reasonable enough to give up.
246 "(\\{[ \t]*)?" /* brace can come here or on the next line */
247 "(#.*)?$\n" /* comment */
248 "^(BEGIN|END|INIT|CHECK|UNITCHECK|AUTOLOAD|DESTROY)[ \t]*"
249 "(\\{[ \t]*)?" /* brace can come here or on the next line */
251 "^=head[0-9] .*", /* POD */
253 "[[:alpha:]_'][[:alnum:]_']*"
254 "|0[xb]?[0-9a-fA-F_]*"
255 /* taking care not to interpret 3..5 as (3.)(.5) */
256 "|[0-9a-fA-F_]+(\\.[0-9a-fA-F_]+)?([eE][-+]?[0-9_]+)?"
257 "|=>|-[rwxoRWXOezsfdlpSugkbctTBMAC>]|~~|::"
258 "|&&=|\\|\\|=|//=|\\*\\*="
259 "|&&|\\|\\||//|\\+\\+|--|\\*\\*|\\.\\.\\.?"
264 "^[\t ]*(((public|protected|private|static|abstract|final)[\t ]+)*function.*)$\n"
265 "^[\t ]*((((final|abstract)[\t ]+)?class|enum|interface|trait).*)$",
267 "[a-zA-Z_][a-zA-Z0-9_]*"
268 "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+"
269 "|[-+*/<>%&^|=!.]=|--|\\+\\+|<<=?|>>=?|===|&&|\\|\\||::|->"),
271 "^[ \t]*((class|(async[ \t]+)?def)[ \t].*)$",
273 "[a-zA-Z_][a-zA-Z0-9_]*"
274 "|[-+0-9.e]+[jJlL]?|0[xX]?[0-9a-fA-F]+[lL]?"
275 "|[-+*/<>%&^|=!]=|//=?|<<=?|>>=?|\\*\\*=?"),
278 "^[ \t]*((class|module|def)[ \t].*)$",
280 "(@|@@|\\$)?[a-zA-Z_][a-zA-Z0-9_]*"
281 "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+|\\?(\\\\C-)?(\\\\M-)?."
282 "|//=?|[-+*/<>%&^|=!]=|<<=?|>>=?|===|\\.{1,3}|::|[!=]~"),
284 "^[\t ]*((pub(\\([^\\)]+\\))?[\t ]+)?((async|const|unsafe|extern([\t ]+\"[^\"]+\"))[\t ]+)?(struct|enum|union|mod|trait|fn|impl|macro_rules!)[< \t]+[^;]*)$",
286 "[a-zA-Z_][a-zA-Z0-9_]*"
287 "|[0-9][0-9_a-fA-Fiosuxz]*(\\.([0-9]*[eE][+-]?)?[0-9_fF]*)?"
288 "|[-+*\\/<>%&^|=!:]=|<<=?|>>=?|&&|\\|\\||->|=>|\\.{2}=|\\.{3}|::"),
290 "^[\t ]*(\\(((define|def(struct|syntax|class|method|rules|record|proto|alias)?)[-*/ \t]|(library|module|struct|class)[*+ \t]).*)$",
292 * R7RS valid identifiers include any sequence enclosed
293 * within vertical lines having no backslashes
296 /* All other words should be delimited by spaces or parentheses */
297 "|([^][)(}{[ \t])+"),
298 PATTERNS("tex", "^(\\\\((sub)*section|chapter|part)\\*{0,1}\\{.*)$",
299 "\\\\[a-zA-Z@]+|\\\\.|([a-zA-Z0-9]|[^\x01-\x7f])+"),
300 { "default", NULL
, NULL
, -1, { NULL
, 0 } },
305 static struct userdiff_driver driver_true
= {
310 static struct userdiff_driver driver_false
= {
315 struct find_by_namelen_data
{
318 struct userdiff_driver
*driver
;
321 static int userdiff_find_by_namelen_cb(struct userdiff_driver
*driver
,
322 enum userdiff_driver_type type UNUSED
,
325 struct find_by_namelen_data
*cb_data
= priv
;
327 if (!strncmp(driver
->name
, cb_data
->name
, cb_data
->len
) &&
328 !driver
->name
[cb_data
->len
]) {
329 cb_data
->driver
= driver
;
330 return 1; /* tell the caller to stop iterating */
335 static int regexec_supports_multi_byte_chars(void)
337 static const char not_space
[] = "[^[:space:]]";
338 static const char utf8_multi_byte_char
[] = "\xc2\xa3";
341 static int result
= -1;
345 if (regcomp(&re
, not_space
, REG_EXTENDED
))
346 BUG("invalid regular expression: %s", not_space
);
347 result
= !regexec(&re
, utf8_multi_byte_char
, 1, &match
, 0) &&
349 match
.rm_eo
== strlen(utf8_multi_byte_char
);
354 static struct userdiff_driver
*userdiff_find_by_namelen(const char *name
, size_t len
)
356 struct find_by_namelen_data udcbdata
= {
360 for_each_userdiff_driver(userdiff_find_by_namelen_cb
, &udcbdata
);
361 return udcbdata
.driver
;
364 static int parse_funcname(struct userdiff_funcname
*f
, const char *k
,
365 const char *v
, int cflags
)
367 if (git_config_string(&f
->pattern
, k
, v
) < 0)
373 static int parse_tristate(int *b
, const char *k
, const char *v
)
375 if (v
&& !strcasecmp(v
, "auto"))
378 *b
= git_config_bool(k
, v
);
382 static int parse_bool(int *b
, const char *k
, const char *v
)
384 *b
= git_config_bool(k
, v
);
388 int userdiff_config(const char *k
, const char *v
)
390 struct userdiff_driver
*drv
;
391 const char *name
, *type
;
394 if (parse_config_key(k
, "diff", &name
, &namelen
, &type
) || !name
)
397 drv
= userdiff_find_by_namelen(name
, namelen
);
399 ALLOC_GROW(drivers
, ndrivers
+1, drivers_alloc
);
400 drv
= &drivers
[ndrivers
++];
401 memset(drv
, 0, sizeof(*drv
));
402 drv
->name
= xmemdupz(name
, namelen
);
406 if (!strcmp(type
, "funcname"))
407 return parse_funcname(&drv
->funcname
, k
, v
, 0);
408 if (!strcmp(type
, "xfuncname"))
409 return parse_funcname(&drv
->funcname
, k
, v
, REG_EXTENDED
);
410 if (!strcmp(type
, "binary"))
411 return parse_tristate(&drv
->binary
, k
, v
);
412 if (!strcmp(type
, "command"))
413 return git_config_string(&drv
->external
, k
, v
);
414 if (!strcmp(type
, "textconv"))
415 return git_config_string(&drv
->textconv
, k
, v
);
416 if (!strcmp(type
, "cachetextconv"))
417 return parse_bool(&drv
->textconv_want_cache
, k
, v
);
418 if (!strcmp(type
, "wordregex"))
419 return git_config_string(&drv
->word_regex
, k
, v
);
420 if (!strcmp(type
, "algorithm"))
421 return git_config_string(&drv
->algorithm
, k
, v
);
426 struct userdiff_driver
*userdiff_find_by_name(const char *name
)
428 int len
= strlen(name
);
429 struct userdiff_driver
*driver
= userdiff_find_by_namelen(name
, len
);
430 if (driver
&& driver
->word_regex_multi_byte
) {
431 if (regexec_supports_multi_byte_chars())
432 driver
->word_regex
= driver
->word_regex_multi_byte
;
433 driver
->word_regex_multi_byte
= NULL
;
438 struct userdiff_driver
*userdiff_find_by_path(struct index_state
*istate
,
441 static struct attr_check
*check
;
444 check
= attr_check_initl("diff", NULL
);
447 git_check_attr(istate
, NULL
, path
, check
);
449 if (ATTR_TRUE(check
->items
[0].value
))
451 if (ATTR_FALSE(check
->items
[0].value
))
452 return &driver_false
;
453 if (ATTR_UNSET(check
->items
[0].value
))
455 return userdiff_find_by_name(check
->items
[0].value
);
458 struct userdiff_driver
*userdiff_get_textconv(struct repository
*r
,
459 struct userdiff_driver
*driver
)
461 if (!driver
->textconv
)
464 if (driver
->textconv_want_cache
&& !driver
->textconv_cache
) {
465 struct notes_cache
*c
= xmalloc(sizeof(*c
));
466 struct strbuf name
= STRBUF_INIT
;
468 strbuf_addf(&name
, "textconv/%s", driver
->name
);
469 notes_cache_init(r
, c
, name
.buf
, driver
->textconv
);
470 driver
->textconv_cache
= c
;
471 strbuf_release(&name
);
477 static int for_each_userdiff_driver_list(each_userdiff_driver_fn fn
,
478 enum userdiff_driver_type type
, void *cb_data
,
479 struct userdiff_driver
*drv
,
484 for (i
= 0; i
< drv_size
; i
++) {
485 struct userdiff_driver
*item
= drv
+ i
;
486 if ((ret
= fn(item
, type
, cb_data
)))
492 int for_each_userdiff_driver(each_userdiff_driver_fn fn
, void *cb_data
)
496 ret
= for_each_userdiff_driver_list(fn
, USERDIFF_DRIVER_TYPE_CUSTOM
,
497 cb_data
, drivers
, ndrivers
);
501 ret
= for_each_userdiff_driver_list(fn
, USERDIFF_DRIVER_TYPE_BUILTIN
,
502 cb_data
, builtin_drivers
,
503 ARRAY_SIZE(builtin_drivers
));