]>
Commit | Line | Data |
---|---|---|
d9bae1a1 | 1 | #include "cache.h" |
b2141fc1 | 2 | #include "config.h" |
be58e70d | 3 | #include "userdiff.h" |
be58e70d JK |
4 | #include "attr.h" |
5 | ||
6 | static struct userdiff_driver *drivers; | |
7 | static int ndrivers; | |
8 | static 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]+" } | |
b6029b32 JH |
16 | |
17 | /* | |
18 | * Built-in drivers for various languages, sorted by their names | |
19 | * (except that the "default" is left at the end). | |
20 | * | |
21 | * When writing or updating patterns, assume that the contents these | |
22 | * patterns are applied to are syntactically correct. The patterns | |
23 | * can be simple without implementing all syntactical corner cases, as | |
24 | * long as they are sufficiently permissive. | |
25 | */ | |
be58e70d | 26 | static struct userdiff_driver builtin_drivers[] = { |
e90d065e | 27 | IPATTERN("ada", |
39a87a29 | 28 | "!^(.*[ \t])?(is[ \t]+new|renames|is[ \t]+separate)([ \t].*)?$\n" |
e90d065e AJ |
29 | "!^[ \t]*with[ \t].*$\n" |
30 | "^[ \t]*((procedure|function)[ \t]+.*)$\n" | |
31 | "^[ \t]*((package|protected|task)[ \t]+.*)$", | |
32 | /* -- */ | |
33 | "[a-zA-Z][a-zA-Z0-9_]*" | |
39a87a29 | 34 | "|[-+]?[0-9][0-9#_.aAbBcCdDeEfF]*([eE][+-]?[0-9_]+)?" |
e90d065e | 35 | "|=>|\\.\\.|\\*\\*|:=|/=|>=|<=|<<|>>|<>"), |
2ff6c346 VE |
36 | PATTERNS("bash", |
37 | /* Optional leading indentation */ | |
38 | "^[ \t]*" | |
39 | /* Start of captured text */ | |
40 | "(" | |
41 | "(" | |
42 | /* POSIX identifier with mandatory parentheses */ | |
43 | "[a-zA-Z_][a-zA-Z0-9_]*[ \t]*\\([ \t]*\\))" | |
44 | "|" | |
45 | /* Bashism identifier with optional parentheses */ | |
46 | "(function[ \t]+[a-zA-Z_][a-zA-Z0-9_]*(([ \t]*\\([ \t]*\\))|([ \t]+))" | |
47 | ")" | |
48 | /* Optional whitespace */ | |
49 | "[ \t]*" | |
50 | /* Compound command starting with `{`, `(`, `((` or `[[` */ | |
51 | "(\\{|\\(\\(?|\\[\\[)" | |
52 | /* End of captured text */ | |
53 | ")", | |
54 | /* -- */ | |
55 | /* Characters not in the default $IFS value */ | |
56 | "[^ \t]+"), | |
6d1c9c52 ÆAB |
57 | PATTERNS("bibtex", |
58 | "(@[a-zA-Z]{1,}[ \t]*\\{{0,1}[ \t]*[^ \t\"@',\\#}{~%]*).*$", | |
59 | /* -- */ | |
ddd164d0 ÆAB |
60 | "[={}\"]|[^={}\" \t]+"), |
61 | PATTERNS("cpp", | |
62 | /* Jump targets or access declarations */ | |
63 | "!^[ \t]*[A-Za-z_][A-Za-z_0-9]*:[[:space:]]*($|/[/*])\n" | |
64 | /* functions/methods, variables, and compounds at top level */ | |
65 | "^((::[[:space:]]*)?[A-Za-z_].*)$", | |
66 | /* -- */ | |
67 | "[a-zA-Z_][a-zA-Z0-9_]*" | |
68 | "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lLuU]*" | |
69 | "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->\\*?|\\.\\*"), | |
70 | PATTERNS("csharp", | |
71 | /* Keywords */ | |
72 | "!^[ \t]*(do|while|for|if|else|instanceof|new|return|switch|case|throw|catch|using)\n" | |
73 | /* Methods and constructors */ | |
74 | "^[ \t]*(((static|public|internal|private|protected|new|virtual|sealed|override|unsafe|async)[ \t]+)*[][<>@.~_[:alnum:]]+[ \t]+[<>@._[:alnum:]]+[ \t]*\\(.*\\))[ \t]*$\n" | |
75 | /* Properties */ | |
76 | "^[ \t]*(((static|public|internal|private|protected|new|virtual|sealed|override|unsafe)[ \t]+)*[][<>@.~_[:alnum:]]+[ \t]+[@._[:alnum:]]+)[ \t]*$\n" | |
77 | /* Type definitions */ | |
c4e31781 | 78 | "^[ \t]*(((static|public|internal|private|protected|new|unsafe|sealed|abstract|partial)[ \t]+)*(class|enum|interface|struct|record)[ \t]+.*)$\n" |
ddd164d0 ÆAB |
79 | /* Namespace */ |
80 | "^[ \t]*(namespace[ \t]+.*)$", | |
81 | /* -- */ | |
82 | "[a-zA-Z_][a-zA-Z0-9_]*" | |
83 | "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?" | |
84 | "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->"), | |
85 | IPATTERN("css", | |
86 | "![:;][[:space:]]*$\n" | |
87 | "^[:[@.#]?[_a-z0-9].*$", | |
88 | /* -- */ | |
89 | /* | |
90 | * This regex comes from W3C CSS specs. Should theoretically also | |
91 | * allow ISO 10646 characters U+00A0 and higher, | |
92 | * but they are not handled in this regex. | |
93 | */ | |
94 | "-?[_a-zA-Z][-_a-zA-Z0-9]*" /* identifiers */ | |
95 | "|-?[0-9]+|\\#[0-9a-fA-F]+" /* numbers */ | |
96 | ), | |
3c81760b SB |
97 | PATTERNS("dts", |
98 | "!;\n" | |
8da56a48 | 99 | "!=\n" |
3c81760b | 100 | /* lines beginning with a word optionally preceded by '&' or the root */ |
8da56a48 | 101 | "^[ \t]*((/[ \t]*\\{|&?[a-zA-Z_]).*)", |
3c81760b SB |
102 | /* -- */ |
103 | /* Property names and math operators */ | |
104 | "[a-zA-Z0-9,._+?#-]+" | |
105 | "|[-+*/%&^|!~]|>>|<<|&&|\\|\\|"), | |
a807200f ŁN |
106 | PATTERNS("elixir", |
107 | "^[ \t]*((def(macro|module|impl|protocol|p)?|test)[ \t].*)$", | |
d1b1384d | 108 | /* -- */ |
a807200f | 109 | /* Atoms, names, and module attributes */ |
d1b1384d | 110 | "[@:]?[a-zA-Z0-9@_?!]+" |
a807200f ŁN |
111 | /* Numbers with specific base */ |
112 | "|[-+]?0[xob][0-9a-fA-F]+" | |
113 | /* Numbers */ | |
114 | "|[-+]?[0-9][0-9_.]*([eE][-+]?[0-9_]+)?" | |
115 | /* Operators and atoms that represent them */ | |
116 | "|:?(\\+\\+|--|\\.\\.|~~~|<>|\\^\\^\\^|<?\\|>|<<<?|>?>>|<<?~|~>?>|<~>|<=|>=|===?|!==?|=~|&&&?|\\|\\|\\|?|=>|<-|\\\\\\\\|->)" | |
117 | /* Not real operators, but should be grouped */ | |
118 | "|:?%[A-Za-z0-9_.]\\{\\}?"), | |
909a5494 | 119 | IPATTERN("fortran", |
b79e6925 | 120 | /* Don't match comment lines */ |
909a5494 | 121 | "!^([C*]|[ \t]*!)\n" |
b79e6925 | 122 | /* Don't match 'module procedure' lines */ |
909a5494 | 123 | "!^[ \t]*MODULE[ \t]+PROCEDURE[ \t]\n" |
b79e6925 | 124 | /* Program, module, block data */ |
909a5494 | 125 | "^[ \t]*((END[ \t]+)?(PROGRAM|MODULE|BLOCK[ \t]+DATA" |
b79e6925 | 126 | /* Subroutines and functions */ |
75c3b6b2 | 127 | "|([^!'\" \t]+[ \t]+)*(SUBROUTINE|FUNCTION))[ \t]+[A-Z].*)$", |
909a5494 BC |
128 | /* -- */ |
129 | "[a-zA-Z][a-zA-Z0-9_]*" | |
130 | "|\\.([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])\\." | |
131 | /* numbers and format statements like 2E14.4, or ES12.6, 9X. | |
132 | * Don't worry about format statements without leading digits since | |
133 | * they would have been matched above as a variable anyway. */ | |
134 | "|[-+]?[0-9.]+([AaIiDdEeFfLlTtXx][Ss]?[-+]?[0-9.]*)?(_[a-zA-Z0-9][a-zA-Z0-9_]*)?" | |
664d44ee | 135 | "|//|\\*\\*|::|[/<>=]="), |
6d1c9c52 ÆAB |
136 | IPATTERN("fountain", |
137 | "^((\\.[^.]|(int|ext|est|int\\.?/ext|i/e)[. ]).*)$", | |
138 | /* -- */ | |
69f9c87d | 139 | "[^ \t-]+"), |
1dbf0c0a AG |
140 | PATTERNS("golang", |
141 | /* Functions */ | |
142 | "^[ \t]*(func[ \t]*.*(\\{[ \t]*)?)\n" | |
143 | /* Structs and interfaces */ | |
144 | "^[ \t]*(type[ \t].*(struct|interface)[ \t]*(\\{[ \t]*)?)", | |
145 | /* -- */ | |
146 | "[a-zA-Z_][a-zA-Z0-9_]*" | |
147 | "|[-+0-9.eE]+i?|0[xX]?[0-9a-fA-F]+i?" | |
148 | "|[-+*/<>%&^|=!:]=|--|\\+\\+|<<=?|>>=?|&\\^=?|&&|\\|\\||<-|\\.{3}"), | |
6d1c9c52 ÆAB |
149 | PATTERNS("html", |
150 | "^[ \t]*(<[Hh][1-6]([ \t].*)?>.*)$", | |
151 | /* -- */ | |
664d44ee | 152 | "[^<>= \t]+"), |
80c49c3d | 153 | PATTERNS("java", |
be58e70d | 154 | "!^[ \t]*(catch|do|for|if|instanceof|new|return|switch|throw|while)\n" |
a8cbc895 TH |
155 | /* Class, enum, and interface declarations */ |
156 | "^[ \t]*(([a-z]+[ \t]+)*(class|enum|interface)[ \t]+[A-Za-z][A-Za-z0-9_$]*[ \t]+.*)$\n" | |
157 | /* Method definitions; note that constructor signatures are not */ | |
158 | /* matched because they are indistinguishable from method calls. */ | |
159 | "^[ \t]*(([A-Za-z_<>&][][?&<>.,A-Za-z_0-9]*[ \t]+)+[A-Za-z_][A-Za-z_0-9]*[ \t]*\\([^;]*)$", | |
959e2e64 | 160 | /* -- */ |
80c49c3d TR |
161 | "[a-zA-Z_][a-zA-Z0-9_]*" |
162 | "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?" | |
163 | "|[-+*/<>%&^|=!]=" | |
664d44ee | 164 | "|--|\\+\\+|<<=?|>>>?=?|&&|\\|\\|"), |
09dad925 AH |
165 | PATTERNS("markdown", |
166 | "^ {0,3}#{1,6}[ \t].*", | |
6d1c9c52 | 167 | /* -- */ |
09dad925 | 168 | "[^<>= \t]+"), |
53b10a14 | 169 | PATTERNS("matlab", |
2731a784 BL |
170 | /* |
171 | * Octave pattern is mostly the same as matlab, except that '%%%' and | |
91bf382f | 172 | * '##' can also be used to begin code sections, in addition to '%%' |
2731a784 BL |
173 | * that is understood by both. |
174 | */ | |
91bf382f | 175 | "^[[:space:]]*((classdef|function)[[:space:]].*)$|^(%%%?|##)[[:space:]].*$", |
6d1c9c52 | 176 | /* -- */ |
53b10a14 | 177 | "[a-zA-Z_][a-zA-Z0-9_]*|[-+0-9.e]+|[=~<>]=|\\.[*/\\^']|\\|\\||&&"), |
80c49c3d | 178 | PATTERNS("objc", |
be58e70d JK |
179 | /* Negate C statements that can look like functions */ |
180 | "!^[ \t]*(do|for|if|else|return|switch|while)\n" | |
181 | /* Objective-C methods */ | |
182 | "^[ \t]*([-+][ \t]*\\([ \t]*[A-Za-z_][A-Za-z_0-9* \t]*\\)[ \t]*[A-Za-z_].*)$\n" | |
183 | /* C functions */ | |
959e2e64 | 184 | "^[ \t]*(([A-Za-z_][A-Za-z_0-9]*[ \t]+)+[A-Za-z_][A-Za-z_0-9]*[ \t]*\\([^;]*)$\n" |
be58e70d | 185 | /* Objective-C class/protocol definitions */ |
80c49c3d TR |
186 | "^(@(implementation|interface|protocol)[ \t].*)$", |
187 | /* -- */ | |
188 | "[a-zA-Z_][a-zA-Z0-9_]*" | |
189 | "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?" | |
664d44ee | 190 | "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->"), |
80c49c3d | 191 | PATTERNS("pascal", |
82512e00 ÆAB |
192 | "^(((class[ \t]+)?(procedure|function)|constructor|destructor|interface" |
193 | "|implementation|initialization|finalization)[ \t]*.*)$\n" | |
80c49c3d TR |
194 | "^(.*=[ \t]*(class|record).*)$", |
195 | /* -- */ | |
196 | "[a-zA-Z_][a-zA-Z0-9_]*" | |
197 | "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+" | |
664d44ee | 198 | "|<>|<=|>=|:=|\\.\\."), |
71a5d4bc | 199 | PATTERNS("perl", |
ea2ca449 JN |
200 | "^package .*\n" |
201 | "^sub [[:alnum:]_':]+[ \t]*" | |
202 | "(\\([^)]*\\)[ \t]*)?" /* prototype */ | |
203 | /* | |
204 | * Attributes. A regex can't count nested parentheses, | |
205 | * so just slurp up whatever we see, taking care not | |
206 | * to accept lines like "sub foo; # defined elsewhere". | |
207 | * | |
208 | * An attribute could contain a semicolon, but at that | |
209 | * point it seems reasonable enough to give up. | |
210 | */ | |
211 | "(:[^;#]*)?" | |
212 | "(\\{[ \t]*)?" /* brace can come here or on the next line */ | |
213 | "(#.*)?$\n" /* comment */ | |
f143d9c6 | 214 | "^(BEGIN|END|INIT|CHECK|UNITCHECK|AUTOLOAD|DESTROY)[ \t]*" |
ea2ca449 JN |
215 | "(\\{[ \t]*)?" /* brace can come here or on the next line */ |
216 | "(#.*)?$\n" | |
12f0967a | 217 | "^=head[0-9] .*", /* POD */ |
71a5d4bc JN |
218 | /* -- */ |
219 | "[[:alpha:]_'][[:alnum:]_']*" | |
220 | "|0[xb]?[0-9a-fA-F_]*" | |
221 | /* taking care not to interpret 3..5 as (3.)(.5) */ | |
222 | "|[0-9a-fA-F_]+(\\.[0-9a-fA-F_]+)?([eE][-+]?[0-9_]+)?" | |
223 | "|=>|-[rwxoRWXOezsfdlpSugkbctTBMAC>]|~~|::" | |
224 | "|&&=|\\|\\|=|//=|\\*\\*=" | |
225 | "|&&|\\|\\||//|\\+\\+|--|\\*\\*|\\.\\.\\.?" | |
226 | "|[-+*/%.^&<>=!|]=" | |
227 | "|=~|!~" | |
664d44ee | 228 | "|<<|<>|<=>|>>"), |
6d2f208c | 229 | PATTERNS("php", |
aff92827 | 230 | "^[\t ]*(((public|protected|private|static|abstract|final)[\t ]+)*function.*)$\n" |
2c7f3aac | 231 | "^[\t ]*((((final|abstract)[\t ]+)?class|enum|interface|trait).*)$", |
80c49c3d TR |
232 | /* -- */ |
233 | "[a-zA-Z_][a-zA-Z0-9_]*" | |
234 | "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+" | |
664d44ee | 235 | "|[-+*/<>%&^|=!.]=|--|\\+\\+|<<=?|>>=?|===|&&|\\|\\||::|->"), |
6d1c9c52 ÆAB |
236 | PATTERNS("python", |
237 | "^[ \t]*((class|(async[ \t]+)?def)[ \t].*)$", | |
80c49c3d TR |
238 | /* -- */ |
239 | "[a-zA-Z_][a-zA-Z0-9_]*" | |
240 | "|[-+0-9.e]+[jJlL]?|0[xX]?[0-9a-fA-F]+[lL]?" | |
664d44ee | 241 | "|[-+*/<>%&^|=!]=|//=?|<<=?|>>=?|\\*\\*=?"), |
80c49c3d | 242 | /* -- */ |
6d1c9c52 ÆAB |
243 | PATTERNS("ruby", |
244 | "^[ \t]*((class|module|def)[ \t].*)$", | |
80c49c3d TR |
245 | /* -- */ |
246 | "(@|@@|\\$)?[a-zA-Z_][a-zA-Z0-9_]*" | |
247 | "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+|\\?(\\\\C-)?(\\\\M-)?." | |
664d44ee | 248 | "|//=?|[-+*/<>%&^|=!]=|<<=?|>>=?|===|\\.{1,3}|::|[!=]~"), |
d74e7860 | 249 | PATTERNS("rust", |
a04c7e0f | 250 | "^[\t ]*((pub(\\([^\\)]+\\))?[\t ]+)?((async|const|unsafe|extern([\t ]+\"[^\"]+\"))[\t ]+)?(struct|enum|union|mod|trait|fn|impl|macro_rules!)[< \t]+[^;]*)$", |
d74e7860 MAL |
251 | /* -- */ |
252 | "[a-zA-Z_][a-zA-Z0-9_]*" | |
33be7b38 | 253 | "|[0-9][0-9_a-fA-Fiosuxz]*(\\.([0-9]*[eE][+-]?)?[0-9_fF]*)?" |
d74e7860 | 254 | "|[-+*\\/<>%&^|=!:]=|<<=?|>>=?|&&|\\|\\||->|=>|\\.{2}=|\\.{3}|::"), |
a4373903 AR |
255 | PATTERNS("scheme", |
256 | "^[\t ]*(\\(((define|def(struct|syntax|class|method|rules|record|proto|alias)?)[-*/ \t]|(library|module|struct|class)[*+ \t]).*)$", | |
257 | /* | |
258 | * R7RS valid identifiers include any sequence enclosed | |
259 | * within vertical lines having no backslashes | |
260 | */ | |
261 | "\\|([^\\\\]*)\\|" | |
262 | /* All other words should be delimited by spaces or parentheses */ | |
263 | "|([^][)(}{[ \t])+"), | |
80c49c3d | 264 | PATTERNS("tex", "^(\\\\((sub)*section|chapter|part)\\*{0,1}\\{.*)$", |
664d44ee | 265 | "\\\\[a-zA-Z@]+|\\\\.|[a-zA-Z0-9\x80-\xff]+"), |
122aa6f9 | 266 | { "default", NULL, -1, { NULL, 0 } }, |
be58e70d | 267 | }; |
80c49c3d | 268 | #undef PATTERNS |
909a5494 | 269 | #undef IPATTERN |
be58e70d JK |
270 | |
271 | static struct userdiff_driver driver_true = { | |
272 | "diff=true", | |
273 | NULL, | |
122aa6f9 | 274 | 0, |
be58e70d JK |
275 | { NULL, 0 } |
276 | }; | |
be58e70d JK |
277 | |
278 | static struct userdiff_driver driver_false = { | |
279 | "!diff", | |
280 | NULL, | |
122aa6f9 | 281 | 1, |
be58e70d JK |
282 | { NULL, 0 } |
283 | }; | |
be58e70d | 284 | |
f12fa9ee ÆAB |
285 | struct find_by_namelen_data { |
286 | const char *name; | |
287 | size_t len; | |
288 | struct userdiff_driver *driver; | |
289 | }; | |
290 | ||
291 | static int userdiff_find_by_namelen_cb(struct userdiff_driver *driver, | |
292 | enum userdiff_driver_type type, void *priv) | |
be58e70d | 293 | { |
f12fa9ee ÆAB |
294 | struct find_by_namelen_data *cb_data = priv; |
295 | ||
296 | if (!strncmp(driver->name, cb_data->name, cb_data->len) && | |
297 | !driver->name[cb_data->len]) { | |
298 | cb_data->driver = driver; | |
299 | return 1; /* tell the caller to stop iterating */ | |
be58e70d | 300 | } |
f12fa9ee ÆAB |
301 | return 0; |
302 | } | |
303 | ||
304 | static struct userdiff_driver *userdiff_find_by_namelen(const char *name, size_t len) | |
305 | { | |
306 | struct find_by_namelen_data udcbdata = { | |
307 | .name = name, | |
308 | .len = len, | |
309 | }; | |
310 | for_each_userdiff_driver(userdiff_find_by_namelen_cb, &udcbdata); | |
311 | return udcbdata.driver; | |
be58e70d JK |
312 | } |
313 | ||
be58e70d JK |
314 | static int parse_funcname(struct userdiff_funcname *f, const char *k, |
315 | const char *v, int cflags) | |
316 | { | |
317 | if (git_config_string(&f->pattern, k, v) < 0) | |
318 | return -1; | |
319 | f->cflags = cflags; | |
6680a087 | 320 | return 0; |
be58e70d JK |
321 | } |
322 | ||
122aa6f9 JK |
323 | static int parse_tristate(int *b, const char *k, const char *v) |
324 | { | |
325 | if (v && !strcasecmp(v, "auto")) | |
326 | *b = -1; | |
327 | else | |
328 | *b = git_config_bool(k, v); | |
6680a087 | 329 | return 0; |
122aa6f9 JK |
330 | } |
331 | ||
d9bae1a1 JK |
332 | static int parse_bool(int *b, const char *k, const char *v) |
333 | { | |
334 | *b = git_config_bool(k, v); | |
6680a087 | 335 | return 0; |
d9bae1a1 JK |
336 | } |
337 | ||
c7534ef4 | 338 | int userdiff_config(const char *k, const char *v) |
be58e70d JK |
339 | { |
340 | struct userdiff_driver *drv; | |
0a5987fe | 341 | const char *name, *type; |
f5914f4b | 342 | size_t namelen; |
0a5987fe JK |
343 | |
344 | if (parse_config_key(k, "diff", &name, &namelen, &type) || !name) | |
345 | return 0; | |
346 | ||
347 | drv = userdiff_find_by_namelen(name, namelen); | |
348 | if (!drv) { | |
349 | ALLOC_GROW(drivers, ndrivers+1, drivers_alloc); | |
350 | drv = &drivers[ndrivers++]; | |
351 | memset(drv, 0, sizeof(*drv)); | |
352 | drv->name = xmemdupz(name, namelen); | |
353 | drv->binary = -1; | |
354 | } | |
be58e70d | 355 | |
0a5987fe | 356 | if (!strcmp(type, "funcname")) |
be58e70d | 357 | return parse_funcname(&drv->funcname, k, v, 0); |
0a5987fe | 358 | if (!strcmp(type, "xfuncname")) |
be58e70d | 359 | return parse_funcname(&drv->funcname, k, v, REG_EXTENDED); |
0a5987fe | 360 | if (!strcmp(type, "binary")) |
122aa6f9 | 361 | return parse_tristate(&drv->binary, k, v); |
0a5987fe | 362 | if (!strcmp(type, "command")) |
6680a087 | 363 | return git_config_string(&drv->external, k, v); |
0a5987fe | 364 | if (!strcmp(type, "textconv")) |
6680a087 | 365 | return git_config_string(&drv->textconv, k, v); |
0a5987fe | 366 | if (!strcmp(type, "cachetextconv")) |
d9bae1a1 | 367 | return parse_bool(&drv->textconv_want_cache, k, v); |
0a5987fe | 368 | if (!strcmp(type, "wordregex")) |
6680a087 | 369 | return git_config_string(&drv->word_regex, k, v); |
be58e70d JK |
370 | |
371 | return 0; | |
372 | } | |
373 | ||
3b335762 NTND |
374 | struct userdiff_driver *userdiff_find_by_name(const char *name) |
375 | { | |
be58e70d JK |
376 | int len = strlen(name); |
377 | return userdiff_find_by_namelen(name, len); | |
378 | } | |
379 | ||
acd00ea0 NTND |
380 | struct userdiff_driver *userdiff_find_by_path(struct index_state *istate, |
381 | const char *path) | |
be58e70d | 382 | { |
2aef63d3 | 383 | static struct attr_check *check; |
be58e70d | 384 | |
2aef63d3 JH |
385 | if (!check) |
386 | check = attr_check_initl("diff", NULL); | |
be58e70d JK |
387 | if (!path) |
388 | return NULL; | |
11877b9e | 389 | git_check_attr(istate, path, check); |
be58e70d | 390 | |
2aef63d3 | 391 | if (ATTR_TRUE(check->items[0].value)) |
be58e70d | 392 | return &driver_true; |
2aef63d3 | 393 | if (ATTR_FALSE(check->items[0].value)) |
be58e70d | 394 | return &driver_false; |
2aef63d3 | 395 | if (ATTR_UNSET(check->items[0].value)) |
be58e70d | 396 | return NULL; |
2aef63d3 | 397 | return userdiff_find_by_name(check->items[0].value); |
be58e70d | 398 | } |
3813e690 | 399 | |
bd7ad45b NTND |
400 | struct userdiff_driver *userdiff_get_textconv(struct repository *r, |
401 | struct userdiff_driver *driver) | |
3813e690 JK |
402 | { |
403 | if (!driver->textconv) | |
404 | return NULL; | |
405 | ||
406 | if (driver->textconv_want_cache && !driver->textconv_cache) { | |
407 | struct notes_cache *c = xmalloc(sizeof(*c)); | |
408 | struct strbuf name = STRBUF_INIT; | |
409 | ||
410 | strbuf_addf(&name, "textconv/%s", driver->name); | |
bd7ad45b | 411 | notes_cache_init(r, c, name.buf, driver->textconv); |
3813e690 | 412 | driver->textconv_cache = c; |
460c7eb2 | 413 | strbuf_release(&name); |
3813e690 JK |
414 | } |
415 | ||
416 | return driver; | |
417 | } | |
f12fa9ee ÆAB |
418 | |
419 | static int for_each_userdiff_driver_list(each_userdiff_driver_fn fn, | |
420 | enum userdiff_driver_type type, void *cb_data, | |
421 | struct userdiff_driver *drv, | |
422 | int drv_size) | |
423 | { | |
424 | int i; | |
425 | int ret; | |
426 | for (i = 0; i < drv_size; i++) { | |
427 | struct userdiff_driver *item = drv + i; | |
428 | if ((ret = fn(item, type, cb_data))) | |
429 | return ret; | |
430 | } | |
431 | return 0; | |
432 | } | |
433 | ||
434 | int for_each_userdiff_driver(each_userdiff_driver_fn fn, void *cb_data) | |
435 | { | |
436 | int ret; | |
437 | ||
438 | ret = for_each_userdiff_driver_list(fn, USERDIFF_DRIVER_TYPE_CUSTOM, | |
439 | cb_data, drivers, ndrivers); | |
440 | if (ret) | |
441 | return ret; | |
442 | ||
443 | ret = for_each_userdiff_driver_list(fn, USERDIFF_DRIVER_TYPE_BUILTIN, | |
444 | cb_data, builtin_drivers, | |
445 | ARRAY_SIZE(builtin_drivers)); | |
446 | if (ret) | |
447 | return ret; | |
448 | ||
449 | return 0; | |
450 | } |