]> git.ipfire.org Git - thirdparty/git.git/blame - userdiff.c
Sync with maint
[thirdparty/git.git] / userdiff.c
CommitLineData
d9bae1a1 1#include "cache.h"
b2141fc1 2#include "config.h"
be58e70d 3#include "userdiff.h"
be58e70d
JK
4#include "attr.h"
5
6static struct userdiff_driver *drivers;
7static int ndrivers;
8static int drivers_alloc;
9
ae3b970a 10#define PATTERNS(name, pattern, word_regex) \
664d44ee
JN
11 { name, NULL, -1, { pattern, REG_EXTENDED }, \
12 word_regex "|[^[:space:]]|[\xc0-\xff][\x80-\xbf]+" }
909a5494 13#define IPATTERN(name, pattern, word_regex) \
664d44ee
JN
14 { name, NULL, -1, { pattern, REG_EXTENDED | REG_ICASE }, \
15 word_regex "|[^[:space:]]|[\xc0-\xff][\x80-\xbf]+" }
be58e70d 16static struct userdiff_driver builtin_drivers[] = {
e90d065e 17IPATTERN("ada",
39a87a29 18 "!^(.*[ \t])?(is[ \t]+new|renames|is[ \t]+separate)([ \t].*)?$\n"
e90d065e
AJ
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_]*"
39a87a29 24 "|[-+]?[0-9][0-9#_.aAbBcCdDeEfF]*([eE][+-]?[0-9_]+)?"
e90d065e 25 "|=>|\\.\\.|\\*\\*|:=|/=|>=|<=|<<|>>|<>"),
3c81760b
SB
26PATTERNS("dts",
27 "!;\n"
8da56a48 28 "!=\n"
3c81760b 29 /* lines beginning with a word optionally preceded by '&' or the root */
8da56a48 30 "^[ \t]*((/[ \t]*\\{|&?[a-zA-Z_]).*)",
3c81760b
SB
31 /* -- */
32 /* Property names and math operators */
33 "[a-zA-Z0-9,._+?#-]+"
34 "|[-+*/%&^|!~]|>>|<<|&&|\\|\\|"),
a807200f
ŁN
35PATTERNS("elixir",
36 "^[ \t]*((def(macro|module|impl|protocol|p)?|test)[ \t].*)$",
d1b1384d 37 /* -- */
a807200f 38 /* Atoms, names, and module attributes */
d1b1384d 39 "[@:]?[a-zA-Z0-9@_?!]+"
a807200f
ŁN
40 /* Numbers with specific base */
41 "|[-+]?0[xob][0-9a-fA-F]+"
42 /* Numbers */
43 "|[-+]?[0-9][0-9_.]*([eE][-+]?[0-9_]+)?"
44 /* Operators and atoms that represent them */
45 "|:?(\\+\\+|--|\\.\\.|~~~|<>|\\^\\^\\^|<?\\|>|<<<?|>?>>|<<?~|~>?>|<~>|<=|>=|===?|!==?|=~|&&&?|\\|\\|\\|?|=>|<-|\\\\\\\\|->)"
46 /* Not real operators, but should be grouped */
47 "|:?%[A-Za-z0-9_.]\\{\\}?"),
909a5494
BC
48IPATTERN("fortran",
49 "!^([C*]|[ \t]*!)\n"
50 "!^[ \t]*MODULE[ \t]+PROCEDURE[ \t]\n"
51 "^[ \t]*((END[ \t]+)?(PROGRAM|MODULE|BLOCK[ \t]+DATA"
52 "|([^'\" \t]+[ \t]+)*(SUBROUTINE|FUNCTION))[ \t]+[A-Z].*)$",
53 /* -- */
54 "[a-zA-Z][a-zA-Z0-9_]*"
55 "|\\.([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])\\."
56 /* numbers and format statements like 2E14.4, or ES12.6, 9X.
57 * Don't worry about format statements without leading digits since
58 * they would have been matched above as a variable anyway. */
59 "|[-+]?[0-9.]+([AaIiDdEeFfLlTtXx][Ss]?[-+]?[0-9.]*)?(_[a-zA-Z0-9][a-zA-Z0-9_]*)?"
664d44ee 60 "|//|\\*\\*|::|[/<>=]="),
69f9c87d
ZB
61IPATTERN("fountain", "^((\\.[^.]|(int|ext|est|int\\.?/ext|i/e)[. ]).*)$",
62 "[^ \t-]+"),
1dbf0c0a
AG
63PATTERNS("golang",
64 /* Functions */
65 "^[ \t]*(func[ \t]*.*(\\{[ \t]*)?)\n"
66 /* Structs and interfaces */
67 "^[ \t]*(type[ \t].*(struct|interface)[ \t]*(\\{[ \t]*)?)",
68 /* -- */
69 "[a-zA-Z_][a-zA-Z0-9_]*"
70 "|[-+0-9.eE]+i?|0[xX]?[0-9a-fA-F]+i?"
71 "|[-+*/<>%&^|=!:]=|--|\\+\\+|<<=?|>>=?|&\\^=?|&&|\\|\\||<-|\\.{3}"),
9c03caca 72PATTERNS("html", "^[ \t]*(<[Hh][1-6]([ \t].*)?>.*)$",
664d44ee 73 "[^<>= \t]+"),
80c49c3d 74PATTERNS("java",
be58e70d 75 "!^[ \t]*(catch|do|for|if|instanceof|new|return|switch|throw|while)\n"
959e2e64
PB
76 "^[ \t]*(([A-Za-z_][A-Za-z_0-9]*[ \t]+)+[A-Za-z_][A-Za-z_0-9]*[ \t]*\\([^;]*)$",
77 /* -- */
80c49c3d
TR
78 "[a-zA-Z_][a-zA-Z0-9_]*"
79 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?"
80 "|[-+*/<>%&^|=!]="
664d44ee 81 "|--|\\+\\+|<<=?|>>>?=?|&&|\\|\\|"),
53b10a14 82PATTERNS("matlab",
2731a784
BL
83 /*
84 * Octave pattern is mostly the same as matlab, except that '%%%' and
91bf382f 85 * '##' can also be used to begin code sections, in addition to '%%'
2731a784
BL
86 * that is understood by both.
87 */
91bf382f 88 "^[[:space:]]*((classdef|function)[[:space:]].*)$|^(%%%?|##)[[:space:]].*$",
53b10a14 89 "[a-zA-Z_][a-zA-Z0-9_]*|[-+0-9.e]+|[=~<>]=|\\.[*/\\^']|\\|\\||&&"),
80c49c3d 90PATTERNS("objc",
be58e70d
JK
91 /* Negate C statements that can look like functions */
92 "!^[ \t]*(do|for|if|else|return|switch|while)\n"
93 /* Objective-C methods */
94 "^[ \t]*([-+][ \t]*\\([ \t]*[A-Za-z_][A-Za-z_0-9* \t]*\\)[ \t]*[A-Za-z_].*)$\n"
95 /* C functions */
959e2e64 96 "^[ \t]*(([A-Za-z_][A-Za-z_0-9]*[ \t]+)+[A-Za-z_][A-Za-z_0-9]*[ \t]*\\([^;]*)$\n"
be58e70d 97 /* Objective-C class/protocol definitions */
80c49c3d
TR
98 "^(@(implementation|interface|protocol)[ \t].*)$",
99 /* -- */
100 "[a-zA-Z_][a-zA-Z0-9_]*"
101 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?"
664d44ee 102 "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->"),
80c49c3d 103PATTERNS("pascal",
ad5b6942 104 "^(((class[ \t]+)?(procedure|function)|constructor|destructor|interface|"
be58e70d
JK
105 "implementation|initialization|finalization)[ \t]*.*)$"
106 "\n"
80c49c3d
TR
107 "^(.*=[ \t]*(class|record).*)$",
108 /* -- */
109 "[a-zA-Z_][a-zA-Z0-9_]*"
110 "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+"
664d44ee 111 "|<>|<=|>=|:=|\\.\\."),
71a5d4bc 112PATTERNS("perl",
ea2ca449
JN
113 "^package .*\n"
114 "^sub [[:alnum:]_':]+[ \t]*"
115 "(\\([^)]*\\)[ \t]*)?" /* prototype */
116 /*
117 * Attributes. A regex can't count nested parentheses,
118 * so just slurp up whatever we see, taking care not
119 * to accept lines like "sub foo; # defined elsewhere".
120 *
121 * An attribute could contain a semicolon, but at that
122 * point it seems reasonable enough to give up.
123 */
124 "(:[^;#]*)?"
125 "(\\{[ \t]*)?" /* brace can come here or on the next line */
126 "(#.*)?$\n" /* comment */
f143d9c6 127 "^(BEGIN|END|INIT|CHECK|UNITCHECK|AUTOLOAD|DESTROY)[ \t]*"
ea2ca449
JN
128 "(\\{[ \t]*)?" /* brace can come here or on the next line */
129 "(#.*)?$\n"
12f0967a 130 "^=head[0-9] .*", /* POD */
71a5d4bc
JN
131 /* -- */
132 "[[:alpha:]_'][[:alnum:]_']*"
133 "|0[xb]?[0-9a-fA-F_]*"
134 /* taking care not to interpret 3..5 as (3.)(.5) */
135 "|[0-9a-fA-F_]+(\\.[0-9a-fA-F_]+)?([eE][-+]?[0-9_]+)?"
136 "|=>|-[rwxoRWXOezsfdlpSugkbctTBMAC>]|~~|::"
137 "|&&=|\\|\\|=|//=|\\*\\*="
138 "|&&|\\|\\||//|\\+\\+|--|\\*\\*|\\.\\.\\.?"
139 "|[-+*/%.^&<>=!|]="
140 "|=~|!~"
664d44ee 141 "|<<|<>|<=>|>>"),
6d2f208c
BS
142PATTERNS("php",
143 "^[\t ]*(((public|protected|private|static)[\t ]+)*function.*)$\n"
1ab63164 144 "^[\t ]*((((final|abstract)[\t ]+)?class|interface|trait).*)$",
80c49c3d
TR
145 /* -- */
146 "[a-zA-Z_][a-zA-Z0-9_]*"
147 "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+"
664d44ee 148 "|[-+*/<>%&^|=!.]=|--|\\+\\+|<<=?|>>=?|===|&&|\\|\\||::|->"),
077a1fda 149PATTERNS("python", "^[ \t]*((class|(async[ \t]+)?def)[ \t].*)$",
80c49c3d
TR
150 /* -- */
151 "[a-zA-Z_][a-zA-Z0-9_]*"
152 "|[-+0-9.e]+[jJlL]?|0[xX]?[0-9a-fA-F]+[lL]?"
664d44ee 153 "|[-+*/<>%&^|=!]=|//=?|<<=?|>>=?|\\*\\*=?"),
80c49c3d
TR
154 /* -- */
155PATTERNS("ruby", "^[ \t]*((class|module|def)[ \t].*)$",
156 /* -- */
157 "(@|@@|\\$)?[a-zA-Z_][a-zA-Z0-9_]*"
158 "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+|\\?(\\\\C-)?(\\\\M-)?."
664d44ee 159 "|//=?|[-+*/<>%&^|=!]=|<<=?|>>=?|===|\\.{1,3}|::|[!=]~"),
d74e7860 160PATTERNS("rust",
33be7b38 161 "^[\t ]*((pub(\\([^\\)]+\\))?[\t ]+)?((async|const|unsafe|extern([\t ]+\"[^\"]+\"))[\t ]+)?(struct|enum|union|mod|trait|fn|impl)[< \t]+[^;]*)$",
d74e7860
MAL
162 /* -- */
163 "[a-zA-Z_][a-zA-Z0-9_]*"
33be7b38 164 "|[0-9][0-9_a-fA-Fiosuxz]*(\\.([0-9]*[eE][+-]?)?[0-9_fF]*)?"
d74e7860 165 "|[-+*\\/<>%&^|=!:]=|<<=?|>>=?|&&|\\|\\||->|=>|\\.{2}=|\\.{3}|::"),
80c49c3d
TR
166PATTERNS("bibtex", "(@[a-zA-Z]{1,}[ \t]*\\{{0,1}[ \t]*[^ \t\"@',\\#}{~%]*).*$",
167 "[={}\"]|[^={}\" \t]+"),
168PATTERNS("tex", "^(\\\\((sub)*section|chapter|part)\\*{0,1}\\{.*)$",
664d44ee 169 "\\\\[a-zA-Z@]+|\\\\.|[a-zA-Z0-9\x80-\xff]+"),
80c49c3d
TR
170PATTERNS("cpp",
171 /* Jump targets or access declarations */
8a2e8da3
JS
172 "!^[ \t]*[A-Za-z_][A-Za-z_0-9]*:[[:space:]]*($|/[/*])\n"
173 /* functions/methods, variables, and compounds at top level */
174 "^((::[[:space:]]*)?[A-Za-z_].*)$",
80c49c3d
TR
175 /* -- */
176 "[a-zA-Z_][a-zA-Z0-9_]*"
abf8f986 177 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lLuU]*"
407e07f2 178 "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->\\*?|\\.\\*"),
b221207d
PO
179PATTERNS("csharp",
180 /* Keywords */
181 "!^[ \t]*(do|while|for|if|else|instanceof|new|return|switch|case|throw|catch|using)\n"
182 /* Methods and constructors */
a12cec99 183 "^[ \t]*(((static|public|internal|private|protected|new|virtual|sealed|override|unsafe|async)[ \t]+)*[][<>@.~_[:alnum:]]+[ \t]+[<>@._[:alnum:]]+[ \t]*\\(.*\\))[ \t]*$\n"
b221207d
PO
184 /* Properties */
185 "^[ \t]*(((static|public|internal|private|protected|new|virtual|sealed|override|unsafe)[ \t]+)*[][<>@.~_[:alnum:]]+[ \t]+[@._[:alnum:]]+)[ \t]*$\n"
186 /* Type definitions */
187 "^[ \t]*(((static|public|internal|private|protected|new|unsafe|sealed|abstract|partial)[ \t]+)*(class|enum|interface|struct)[ \t]+.*)$\n"
188 /* Namespace */
189 "^[ \t]*(namespace[ \t]+.*)$",
190 /* -- */
191 "[a-zA-Z_][a-zA-Z0-9_]*"
192 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?"
664d44ee 193 "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->"),
0719f3ee
WD
194IPATTERN("css",
195 "![:;][[:space:]]*$\n"
196 "^[_a-z0-9].*$",
197 /* -- */
198 /*
199 * This regex comes from W3C CSS specs. Should theoretically also
200 * allow ISO 10646 characters U+00A0 and higher,
201 * but they are not handled in this regex.
202 */
203 "-?[_a-zA-Z][-_a-zA-Z0-9]*" /* identifiers */
204 "|-?[0-9]+|\\#[0-9a-fA-F]+" /* numbers */
205),
122aa6f9 206{ "default", NULL, -1, { NULL, 0 } },
be58e70d 207};
80c49c3d 208#undef PATTERNS
909a5494 209#undef IPATTERN
be58e70d
JK
210
211static struct userdiff_driver driver_true = {
212 "diff=true",
213 NULL,
122aa6f9 214 0,
be58e70d
JK
215 { NULL, 0 }
216};
be58e70d
JK
217
218static struct userdiff_driver driver_false = {
219 "!diff",
220 NULL,
122aa6f9 221 1,
be58e70d
JK
222 { NULL, 0 }
223};
be58e70d
JK
224
225static struct userdiff_driver *userdiff_find_by_namelen(const char *k, int len)
226{
227 int i;
228 for (i = 0; i < ndrivers; i++) {
229 struct userdiff_driver *drv = drivers + i;
230 if (!strncmp(drv->name, k, len) && !drv->name[len])
231 return drv;
232 }
233 for (i = 0; i < ARRAY_SIZE(builtin_drivers); i++) {
234 struct userdiff_driver *drv = builtin_drivers + i;
235 if (!strncmp(drv->name, k, len) && !drv->name[len])
236 return drv;
237 }
238 return NULL;
239}
240
be58e70d
JK
241static int parse_funcname(struct userdiff_funcname *f, const char *k,
242 const char *v, int cflags)
243{
244 if (git_config_string(&f->pattern, k, v) < 0)
245 return -1;
246 f->cflags = cflags;
6680a087 247 return 0;
be58e70d
JK
248}
249
122aa6f9
JK
250static int parse_tristate(int *b, const char *k, const char *v)
251{
252 if (v && !strcasecmp(v, "auto"))
253 *b = -1;
254 else
255 *b = git_config_bool(k, v);
6680a087 256 return 0;
122aa6f9
JK
257}
258
d9bae1a1
JK
259static int parse_bool(int *b, const char *k, const char *v)
260{
261 *b = git_config_bool(k, v);
6680a087 262 return 0;
d9bae1a1
JK
263}
264
c7534ef4 265int userdiff_config(const char *k, const char *v)
be58e70d
JK
266{
267 struct userdiff_driver *drv;
0a5987fe
JK
268 const char *name, *type;
269 int namelen;
270
271 if (parse_config_key(k, "diff", &name, &namelen, &type) || !name)
272 return 0;
273
274 drv = userdiff_find_by_namelen(name, namelen);
275 if (!drv) {
276 ALLOC_GROW(drivers, ndrivers+1, drivers_alloc);
277 drv = &drivers[ndrivers++];
278 memset(drv, 0, sizeof(*drv));
279 drv->name = xmemdupz(name, namelen);
280 drv->binary = -1;
281 }
be58e70d 282
0a5987fe 283 if (!strcmp(type, "funcname"))
be58e70d 284 return parse_funcname(&drv->funcname, k, v, 0);
0a5987fe 285 if (!strcmp(type, "xfuncname"))
be58e70d 286 return parse_funcname(&drv->funcname, k, v, REG_EXTENDED);
0a5987fe 287 if (!strcmp(type, "binary"))
122aa6f9 288 return parse_tristate(&drv->binary, k, v);
0a5987fe 289 if (!strcmp(type, "command"))
6680a087 290 return git_config_string(&drv->external, k, v);
0a5987fe 291 if (!strcmp(type, "textconv"))
6680a087 292 return git_config_string(&drv->textconv, k, v);
0a5987fe 293 if (!strcmp(type, "cachetextconv"))
d9bae1a1 294 return parse_bool(&drv->textconv_want_cache, k, v);
0a5987fe 295 if (!strcmp(type, "wordregex"))
6680a087 296 return git_config_string(&drv->word_regex, k, v);
be58e70d
JK
297
298 return 0;
299}
300
3b335762
NTND
301struct userdiff_driver *userdiff_find_by_name(const char *name)
302{
be58e70d
JK
303 int len = strlen(name);
304 return userdiff_find_by_namelen(name, len);
305}
306
acd00ea0
NTND
307struct userdiff_driver *userdiff_find_by_path(struct index_state *istate,
308 const char *path)
be58e70d 309{
2aef63d3 310 static struct attr_check *check;
be58e70d 311
2aef63d3
JH
312 if (!check)
313 check = attr_check_initl("diff", NULL);
be58e70d
JK
314 if (!path)
315 return NULL;
11877b9e 316 git_check_attr(istate, path, check);
be58e70d 317
2aef63d3 318 if (ATTR_TRUE(check->items[0].value))
be58e70d 319 return &driver_true;
2aef63d3 320 if (ATTR_FALSE(check->items[0].value))
be58e70d 321 return &driver_false;
2aef63d3 322 if (ATTR_UNSET(check->items[0].value))
be58e70d 323 return NULL;
2aef63d3 324 return userdiff_find_by_name(check->items[0].value);
be58e70d 325}
3813e690 326
bd7ad45b
NTND
327struct userdiff_driver *userdiff_get_textconv(struct repository *r,
328 struct userdiff_driver *driver)
3813e690
JK
329{
330 if (!driver->textconv)
331 return NULL;
332
333 if (driver->textconv_want_cache && !driver->textconv_cache) {
334 struct notes_cache *c = xmalloc(sizeof(*c));
335 struct strbuf name = STRBUF_INIT;
336
337 strbuf_addf(&name, "textconv/%s", driver->name);
bd7ad45b 338 notes_cache_init(r, c, name.buf, driver->textconv);
3813e690 339 driver->textconv_cache = c;
460c7eb2 340 strbuf_release(&name);
3813e690
JK
341 }
342
343 return driver;
344}