]>
Commit | Line | Data |
---|---|---|
1 | #include "cache.h" | |
2 | #include "config.h" | |
3 | #include "userdiff.h" | |
4 | #include "attr.h" | |
5 | ||
6 | static struct userdiff_driver *drivers; | |
7 | static int ndrivers; | |
8 | static int drivers_alloc; | |
9 | ||
10 | #define PATTERNS(name, pattern, word_regex) \ | |
11 | { name, NULL, -1, { pattern, REG_EXTENDED }, \ | |
12 | word_regex "|[^[:space:]]|[\xc0-\xff][\x80-\xbf]+" } | |
13 | #define IPATTERN(name, pattern, word_regex) \ | |
14 | { name, NULL, -1, { pattern, REG_EXTENDED | REG_ICASE }, \ | |
15 | word_regex "|[^[:space:]]|[\xc0-\xff][\x80-\xbf]+" } | |
16 | static struct userdiff_driver builtin_drivers[] = { | |
17 | IPATTERN("ada", | |
18 | "!^(.*[ \t])?(is[ \t]+new|renames|is[ \t]+separate)([ \t].*)?$\n" | |
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_]*" | |
24 | "|[-+]?[0-9][0-9#_.aAbBcCdDeEfF]*([eE][+-]?[0-9_]+)?" | |
25 | "|=>|\\.\\.|\\*\\*|:=|/=|>=|<=|<<|>>|<>"), | |
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 | "|[-+*/%&^|!~]|>>|<<|&&|\\|\\|"), | |
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_]*)?" | |
46 | "|//|\\*\\*|::|[/<>=]="), | |
47 | IPATTERN("fountain", "^((\\.[^.]|(int|ext|est|int\\.?/ext|i/e)[. ]).*)$", | |
48 | "[^ \t-]+"), | |
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}"), | |
58 | PATTERNS("html", "^[ \t]*(<[Hh][1-6]([ \t].*)?>.*)$", | |
59 | "[^<>= \t]+"), | |
60 | PATTERNS("java", | |
61 | "!^[ \t]*(catch|do|for|if|instanceof|new|return|switch|throw|while)\n" | |
62 | "^[ \t]*(([A-Za-z_][A-Za-z_0-9]*[ \t]+)+[A-Za-z_][A-Za-z_0-9]*[ \t]*\\([^;]*)$", | |
63 | /* -- */ | |
64 | "[a-zA-Z_][a-zA-Z0-9_]*" | |
65 | "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?" | |
66 | "|[-+*/<>%&^|=!]=" | |
67 | "|--|\\+\\+|<<=?|>>>?=?|&&|\\|\\|"), | |
68 | PATTERNS("matlab", | |
69 | /* | |
70 | * Octave pattern is mostly the same as matlab, except that '%%%' and | |
71 | * '##' can also be used to begin code sections, in addition to '%%' | |
72 | * that is understood by both. | |
73 | */ | |
74 | "^[[:space:]]*((classdef|function)[[:space:]].*)$|^(%%%?|##)[[:space:]].*$", | |
75 | "[a-zA-Z_][a-zA-Z0-9_]*|[-+0-9.e]+|[=~<>]=|\\.[*/\\^']|\\|\\||&&"), | |
76 | PATTERNS("objc", | |
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 */ | |
82 | "^[ \t]*(([A-Za-z_][A-Za-z_0-9]*[ \t]+)+[A-Za-z_][A-Za-z_0-9]*[ \t]*\\([^;]*)$\n" | |
83 | /* Objective-C class/protocol definitions */ | |
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]?" | |
88 | "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->"), | |
89 | PATTERNS("pascal", | |
90 | "^(((class[ \t]+)?(procedure|function)|constructor|destructor|interface|" | |
91 | "implementation|initialization|finalization)[ \t]*.*)$" | |
92 | "\n" | |
93 | "^(.*=[ \t]*(class|record).*)$", | |
94 | /* -- */ | |
95 | "[a-zA-Z_][a-zA-Z0-9_]*" | |
96 | "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+" | |
97 | "|<>|<=|>=|:=|\\.\\."), | |
98 | PATTERNS("perl", | |
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 */ | |
113 | "^(BEGIN|END|INIT|CHECK|UNITCHECK|AUTOLOAD|DESTROY)[ \t]*" | |
114 | "(\\{[ \t]*)?" /* brace can come here or on the next line */ | |
115 | "(#.*)?$\n" | |
116 | "^=head[0-9] .*", /* POD */ | |
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 | "|=~|!~" | |
127 | "|<<|<>|<=>|>>"), | |
128 | PATTERNS("php", | |
129 | "^[\t ]*(((public|protected|private|static)[\t ]+)*function.*)$\n" | |
130 | "^[\t ]*((((final|abstract)[\t ]+)?class|interface|trait).*)$", | |
131 | /* -- */ | |
132 | "[a-zA-Z_][a-zA-Z0-9_]*" | |
133 | "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+" | |
134 | "|[-+*/<>%&^|=!.]=|--|\\+\\+|<<=?|>>=?|===|&&|\\|\\||::|->"), | |
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]?" | |
139 | "|[-+*/<>%&^|=!]=|//=?|<<=?|>>=?|\\*\\*=?"), | |
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-)?." | |
145 | "|//=?|[-+*/<>%&^|=!]=|<<=?|>>=?|===|\\.{1,3}|::|[!=]~"), | |
146 | PATTERNS("rust", | |
147 | "^[\t ]*((pub(\\([^\\)]+\\))?[\t ]+)?((async|const|unsafe|extern([\t ]+\"[^\"]+\"))[\t ]+)?(struct|enum|union|mod|trait|fn|impl)[< \t]+[^;]*)$", | |
148 | /* -- */ | |
149 | "[a-zA-Z_][a-zA-Z0-9_]*" | |
150 | "|[0-9][0-9_a-fA-Fiosuxz]*(\\.([0-9]*[eE][+-]?)?[0-9_fF]*)?" | |
151 | "|[-+*\\/<>%&^|=!:]=|<<=?|>>=?|&&|\\|\\||->|=>|\\.{2}=|\\.{3}|::"), | |
152 | PATTERNS("bibtex", "(@[a-zA-Z]{1,}[ \t]*\\{{0,1}[ \t]*[^ \t\"@',\\#}{~%]*).*$", | |
153 | "[={}\"]|[^={}\" \t]+"), | |
154 | PATTERNS("tex", "^(\\\\((sub)*section|chapter|part)\\*{0,1}\\{.*)$", | |
155 | "\\\\[a-zA-Z@]+|\\\\.|[a-zA-Z0-9\x80-\xff]+"), | |
156 | PATTERNS("cpp", | |
157 | /* Jump targets or access declarations */ | |
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_].*)$", | |
161 | /* -- */ | |
162 | "[a-zA-Z_][a-zA-Z0-9_]*" | |
163 | "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lLuU]*" | |
164 | "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->\\*?|\\.\\*"), | |
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 */ | |
169 | "^[ \t]*(((static|public|internal|private|protected|new|virtual|sealed|override|unsafe|async)[ \t]+)*[][<>@.~_[:alnum:]]+[ \t]+[<>@._[:alnum:]]+[ \t]*\\(.*\\))[ \t]*$\n" | |
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]?" | |
179 | "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->"), | |
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 | ), | |
192 | { "default", NULL, -1, { NULL, 0 } }, | |
193 | }; | |
194 | #undef PATTERNS | |
195 | #undef IPATTERN | |
196 | ||
197 | static struct userdiff_driver driver_true = { | |
198 | "diff=true", | |
199 | NULL, | |
200 | 0, | |
201 | { NULL, 0 } | |
202 | }; | |
203 | ||
204 | static struct userdiff_driver driver_false = { | |
205 | "!diff", | |
206 | NULL, | |
207 | 1, | |
208 | { NULL, 0 } | |
209 | }; | |
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 | ||
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; | |
233 | return 0; | |
234 | } | |
235 | ||
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); | |
242 | return 0; | |
243 | } | |
244 | ||
245 | static int parse_bool(int *b, const char *k, const char *v) | |
246 | { | |
247 | *b = git_config_bool(k, v); | |
248 | return 0; | |
249 | } | |
250 | ||
251 | int userdiff_config(const char *k, const char *v) | |
252 | { | |
253 | struct userdiff_driver *drv; | |
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 | } | |
268 | ||
269 | if (!strcmp(type, "funcname")) | |
270 | return parse_funcname(&drv->funcname, k, v, 0); | |
271 | if (!strcmp(type, "xfuncname")) | |
272 | return parse_funcname(&drv->funcname, k, v, REG_EXTENDED); | |
273 | if (!strcmp(type, "binary")) | |
274 | return parse_tristate(&drv->binary, k, v); | |
275 | if (!strcmp(type, "command")) | |
276 | return git_config_string(&drv->external, k, v); | |
277 | if (!strcmp(type, "textconv")) | |
278 | return git_config_string(&drv->textconv, k, v); | |
279 | if (!strcmp(type, "cachetextconv")) | |
280 | return parse_bool(&drv->textconv_want_cache, k, v); | |
281 | if (!strcmp(type, "wordregex")) | |
282 | return git_config_string(&drv->word_regex, k, v); | |
283 | ||
284 | return 0; | |
285 | } | |
286 | ||
287 | struct userdiff_driver *userdiff_find_by_name(const char *name) | |
288 | { | |
289 | int len = strlen(name); | |
290 | return userdiff_find_by_namelen(name, len); | |
291 | } | |
292 | ||
293 | struct userdiff_driver *userdiff_find_by_path(struct index_state *istate, | |
294 | const char *path) | |
295 | { | |
296 | static struct attr_check *check; | |
297 | ||
298 | if (!check) | |
299 | check = attr_check_initl("diff", NULL); | |
300 | if (!path) | |
301 | return NULL; | |
302 | git_check_attr(istate, path, check); | |
303 | ||
304 | if (ATTR_TRUE(check->items[0].value)) | |
305 | return &driver_true; | |
306 | if (ATTR_FALSE(check->items[0].value)) | |
307 | return &driver_false; | |
308 | if (ATTR_UNSET(check->items[0].value)) | |
309 | return NULL; | |
310 | return userdiff_find_by_name(check->items[0].value); | |
311 | } | |
312 | ||
313 | struct userdiff_driver *userdiff_get_textconv(struct repository *r, | |
314 | struct userdiff_driver *driver) | |
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); | |
324 | notes_cache_init(r, c, name.buf, driver->textconv); | |
325 | driver->textconv_cache = c; | |
326 | strbuf_release(&name); | |
327 | } | |
328 | ||
329 | return driver; | |
330 | } |