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]+", \
21 #define IPATTERN(lang, rx, wrx) { \
26 .cflags = REG_EXTENDED | REG_ICASE, \
28 .word_regex = wrx "|[^[:space:]]|[\xc0-\xff][\x80-\xbf]+", \
32 * Built-in drivers for various languages, sorted by their names
33 * (except that the "default" is left at the end).
35 * When writing or updating patterns, assume that the contents these
36 * patterns are applied to are syntactically correct. The patterns
37 * can be simple without implementing all syntactical corner cases, as
38 * long as they are sufficiently permissive.
40 static struct userdiff_driver builtin_drivers
[] = {
42 "!^(.*[ \t])?(is[ \t]+new|renames|is[ \t]+separate)([ \t].*)?$\n"
43 "!^[ \t]*with[ \t].*$\n"
44 "^[ \t]*((procedure|function)[ \t]+.*)$\n"
45 "^[ \t]*((package|protected|task)[ \t]+.*)$",
47 "[a-zA-Z][a-zA-Z0-9_]*"
48 "|[-+]?[0-9][0-9#_.aAbBcCdDeEfF]*([eE][+-]?[0-9_]+)?"
49 "|=>|\\.\\.|\\*\\*|:=|/=|>=|<=|<<|>>|<>"),
51 /* Optional leading indentation */
53 /* Start of captured text */
56 /* POSIX identifier with mandatory parentheses */
57 "[a-zA-Z_][a-zA-Z0-9_]*[ \t]*\\([ \t]*\\))"
59 /* Bashism identifier with optional parentheses */
60 "(function[ \t]+[a-zA-Z_][a-zA-Z0-9_]*(([ \t]*\\([ \t]*\\))|([ \t]+))"
62 /* Optional whitespace */
64 /* Compound command starting with `{`, `(`, `((` or `[[` */
65 "(\\{|\\(\\(?|\\[\\[)"
66 /* End of captured text */
69 /* Characters not in the default $IFS value */
72 "(@[a-zA-Z]{1,}[ \t]*\\{{0,1}[ \t]*[^ \t\"@',\\#}{~%]*).*$",
74 "[={}\"]|[^={}\" \t]+"),
76 /* Jump targets or access declarations */
77 "!^[ \t]*[A-Za-z_][A-Za-z_0-9]*:[[:space:]]*($|/[/*])\n"
78 /* functions/methods, variables, and compounds at top level */
79 "^((::[[:space:]]*)?[A-Za-z_].*)$",
81 /* identifiers and keywords */
82 "[a-zA-Z_][a-zA-Z0-9_]*"
83 /* decimal and octal integers as well as floatingpoint numbers */
84 "|[0-9][0-9.]*([Ee][-+]?[0-9]+)?[fFlLuU]*"
85 /* hexadecimal and binary integers */
86 "|0[xXbB][0-9a-fA-F]+[lLuU]*"
87 /* floatingpoint numbers that begin with a decimal point */
88 "|\\.[0-9][0-9]*([Ee][-+]?[0-9]+)?[fFlL]?"
89 "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->\\*?|\\.\\*|<=>"),
92 "!^[ \t]*(do|while|for|if|else|instanceof|new|return|switch|case|throw|catch|using)\n"
93 /* Methods and constructors */
94 "^[ \t]*(((static|public|internal|private|protected|new|virtual|sealed|override|unsafe|async)[ \t]+)*[][<>@.~_[:alnum:]]+[ \t]+[<>@._[:alnum:]]+[ \t]*\\(.*\\))[ \t]*$\n"
96 "^[ \t]*(((static|public|internal|private|protected|new|virtual|sealed|override|unsafe)[ \t]+)*[][<>@.~_[:alnum:]]+[ \t]+[@._[:alnum:]]+)[ \t]*$\n"
97 /* Type definitions */
98 "^[ \t]*(((static|public|internal|private|protected|new|unsafe|sealed|abstract|partial)[ \t]+)*(class|enum|interface|struct|record)[ \t]+.*)$\n"
100 "^[ \t]*(namespace[ \t]+.*)$",
102 "[a-zA-Z_][a-zA-Z0-9_]*"
103 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?"
104 "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->"),
106 "![:;][[:space:]]*$\n"
107 "^[:[@.#]?[_a-z0-9].*$",
110 * This regex comes from W3C CSS specs. Should theoretically also
111 * allow ISO 10646 characters U+00A0 and higher,
112 * but they are not handled in this regex.
114 "-?[_a-zA-Z][-_a-zA-Z0-9]*" /* identifiers */
115 "|-?[0-9]+|\\#[0-9a-fA-F]+" /* numbers */
120 /* lines beginning with a word optionally preceded by '&' or the root */
121 "^[ \t]*((/[ \t]*\\{|&?[a-zA-Z_]).*)",
123 /* Property names and math operators */
124 "[a-zA-Z0-9,._+?#-]+"
125 "|[-+*/%&^|!~]|>>|<<|&&|\\|\\|"),
127 "^[ \t]*((def(macro|module|impl|protocol|p)?|test)[ \t].*)$",
129 /* Atoms, names, and module attributes */
130 "[@:]?[a-zA-Z0-9@_?!]+"
131 /* Numbers with specific base */
132 "|[-+]?0[xob][0-9a-fA-F]+"
134 "|[-+]?[0-9][0-9_.]*([eE][-+]?[0-9_]+)?"
135 /* Operators and atoms that represent them */
136 "|:?(\\+\\+|--|\\.\\.|~~~|<>|\\^\\^\\^|<?\\|>|<<<?|>?>>|<<?~|~>?>|<~>|<=|>=|===?|!==?|=~|&&&?|\\|\\|\\|?|=>|<-|\\\\\\\\|->)"
137 /* Not real operators, but should be grouped */
138 "|:?%[A-Za-z0-9_.]\\{\\}?"),
140 /* Don't match comment lines */
142 /* Don't match 'module procedure' lines */
143 "!^[ \t]*MODULE[ \t]+PROCEDURE[ \t]\n"
144 /* Program, module, block data */
145 "^[ \t]*((END[ \t]+)?(PROGRAM|MODULE|BLOCK[ \t]+DATA"
146 /* Subroutines and functions */
147 "|([^!'\" \t]+[ \t]+)*(SUBROUTINE|FUNCTION))[ \t]+[A-Z].*)$",
149 "[a-zA-Z][a-zA-Z0-9_]*"
150 "|\\.([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])\\."
151 /* numbers and format statements like 2E14.4, or ES12.6, 9X.
152 * Don't worry about format statements without leading digits since
153 * they would have been matched above as a variable anyway. */
154 "|[-+]?[0-9.]+([AaIiDdEeFfLlTtXx][Ss]?[-+]?[0-9.]*)?(_[a-zA-Z0-9][a-zA-Z0-9_]*)?"
155 "|//|\\*\\*|::|[/<>=]="),
157 "^((\\.[^.]|(int|ext|est|int\\.?/ext|i/e)[. ]).*)$",
162 "^[ \t]*(func[ \t]*.*(\\{[ \t]*)?)\n"
163 /* Structs and interfaces */
164 "^[ \t]*(type[ \t].*(struct|interface)[ \t]*(\\{[ \t]*)?)",
166 "[a-zA-Z_][a-zA-Z0-9_]*"
167 "|[-+0-9.eE]+i?|0[xX]?[0-9a-fA-F]+i?"
168 "|[-+*/<>%&^|=!:]=|--|\\+\\+|<<=?|>>=?|&\\^=?|&&|\\|\\||<-|\\.{3}"),
170 "^[ \t]*(<[Hh][1-6]([ \t].*)?>.*)$",
174 "!^[ \t]*(catch|do|for|if|instanceof|new|return|switch|throw|while)\n"
175 /* Class, enum, interface, and record declarations */
176 "^[ \t]*(([a-z-]+[ \t]+)*(class|enum|interface|record)[ \t]+.*)$\n"
177 /* Method definitions; note that constructor signatures are not */
178 /* matched because they are indistinguishable from method calls. */
179 "^[ \t]*(([A-Za-z_<>&][][?&<>.,A-Za-z_0-9]*[ \t]+)+[A-Za-z_][A-Za-z_0-9]*[ \t]*\\([^;]*)$",
181 "[a-zA-Z_][a-zA-Z0-9_]*"
182 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?"
184 "|--|\\+\\+|<<=?|>>>?=?|&&|\\|\\|"),
186 "^[ \t]*(([a-z]+[ \t]+)*(fun|class|interface)[ \t]+.*)$",
188 "[a-zA-Z_][a-zA-Z0-9_]*"
189 /* hexadecimal and binary numbers */
190 "|0[xXbB][0-9a-fA-F_]+[lLuU]*"
191 /* integers and floats */
192 "|[0-9][0-9_]*([.][0-9_]*)?([Ee][-+]?[0-9]+)?[fFlLuU]*"
193 /* floating point numbers beginning with decimal point */
194 "|[.][0-9][0-9_]*([Ee][-+]?[0-9]+)?[fFlLuU]?"
195 /* unary and binary operators */
196 "|[-+*/<>%&^|=!]==?|--|\\+\\+|<<=|>>=|&&|\\|\\||->|\\.\\*|!!|[?:.][.:]"),
198 "^ {0,3}#{1,6}[ \t].*",
203 * Octave pattern is mostly the same as matlab, except that '%%%' and
204 * '##' can also be used to begin code sections, in addition to '%%'
205 * that is understood by both.
207 "^[[:space:]]*((classdef|function)[[:space:]].*)$|^(%%%?|##)[[:space:]].*$",
209 "[a-zA-Z_][a-zA-Z0-9_]*|[-+0-9.e]+|[=~<>]=|\\.[*/\\^']|\\|\\||&&"),
211 /* Negate C statements that can look like functions */
212 "!^[ \t]*(do|for|if|else|return|switch|while)\n"
213 /* Objective-C methods */
214 "^[ \t]*([-+][ \t]*\\([ \t]*[A-Za-z_][A-Za-z_0-9* \t]*\\)[ \t]*[A-Za-z_].*)$\n"
216 "^[ \t]*(([A-Za-z_][A-Za-z_0-9]*[ \t]+)+[A-Za-z_][A-Za-z_0-9]*[ \t]*\\([^;]*)$\n"
217 /* Objective-C class/protocol definitions */
218 "^(@(implementation|interface|protocol)[ \t].*)$",
220 "[a-zA-Z_][a-zA-Z0-9_]*"
221 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?"
222 "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->"),
224 "^(((class[ \t]+)?(procedure|function)|constructor|destructor|interface"
225 "|implementation|initialization|finalization)[ \t]*.*)$\n"
226 "^(.*=[ \t]*(class|record).*)$",
228 "[a-zA-Z_][a-zA-Z0-9_]*"
229 "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+"
230 "|<>|<=|>=|:=|\\.\\."),
233 "^sub [[:alnum:]_':]+[ \t]*"
234 "(\\([^)]*\\)[ \t]*)?" /* prototype */
236 * Attributes. A regex can't count nested parentheses,
237 * so just slurp up whatever we see, taking care not
238 * to accept lines like "sub foo; # defined elsewhere".
240 * An attribute could contain a semicolon, but at that
241 * point it seems reasonable enough to give up.
244 "(\\{[ \t]*)?" /* brace can come here or on the next line */
245 "(#.*)?$\n" /* comment */
246 "^(BEGIN|END|INIT|CHECK|UNITCHECK|AUTOLOAD|DESTROY)[ \t]*"
247 "(\\{[ \t]*)?" /* brace can come here or on the next line */
249 "^=head[0-9] .*", /* POD */
251 "[[:alpha:]_'][[:alnum:]_']*"
252 "|0[xb]?[0-9a-fA-F_]*"
253 /* taking care not to interpret 3..5 as (3.)(.5) */
254 "|[0-9a-fA-F_]+(\\.[0-9a-fA-F_]+)?([eE][-+]?[0-9_]+)?"
255 "|=>|-[rwxoRWXOezsfdlpSugkbctTBMAC>]|~~|::"
256 "|&&=|\\|\\|=|//=|\\*\\*="
257 "|&&|\\|\\||//|\\+\\+|--|\\*\\*|\\.\\.\\.?"
262 "^[\t ]*(((public|protected|private|static|abstract|final)[\t ]+)*function.*)$\n"
263 "^[\t ]*((((final|abstract)[\t ]+)?class|enum|interface|trait).*)$",
265 "[a-zA-Z_][a-zA-Z0-9_]*"
266 "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+"
267 "|[-+*/<>%&^|=!.]=|--|\\+\\+|<<=?|>>=?|===|&&|\\|\\||::|->"),
269 "^[ \t]*((class|(async[ \t]+)?def)[ \t].*)$",
271 "[a-zA-Z_][a-zA-Z0-9_]*"
272 "|[-+0-9.e]+[jJlL]?|0[xX]?[0-9a-fA-F]+[lL]?"
273 "|[-+*/<>%&^|=!]=|//=?|<<=?|>>=?|\\*\\*=?"),
276 "^[ \t]*((class|module|def)[ \t].*)$",
278 "(@|@@|\\$)?[a-zA-Z_][a-zA-Z0-9_]*"
279 "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+|\\?(\\\\C-)?(\\\\M-)?."
280 "|//=?|[-+*/<>%&^|=!]=|<<=?|>>=?|===|\\.{1,3}|::|[!=]~"),
282 "^[\t ]*((pub(\\([^\\)]+\\))?[\t ]+)?((async|const|unsafe|extern([\t ]+\"[^\"]+\"))[\t ]+)?(struct|enum|union|mod|trait|fn|impl|macro_rules!)[< \t]+[^;]*)$",
284 "[a-zA-Z_][a-zA-Z0-9_]*"
285 "|[0-9][0-9_a-fA-Fiosuxz]*(\\.([0-9]*[eE][+-]?)?[0-9_fF]*)?"
286 "|[-+*\\/<>%&^|=!:]=|<<=?|>>=?|&&|\\|\\||->|=>|\\.{2}=|\\.{3}|::"),
288 "^[\t ]*(\\(((define|def(struct|syntax|class|method|rules|record|proto|alias)?)[-*/ \t]|(library|module|struct|class)[*+ \t]).*)$",
290 * R7RS valid identifiers include any sequence enclosed
291 * within vertical lines having no backslashes
294 /* All other words should be delimited by spaces or parentheses */
295 "|([^][)(}{[ \t])+"),
296 PATTERNS("tex", "^(\\\\((sub)*section|chapter|part)\\*{0,1}\\{.*)$",
297 "\\\\[a-zA-Z@]+|\\\\.|[a-zA-Z0-9\x80-\xff]+"),
298 { "default", NULL
, -1, { NULL
, 0 } },
303 static struct userdiff_driver driver_true
= {
308 static struct userdiff_driver driver_false
= {
313 struct find_by_namelen_data
{
316 struct userdiff_driver
*driver
;
319 static int userdiff_find_by_namelen_cb(struct userdiff_driver
*driver
,
320 enum userdiff_driver_type type UNUSED
,
323 struct find_by_namelen_data
*cb_data
= priv
;
325 if (!strncmp(driver
->name
, cb_data
->name
, cb_data
->len
) &&
326 !driver
->name
[cb_data
->len
]) {
327 cb_data
->driver
= driver
;
328 return 1; /* tell the caller to stop iterating */
333 static struct userdiff_driver
*userdiff_find_by_namelen(const char *name
, size_t len
)
335 struct find_by_namelen_data udcbdata
= {
339 for_each_userdiff_driver(userdiff_find_by_namelen_cb
, &udcbdata
);
340 return udcbdata
.driver
;
343 static int parse_funcname(struct userdiff_funcname
*f
, const char *k
,
344 const char *v
, int cflags
)
346 if (git_config_string(&f
->pattern
, k
, v
) < 0)
352 static int parse_tristate(int *b
, const char *k
, const char *v
)
354 if (v
&& !strcasecmp(v
, "auto"))
357 *b
= git_config_bool(k
, v
);
361 static int parse_bool(int *b
, const char *k
, const char *v
)
363 *b
= git_config_bool(k
, v
);
367 int userdiff_config(const char *k
, const char *v
)
369 struct userdiff_driver
*drv
;
370 const char *name
, *type
;
373 if (parse_config_key(k
, "diff", &name
, &namelen
, &type
) || !name
)
376 drv
= userdiff_find_by_namelen(name
, namelen
);
378 ALLOC_GROW(drivers
, ndrivers
+1, drivers_alloc
);
379 drv
= &drivers
[ndrivers
++];
380 memset(drv
, 0, sizeof(*drv
));
381 drv
->name
= xmemdupz(name
, namelen
);
385 if (!strcmp(type
, "funcname"))
386 return parse_funcname(&drv
->funcname
, k
, v
, 0);
387 if (!strcmp(type
, "xfuncname"))
388 return parse_funcname(&drv
->funcname
, k
, v
, REG_EXTENDED
);
389 if (!strcmp(type
, "binary"))
390 return parse_tristate(&drv
->binary
, k
, v
);
391 if (!strcmp(type
, "command"))
392 return git_config_string(&drv
->external
, k
, v
);
393 if (!strcmp(type
, "textconv"))
394 return git_config_string(&drv
->textconv
, k
, v
);
395 if (!strcmp(type
, "cachetextconv"))
396 return parse_bool(&drv
->textconv_want_cache
, k
, v
);
397 if (!strcmp(type
, "wordregex"))
398 return git_config_string(&drv
->word_regex
, k
, v
);
403 struct userdiff_driver
*userdiff_find_by_name(const char *name
)
405 int len
= strlen(name
);
406 return userdiff_find_by_namelen(name
, len
);
409 struct userdiff_driver
*userdiff_find_by_path(struct index_state
*istate
,
412 static struct attr_check
*check
;
415 check
= attr_check_initl("diff", NULL
);
418 git_check_attr(istate
, NULL
, path
, check
);
420 if (ATTR_TRUE(check
->items
[0].value
))
422 if (ATTR_FALSE(check
->items
[0].value
))
423 return &driver_false
;
424 if (ATTR_UNSET(check
->items
[0].value
))
426 return userdiff_find_by_name(check
->items
[0].value
);
429 struct userdiff_driver
*userdiff_get_textconv(struct repository
*r
,
430 struct userdiff_driver
*driver
)
432 if (!driver
->textconv
)
435 if (driver
->textconv_want_cache
&& !driver
->textconv_cache
) {
436 struct notes_cache
*c
= xmalloc(sizeof(*c
));
437 struct strbuf name
= STRBUF_INIT
;
439 strbuf_addf(&name
, "textconv/%s", driver
->name
);
440 notes_cache_init(r
, c
, name
.buf
, driver
->textconv
);
441 driver
->textconv_cache
= c
;
442 strbuf_release(&name
);
448 static int for_each_userdiff_driver_list(each_userdiff_driver_fn fn
,
449 enum userdiff_driver_type type
, void *cb_data
,
450 struct userdiff_driver
*drv
,
455 for (i
= 0; i
< drv_size
; i
++) {
456 struct userdiff_driver
*item
= drv
+ i
;
457 if ((ret
= fn(item
, type
, cb_data
)))
463 int for_each_userdiff_driver(each_userdiff_driver_fn fn
, void *cb_data
)
467 ret
= for_each_userdiff_driver_list(fn
, USERDIFF_DRIVER_TYPE_CUSTOM
,
468 cb_data
, drivers
, ndrivers
);
472 ret
= for_each_userdiff_driver_list(fn
, USERDIFF_DRIVER_TYPE_BUILTIN
,
473 cb_data
, builtin_drivers
,
474 ARRAY_SIZE(builtin_drivers
));