l10n: git.pot: v2.33.0 round 2 (11 new, 8 removed)
[alt-git.git] / userdiff.c
blobd9b2ba752f0885240ea1089ea90525de2cedf40c
1 #include "cache.h"
2 #include "config.h"
3 #include "userdiff.h"
4 #include "attr.h"
6 static struct userdiff_driver *drivers;
7 static int ndrivers;
8 static int drivers_alloc;
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("bash",
27 /* Optional leading indentation */
28 "^[ \t]*"
29 /* Start of captured text */
30 "("
31 "("
32 /* POSIX identifier with mandatory parentheses */
33 "[a-zA-Z_][a-zA-Z0-9_]*[ \t]*\\([ \t]*\\))"
34 "|"
35 /* Bashism identifier with optional parentheses */
36 "(function[ \t]+[a-zA-Z_][a-zA-Z0-9_]*(([ \t]*\\([ \t]*\\))|([ \t]+))"
37 ")"
38 /* Optional whitespace */
39 "[ \t]*"
40 /* Compound command starting with `{`, `(`, `((` or `[[` */
41 "(\\{|\\(\\(?|\\[\\[)"
42 /* End of captured text */
43 ")",
44 /* -- */
45 /* Characters not in the default $IFS value */
46 "[^ \t]+"),
47 PATTERNS("bibtex",
48 "(@[a-zA-Z]{1,}[ \t]*\\{{0,1}[ \t]*[^ \t\"@',\\#}{~%]*).*$",
49 /* -- */
50 "[={}\"]|[^={}\" \t]+"),
51 PATTERNS("cpp",
52 /* Jump targets or access declarations */
53 "!^[ \t]*[A-Za-z_][A-Za-z_0-9]*:[[:space:]]*($|/[/*])\n"
54 /* functions/methods, variables, and compounds at top level */
55 "^((::[[:space:]]*)?[A-Za-z_].*)$",
56 /* -- */
57 "[a-zA-Z_][a-zA-Z0-9_]*"
58 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lLuU]*"
59 "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->\\*?|\\.\\*"),
60 PATTERNS("csharp",
61 /* Keywords */
62 "!^[ \t]*(do|while|for|if|else|instanceof|new|return|switch|case|throw|catch|using)\n"
63 /* Methods and constructors */
64 "^[ \t]*(((static|public|internal|private|protected|new|virtual|sealed|override|unsafe|async)[ \t]+)*[][<>@.~_[:alnum:]]+[ \t]+[<>@._[:alnum:]]+[ \t]*\\(.*\\))[ \t]*$\n"
65 /* Properties */
66 "^[ \t]*(((static|public|internal|private|protected|new|virtual|sealed|override|unsafe)[ \t]+)*[][<>@.~_[:alnum:]]+[ \t]+[@._[:alnum:]]+)[ \t]*$\n"
67 /* Type definitions */
68 "^[ \t]*(((static|public|internal|private|protected|new|unsafe|sealed|abstract|partial)[ \t]+)*(class|enum|interface|struct|record)[ \t]+.*)$\n"
69 /* Namespace */
70 "^[ \t]*(namespace[ \t]+.*)$",
71 /* -- */
72 "[a-zA-Z_][a-zA-Z0-9_]*"
73 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?"
74 "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->"),
75 IPATTERN("css",
76 "![:;][[:space:]]*$\n"
77 "^[:[@.#]?[_a-z0-9].*$",
78 /* -- */
80 * This regex comes from W3C CSS specs. Should theoretically also
81 * allow ISO 10646 characters U+00A0 and higher,
82 * but they are not handled in this regex.
84 "-?[_a-zA-Z][-_a-zA-Z0-9]*" /* identifiers */
85 "|-?[0-9]+|\\#[0-9a-fA-F]+" /* numbers */
87 PATTERNS("dts",
88 "!;\n"
89 "!=\n"
90 /* lines beginning with a word optionally preceded by '&' or the root */
91 "^[ \t]*((/[ \t]*\\{|&?[a-zA-Z_]).*)",
92 /* -- */
93 /* Property names and math operators */
94 "[a-zA-Z0-9,._+?#-]+"
95 "|[-+*/%&^|!~]|>>|<<|&&|\\|\\|"),
96 PATTERNS("elixir",
97 "^[ \t]*((def(macro|module|impl|protocol|p)?|test)[ \t].*)$",
98 /* -- */
99 /* Atoms, names, and module attributes */
100 "[@:]?[a-zA-Z0-9@_?!]+"
101 /* Numbers with specific base */
102 "|[-+]?0[xob][0-9a-fA-F]+"
103 /* Numbers */
104 "|[-+]?[0-9][0-9_.]*([eE][-+]?[0-9_]+)?"
105 /* Operators and atoms that represent them */
106 "|:?(\\+\\+|--|\\.\\.|~~~|<>|\\^\\^\\^|<?\\|>|<<<?|>?>>|<<?~|~>?>|<~>|<=|>=|===?|!==?|=~|&&&?|\\|\\|\\|?|=>|<-|\\\\\\\\|->)"
107 /* Not real operators, but should be grouped */
108 "|:?%[A-Za-z0-9_.]\\{\\}?"),
109 IPATTERN("fortran",
110 /* Don't match comment lines */
111 "!^([C*]|[ \t]*!)\n"
112 /* Don't match 'module procedure' lines */
113 "!^[ \t]*MODULE[ \t]+PROCEDURE[ \t]\n"
114 /* Program, module, block data */
115 "^[ \t]*((END[ \t]+)?(PROGRAM|MODULE|BLOCK[ \t]+DATA"
116 /* Subroutines and functions */
117 "|([^!'\" \t]+[ \t]+)*(SUBROUTINE|FUNCTION))[ \t]+[A-Z].*)$",
118 /* -- */
119 "[a-zA-Z][a-zA-Z0-9_]*"
120 "|\\.([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])\\."
121 /* numbers and format statements like 2E14.4, or ES12.6, 9X.
122 * Don't worry about format statements without leading digits since
123 * they would have been matched above as a variable anyway. */
124 "|[-+]?[0-9.]+([AaIiDdEeFfLlTtXx][Ss]?[-+]?[0-9.]*)?(_[a-zA-Z0-9][a-zA-Z0-9_]*)?"
125 "|//|\\*\\*|::|[/<>=]="),
126 IPATTERN("fountain",
127 "^((\\.[^.]|(int|ext|est|int\\.?/ext|i/e)[. ]).*)$",
128 /* -- */
129 "[^ \t-]+"),
130 PATTERNS("golang",
131 /* Functions */
132 "^[ \t]*(func[ \t]*.*(\\{[ \t]*)?)\n"
133 /* Structs and interfaces */
134 "^[ \t]*(type[ \t].*(struct|interface)[ \t]*(\\{[ \t]*)?)",
135 /* -- */
136 "[a-zA-Z_][a-zA-Z0-9_]*"
137 "|[-+0-9.eE]+i?|0[xX]?[0-9a-fA-F]+i?"
138 "|[-+*/<>%&^|=!:]=|--|\\+\\+|<<=?|>>=?|&\\^=?|&&|\\|\\||<-|\\.{3}"),
139 PATTERNS("html",
140 "^[ \t]*(<[Hh][1-6]([ \t].*)?>.*)$",
141 /* -- */
142 "[^<>= \t]+"),
143 PATTERNS("java",
144 "!^[ \t]*(catch|do|for|if|instanceof|new|return|switch|throw|while)\n"
145 "^[ \t]*(([A-Za-z_][A-Za-z_0-9]*[ \t]+)+[A-Za-z_][A-Za-z_0-9]*[ \t]*\\([^;]*)$",
146 /* -- */
147 "[a-zA-Z_][a-zA-Z0-9_]*"
148 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?"
149 "|[-+*/<>%&^|=!]="
150 "|--|\\+\\+|<<=?|>>>?=?|&&|\\|\\|"),
151 PATTERNS("markdown",
152 "^ {0,3}#{1,6}[ \t].*",
153 /* -- */
154 "[^<>= \t]+"),
155 PATTERNS("matlab",
157 * Octave pattern is mostly the same as matlab, except that '%%%' and
158 * '##' can also be used to begin code sections, in addition to '%%'
159 * that is understood by both.
161 "^[[:space:]]*((classdef|function)[[:space:]].*)$|^(%%%?|##)[[:space:]].*$",
162 /* -- */
163 "[a-zA-Z_][a-zA-Z0-9_]*|[-+0-9.e]+|[=~<>]=|\\.[*/\\^']|\\|\\||&&"),
164 PATTERNS("objc",
165 /* Negate C statements that can look like functions */
166 "!^[ \t]*(do|for|if|else|return|switch|while)\n"
167 /* Objective-C methods */
168 "^[ \t]*([-+][ \t]*\\([ \t]*[A-Za-z_][A-Za-z_0-9* \t]*\\)[ \t]*[A-Za-z_].*)$\n"
169 /* C functions */
170 "^[ \t]*(([A-Za-z_][A-Za-z_0-9]*[ \t]+)+[A-Za-z_][A-Za-z_0-9]*[ \t]*\\([^;]*)$\n"
171 /* Objective-C class/protocol definitions */
172 "^(@(implementation|interface|protocol)[ \t].*)$",
173 /* -- */
174 "[a-zA-Z_][a-zA-Z0-9_]*"
175 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?"
176 "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->"),
177 PATTERNS("pascal",
178 "^(((class[ \t]+)?(procedure|function)|constructor|destructor|interface"
179 "|implementation|initialization|finalization)[ \t]*.*)$\n"
180 "^(.*=[ \t]*(class|record).*)$",
181 /* -- */
182 "[a-zA-Z_][a-zA-Z0-9_]*"
183 "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+"
184 "|<>|<=|>=|:=|\\.\\."),
185 PATTERNS("perl",
186 "^package .*\n"
187 "^sub [[:alnum:]_':]+[ \t]*"
188 "(\\([^)]*\\)[ \t]*)?" /* prototype */
190 * Attributes. A regex can't count nested parentheses,
191 * so just slurp up whatever we see, taking care not
192 * to accept lines like "sub foo; # defined elsewhere".
194 * An attribute could contain a semicolon, but at that
195 * point it seems reasonable enough to give up.
197 "(:[^;#]*)?"
198 "(\\{[ \t]*)?" /* brace can come here or on the next line */
199 "(#.*)?$\n" /* comment */
200 "^(BEGIN|END|INIT|CHECK|UNITCHECK|AUTOLOAD|DESTROY)[ \t]*"
201 "(\\{[ \t]*)?" /* brace can come here or on the next line */
202 "(#.*)?$\n"
203 "^=head[0-9] .*", /* POD */
204 /* -- */
205 "[[:alpha:]_'][[:alnum:]_']*"
206 "|0[xb]?[0-9a-fA-F_]*"
207 /* taking care not to interpret 3..5 as (3.)(.5) */
208 "|[0-9a-fA-F_]+(\\.[0-9a-fA-F_]+)?([eE][-+]?[0-9_]+)?"
209 "|=>|-[rwxoRWXOezsfdlpSugkbctTBMAC>]|~~|::"
210 "|&&=|\\|\\|=|//=|\\*\\*="
211 "|&&|\\|\\||//|\\+\\+|--|\\*\\*|\\.\\.\\.?"
212 "|[-+*/%.^&<>=!|]="
213 "|=~|!~"
214 "|<<|<>|<=>|>>"),
215 PATTERNS("php",
216 "^[\t ]*(((public|protected|private|static|abstract|final)[\t ]+)*function.*)$\n"
217 "^[\t ]*((((final|abstract)[\t ]+)?class|interface|trait).*)$",
218 /* -- */
219 "[a-zA-Z_][a-zA-Z0-9_]*"
220 "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+"
221 "|[-+*/<>%&^|=!.]=|--|\\+\\+|<<=?|>>=?|===|&&|\\|\\||::|->"),
222 PATTERNS("python",
223 "^[ \t]*((class|(async[ \t]+)?def)[ \t].*)$",
224 /* -- */
225 "[a-zA-Z_][a-zA-Z0-9_]*"
226 "|[-+0-9.e]+[jJlL]?|0[xX]?[0-9a-fA-F]+[lL]?"
227 "|[-+*/<>%&^|=!]=|//=?|<<=?|>>=?|\\*\\*=?"),
228 /* -- */
229 PATTERNS("ruby",
230 "^[ \t]*((class|module|def)[ \t].*)$",
231 /* -- */
232 "(@|@@|\\$)?[a-zA-Z_][a-zA-Z0-9_]*"
233 "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+|\\?(\\\\C-)?(\\\\M-)?."
234 "|//=?|[-+*/<>%&^|=!]=|<<=?|>>=?|===|\\.{1,3}|::|[!=]~"),
235 PATTERNS("rust",
236 "^[\t ]*((pub(\\([^\\)]+\\))?[\t ]+)?((async|const|unsafe|extern([\t ]+\"[^\"]+\"))[\t ]+)?(struct|enum|union|mod|trait|fn|impl|macro_rules!)[< \t]+[^;]*)$",
237 /* -- */
238 "[a-zA-Z_][a-zA-Z0-9_]*"
239 "|[0-9][0-9_a-fA-Fiosuxz]*(\\.([0-9]*[eE][+-]?)?[0-9_fF]*)?"
240 "|[-+*\\/<>%&^|=!:]=|<<=?|>>=?|&&|\\|\\||->|=>|\\.{2}=|\\.{3}|::"),
241 PATTERNS("scheme",
242 "^[\t ]*(\\(((define|def(struct|syntax|class|method|rules|record|proto|alias)?)[-*/ \t]|(library|module|struct|class)[*+ \t]).*)$",
244 * R7RS valid identifiers include any sequence enclosed
245 * within vertical lines having no backslashes
247 "\\|([^\\\\]*)\\|"
248 /* All other words should be delimited by spaces or parentheses */
249 "|([^][)(}{[ \t])+"),
250 PATTERNS("tex", "^(\\\\((sub)*section|chapter|part)\\*{0,1}\\{.*)$",
251 "\\\\[a-zA-Z@]+|\\\\.|[a-zA-Z0-9\x80-\xff]+"),
252 { "default", NULL, -1, { NULL, 0 } },
254 #undef PATTERNS
255 #undef IPATTERN
257 static struct userdiff_driver driver_true = {
258 "diff=true",
259 NULL,
261 { NULL, 0 }
264 static struct userdiff_driver driver_false = {
265 "!diff",
266 NULL,
268 { NULL, 0 }
271 struct find_by_namelen_data {
272 const char *name;
273 size_t len;
274 struct userdiff_driver *driver;
277 static int userdiff_find_by_namelen_cb(struct userdiff_driver *driver,
278 enum userdiff_driver_type type, void *priv)
280 struct find_by_namelen_data *cb_data = priv;
282 if (!strncmp(driver->name, cb_data->name, cb_data->len) &&
283 !driver->name[cb_data->len]) {
284 cb_data->driver = driver;
285 return 1; /* tell the caller to stop iterating */
287 return 0;
290 static struct userdiff_driver *userdiff_find_by_namelen(const char *name, size_t len)
292 struct find_by_namelen_data udcbdata = {
293 .name = name,
294 .len = len,
296 for_each_userdiff_driver(userdiff_find_by_namelen_cb, &udcbdata);
297 return udcbdata.driver;
300 static int parse_funcname(struct userdiff_funcname *f, const char *k,
301 const char *v, int cflags)
303 if (git_config_string(&f->pattern, k, v) < 0)
304 return -1;
305 f->cflags = cflags;
306 return 0;
309 static int parse_tristate(int *b, const char *k, const char *v)
311 if (v && !strcasecmp(v, "auto"))
312 *b = -1;
313 else
314 *b = git_config_bool(k, v);
315 return 0;
318 static int parse_bool(int *b, const char *k, const char *v)
320 *b = git_config_bool(k, v);
321 return 0;
324 int userdiff_config(const char *k, const char *v)
326 struct userdiff_driver *drv;
327 const char *name, *type;
328 size_t namelen;
330 if (parse_config_key(k, "diff", &name, &namelen, &type) || !name)
331 return 0;
333 drv = userdiff_find_by_namelen(name, namelen);
334 if (!drv) {
335 ALLOC_GROW(drivers, ndrivers+1, drivers_alloc);
336 drv = &drivers[ndrivers++];
337 memset(drv, 0, sizeof(*drv));
338 drv->name = xmemdupz(name, namelen);
339 drv->binary = -1;
342 if (!strcmp(type, "funcname"))
343 return parse_funcname(&drv->funcname, k, v, 0);
344 if (!strcmp(type, "xfuncname"))
345 return parse_funcname(&drv->funcname, k, v, REG_EXTENDED);
346 if (!strcmp(type, "binary"))
347 return parse_tristate(&drv->binary, k, v);
348 if (!strcmp(type, "command"))
349 return git_config_string(&drv->external, k, v);
350 if (!strcmp(type, "textconv"))
351 return git_config_string(&drv->textconv, k, v);
352 if (!strcmp(type, "cachetextconv"))
353 return parse_bool(&drv->textconv_want_cache, k, v);
354 if (!strcmp(type, "wordregex"))
355 return git_config_string(&drv->word_regex, k, v);
357 return 0;
360 struct userdiff_driver *userdiff_find_by_name(const char *name)
362 int len = strlen(name);
363 return userdiff_find_by_namelen(name, len);
366 struct userdiff_driver *userdiff_find_by_path(struct index_state *istate,
367 const char *path)
369 static struct attr_check *check;
371 if (!check)
372 check = attr_check_initl("diff", NULL);
373 if (!path)
374 return NULL;
375 git_check_attr(istate, path, check);
377 if (ATTR_TRUE(check->items[0].value))
378 return &driver_true;
379 if (ATTR_FALSE(check->items[0].value))
380 return &driver_false;
381 if (ATTR_UNSET(check->items[0].value))
382 return NULL;
383 return userdiff_find_by_name(check->items[0].value);
386 struct userdiff_driver *userdiff_get_textconv(struct repository *r,
387 struct userdiff_driver *driver)
389 if (!driver->textconv)
390 return NULL;
392 if (driver->textconv_want_cache && !driver->textconv_cache) {
393 struct notes_cache *c = xmalloc(sizeof(*c));
394 struct strbuf name = STRBUF_INIT;
396 strbuf_addf(&name, "textconv/%s", driver->name);
397 notes_cache_init(r, c, name.buf, driver->textconv);
398 driver->textconv_cache = c;
399 strbuf_release(&name);
402 return driver;
405 static int for_each_userdiff_driver_list(each_userdiff_driver_fn fn,
406 enum userdiff_driver_type type, void *cb_data,
407 struct userdiff_driver *drv,
408 int drv_size)
410 int i;
411 int ret;
412 for (i = 0; i < drv_size; i++) {
413 struct userdiff_driver *item = drv + i;
414 if ((ret = fn(item, type, cb_data)))
415 return ret;
417 return 0;
420 int for_each_userdiff_driver(each_userdiff_driver_fn fn, void *cb_data)
422 int ret;
424 ret = for_each_userdiff_driver_list(fn, USERDIFF_DRIVER_TYPE_CUSTOM,
425 cb_data, drivers, ndrivers);
426 if (ret)
427 return ret;
429 ret = for_each_userdiff_driver_list(fn, USERDIFF_DRIVER_TYPE_BUILTIN,
430 cb_data, builtin_drivers,
431 ARRAY_SIZE(builtin_drivers));
432 if (ret)
433 return ret;
435 return 0;