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