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