]>
Commit | Line | Data |
---|---|---|
d9bae1a1 | 1 | #include "cache.h" |
b2141fc1 | 2 | #include "config.h" |
be58e70d | 3 | #include "userdiff.h" |
be58e70d JK |
4 | #include "attr.h" |
5 | ||
6 | static struct userdiff_driver *drivers; | |
7 | static int ndrivers; | |
8 | static int drivers_alloc; | |
9 | ||
ae3b970a | 10 | #define PATTERNS(name, pattern, word_regex) \ |
664d44ee JN |
11 | { name, NULL, -1, { pattern, REG_EXTENDED }, \ |
12 | word_regex "|[^[:space:]]|[\xc0-\xff][\x80-\xbf]+" } | |
909a5494 | 13 | #define IPATTERN(name, pattern, word_regex) \ |
664d44ee JN |
14 | { name, NULL, -1, { pattern, REG_EXTENDED | REG_ICASE }, \ |
15 | word_regex "|[^[:space:]]|[\xc0-\xff][\x80-\xbf]+" } | |
be58e70d | 16 | static struct userdiff_driver builtin_drivers[] = { |
e90d065e | 17 | IPATTERN("ada", |
39a87a29 | 18 | "!^(.*[ \t])?(is[ \t]+new|renames|is[ \t]+separate)([ \t].*)?$\n" |
e90d065e AJ |
19 | "!^[ \t]*with[ \t].*$\n" |
20 | "^[ \t]*((procedure|function)[ \t]+.*)$\n" | |
21 | "^[ \t]*((package|protected|task)[ \t]+.*)$", | |
22 | /* -- */ | |
23 | "[a-zA-Z][a-zA-Z0-9_]*" | |
39a87a29 | 24 | "|[-+]?[0-9][0-9#_.aAbBcCdDeEfF]*([eE][+-]?[0-9_]+)?" |
e90d065e | 25 | "|=>|\\.\\.|\\*\\*|:=|/=|>=|<=|<<|>>|<>"), |
3c81760b SB |
26 | PATTERNS("dts", |
27 | "!;\n" | |
8da56a48 | 28 | "!=\n" |
3c81760b | 29 | /* lines beginning with a word optionally preceded by '&' or the root */ |
8da56a48 | 30 | "^[ \t]*((/[ \t]*\\{|&?[a-zA-Z_]).*)", |
3c81760b SB |
31 | /* -- */ |
32 | /* Property names and math operators */ | |
33 | "[a-zA-Z0-9,._+?#-]+" | |
34 | "|[-+*/%&^|!~]|>>|<<|&&|\\|\\|"), | |
a807200f ŁN |
35 | PATTERNS("elixir", |
36 | "^[ \t]*((def(macro|module|impl|protocol|p)?|test)[ \t].*)$", | |
d1b1384d | 37 | /* -- */ |
a807200f | 38 | /* Atoms, names, and module attributes */ |
d1b1384d | 39 | "[@:]?[a-zA-Z0-9@_?!]+" |
a807200f ŁN |
40 | /* Numbers with specific base */ |
41 | "|[-+]?0[xob][0-9a-fA-F]+" | |
42 | /* Numbers */ | |
43 | "|[-+]?[0-9][0-9_.]*([eE][-+]?[0-9_]+)?" | |
44 | /* Operators and atoms that represent them */ | |
45 | "|:?(\\+\\+|--|\\.\\.|~~~|<>|\\^\\^\\^|<?\\|>|<<<?|>?>>|<<?~|~>?>|<~>|<=|>=|===?|!==?|=~|&&&?|\\|\\|\\|?|=>|<-|\\\\\\\\|->)" | |
46 | /* Not real operators, but should be grouped */ | |
47 | "|:?%[A-Za-z0-9_.]\\{\\}?"), | |
909a5494 BC |
48 | IPATTERN("fortran", |
49 | "!^([C*]|[ \t]*!)\n" | |
50 | "!^[ \t]*MODULE[ \t]+PROCEDURE[ \t]\n" | |
51 | "^[ \t]*((END[ \t]+)?(PROGRAM|MODULE|BLOCK[ \t]+DATA" | |
52 | "|([^'\" \t]+[ \t]+)*(SUBROUTINE|FUNCTION))[ \t]+[A-Z].*)$", | |
53 | /* -- */ | |
54 | "[a-zA-Z][a-zA-Z0-9_]*" | |
55 | "|\\.([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])\\." | |
56 | /* numbers and format statements like 2E14.4, or ES12.6, 9X. | |
57 | * Don't worry about format statements without leading digits since | |
58 | * they would have been matched above as a variable anyway. */ | |
59 | "|[-+]?[0-9.]+([AaIiDdEeFfLlTtXx][Ss]?[-+]?[0-9.]*)?(_[a-zA-Z0-9][a-zA-Z0-9_]*)?" | |
664d44ee | 60 | "|//|\\*\\*|::|[/<>=]="), |
69f9c87d ZB |
61 | IPATTERN("fountain", "^((\\.[^.]|(int|ext|est|int\\.?/ext|i/e)[. ]).*)$", |
62 | "[^ \t-]+"), | |
1dbf0c0a AG |
63 | PATTERNS("golang", |
64 | /* Functions */ | |
65 | "^[ \t]*(func[ \t]*.*(\\{[ \t]*)?)\n" | |
66 | /* Structs and interfaces */ | |
67 | "^[ \t]*(type[ \t].*(struct|interface)[ \t]*(\\{[ \t]*)?)", | |
68 | /* -- */ | |
69 | "[a-zA-Z_][a-zA-Z0-9_]*" | |
70 | "|[-+0-9.eE]+i?|0[xX]?[0-9a-fA-F]+i?" | |
71 | "|[-+*/<>%&^|=!:]=|--|\\+\\+|<<=?|>>=?|&\\^=?|&&|\\|\\||<-|\\.{3}"), | |
9c03caca | 72 | PATTERNS("html", "^[ \t]*(<[Hh][1-6]([ \t].*)?>.*)$", |
664d44ee | 73 | "[^<>= \t]+"), |
80c49c3d | 74 | PATTERNS("java", |
be58e70d | 75 | "!^[ \t]*(catch|do|for|if|instanceof|new|return|switch|throw|while)\n" |
959e2e64 PB |
76 | "^[ \t]*(([A-Za-z_][A-Za-z_0-9]*[ \t]+)+[A-Za-z_][A-Za-z_0-9]*[ \t]*\\([^;]*)$", |
77 | /* -- */ | |
80c49c3d TR |
78 | "[a-zA-Z_][a-zA-Z0-9_]*" |
79 | "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?" | |
80 | "|[-+*/<>%&^|=!]=" | |
664d44ee | 81 | "|--|\\+\\+|<<=?|>>>?=?|&&|\\|\\|"), |
09dad925 AH |
82 | PATTERNS("markdown", |
83 | "^ {0,3}#{1,6}[ \t].*", | |
84 | "[^<>= \t]+"), | |
53b10a14 | 85 | PATTERNS("matlab", |
2731a784 BL |
86 | /* |
87 | * Octave pattern is mostly the same as matlab, except that '%%%' and | |
91bf382f | 88 | * '##' can also be used to begin code sections, in addition to '%%' |
2731a784 BL |
89 | * that is understood by both. |
90 | */ | |
91bf382f | 91 | "^[[:space:]]*((classdef|function)[[:space:]].*)$|^(%%%?|##)[[:space:]].*$", |
53b10a14 | 92 | "[a-zA-Z_][a-zA-Z0-9_]*|[-+0-9.e]+|[=~<>]=|\\.[*/\\^']|\\|\\||&&"), |
80c49c3d | 93 | PATTERNS("objc", |
be58e70d JK |
94 | /* Negate C statements that can look like functions */ |
95 | "!^[ \t]*(do|for|if|else|return|switch|while)\n" | |
96 | /* Objective-C methods */ | |
97 | "^[ \t]*([-+][ \t]*\\([ \t]*[A-Za-z_][A-Za-z_0-9* \t]*\\)[ \t]*[A-Za-z_].*)$\n" | |
98 | /* C functions */ | |
959e2e64 | 99 | "^[ \t]*(([A-Za-z_][A-Za-z_0-9]*[ \t]+)+[A-Za-z_][A-Za-z_0-9]*[ \t]*\\([^;]*)$\n" |
be58e70d | 100 | /* Objective-C class/protocol definitions */ |
80c49c3d TR |
101 | "^(@(implementation|interface|protocol)[ \t].*)$", |
102 | /* -- */ | |
103 | "[a-zA-Z_][a-zA-Z0-9_]*" | |
104 | "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?" | |
664d44ee | 105 | "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->"), |
80c49c3d | 106 | PATTERNS("pascal", |
ad5b6942 | 107 | "^(((class[ \t]+)?(procedure|function)|constructor|destructor|interface|" |
be58e70d JK |
108 | "implementation|initialization|finalization)[ \t]*.*)$" |
109 | "\n" | |
80c49c3d TR |
110 | "^(.*=[ \t]*(class|record).*)$", |
111 | /* -- */ | |
112 | "[a-zA-Z_][a-zA-Z0-9_]*" | |
113 | "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+" | |
664d44ee | 114 | "|<>|<=|>=|:=|\\.\\."), |
71a5d4bc | 115 | PATTERNS("perl", |
ea2ca449 JN |
116 | "^package .*\n" |
117 | "^sub [[:alnum:]_':]+[ \t]*" | |
118 | "(\\([^)]*\\)[ \t]*)?" /* prototype */ | |
119 | /* | |
120 | * Attributes. A regex can't count nested parentheses, | |
121 | * so just slurp up whatever we see, taking care not | |
122 | * to accept lines like "sub foo; # defined elsewhere". | |
123 | * | |
124 | * An attribute could contain a semicolon, but at that | |
125 | * point it seems reasonable enough to give up. | |
126 | */ | |
127 | "(:[^;#]*)?" | |
128 | "(\\{[ \t]*)?" /* brace can come here or on the next line */ | |
129 | "(#.*)?$\n" /* comment */ | |
f143d9c6 | 130 | "^(BEGIN|END|INIT|CHECK|UNITCHECK|AUTOLOAD|DESTROY)[ \t]*" |
ea2ca449 JN |
131 | "(\\{[ \t]*)?" /* brace can come here or on the next line */ |
132 | "(#.*)?$\n" | |
12f0967a | 133 | "^=head[0-9] .*", /* POD */ |
71a5d4bc JN |
134 | /* -- */ |
135 | "[[:alpha:]_'][[:alnum:]_']*" | |
136 | "|0[xb]?[0-9a-fA-F_]*" | |
137 | /* taking care not to interpret 3..5 as (3.)(.5) */ | |
138 | "|[0-9a-fA-F_]+(\\.[0-9a-fA-F_]+)?([eE][-+]?[0-9_]+)?" | |
139 | "|=>|-[rwxoRWXOezsfdlpSugkbctTBMAC>]|~~|::" | |
140 | "|&&=|\\|\\|=|//=|\\*\\*=" | |
141 | "|&&|\\|\\||//|\\+\\+|--|\\*\\*|\\.\\.\\.?" | |
142 | "|[-+*/%.^&<>=!|]=" | |
143 | "|=~|!~" | |
664d44ee | 144 | "|<<|<>|<=>|>>"), |
6d2f208c BS |
145 | PATTERNS("php", |
146 | "^[\t ]*(((public|protected|private|static)[\t ]+)*function.*)$\n" | |
1ab63164 | 147 | "^[\t ]*((((final|abstract)[\t ]+)?class|interface|trait).*)$", |
80c49c3d TR |
148 | /* -- */ |
149 | "[a-zA-Z_][a-zA-Z0-9_]*" | |
150 | "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+" | |
664d44ee | 151 | "|[-+*/<>%&^|=!.]=|--|\\+\\+|<<=?|>>=?|===|&&|\\|\\||::|->"), |
077a1fda | 152 | PATTERNS("python", "^[ \t]*((class|(async[ \t]+)?def)[ \t].*)$", |
80c49c3d TR |
153 | /* -- */ |
154 | "[a-zA-Z_][a-zA-Z0-9_]*" | |
155 | "|[-+0-9.e]+[jJlL]?|0[xX]?[0-9a-fA-F]+[lL]?" | |
664d44ee | 156 | "|[-+*/<>%&^|=!]=|//=?|<<=?|>>=?|\\*\\*=?"), |
80c49c3d TR |
157 | /* -- */ |
158 | PATTERNS("ruby", "^[ \t]*((class|module|def)[ \t].*)$", | |
159 | /* -- */ | |
160 | "(@|@@|\\$)?[a-zA-Z_][a-zA-Z0-9_]*" | |
161 | "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+|\\?(\\\\C-)?(\\\\M-)?." | |
664d44ee | 162 | "|//=?|[-+*/<>%&^|=!]=|<<=?|>>=?|===|\\.{1,3}|::|[!=]~"), |
d74e7860 | 163 | PATTERNS("rust", |
33be7b38 | 164 | "^[\t ]*((pub(\\([^\\)]+\\))?[\t ]+)?((async|const|unsafe|extern([\t ]+\"[^\"]+\"))[\t ]+)?(struct|enum|union|mod|trait|fn|impl)[< \t]+[^;]*)$", |
d74e7860 MAL |
165 | /* -- */ |
166 | "[a-zA-Z_][a-zA-Z0-9_]*" | |
33be7b38 | 167 | "|[0-9][0-9_a-fA-Fiosuxz]*(\\.([0-9]*[eE][+-]?)?[0-9_fF]*)?" |
d74e7860 | 168 | "|[-+*\\/<>%&^|=!:]=|<<=?|>>=?|&&|\\|\\||->|=>|\\.{2}=|\\.{3}|::"), |
80c49c3d TR |
169 | PATTERNS("bibtex", "(@[a-zA-Z]{1,}[ \t]*\\{{0,1}[ \t]*[^ \t\"@',\\#}{~%]*).*$", |
170 | "[={}\"]|[^={}\" \t]+"), | |
171 | PATTERNS("tex", "^(\\\\((sub)*section|chapter|part)\\*{0,1}\\{.*)$", | |
664d44ee | 172 | "\\\\[a-zA-Z@]+|\\\\.|[a-zA-Z0-9\x80-\xff]+"), |
80c49c3d TR |
173 | PATTERNS("cpp", |
174 | /* Jump targets or access declarations */ | |
8a2e8da3 JS |
175 | "!^[ \t]*[A-Za-z_][A-Za-z_0-9]*:[[:space:]]*($|/[/*])\n" |
176 | /* functions/methods, variables, and compounds at top level */ | |
177 | "^((::[[:space:]]*)?[A-Za-z_].*)$", | |
80c49c3d TR |
178 | /* -- */ |
179 | "[a-zA-Z_][a-zA-Z0-9_]*" | |
abf8f986 | 180 | "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lLuU]*" |
407e07f2 | 181 | "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->\\*?|\\.\\*"), |
b221207d PO |
182 | PATTERNS("csharp", |
183 | /* Keywords */ | |
184 | "!^[ \t]*(do|while|for|if|else|instanceof|new|return|switch|case|throw|catch|using)\n" | |
185 | /* Methods and constructors */ | |
a12cec99 | 186 | "^[ \t]*(((static|public|internal|private|protected|new|virtual|sealed|override|unsafe|async)[ \t]+)*[][<>@.~_[:alnum:]]+[ \t]+[<>@._[:alnum:]]+[ \t]*\\(.*\\))[ \t]*$\n" |
b221207d PO |
187 | /* Properties */ |
188 | "^[ \t]*(((static|public|internal|private|protected|new|virtual|sealed|override|unsafe)[ \t]+)*[][<>@.~_[:alnum:]]+[ \t]+[@._[:alnum:]]+)[ \t]*$\n" | |
189 | /* Type definitions */ | |
190 | "^[ \t]*(((static|public|internal|private|protected|new|unsafe|sealed|abstract|partial)[ \t]+)*(class|enum|interface|struct)[ \t]+.*)$\n" | |
191 | /* Namespace */ | |
192 | "^[ \t]*(namespace[ \t]+.*)$", | |
193 | /* -- */ | |
194 | "[a-zA-Z_][a-zA-Z0-9_]*" | |
195 | "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?" | |
664d44ee | 196 | "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->"), |
0719f3ee WD |
197 | IPATTERN("css", |
198 | "![:;][[:space:]]*$\n" | |
199 | "^[_a-z0-9].*$", | |
200 | /* -- */ | |
201 | /* | |
202 | * This regex comes from W3C CSS specs. Should theoretically also | |
203 | * allow ISO 10646 characters U+00A0 and higher, | |
204 | * but they are not handled in this regex. | |
205 | */ | |
206 | "-?[_a-zA-Z][-_a-zA-Z0-9]*" /* identifiers */ | |
207 | "|-?[0-9]+|\\#[0-9a-fA-F]+" /* numbers */ | |
208 | ), | |
122aa6f9 | 209 | { "default", NULL, -1, { NULL, 0 } }, |
be58e70d | 210 | }; |
80c49c3d | 211 | #undef PATTERNS |
909a5494 | 212 | #undef IPATTERN |
be58e70d JK |
213 | |
214 | static struct userdiff_driver driver_true = { | |
215 | "diff=true", | |
216 | NULL, | |
122aa6f9 | 217 | 0, |
be58e70d JK |
218 | { NULL, 0 } |
219 | }; | |
be58e70d JK |
220 | |
221 | static struct userdiff_driver driver_false = { | |
222 | "!diff", | |
223 | NULL, | |
122aa6f9 | 224 | 1, |
be58e70d JK |
225 | { NULL, 0 } |
226 | }; | |
be58e70d | 227 | |
f5914f4b | 228 | static struct userdiff_driver *userdiff_find_by_namelen(const char *k, size_t len) |
be58e70d JK |
229 | { |
230 | int i; | |
231 | for (i = 0; i < ndrivers; i++) { | |
232 | struct userdiff_driver *drv = drivers + i; | |
233 | if (!strncmp(drv->name, k, len) && !drv->name[len]) | |
234 | return drv; | |
235 | } | |
236 | for (i = 0; i < ARRAY_SIZE(builtin_drivers); i++) { | |
237 | struct userdiff_driver *drv = builtin_drivers + i; | |
238 | if (!strncmp(drv->name, k, len) && !drv->name[len]) | |
239 | return drv; | |
240 | } | |
241 | return NULL; | |
242 | } | |
243 | ||
be58e70d JK |
244 | static int parse_funcname(struct userdiff_funcname *f, const char *k, |
245 | const char *v, int cflags) | |
246 | { | |
247 | if (git_config_string(&f->pattern, k, v) < 0) | |
248 | return -1; | |
249 | f->cflags = cflags; | |
6680a087 | 250 | return 0; |
be58e70d JK |
251 | } |
252 | ||
122aa6f9 JK |
253 | static int parse_tristate(int *b, const char *k, const char *v) |
254 | { | |
255 | if (v && !strcasecmp(v, "auto")) | |
256 | *b = -1; | |
257 | else | |
258 | *b = git_config_bool(k, v); | |
6680a087 | 259 | return 0; |
122aa6f9 JK |
260 | } |
261 | ||
d9bae1a1 JK |
262 | static int parse_bool(int *b, const char *k, const char *v) |
263 | { | |
264 | *b = git_config_bool(k, v); | |
6680a087 | 265 | return 0; |
d9bae1a1 JK |
266 | } |
267 | ||
c7534ef4 | 268 | int userdiff_config(const char *k, const char *v) |
be58e70d JK |
269 | { |
270 | struct userdiff_driver *drv; | |
0a5987fe | 271 | const char *name, *type; |
f5914f4b | 272 | size_t namelen; |
0a5987fe JK |
273 | |
274 | if (parse_config_key(k, "diff", &name, &namelen, &type) || !name) | |
275 | return 0; | |
276 | ||
277 | drv = userdiff_find_by_namelen(name, namelen); | |
278 | if (!drv) { | |
279 | ALLOC_GROW(drivers, ndrivers+1, drivers_alloc); | |
280 | drv = &drivers[ndrivers++]; | |
281 | memset(drv, 0, sizeof(*drv)); | |
282 | drv->name = xmemdupz(name, namelen); | |
283 | drv->binary = -1; | |
284 | } | |
be58e70d | 285 | |
0a5987fe | 286 | if (!strcmp(type, "funcname")) |
be58e70d | 287 | return parse_funcname(&drv->funcname, k, v, 0); |
0a5987fe | 288 | if (!strcmp(type, "xfuncname")) |
be58e70d | 289 | return parse_funcname(&drv->funcname, k, v, REG_EXTENDED); |
0a5987fe | 290 | if (!strcmp(type, "binary")) |
122aa6f9 | 291 | return parse_tristate(&drv->binary, k, v); |
0a5987fe | 292 | if (!strcmp(type, "command")) |
6680a087 | 293 | return git_config_string(&drv->external, k, v); |
0a5987fe | 294 | if (!strcmp(type, "textconv")) |
6680a087 | 295 | return git_config_string(&drv->textconv, k, v); |
0a5987fe | 296 | if (!strcmp(type, "cachetextconv")) |
d9bae1a1 | 297 | return parse_bool(&drv->textconv_want_cache, k, v); |
0a5987fe | 298 | if (!strcmp(type, "wordregex")) |
6680a087 | 299 | return git_config_string(&drv->word_regex, k, v); |
be58e70d JK |
300 | |
301 | return 0; | |
302 | } | |
303 | ||
3b335762 NTND |
304 | struct userdiff_driver *userdiff_find_by_name(const char *name) |
305 | { | |
be58e70d JK |
306 | int len = strlen(name); |
307 | return userdiff_find_by_namelen(name, len); | |
308 | } | |
309 | ||
acd00ea0 NTND |
310 | struct userdiff_driver *userdiff_find_by_path(struct index_state *istate, |
311 | const char *path) | |
be58e70d | 312 | { |
2aef63d3 | 313 | static struct attr_check *check; |
be58e70d | 314 | |
2aef63d3 JH |
315 | if (!check) |
316 | check = attr_check_initl("diff", NULL); | |
be58e70d JK |
317 | if (!path) |
318 | return NULL; | |
11877b9e | 319 | git_check_attr(istate, path, check); |
be58e70d | 320 | |
2aef63d3 | 321 | if (ATTR_TRUE(check->items[0].value)) |
be58e70d | 322 | return &driver_true; |
2aef63d3 | 323 | if (ATTR_FALSE(check->items[0].value)) |
be58e70d | 324 | return &driver_false; |
2aef63d3 | 325 | if (ATTR_UNSET(check->items[0].value)) |
be58e70d | 326 | return NULL; |
2aef63d3 | 327 | return userdiff_find_by_name(check->items[0].value); |
be58e70d | 328 | } |
3813e690 | 329 | |
bd7ad45b NTND |
330 | struct userdiff_driver *userdiff_get_textconv(struct repository *r, |
331 | struct userdiff_driver *driver) | |
3813e690 JK |
332 | { |
333 | if (!driver->textconv) | |
334 | return NULL; | |
335 | ||
336 | if (driver->textconv_want_cache && !driver->textconv_cache) { | |
337 | struct notes_cache *c = xmalloc(sizeof(*c)); | |
338 | struct strbuf name = STRBUF_INIT; | |
339 | ||
340 | strbuf_addf(&name, "textconv/%s", driver->name); | |
bd7ad45b | 341 | notes_cache_init(r, c, name.buf, driver->textconv); |
3813e690 | 342 | driver->textconv_cache = c; |
460c7eb2 | 343 | strbuf_release(&name); |
3813e690 JK |
344 | } |
345 | ||
346 | return driver; | |
347 | } |