Sync with maint
[git/git.git] / userdiff.c
CommitLineData
d9bae1a1 1#include "cache.h"
be58e70d
JK
2#include "userdiff.h"
3#include "cache.h"
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[] = {
909a5494
BC
17IPATTERN("fortran",
18 "!^([C*]|[ \t]*!)\n"
19 "!^[ \t]*MODULE[ \t]+PROCEDURE[ \t]\n"
20 "^[ \t]*((END[ \t]+)?(PROGRAM|MODULE|BLOCK[ \t]+DATA"
21 "|([^'\" \t]+[ \t]+)*(SUBROUTINE|FUNCTION))[ \t]+[A-Z].*)$",
22 /* -- */
23 "[a-zA-Z][a-zA-Z0-9_]*"
24 "|\\.([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])\\."
25 /* numbers and format statements like 2E14.4, or ES12.6, 9X.
26 * Don't worry about format statements without leading digits since
27 * they would have been matched above as a variable anyway. */
28 "|[-+]?[0-9.]+([AaIiDdEeFfLlTtXx][Ss]?[-+]?[0-9.]*)?(_[a-zA-Z0-9][a-zA-Z0-9_]*)?"
664d44ee 29 "|//|\\*\\*|::|[/<>=]="),
80c49c3d 30PATTERNS("html", "^[ \t]*(<[Hh][1-6][ \t].*>.*)$",
664d44ee 31 "[^<>= \t]+"),
80c49c3d 32PATTERNS("java",
be58e70d 33 "!^[ \t]*(catch|do|for|if|instanceof|new|return|switch|throw|while)\n"
959e2e64
PB
34 "^[ \t]*(([A-Za-z_][A-Za-z_0-9]*[ \t]+)+[A-Za-z_][A-Za-z_0-9]*[ \t]*\\([^;]*)$",
35 /* -- */
80c49c3d
TR
36 "[a-zA-Z_][a-zA-Z0-9_]*"
37 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?"
38 "|[-+*/<>%&^|=!]="
664d44ee 39 "|--|\\+\\+|<<=?|>>>?=?|&&|\\|\\|"),
53b10a14
GH
40PATTERNS("matlab",
41 "^[[:space:]]*((classdef|function)[[:space:]].*)$|^%%[[:space:]].*$",
42 "[a-zA-Z_][a-zA-Z0-9_]*|[-+0-9.e]+|[=~<>]=|\\.[*/\\^']|\\|\\||&&"),
80c49c3d 43PATTERNS("objc",
be58e70d
JK
44 /* Negate C statements that can look like functions */
45 "!^[ \t]*(do|for|if|else|return|switch|while)\n"
46 /* Objective-C methods */
47 "^[ \t]*([-+][ \t]*\\([ \t]*[A-Za-z_][A-Za-z_0-9* \t]*\\)[ \t]*[A-Za-z_].*)$\n"
48 /* C functions */
959e2e64 49 "^[ \t]*(([A-Za-z_][A-Za-z_0-9]*[ \t]+)+[A-Za-z_][A-Za-z_0-9]*[ \t]*\\([^;]*)$\n"
be58e70d 50 /* Objective-C class/protocol definitions */
80c49c3d
TR
51 "^(@(implementation|interface|protocol)[ \t].*)$",
52 /* -- */
53 "[a-zA-Z_][a-zA-Z0-9_]*"
54 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?"
664d44ee 55 "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->"),
80c49c3d 56PATTERNS("pascal",
ad5b6942 57 "^(((class[ \t]+)?(procedure|function)|constructor|destructor|interface|"
be58e70d
JK
58 "implementation|initialization|finalization)[ \t]*.*)$"
59 "\n"
80c49c3d
TR
60 "^(.*=[ \t]*(class|record).*)$",
61 /* -- */
62 "[a-zA-Z_][a-zA-Z0-9_]*"
63 "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+"
664d44ee 64 "|<>|<=|>=|:=|\\.\\."),
71a5d4bc 65PATTERNS("perl",
ea2ca449
JN
66 "^package .*\n"
67 "^sub [[:alnum:]_':]+[ \t]*"
68 "(\\([^)]*\\)[ \t]*)?" /* prototype */
69 /*
70 * Attributes. A regex can't count nested parentheses,
71 * so just slurp up whatever we see, taking care not
72 * to accept lines like "sub foo; # defined elsewhere".
73 *
74 * An attribute could contain a semicolon, but at that
75 * point it seems reasonable enough to give up.
76 */
77 "(:[^;#]*)?"
78 "(\\{[ \t]*)?" /* brace can come here or on the next line */
79 "(#.*)?$\n" /* comment */
f143d9c6 80 "^(BEGIN|END|INIT|CHECK|UNITCHECK|AUTOLOAD|DESTROY)[ \t]*"
ea2ca449
JN
81 "(\\{[ \t]*)?" /* brace can come here or on the next line */
82 "(#.*)?$\n"
12f0967a 83 "^=head[0-9] .*", /* POD */
71a5d4bc
JN
84 /* -- */
85 "[[:alpha:]_'][[:alnum:]_']*"
86 "|0[xb]?[0-9a-fA-F_]*"
87 /* taking care not to interpret 3..5 as (3.)(.5) */
88 "|[0-9a-fA-F_]+(\\.[0-9a-fA-F_]+)?([eE][-+]?[0-9_]+)?"
89 "|=>|-[rwxoRWXOezsfdlpSugkbctTBMAC>]|~~|::"
90 "|&&=|\\|\\|=|//=|\\*\\*="
91 "|&&|\\|\\||//|\\+\\+|--|\\*\\*|\\.\\.\\.?"
92 "|[-+*/%.^&<>=!|]="
93 "|=~|!~"
664d44ee 94 "|<<|<>|<=>|>>"),
6d2f208c
BS
95PATTERNS("php",
96 "^[\t ]*(((public|protected|private|static)[\t ]+)*function.*)$\n"
97 "^[\t ]*(class.*)$",
80c49c3d
TR
98 /* -- */
99 "[a-zA-Z_][a-zA-Z0-9_]*"
100 "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+"
664d44ee 101 "|[-+*/<>%&^|=!.]=|--|\\+\\+|<<=?|>>=?|===|&&|\\|\\||::|->"),
80c49c3d
TR
102PATTERNS("python", "^[ \t]*((class|def)[ \t].*)$",
103 /* -- */
104 "[a-zA-Z_][a-zA-Z0-9_]*"
105 "|[-+0-9.e]+[jJlL]?|0[xX]?[0-9a-fA-F]+[lL]?"
664d44ee 106 "|[-+*/<>%&^|=!]=|//=?|<<=?|>>=?|\\*\\*=?"),
80c49c3d
TR
107 /* -- */
108PATTERNS("ruby", "^[ \t]*((class|module|def)[ \t].*)$",
109 /* -- */
110 "(@|@@|\\$)?[a-zA-Z_][a-zA-Z0-9_]*"
111 "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+|\\?(\\\\C-)?(\\\\M-)?."
664d44ee 112 "|//=?|[-+*/<>%&^|=!]=|<<=?|>>=?|===|\\.{1,3}|::|[!=]~"),
80c49c3d
TR
113PATTERNS("bibtex", "(@[a-zA-Z]{1,}[ \t]*\\{{0,1}[ \t]*[^ \t\"@',\\#}{~%]*).*$",
114 "[={}\"]|[^={}\" \t]+"),
115PATTERNS("tex", "^(\\\\((sub)*section|chapter|part)\\*{0,1}\\{.*)$",
664d44ee 116 "\\\\[a-zA-Z@]+|\\\\.|[a-zA-Z0-9\x80-\xff]+"),
80c49c3d
TR
117PATTERNS("cpp",
118 /* Jump targets or access declarations */
119 "!^[ \t]*[A-Za-z_][A-Za-z_0-9]*:.*$\n"
120 /* C/++ functions/methods at top level */
37e7793d 121 "^([A-Za-z_][A-Za-z_0-9]*([ \t*]+[A-Za-z_][A-Za-z_0-9]*([ \t]*::[ \t]*[^[:space:]]+)?){1,}[ \t]*\\([^;]*)$\n"
80c49c3d
TR
122 /* compound type at top level */
123 "^((struct|class|enum)[^;]*)$",
124 /* -- */
125 "[a-zA-Z_][a-zA-Z0-9_]*"
126 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?"
664d44ee 127 "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->"),
b221207d
PO
128PATTERNS("csharp",
129 /* Keywords */
130 "!^[ \t]*(do|while|for|if|else|instanceof|new|return|switch|case|throw|catch|using)\n"
131 /* Methods and constructors */
132 "^[ \t]*(((static|public|internal|private|protected|new|virtual|sealed|override|unsafe)[ \t]+)*[][<>@.~_[:alnum:]]+[ \t]+[<>@._[:alnum:]]+[ \t]*\\(.*\\))[ \t]*$\n"
133 /* Properties */
134 "^[ \t]*(((static|public|internal|private|protected|new|virtual|sealed|override|unsafe)[ \t]+)*[][<>@.~_[:alnum:]]+[ \t]+[@._[:alnum:]]+)[ \t]*$\n"
135 /* Type definitions */
136 "^[ \t]*(((static|public|internal|private|protected|new|unsafe|sealed|abstract|partial)[ \t]+)*(class|enum|interface|struct)[ \t]+.*)$\n"
137 /* Namespace */
138 "^[ \t]*(namespace[ \t]+.*)$",
139 /* -- */
140 "[a-zA-Z_][a-zA-Z0-9_]*"
141 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?"
664d44ee 142 "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->"),
122aa6f9 143{ "default", NULL, -1, { NULL, 0 } },
be58e70d 144};
80c49c3d 145#undef PATTERNS
909a5494 146#undef IPATTERN
be58e70d
JK
147
148static struct userdiff_driver driver_true = {
149 "diff=true",
150 NULL,
122aa6f9 151 0,
be58e70d
JK
152 { NULL, 0 }
153};
be58e70d
JK
154
155static struct userdiff_driver driver_false = {
156 "!diff",
157 NULL,
122aa6f9 158 1,
be58e70d
JK
159 { NULL, 0 }
160};
be58e70d
JK
161
162static struct userdiff_driver *userdiff_find_by_namelen(const char *k, int len)
163{
164 int i;
165 for (i = 0; i < ndrivers; i++) {
166 struct userdiff_driver *drv = drivers + i;
167 if (!strncmp(drv->name, k, len) && !drv->name[len])
168 return drv;
169 }
170 for (i = 0; i < ARRAY_SIZE(builtin_drivers); i++) {
171 struct userdiff_driver *drv = builtin_drivers + i;
172 if (!strncmp(drv->name, k, len) && !drv->name[len])
173 return drv;
174 }
175 return NULL;
176}
177
178static struct userdiff_driver *parse_driver(const char *var,
179 const char *value, const char *type)
180{
181 struct userdiff_driver *drv;
182 const char *dot;
183 const char *name;
184 int namelen;
185
186 if (prefixcmp(var, "diff."))
187 return NULL;
188 dot = strrchr(var, '.');
189 if (dot == var + 4)
190 return NULL;
191 if (strcmp(type, dot+1))
192 return NULL;
193
194 name = var + 5;
195 namelen = dot - name;
196 drv = userdiff_find_by_namelen(name, namelen);
197 if (!drv) {
198 ALLOC_GROW(drivers, ndrivers+1, drivers_alloc);
199 drv = &drivers[ndrivers++];
200 memset(drv, 0, sizeof(*drv));
201 drv->name = xmemdupz(name, namelen);
122aa6f9 202 drv->binary = -1;
be58e70d
JK
203 }
204 return drv;
205}
206
207static int parse_funcname(struct userdiff_funcname *f, const char *k,
208 const char *v, int cflags)
209{
210 if (git_config_string(&f->pattern, k, v) < 0)
211 return -1;
212 f->cflags = cflags;
213 return 1;
214}
215
216static int parse_string(const char **d, const char *k, const char *v)
217{
218 if (git_config_string(d, k, v) < 0)
219 return -1;
220 return 1;
221}
222
122aa6f9
JK
223static int parse_tristate(int *b, const char *k, const char *v)
224{
225 if (v && !strcasecmp(v, "auto"))
226 *b = -1;
227 else
228 *b = git_config_bool(k, v);
229 return 1;
230}
231
d9bae1a1
JK
232static int parse_bool(int *b, const char *k, const char *v)
233{
234 *b = git_config_bool(k, v);
235 return 1;
236}
237
c7534ef4 238int userdiff_config(const char *k, const char *v)
be58e70d
JK
239{
240 struct userdiff_driver *drv;
241
242 if ((drv = parse_driver(k, v, "funcname")))
243 return parse_funcname(&drv->funcname, k, v, 0);
244 if ((drv = parse_driver(k, v, "xfuncname")))
245 return parse_funcname(&drv->funcname, k, v, REG_EXTENDED);
122aa6f9
JK
246 if ((drv = parse_driver(k, v, "binary")))
247 return parse_tristate(&drv->binary, k, v);
be58e70d
JK
248 if ((drv = parse_driver(k, v, "command")))
249 return parse_string(&drv->external, k, v);
9cb92c39
JK
250 if ((drv = parse_driver(k, v, "textconv")))
251 return parse_string(&drv->textconv, k, v);
d9bae1a1
JK
252 if ((drv = parse_driver(k, v, "cachetextconv")))
253 return parse_bool(&drv->textconv_want_cache, k, v);
80c49c3d
TR
254 if ((drv = parse_driver(k, v, "wordregex")))
255 return parse_string(&drv->word_regex, k, v);
be58e70d
JK
256
257 return 0;
258}
259
260struct userdiff_driver *userdiff_find_by_name(const char *name) {
261 int len = strlen(name);
262 return userdiff_find_by_namelen(name, len);
263}
264
265struct userdiff_driver *userdiff_find_by_path(const char *path)
266{
267 static struct git_attr *attr;
268 struct git_attr_check check;
269
270 if (!attr)
7fb0eaa2 271 attr = git_attr("diff");
be58e70d
JK
272 check.attr = attr;
273
274 if (!path)
275 return NULL;
d932f4eb 276 if (git_check_attr(path, 1, &check))
be58e70d
JK
277 return NULL;
278
279 if (ATTR_TRUE(check.value))
280 return &driver_true;
281 if (ATTR_FALSE(check.value))
282 return &driver_false;
283 if (ATTR_UNSET(check.value))
284 return NULL;
285 return userdiff_find_by_name(check.value);
286}
3813e690
JK
287
288struct userdiff_driver *userdiff_get_textconv(struct userdiff_driver *driver)
289{
290 if (!driver->textconv)
291 return NULL;
292
293 if (driver->textconv_want_cache && !driver->textconv_cache) {
294 struct notes_cache *c = xmalloc(sizeof(*c));
295 struct strbuf name = STRBUF_INIT;
296
297 strbuf_addf(&name, "textconv/%s", driver->name);
298 notes_cache_init(c, name.buf, driver->textconv);
299 driver->textconv_cache = c;
300 }
301
302 return driver;
303}