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