]>
Commit | Line | Data |
---|---|---|
d9bae1a1 | 1 | #include "cache.h" |
be58e70d JK |
2 | #include "userdiff.h" |
3 | #include "cache.h" | |
4 | #include "attr.h" | |
5 | ||
6 | static struct userdiff_driver *drivers; | |
7 | static int ndrivers; | |
8 | static 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 | 14 | static struct userdiff_driver builtin_drivers[] = { |
909a5494 BC |
15 | IPATTERN("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 |
29 | PATTERNS("html", "^[ \t]*(<[Hh][1-6][ \t].*>.*)$", |
30 | "[^<>= \t]+|[^[:space:]]|[\x80-\xff]+"), | |
31 | PATTERNS("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]+"), | |
40 | PATTERNS("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]+"), | |
54 | PATTERNS("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 |
64 | PATTERNS("perl", |
65 | "^[ \t]*package .*;\n" | |
66 | "^[ \t]*sub .* \\{", | |
67 | /* -- */ | |
68 | "[[:alpha:]_'][[:alnum:]_']*" | |
69 | "|0[xb]?[0-9a-fA-F_]*" | |
70 | /* taking care not to interpret 3..5 as (3.)(.5) */ | |
71 | "|[0-9a-fA-F_]+(\\.[0-9a-fA-F_]+)?([eE][-+]?[0-9_]+)?" | |
72 | "|=>|-[rwxoRWXOezsfdlpSugkbctTBMAC>]|~~|::" | |
73 | "|&&=|\\|\\|=|//=|\\*\\*=" | |
74 | "|&&|\\|\\||//|\\+\\+|--|\\*\\*|\\.\\.\\.?" | |
75 | "|[-+*/%.^&<>=!|]=" | |
76 | "|=~|!~" | |
77 | "|<<|<>|<=>|>>" | |
78 | "|[^[:space:]]"), | |
6d2f208c BS |
79 | PATTERNS("php", |
80 | "^[\t ]*(((public|protected|private|static)[\t ]+)*function.*)$\n" | |
81 | "^[\t ]*(class.*)$", | |
80c49c3d TR |
82 | /* -- */ |
83 | "[a-zA-Z_][a-zA-Z0-9_]*" | |
84 | "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+" | |
85 | "|[-+*/<>%&^|=!.]=|--|\\+\\+|<<=?|>>=?|===|&&|\\|\\||::|->" | |
86 | "|[^[:space:]]|[\x80-\xff]+"), | |
87 | PATTERNS("python", "^[ \t]*((class|def)[ \t].*)$", | |
88 | /* -- */ | |
89 | "[a-zA-Z_][a-zA-Z0-9_]*" | |
90 | "|[-+0-9.e]+[jJlL]?|0[xX]?[0-9a-fA-F]+[lL]?" | |
91 | "|[-+*/<>%&^|=!]=|//=?|<<=?|>>=?|\\*\\*=?" | |
b34f69f9 | 92 | "|[^[:space:]]|[\x80-\xff]+"), |
80c49c3d TR |
93 | /* -- */ |
94 | PATTERNS("ruby", "^[ \t]*((class|module|def)[ \t].*)$", | |
95 | /* -- */ | |
96 | "(@|@@|\\$)?[a-zA-Z_][a-zA-Z0-9_]*" | |
97 | "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+|\\?(\\\\C-)?(\\\\M-)?." | |
98 | "|//=?|[-+*/<>%&^|=!]=|<<=?|>>=?|===|\\.{1,3}|::|[!=]~" | |
b34f69f9 | 99 | "|[^[:space:]]|[\x80-\xff]+"), |
80c49c3d TR |
100 | PATTERNS("bibtex", "(@[a-zA-Z]{1,}[ \t]*\\{{0,1}[ \t]*[^ \t\"@',\\#}{~%]*).*$", |
101 | "[={}\"]|[^={}\" \t]+"), | |
102 | PATTERNS("tex", "^(\\\\((sub)*section|chapter|part)\\*{0,1}\\{.*)$", | |
103 | "\\\\[a-zA-Z@]+|\\\\.|[a-zA-Z0-9\x80-\xff]+|[^[:space:]]"), | |
104 | PATTERNS("cpp", | |
105 | /* Jump targets or access declarations */ | |
106 | "!^[ \t]*[A-Za-z_][A-Za-z_0-9]*:.*$\n" | |
107 | /* C/++ functions/methods at top level */ | |
108 | "^([A-Za-z_][A-Za-z_0-9]*([ \t]+[A-Za-z_][A-Za-z_0-9]*([ \t]*::[ \t]*[^[:space:]]+)?){1,}[ \t]*\\([^;]*)$\n" | |
109 | /* compound type at top level */ | |
110 | "^((struct|class|enum)[^;]*)$", | |
111 | /* -- */ | |
112 | "[a-zA-Z_][a-zA-Z0-9_]*" | |
113 | "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?" | |
114 | "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->" | |
115 | "|[^[:space:]]|[\x80-\xff]+"), | |
b221207d PO |
116 | PATTERNS("csharp", |
117 | /* Keywords */ | |
118 | "!^[ \t]*(do|while|for|if|else|instanceof|new|return|switch|case|throw|catch|using)\n" | |
119 | /* Methods and constructors */ | |
120 | "^[ \t]*(((static|public|internal|private|protected|new|virtual|sealed|override|unsafe)[ \t]+)*[][<>@.~_[:alnum:]]+[ \t]+[<>@._[:alnum:]]+[ \t]*\\(.*\\))[ \t]*$\n" | |
121 | /* Properties */ | |
122 | "^[ \t]*(((static|public|internal|private|protected|new|virtual|sealed|override|unsafe)[ \t]+)*[][<>@.~_[:alnum:]]+[ \t]+[@._[:alnum:]]+)[ \t]*$\n" | |
123 | /* Type definitions */ | |
124 | "^[ \t]*(((static|public|internal|private|protected|new|unsafe|sealed|abstract|partial)[ \t]+)*(class|enum|interface|struct)[ \t]+.*)$\n" | |
125 | /* Namespace */ | |
126 | "^[ \t]*(namespace[ \t]+.*)$", | |
127 | /* -- */ | |
128 | "[a-zA-Z_][a-zA-Z0-9_]*" | |
129 | "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?" | |
130 | "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->" | |
131 | "|[^[:space:]]|[\x80-\xff]+"), | |
122aa6f9 | 132 | { "default", NULL, -1, { NULL, 0 } }, |
be58e70d | 133 | }; |
80c49c3d | 134 | #undef PATTERNS |
909a5494 | 135 | #undef IPATTERN |
be58e70d JK |
136 | |
137 | static struct userdiff_driver driver_true = { | |
138 | "diff=true", | |
139 | NULL, | |
122aa6f9 | 140 | 0, |
be58e70d JK |
141 | { NULL, 0 } |
142 | }; | |
be58e70d JK |
143 | |
144 | static struct userdiff_driver driver_false = { | |
145 | "!diff", | |
146 | NULL, | |
122aa6f9 | 147 | 1, |
be58e70d JK |
148 | { NULL, 0 } |
149 | }; | |
be58e70d JK |
150 | |
151 | static struct userdiff_driver *userdiff_find_by_namelen(const char *k, int len) | |
152 | { | |
153 | int i; | |
154 | for (i = 0; i < ndrivers; i++) { | |
155 | struct userdiff_driver *drv = drivers + i; | |
156 | if (!strncmp(drv->name, k, len) && !drv->name[len]) | |
157 | return drv; | |
158 | } | |
159 | for (i = 0; i < ARRAY_SIZE(builtin_drivers); i++) { | |
160 | struct userdiff_driver *drv = builtin_drivers + i; | |
161 | if (!strncmp(drv->name, k, len) && !drv->name[len]) | |
162 | return drv; | |
163 | } | |
164 | return NULL; | |
165 | } | |
166 | ||
167 | static struct userdiff_driver *parse_driver(const char *var, | |
168 | const char *value, const char *type) | |
169 | { | |
170 | struct userdiff_driver *drv; | |
171 | const char *dot; | |
172 | const char *name; | |
173 | int namelen; | |
174 | ||
175 | if (prefixcmp(var, "diff.")) | |
176 | return NULL; | |
177 | dot = strrchr(var, '.'); | |
178 | if (dot == var + 4) | |
179 | return NULL; | |
180 | if (strcmp(type, dot+1)) | |
181 | return NULL; | |
182 | ||
183 | name = var + 5; | |
184 | namelen = dot - name; | |
185 | drv = userdiff_find_by_namelen(name, namelen); | |
186 | if (!drv) { | |
187 | ALLOC_GROW(drivers, ndrivers+1, drivers_alloc); | |
188 | drv = &drivers[ndrivers++]; | |
189 | memset(drv, 0, sizeof(*drv)); | |
190 | drv->name = xmemdupz(name, namelen); | |
122aa6f9 | 191 | drv->binary = -1; |
be58e70d JK |
192 | } |
193 | return drv; | |
194 | } | |
195 | ||
196 | static int parse_funcname(struct userdiff_funcname *f, const char *k, | |
197 | const char *v, int cflags) | |
198 | { | |
199 | if (git_config_string(&f->pattern, k, v) < 0) | |
200 | return -1; | |
201 | f->cflags = cflags; | |
202 | return 1; | |
203 | } | |
204 | ||
205 | static int parse_string(const char **d, const char *k, const char *v) | |
206 | { | |
207 | if (git_config_string(d, k, v) < 0) | |
208 | return -1; | |
209 | return 1; | |
210 | } | |
211 | ||
122aa6f9 JK |
212 | static int parse_tristate(int *b, const char *k, const char *v) |
213 | { | |
214 | if (v && !strcasecmp(v, "auto")) | |
215 | *b = -1; | |
216 | else | |
217 | *b = git_config_bool(k, v); | |
218 | return 1; | |
219 | } | |
220 | ||
d9bae1a1 JK |
221 | static int parse_bool(int *b, const char *k, const char *v) |
222 | { | |
223 | *b = git_config_bool(k, v); | |
224 | return 1; | |
225 | } | |
226 | ||
c7534ef4 | 227 | int userdiff_config(const char *k, const char *v) |
be58e70d JK |
228 | { |
229 | struct userdiff_driver *drv; | |
230 | ||
231 | if ((drv = parse_driver(k, v, "funcname"))) | |
232 | return parse_funcname(&drv->funcname, k, v, 0); | |
233 | if ((drv = parse_driver(k, v, "xfuncname"))) | |
234 | return parse_funcname(&drv->funcname, k, v, REG_EXTENDED); | |
122aa6f9 JK |
235 | if ((drv = parse_driver(k, v, "binary"))) |
236 | return parse_tristate(&drv->binary, k, v); | |
be58e70d JK |
237 | if ((drv = parse_driver(k, v, "command"))) |
238 | return parse_string(&drv->external, k, v); | |
9cb92c39 JK |
239 | if ((drv = parse_driver(k, v, "textconv"))) |
240 | return parse_string(&drv->textconv, k, v); | |
d9bae1a1 JK |
241 | if ((drv = parse_driver(k, v, "cachetextconv"))) |
242 | return parse_bool(&drv->textconv_want_cache, k, v); | |
80c49c3d TR |
243 | if ((drv = parse_driver(k, v, "wordregex"))) |
244 | return parse_string(&drv->word_regex, k, v); | |
be58e70d JK |
245 | |
246 | return 0; | |
247 | } | |
248 | ||
249 | struct userdiff_driver *userdiff_find_by_name(const char *name) { | |
250 | int len = strlen(name); | |
251 | return userdiff_find_by_namelen(name, len); | |
252 | } | |
253 | ||
254 | struct userdiff_driver *userdiff_find_by_path(const char *path) | |
255 | { | |
256 | static struct git_attr *attr; | |
257 | struct git_attr_check check; | |
258 | ||
259 | if (!attr) | |
7fb0eaa2 | 260 | attr = git_attr("diff"); |
be58e70d JK |
261 | check.attr = attr; |
262 | ||
263 | if (!path) | |
264 | return NULL; | |
265 | if (git_checkattr(path, 1, &check)) | |
266 | return NULL; | |
267 | ||
268 | if (ATTR_TRUE(check.value)) | |
269 | return &driver_true; | |
270 | if (ATTR_FALSE(check.value)) | |
271 | return &driver_false; | |
272 | if (ATTR_UNSET(check.value)) | |
273 | return NULL; | |
274 | return userdiff_find_by_name(check.value); | |
275 | } |