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