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