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