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