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