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