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