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