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