]> git.ipfire.org Git - thirdparty/git.git/blob - userdiff.c
Merge branch 'master' of github.com:vnwildman/git
[thirdparty/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 PATTERNS("dts",
27 "!;\n"
28 "!=\n"
29 /* lines beginning with a word optionally preceded by '&' or the root */
30 "^[ \t]*((/[ \t]*\\{|&?[a-zA-Z_]).*)",
31 /* -- */
32 /* Property names and math operators */
33 "[a-zA-Z0-9,._+?#-]+"
34 "|[-+*/%&^|!~]|>>|<<|&&|\\|\\|"),
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_]*)?"
47 "|//|\\*\\*|::|[/<>=]="),
48 IPATTERN("fountain", "^((\\.[^.]|(int|ext|est|int\\.?/ext|i/e)[. ]).*)$",
49 "[^ \t-]+"),
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}"),
59 PATTERNS("html", "^[ \t]*(<[Hh][1-6]([ \t].*)?>.*)$",
60 "[^<>= \t]+"),
61 PATTERNS("java",
62 "!^[ \t]*(catch|do|for|if|instanceof|new|return|switch|throw|while)\n"
63 "^[ \t]*(([A-Za-z_][A-Za-z_0-9]*[ \t]+)+[A-Za-z_][A-Za-z_0-9]*[ \t]*\\([^;]*)$",
64 /* -- */
65 "[a-zA-Z_][a-zA-Z0-9_]*"
66 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?"
67 "|[-+*/<>%&^|=!]="
68 "|--|\\+\\+|<<=?|>>>?=?|&&|\\|\\|"),
69 PATTERNS("matlab",
70 /*
71 * Octave pattern is mostly the same as matlab, except that '%%%' and
72 * '##' can also be used to begin code sections, in addition to '%%'
73 * that is understood by both.
74 */
75 "^[[:space:]]*((classdef|function)[[:space:]].*)$|^(%%%?|##)[[:space:]].*$",
76 "[a-zA-Z_][a-zA-Z0-9_]*|[-+0-9.e]+|[=~<>]=|\\.[*/\\^']|\\|\\||&&"),
77 PATTERNS("objc",
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 */
83 "^[ \t]*(([A-Za-z_][A-Za-z_0-9]*[ \t]+)+[A-Za-z_][A-Za-z_0-9]*[ \t]*\\([^;]*)$\n"
84 /* Objective-C class/protocol definitions */
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]?"
89 "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->"),
90 PATTERNS("pascal",
91 "^(((class[ \t]+)?(procedure|function)|constructor|destructor|interface|"
92 "implementation|initialization|finalization)[ \t]*.*)$"
93 "\n"
94 "^(.*=[ \t]*(class|record).*)$",
95 /* -- */
96 "[a-zA-Z_][a-zA-Z0-9_]*"
97 "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+"
98 "|<>|<=|>=|:=|\\.\\."),
99 PATTERNS("perl",
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 */
114 "^(BEGIN|END|INIT|CHECK|UNITCHECK|AUTOLOAD|DESTROY)[ \t]*"
115 "(\\{[ \t]*)?" /* brace can come here or on the next line */
116 "(#.*)?$\n"
117 "^=head[0-9] .*", /* POD */
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 "|=~|!~"
128 "|<<|<>|<=>|>>"),
129 PATTERNS("php",
130 "^[\t ]*(((public|protected|private|static)[\t ]+)*function.*)$\n"
131 "^[\t ]*((((final|abstract)[\t ]+)?class|interface|trait).*)$",
132 /* -- */
133 "[a-zA-Z_][a-zA-Z0-9_]*"
134 "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+"
135 "|[-+*/<>%&^|=!.]=|--|\\+\\+|<<=?|>>=?|===|&&|\\|\\||::|->"),
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]?"
140 "|[-+*/<>%&^|=!]=|//=?|<<=?|>>=?|\\*\\*=?"),
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-)?."
146 "|//=?|[-+*/<>%&^|=!]=|<<=?|>>=?|===|\\.{1,3}|::|[!=]~"),
147 PATTERNS("rust",
148 "^[\t ]*((pub(\\([^\\)]+\\))?[\t ]+)?((async|const|unsafe|extern([\t ]+\"[^\"]+\"))[\t ]+)?(struct|enum|union|mod|trait|fn|impl)[< \t]+[^;]*)$",
149 /* -- */
150 "[a-zA-Z_][a-zA-Z0-9_]*"
151 "|[0-9][0-9_a-fA-Fiosuxz]*(\\.([0-9]*[eE][+-]?)?[0-9_fF]*)?"
152 "|[-+*\\/<>%&^|=!:]=|<<=?|>>=?|&&|\\|\\||->|=>|\\.{2}=|\\.{3}|::"),
153 PATTERNS("bibtex", "(@[a-zA-Z]{1,}[ \t]*\\{{0,1}[ \t]*[^ \t\"@',\\#}{~%]*).*$",
154 "[={}\"]|[^={}\" \t]+"),
155 PATTERNS("tex", "^(\\\\((sub)*section|chapter|part)\\*{0,1}\\{.*)$",
156 "\\\\[a-zA-Z@]+|\\\\.|[a-zA-Z0-9\x80-\xff]+"),
157 PATTERNS("cpp",
158 /* Jump targets or access declarations */
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_].*)$",
162 /* -- */
163 "[a-zA-Z_][a-zA-Z0-9_]*"
164 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lLuU]*"
165 "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->\\*?|\\.\\*"),
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 */
170 "^[ \t]*(((static|public|internal|private|protected|new|virtual|sealed|override|unsafe|async)[ \t]+)*[][<>@.~_[:alnum:]]+[ \t]+[<>@._[:alnum:]]+[ \t]*\\(.*\\))[ \t]*$\n"
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]?"
180 "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->"),
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 ),
193 { "default", NULL, -1, { NULL, 0 } },
194 };
195 #undef PATTERNS
196 #undef IPATTERN
197
198 static struct userdiff_driver driver_true = {
199 "diff=true",
200 NULL,
201 0,
202 { NULL, 0 }
203 };
204
205 static struct userdiff_driver driver_false = {
206 "!diff",
207 NULL,
208 1,
209 { NULL, 0 }
210 };
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
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;
234 return 0;
235 }
236
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);
243 return 0;
244 }
245
246 static int parse_bool(int *b, const char *k, const char *v)
247 {
248 *b = git_config_bool(k, v);
249 return 0;
250 }
251
252 int userdiff_config(const char *k, const char *v)
253 {
254 struct userdiff_driver *drv;
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 }
269
270 if (!strcmp(type, "funcname"))
271 return parse_funcname(&drv->funcname, k, v, 0);
272 if (!strcmp(type, "xfuncname"))
273 return parse_funcname(&drv->funcname, k, v, REG_EXTENDED);
274 if (!strcmp(type, "binary"))
275 return parse_tristate(&drv->binary, k, v);
276 if (!strcmp(type, "command"))
277 return git_config_string(&drv->external, k, v);
278 if (!strcmp(type, "textconv"))
279 return git_config_string(&drv->textconv, k, v);
280 if (!strcmp(type, "cachetextconv"))
281 return parse_bool(&drv->textconv_want_cache, k, v);
282 if (!strcmp(type, "wordregex"))
283 return git_config_string(&drv->word_regex, k, v);
284
285 return 0;
286 }
287
288 struct userdiff_driver *userdiff_find_by_name(const char *name)
289 {
290 int len = strlen(name);
291 return userdiff_find_by_namelen(name, len);
292 }
293
294 struct userdiff_driver *userdiff_find_by_path(struct index_state *istate,
295 const char *path)
296 {
297 static struct attr_check *check;
298
299 if (!check)
300 check = attr_check_initl("diff", NULL);
301 if (!path)
302 return NULL;
303 git_check_attr(istate, path, check);
304
305 if (ATTR_TRUE(check->items[0].value))
306 return &driver_true;
307 if (ATTR_FALSE(check->items[0].value))
308 return &driver_false;
309 if (ATTR_UNSET(check->items[0].value))
310 return NULL;
311 return userdiff_find_by_name(check->items[0].value);
312 }
313
314 struct userdiff_driver *userdiff_get_textconv(struct repository *r,
315 struct userdiff_driver *driver)
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);
325 notes_cache_init(r, c, name.buf, driver->textconv);
326 driver->textconv_cache = c;
327 strbuf_release(&name);
328 }
329
330 return driver;
331 }