fetch: pass the full local reference name to `format_display`
[alt-git.git] / userdiff.c
blob58a3d59ef8f471b9c6ffc34cad10e56689c3354c
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(lang, rx, wrx) { \
11 .name = lang, \
12 .binary = -1, \
13 .funcname = { \
14 .pattern = rx, \
15 .cflags = REG_EXTENDED, \
16 }, \
17 .word_regex = wrx "|[^[:space:]]|[\xc0-\xff][\x80-\xbf]+", \
19 #define IPATTERN(lang, rx, wrx) { \
20 .name = lang, \
21 .binary = -1, \
22 .funcname = { \
23 .pattern = rx, \
24 .cflags = REG_EXTENDED | REG_ICASE, \
25 }, \
26 .word_regex = wrx "|[^[:space:]]|[\xc0-\xff][\x80-\xbf]+", \
30 * Built-in drivers for various languages, sorted by their names
31 * (except that the "default" is left at the end).
33 * When writing or updating patterns, assume that the contents these
34 * patterns are applied to are syntactically correct. The patterns
35 * can be simple without implementing all syntactical corner cases, as
36 * long as they are sufficiently permissive.
38 static struct userdiff_driver builtin_drivers[] = {
39 IPATTERN("ada",
40 "!^(.*[ \t])?(is[ \t]+new|renames|is[ \t]+separate)([ \t].*)?$\n"
41 "!^[ \t]*with[ \t].*$\n"
42 "^[ \t]*((procedure|function)[ \t]+.*)$\n"
43 "^[ \t]*((package|protected|task)[ \t]+.*)$",
44 /* -- */
45 "[a-zA-Z][a-zA-Z0-9_]*"
46 "|[-+]?[0-9][0-9#_.aAbBcCdDeEfF]*([eE][+-]?[0-9_]+)?"
47 "|=>|\\.\\.|\\*\\*|:=|/=|>=|<=|<<|>>|<>"),
48 PATTERNS("bash",
49 /* Optional leading indentation */
50 "^[ \t]*"
51 /* Start of captured text */
52 "("
53 "("
54 /* POSIX identifier with mandatory parentheses */
55 "[a-zA-Z_][a-zA-Z0-9_]*[ \t]*\\([ \t]*\\))"
56 "|"
57 /* Bashism identifier with optional parentheses */
58 "(function[ \t]+[a-zA-Z_][a-zA-Z0-9_]*(([ \t]*\\([ \t]*\\))|([ \t]+))"
59 ")"
60 /* Optional whitespace */
61 "[ \t]*"
62 /* Compound command starting with `{`, `(`, `((` or `[[` */
63 "(\\{|\\(\\(?|\\[\\[)"
64 /* End of captured text */
65 ")",
66 /* -- */
67 /* Characters not in the default $IFS value */
68 "[^ \t]+"),
69 PATTERNS("bibtex",
70 "(@[a-zA-Z]{1,}[ \t]*\\{{0,1}[ \t]*[^ \t\"@',\\#}{~%]*).*$",
71 /* -- */
72 "[={}\"]|[^={}\" \t]+"),
73 PATTERNS("cpp",
74 /* Jump targets or access declarations */
75 "!^[ \t]*[A-Za-z_][A-Za-z_0-9]*:[[:space:]]*($|/[/*])\n"
76 /* functions/methods, variables, and compounds at top level */
77 "^((::[[:space:]]*)?[A-Za-z_].*)$",
78 /* -- */
79 /* identifiers and keywords */
80 "[a-zA-Z_][a-zA-Z0-9_]*"
81 /* decimal and octal integers as well as floatingpoint numbers */
82 "|[0-9][0-9.]*([Ee][-+]?[0-9]+)?[fFlLuU]*"
83 /* hexadecimal and binary integers */
84 "|0[xXbB][0-9a-fA-F]+[lLuU]*"
85 /* floatingpoint numbers that begin with a decimal point */
86 "|\\.[0-9][0-9]*([Ee][-+]?[0-9]+)?[fFlL]?"
87 "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->\\*?|\\.\\*|<=>"),
88 PATTERNS("csharp",
89 /* Keywords */
90 "!^[ \t]*(do|while|for|if|else|instanceof|new|return|switch|case|throw|catch|using)\n"
91 /* Methods and constructors */
92 "^[ \t]*(((static|public|internal|private|protected|new|virtual|sealed|override|unsafe|async)[ \t]+)*[][<>@.~_[:alnum:]]+[ \t]+[<>@._[:alnum:]]+[ \t]*\\(.*\\))[ \t]*$\n"
93 /* Properties */
94 "^[ \t]*(((static|public|internal|private|protected|new|virtual|sealed|override|unsafe)[ \t]+)*[][<>@.~_[:alnum:]]+[ \t]+[@._[:alnum:]]+)[ \t]*$\n"
95 /* Type definitions */
96 "^[ \t]*(((static|public|internal|private|protected|new|unsafe|sealed|abstract|partial)[ \t]+)*(class|enum|interface|struct|record)[ \t]+.*)$\n"
97 /* Namespace */
98 "^[ \t]*(namespace[ \t]+.*)$",
99 /* -- */
100 "[a-zA-Z_][a-zA-Z0-9_]*"
101 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?"
102 "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->"),
103 IPATTERN("css",
104 "![:;][[:space:]]*$\n"
105 "^[:[@.#]?[_a-z0-9].*$",
106 /* -- */
108 * This regex comes from W3C CSS specs. Should theoretically also
109 * allow ISO 10646 characters U+00A0 and higher,
110 * but they are not handled in this regex.
112 "-?[_a-zA-Z][-_a-zA-Z0-9]*" /* identifiers */
113 "|-?[0-9]+|\\#[0-9a-fA-F]+" /* numbers */
115 PATTERNS("dts",
116 "!;\n"
117 "!=\n"
118 /* lines beginning with a word optionally preceded by '&' or the root */
119 "^[ \t]*((/[ \t]*\\{|&?[a-zA-Z_]).*)",
120 /* -- */
121 /* Property names and math operators */
122 "[a-zA-Z0-9,._+?#-]+"
123 "|[-+*/%&^|!~]|>>|<<|&&|\\|\\|"),
124 PATTERNS("elixir",
125 "^[ \t]*((def(macro|module|impl|protocol|p)?|test)[ \t].*)$",
126 /* -- */
127 /* Atoms, names, and module attributes */
128 "[@:]?[a-zA-Z0-9@_?!]+"
129 /* Numbers with specific base */
130 "|[-+]?0[xob][0-9a-fA-F]+"
131 /* Numbers */
132 "|[-+]?[0-9][0-9_.]*([eE][-+]?[0-9_]+)?"
133 /* Operators and atoms that represent them */
134 "|:?(\\+\\+|--|\\.\\.|~~~|<>|\\^\\^\\^|<?\\|>|<<<?|>?>>|<<?~|~>?>|<~>|<=|>=|===?|!==?|=~|&&&?|\\|\\|\\|?|=>|<-|\\\\\\\\|->)"
135 /* Not real operators, but should be grouped */
136 "|:?%[A-Za-z0-9_.]\\{\\}?"),
137 IPATTERN("fortran",
138 /* Don't match comment lines */
139 "!^([C*]|[ \t]*!)\n"
140 /* Don't match 'module procedure' lines */
141 "!^[ \t]*MODULE[ \t]+PROCEDURE[ \t]\n"
142 /* Program, module, block data */
143 "^[ \t]*((END[ \t]+)?(PROGRAM|MODULE|BLOCK[ \t]+DATA"
144 /* Subroutines and functions */
145 "|([^!'\" \t]+[ \t]+)*(SUBROUTINE|FUNCTION))[ \t]+[A-Z].*)$",
146 /* -- */
147 "[a-zA-Z][a-zA-Z0-9_]*"
148 "|\\.([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])\\."
149 /* numbers and format statements like 2E14.4, or ES12.6, 9X.
150 * Don't worry about format statements without leading digits since
151 * they would have been matched above as a variable anyway. */
152 "|[-+]?[0-9.]+([AaIiDdEeFfLlTtXx][Ss]?[-+]?[0-9.]*)?(_[a-zA-Z0-9][a-zA-Z0-9_]*)?"
153 "|//|\\*\\*|::|[/<>=]="),
154 IPATTERN("fountain",
155 "^((\\.[^.]|(int|ext|est|int\\.?/ext|i/e)[. ]).*)$",
156 /* -- */
157 "[^ \t-]+"),
158 PATTERNS("golang",
159 /* Functions */
160 "^[ \t]*(func[ \t]*.*(\\{[ \t]*)?)\n"
161 /* Structs and interfaces */
162 "^[ \t]*(type[ \t].*(struct|interface)[ \t]*(\\{[ \t]*)?)",
163 /* -- */
164 "[a-zA-Z_][a-zA-Z0-9_]*"
165 "|[-+0-9.eE]+i?|0[xX]?[0-9a-fA-F]+i?"
166 "|[-+*/<>%&^|=!:]=|--|\\+\\+|<<=?|>>=?|&\\^=?|&&|\\|\\||<-|\\.{3}"),
167 PATTERNS("html",
168 "^[ \t]*(<[Hh][1-6]([ \t].*)?>.*)$",
169 /* -- */
170 "[^<>= \t]+"),
171 PATTERNS("java",
172 "!^[ \t]*(catch|do|for|if|instanceof|new|return|switch|throw|while)\n"
173 /* Class, enum, interface, and record declarations */
174 "^[ \t]*(([a-z-]+[ \t]+)*(class|enum|interface|record)[ \t]+.*)$\n"
175 /* Method definitions; note that constructor signatures are not */
176 /* matched because they are indistinguishable from method calls. */
177 "^[ \t]*(([A-Za-z_<>&][][?&<>.,A-Za-z_0-9]*[ \t]+)+[A-Za-z_][A-Za-z_0-9]*[ \t]*\\([^;]*)$",
178 /* -- */
179 "[a-zA-Z_][a-zA-Z0-9_]*"
180 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?"
181 "|[-+*/<>%&^|=!]="
182 "|--|\\+\\+|<<=?|>>>?=?|&&|\\|\\|"),
183 PATTERNS("kotlin",
184 "^[ \t]*(([a-z]+[ \t]+)*(fun|class|interface)[ \t]+.*)$",
185 /* -- */
186 "[a-zA-Z_][a-zA-Z0-9_]*"
187 /* hexadecimal and binary numbers */
188 "|0[xXbB][0-9a-fA-F_]+[lLuU]*"
189 /* integers and floats */
190 "|[0-9][0-9_]*([.][0-9_]*)?([Ee][-+]?[0-9]+)?[fFlLuU]*"
191 /* floating point numbers beginning with decimal point */
192 "|[.][0-9][0-9_]*([Ee][-+]?[0-9]+)?[fFlLuU]?"
193 /* unary and binary operators */
194 "|[-+*/<>%&^|=!]==?|--|\\+\\+|<<=|>>=|&&|\\|\\||->|\\.\\*|!!|[?:.][.:]"),
195 PATTERNS("markdown",
196 "^ {0,3}#{1,6}[ \t].*",
197 /* -- */
198 "[^<>= \t]+"),
199 PATTERNS("matlab",
201 * Octave pattern is mostly the same as matlab, except that '%%%' and
202 * '##' can also be used to begin code sections, in addition to '%%'
203 * that is understood by both.
205 "^[[:space:]]*((classdef|function)[[:space:]].*)$|^(%%%?|##)[[:space:]].*$",
206 /* -- */
207 "[a-zA-Z_][a-zA-Z0-9_]*|[-+0-9.e]+|[=~<>]=|\\.[*/\\^']|\\|\\||&&"),
208 PATTERNS("objc",
209 /* Negate C statements that can look like functions */
210 "!^[ \t]*(do|for|if|else|return|switch|while)\n"
211 /* Objective-C methods */
212 "^[ \t]*([-+][ \t]*\\([ \t]*[A-Za-z_][A-Za-z_0-9* \t]*\\)[ \t]*[A-Za-z_].*)$\n"
213 /* C functions */
214 "^[ \t]*(([A-Za-z_][A-Za-z_0-9]*[ \t]+)+[A-Za-z_][A-Za-z_0-9]*[ \t]*\\([^;]*)$\n"
215 /* Objective-C class/protocol definitions */
216 "^(@(implementation|interface|protocol)[ \t].*)$",
217 /* -- */
218 "[a-zA-Z_][a-zA-Z0-9_]*"
219 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?"
220 "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->"),
221 PATTERNS("pascal",
222 "^(((class[ \t]+)?(procedure|function)|constructor|destructor|interface"
223 "|implementation|initialization|finalization)[ \t]*.*)$\n"
224 "^(.*=[ \t]*(class|record).*)$",
225 /* -- */
226 "[a-zA-Z_][a-zA-Z0-9_]*"
227 "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+"
228 "|<>|<=|>=|:=|\\.\\."),
229 PATTERNS("perl",
230 "^package .*\n"
231 "^sub [[:alnum:]_':]+[ \t]*"
232 "(\\([^)]*\\)[ \t]*)?" /* prototype */
234 * Attributes. A regex can't count nested parentheses,
235 * so just slurp up whatever we see, taking care not
236 * to accept lines like "sub foo; # defined elsewhere".
238 * An attribute could contain a semicolon, but at that
239 * point it seems reasonable enough to give up.
241 "(:[^;#]*)?"
242 "(\\{[ \t]*)?" /* brace can come here or on the next line */
243 "(#.*)?$\n" /* comment */
244 "^(BEGIN|END|INIT|CHECK|UNITCHECK|AUTOLOAD|DESTROY)[ \t]*"
245 "(\\{[ \t]*)?" /* brace can come here or on the next line */
246 "(#.*)?$\n"
247 "^=head[0-9] .*", /* POD */
248 /* -- */
249 "[[:alpha:]_'][[:alnum:]_']*"
250 "|0[xb]?[0-9a-fA-F_]*"
251 /* taking care not to interpret 3..5 as (3.)(.5) */
252 "|[0-9a-fA-F_]+(\\.[0-9a-fA-F_]+)?([eE][-+]?[0-9_]+)?"
253 "|=>|-[rwxoRWXOezsfdlpSugkbctTBMAC>]|~~|::"
254 "|&&=|\\|\\|=|//=|\\*\\*="
255 "|&&|\\|\\||//|\\+\\+|--|\\*\\*|\\.\\.\\.?"
256 "|[-+*/%.^&<>=!|]="
257 "|=~|!~"
258 "|<<|<>|<=>|>>"),
259 PATTERNS("php",
260 "^[\t ]*(((public|protected|private|static|abstract|final)[\t ]+)*function.*)$\n"
261 "^[\t ]*((((final|abstract)[\t ]+)?class|enum|interface|trait).*)$",
262 /* -- */
263 "[a-zA-Z_][a-zA-Z0-9_]*"
264 "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+"
265 "|[-+*/<>%&^|=!.]=|--|\\+\\+|<<=?|>>=?|===|&&|\\|\\||::|->"),
266 PATTERNS("python",
267 "^[ \t]*((class|(async[ \t]+)?def)[ \t].*)$",
268 /* -- */
269 "[a-zA-Z_][a-zA-Z0-9_]*"
270 "|[-+0-9.e]+[jJlL]?|0[xX]?[0-9a-fA-F]+[lL]?"
271 "|[-+*/<>%&^|=!]=|//=?|<<=?|>>=?|\\*\\*=?"),
272 /* -- */
273 PATTERNS("ruby",
274 "^[ \t]*((class|module|def)[ \t].*)$",
275 /* -- */
276 "(@|@@|\\$)?[a-zA-Z_][a-zA-Z0-9_]*"
277 "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+|\\?(\\\\C-)?(\\\\M-)?."
278 "|//=?|[-+*/<>%&^|=!]=|<<=?|>>=?|===|\\.{1,3}|::|[!=]~"),
279 PATTERNS("rust",
280 "^[\t ]*((pub(\\([^\\)]+\\))?[\t ]+)?((async|const|unsafe|extern([\t ]+\"[^\"]+\"))[\t ]+)?(struct|enum|union|mod|trait|fn|impl|macro_rules!)[< \t]+[^;]*)$",
281 /* -- */
282 "[a-zA-Z_][a-zA-Z0-9_]*"
283 "|[0-9][0-9_a-fA-Fiosuxz]*(\\.([0-9]*[eE][+-]?)?[0-9_fF]*)?"
284 "|[-+*\\/<>%&^|=!:]=|<<=?|>>=?|&&|\\|\\||->|=>|\\.{2}=|\\.{3}|::"),
285 PATTERNS("scheme",
286 "^[\t ]*(\\(((define|def(struct|syntax|class|method|rules|record|proto|alias)?)[-*/ \t]|(library|module|struct|class)[*+ \t]).*)$",
288 * R7RS valid identifiers include any sequence enclosed
289 * within vertical lines having no backslashes
291 "\\|([^\\\\]*)\\|"
292 /* All other words should be delimited by spaces or parentheses */
293 "|([^][)(}{[ \t])+"),
294 PATTERNS("tex", "^(\\\\((sub)*section|chapter|part)\\*{0,1}\\{.*)$",
295 "\\\\[a-zA-Z@]+|\\\\.|[a-zA-Z0-9\x80-\xff]+"),
296 { "default", NULL, NULL, -1, { NULL, 0 } },
298 #undef PATTERNS
299 #undef IPATTERN
301 static struct userdiff_driver driver_true = {
302 .name = "diff=true",
303 .binary = 0,
306 static struct userdiff_driver driver_false = {
307 .name = "!diff",
308 .binary = 1,
311 struct find_by_namelen_data {
312 const char *name;
313 size_t len;
314 struct userdiff_driver *driver;
317 static int userdiff_find_by_namelen_cb(struct userdiff_driver *driver,
318 enum userdiff_driver_type type UNUSED,
319 void *priv)
321 struct find_by_namelen_data *cb_data = priv;
323 if (!strncmp(driver->name, cb_data->name, cb_data->len) &&
324 !driver->name[cb_data->len]) {
325 cb_data->driver = driver;
326 return 1; /* tell the caller to stop iterating */
328 return 0;
331 static struct userdiff_driver *userdiff_find_by_namelen(const char *name, size_t len)
333 struct find_by_namelen_data udcbdata = {
334 .name = name,
335 .len = len,
337 for_each_userdiff_driver(userdiff_find_by_namelen_cb, &udcbdata);
338 return udcbdata.driver;
341 static int parse_funcname(struct userdiff_funcname *f, const char *k,
342 const char *v, int cflags)
344 if (git_config_string(&f->pattern, k, v) < 0)
345 return -1;
346 f->cflags = cflags;
347 return 0;
350 static int parse_tristate(int *b, const char *k, const char *v)
352 if (v && !strcasecmp(v, "auto"))
353 *b = -1;
354 else
355 *b = git_config_bool(k, v);
356 return 0;
359 static int parse_bool(int *b, const char *k, const char *v)
361 *b = git_config_bool(k, v);
362 return 0;
365 int userdiff_config(const char *k, const char *v)
367 struct userdiff_driver *drv;
368 const char *name, *type;
369 size_t namelen;
371 if (parse_config_key(k, "diff", &name, &namelen, &type) || !name)
372 return 0;
374 drv = userdiff_find_by_namelen(name, namelen);
375 if (!drv) {
376 ALLOC_GROW(drivers, ndrivers+1, drivers_alloc);
377 drv = &drivers[ndrivers++];
378 memset(drv, 0, sizeof(*drv));
379 drv->name = xmemdupz(name, namelen);
380 drv->binary = -1;
383 if (!strcmp(type, "funcname"))
384 return parse_funcname(&drv->funcname, k, v, 0);
385 if (!strcmp(type, "xfuncname"))
386 return parse_funcname(&drv->funcname, k, v, REG_EXTENDED);
387 if (!strcmp(type, "binary"))
388 return parse_tristate(&drv->binary, k, v);
389 if (!strcmp(type, "command"))
390 return git_config_string(&drv->external, k, v);
391 if (!strcmp(type, "textconv"))
392 return git_config_string(&drv->textconv, k, v);
393 if (!strcmp(type, "cachetextconv"))
394 return parse_bool(&drv->textconv_want_cache, k, v);
395 if (!strcmp(type, "wordregex"))
396 return git_config_string(&drv->word_regex, k, v);
397 if (!strcmp(type, "algorithm"))
398 return git_config_string(&drv->algorithm, k, v);
400 return 0;
403 struct userdiff_driver *userdiff_find_by_name(const char *name)
405 int len = strlen(name);
406 return userdiff_find_by_namelen(name, len);
409 struct userdiff_driver *userdiff_find_by_path(struct index_state *istate,
410 const char *path)
412 static struct attr_check *check;
414 if (!check)
415 check = attr_check_initl("diff", NULL);
416 if (!path)
417 return NULL;
418 git_check_attr(istate, NULL, path, check);
420 if (ATTR_TRUE(check->items[0].value))
421 return &driver_true;
422 if (ATTR_FALSE(check->items[0].value))
423 return &driver_false;
424 if (ATTR_UNSET(check->items[0].value))
425 return NULL;
426 return userdiff_find_by_name(check->items[0].value);
429 struct userdiff_driver *userdiff_get_textconv(struct repository *r,
430 struct userdiff_driver *driver)
432 if (!driver->textconv)
433 return NULL;
435 if (driver->textconv_want_cache && !driver->textconv_cache) {
436 struct notes_cache *c = xmalloc(sizeof(*c));
437 struct strbuf name = STRBUF_INIT;
439 strbuf_addf(&name, "textconv/%s", driver->name);
440 notes_cache_init(r, c, name.buf, driver->textconv);
441 driver->textconv_cache = c;
442 strbuf_release(&name);
445 return driver;
448 static int for_each_userdiff_driver_list(each_userdiff_driver_fn fn,
449 enum userdiff_driver_type type, void *cb_data,
450 struct userdiff_driver *drv,
451 int drv_size)
453 int i;
454 int ret;
455 for (i = 0; i < drv_size; i++) {
456 struct userdiff_driver *item = drv + i;
457 if ((ret = fn(item, type, cb_data)))
458 return ret;
460 return 0;
463 int for_each_userdiff_driver(each_userdiff_driver_fn fn, void *cb_data)
465 int ret;
467 ret = for_each_userdiff_driver_list(fn, USERDIFF_DRIVER_TYPE_CUSTOM,
468 cb_data, drivers, ndrivers);
469 if (ret)
470 return ret;
472 ret = for_each_userdiff_driver_list(fn, USERDIFF_DRIVER_TYPE_BUILTIN,
473 cb_data, builtin_drivers,
474 ARRAY_SIZE(builtin_drivers));
475 if (ret)
476 return ret;
478 return 0;