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