🌐 AI搜索 & 代理 主页
blob: fde02f225b2b9ed17a8246913c3bddfa6483c98b [file] [log] [blame]
Jeff Kingd9bae1a2010-04-02 00:12:151#include "cache.h"
Brandon Williamsb2141fc2017-06-14 18:07:362#include "config.h"
Jeff Kingbe58e702008-10-05 21:43:213#include "userdiff.h"
Jeff Kingbe58e702008-10-05 21:43:214#include "attr.h"
5
6static struct userdiff_driver *drivers;
7static int ndrivers;
8static int drivers_alloc;
9
Boyd Stephen Smith Jrae3b9702009-01-21 04:59:5410#define PATTERNS(name, pattern, word_regex) \
Jonathan Nieder664d44e2011-01-11 21:48:5011 { name, NULL, -1, { pattern, REG_EXTENDED }, \
12 word_regex "|[^[:space:]]|[\xc0-\xff][\x80-\xbf]+" }
Brandon Casey909a5492010-09-10 16:18:1413#define IPATTERN(name, pattern, word_regex) \
Jonathan Nieder664d44e2011-01-11 21:48:5014 { name, NULL, -1, { pattern, REG_EXTENDED | REG_ICASE }, \
15 word_regex "|[^[:space:]]|[\xc0-\xff][\x80-\xbf]+" }
Jeff Kingbe58e702008-10-05 21:43:2116static struct userdiff_driver builtin_drivers[] = {
Adrian Johnsone90d0652012-09-16 03:54:1517IPATTERN("ada",
Adrian Johnson39a87a22014-02-03 11:33:1618 "!^(.*[ \t])?(is[ \t]+new|renames|is[ \t]+separate)([ \t].*)?$\n"
Adrian Johnsone90d0652012-09-16 03:54:1519 "!^[ \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_]*"
Adrian Johnson39a87a22014-02-03 11:33:1624 "|[-+]?[0-9][0-9#_.aAbBcCdDeEfF]*([eE][+-]?[0-9_]+)?"
Adrian Johnsone90d0652012-09-16 03:54:1525 "|=>|\\.\\.|\\*\\*|:=|/=|>=|<=|<<|>>|<>"),
Stephen Boyd3c817602019-08-19 21:22:4326PATTERNS("dts",
27 "!;\n"
Stephen Boyd8da56a42019-10-20 18:52:3028 "!=\n"
Stephen Boyd3c817602019-08-19 21:22:4329 /* lines beginning with a word optionally preceded by '&' or the root */
Stephen Boyd8da56a42019-10-20 18:52:3030 "^[ \t]*((/[ \t]*\\{|&?[a-zA-Z_]).*)",
Stephen Boyd3c817602019-08-19 21:22:4331 /* -- */
32 /* Property names and math operators */
33 "[a-zA-Z0-9,._+?#-]+"
34 "|[-+*/%&^|!~]|>>|<<|&&|\\|\\|"),
Łukasz Niemiera8072002019-11-08 21:38:2435PATTERNS("elixir",
36 "^[ \t]*((def(macro|module|impl|protocol|p)?|test)[ \t].*)$",
Ed Masted1b13842019-12-13 17:55:3537 /* -- */
Łukasz Niemiera8072002019-11-08 21:38:2438 /* Atoms, names, and module attributes */
Ed Masted1b13842019-12-13 17:55:3539 "[@:]?[a-zA-Z0-9@_?!]+"
Łukasz Niemiera8072002019-11-08 21:38:2440 /* Numbers with specific base */
41 "|[-+]?0[xob][0-9a-fA-F]+"
42 /* Numbers */
43 "|[-+]?[0-9][0-9_.]*([eE][-+]?[0-9_]+)?"
44 /* Operators and atoms that represent them */
45 "|:?(\\+\\+|--|\\.\\.|~~~|<>|\\^\\^\\^|<?\\|>|<<<?|>?>>|<<?~|~>?>|<~>|<=|>=|===?|!==?|=~|&&&?|\\|\\|\\|?|=>|<-|\\\\\\\\|->)"
46 /* Not real operators, but should be grouped */
47 "|:?%[A-Za-z0-9_.]\\{\\}?"),
Brandon Casey909a5492010-09-10 16:18:1448IPATTERN("fortran",
Philippe Blainb79e6922020-08-12 22:30:2849 /* Don't match comment lines */
Brandon Casey909a5492010-09-10 16:18:1450 "!^([C*]|[ \t]*!)\n"
Philippe Blainb79e6922020-08-12 22:30:2851 /* Don't match 'module procedure' lines */
Brandon Casey909a5492010-09-10 16:18:1452 "!^[ \t]*MODULE[ \t]+PROCEDURE[ \t]\n"
Philippe Blainb79e6922020-08-12 22:30:2853 /* Program, module, block data */
Brandon Casey909a5492010-09-10 16:18:1454 "^[ \t]*((END[ \t]+)?(PROGRAM|MODULE|BLOCK[ \t]+DATA"
Philippe Blainb79e6922020-08-12 22:30:2855 /* Subroutines and functions */
Philippe Blain75c3b6b2020-08-12 22:30:2956 "|([^!'\" \t]+[ \t]+)*(SUBROUTINE|FUNCTION))[ \t]+[A-Z].*)$",
Brandon Casey909a5492010-09-10 16:18:1457 /* -- */
58 "[a-zA-Z][a-zA-Z0-9_]*"
59 "|\\.([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])\\."
60 /* numbers and format statements like 2E14.4, or ES12.6, 9X.
61 * Don't worry about format statements without leading digits since
62 * they would have been matched above as a variable anyway. */
63 "|[-+]?[0-9.]+([AaIiDdEeFfLlTtXx][Ss]?[-+]?[0-9.]*)?(_[a-zA-Z0-9][a-zA-Z0-9_]*)?"
Jonathan Nieder664d44e2011-01-11 21:48:5064 "|//|\\*\\*|::|[/<>=]="),
Zoë Blade69f9c872015-07-21 13:22:4665IPATTERN("fountain", "^((\\.[^.]|(int|ext|est|int\\.?/ext|i/e)[. ]).*)$",
66 "[^ \t-]+"),
Alban Gruin1dbf0c02018-03-01 11:19:0767PATTERNS("golang",
68 /* Functions */
69 "^[ \t]*(func[ \t]*.*(\\{[ \t]*)?)\n"
70 /* Structs and interfaces */
71 "^[ \t]*(type[ \t].*(struct|interface)[ \t]*(\\{[ \t]*)?)",
72 /* -- */
73 "[a-zA-Z_][a-zA-Z0-9_]*"
74 "|[-+0-9.eE]+i?|0[xX]?[0-9a-fA-F]+i?"
75 "|[-+*/<>%&^|=!:]=|--|\\+\\+|<<=?|>>=?|&\\^=?|&&|\\|\\||<-|\\.{3}"),
Ilya Kantor9c03cac2017-09-23 07:53:4776PATTERNS("html", "^[ \t]*(<[Hh][1-6]([ \t].*)?>.*)$",
Jonathan Nieder664d44e2011-01-11 21:48:5077 "[^<>= \t]+"),
Thomas Rast80c49c32009-01-17 16:29:4878PATTERNS("java",
Jeff Kingbe58e702008-10-05 21:43:2179 "!^[ \t]*(catch|do|for|if|instanceof|new|return|switch|throw|while)\n"
Paolo Bonzini959e2e62009-06-17 14:26:0680 "^[ \t]*(([A-Za-z_][A-Za-z_0-9]*[ \t]+)+[A-Za-z_][A-Za-z_0-9]*[ \t]*\\([^;]*)$",
81 /* -- */
Thomas Rast80c49c32009-01-17 16:29:4882 "[a-zA-Z_][a-zA-Z0-9_]*"
83 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?"
84 "|[-+*/<>%&^|=!]="
Jonathan Nieder664d44e2011-01-11 21:48:5085 "|--|\\+\\+|<<=?|>>>?=?|&&|\\|\\|"),
Ash Holland09dad922020-05-02 13:15:4386PATTERNS("markdown",
87 "^ {0,3}#{1,6}[ \t].*",
88 "[^<>= \t]+"),
Gustaf Hendeby53b10a12011-11-15 20:15:0389PATTERNS("matlab",
Boxuan Li2731a782019-05-29 16:15:3990 /*
91 * Octave pattern is mostly the same as matlab, except that '%%%' and
Boxuan Li91bf3822019-05-18 03:46:2392 * '##' can also be used to begin code sections, in addition to '%%'
Boxuan Li2731a782019-05-29 16:15:3993 * that is understood by both.
94 */
Boxuan Li91bf3822019-05-18 03:46:2395 "^[[:space:]]*((classdef|function)[[:space:]].*)$|^(%%%?|##)[[:space:]].*$",
Gustaf Hendeby53b10a12011-11-15 20:15:0396 "[a-zA-Z_][a-zA-Z0-9_]*|[-+0-9.e]+|[=~<>]=|\\.[*/\\^']|\\|\\||&&"),
Thomas Rast80c49c32009-01-17 16:29:4897PATTERNS("objc",
Jeff Kingbe58e702008-10-05 21:43:2198 /* Negate C statements that can look like functions */
99 "!^[ \t]*(do|for|if|else|return|switch|while)\n"
100 /* Objective-C methods */
101 "^[ \t]*([-+][ \t]*\\([ \t]*[A-Za-z_][A-Za-z_0-9* \t]*\\)[ \t]*[A-Za-z_].*)$\n"
102 /* C functions */
Paolo Bonzini959e2e62009-06-17 14:26:06103 "^[ \t]*(([A-Za-z_][A-Za-z_0-9]*[ \t]+)+[A-Za-z_][A-Za-z_0-9]*[ \t]*\\([^;]*)$\n"
Jeff Kingbe58e702008-10-05 21:43:21104 /* Objective-C class/protocol definitions */
Thomas Rast80c49c32009-01-17 16:29:48105 "^(@(implementation|interface|protocol)[ \t].*)$",
106 /* -- */
107 "[a-zA-Z_][a-zA-Z0-9_]*"
108 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?"
Jonathan Nieder664d44e2011-01-11 21:48:50109 "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->"),
Thomas Rast80c49c32009-01-17 16:29:48110PATTERNS("pascal",
Alexey Shumkinad5b6942011-01-11 08:53:59111 "^(((class[ \t]+)?(procedure|function)|constructor|destructor|interface|"
Jeff Kingbe58e702008-10-05 21:43:21112 "implementation|initialization|finalization)[ \t]*.*)$"
113 "\n"
Thomas Rast80c49c32009-01-17 16:29:48114 "^(.*=[ \t]*(class|record).*)$",
115 /* -- */
116 "[a-zA-Z_][a-zA-Z0-9_]*"
117 "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+"
Jonathan Nieder664d44e2011-01-11 21:48:50118 "|<>|<=|>=|:=|\\.\\."),
Jonathan Nieder71a5d4b2010-12-26 09:07:31119PATTERNS("perl",
Jonathan Niederea2ca442011-05-21 19:38:26120 "^package .*\n"
121 "^sub [[:alnum:]_':]+[ \t]*"
122 "(\\([^)]*\\)[ \t]*)?" /* prototype */
123 /*
124 * Attributes. A regex can't count nested parentheses,
125 * so just slurp up whatever we see, taking care not
126 * to accept lines like "sub foo; # defined elsewhere".
127 *
128 * An attribute could contain a semicolon, but at that
129 * point it seems reasonable enough to give up.
130 */
131 "(:[^;#]*)?"
132 "(\\{[ \t]*)?" /* brace can come here or on the next line */
133 "(#.*)?$\n" /* comment */
Jonathan Niederf143d9c2011-05-22 17:29:32134 "^(BEGIN|END|INIT|CHECK|UNITCHECK|AUTOLOAD|DESTROY)[ \t]*"
Jonathan Niederea2ca442011-05-21 19:38:26135 "(\\{[ \t]*)?" /* brace can come here or on the next line */
136 "(#.*)?$\n"
Jonathan Nieder12f09672011-05-21 19:35:51137 "^=head[0-9] .*", /* POD */
Jonathan Nieder71a5d4b2010-12-26 09:07:31138 /* -- */
139 "[[:alpha:]_'][[:alnum:]_']*"
140 "|0[xb]?[0-9a-fA-F_]*"
141 /* taking care not to interpret 3..5 as (3.)(.5) */
142 "|[0-9a-fA-F_]+(\\.[0-9a-fA-F_]+)?([eE][-+]?[0-9_]+)?"
143 "|=>|-[rwxoRWXOezsfdlpSugkbctTBMAC>]|~~|::"
144 "|&&=|\\|\\|=|//=|\\*\\*="
145 "|&&|\\|\\||//|\\+\\+|--|\\*\\*|\\.\\.\\.?"
146 "|[-+*/%.^&<>=!|]="
147 "|=~|!~"
Jonathan Nieder664d44e2011-01-11 21:48:50148 "|<<|<>|<=>|>>"),
Björn Steinbrink6d2f2082010-05-23 18:05:40149PATTERNS("php",
150 "^[\t ]*(((public|protected|private|static)[\t ]+)*function.*)$\n"
Kana Natsuno1ab63162018-07-03 13:15:40151 "^[\t ]*((((final|abstract)[\t ]+)?class|interface|trait).*)$",
Thomas Rast80c49c32009-01-17 16:29:48152 /* -- */
153 "[a-zA-Z_][a-zA-Z0-9_]*"
154 "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+"
Jonathan Nieder664d44e2011-01-11 21:48:50155 "|[-+*/<>%&^|=!.]=|--|\\+\\+|<<=?|>>=?|===|&&|\\|\\||::|->"),
Josh Holland077a1fd2019-11-19 15:08:10156PATTERNS("python", "^[ \t]*((class|(async[ \t]+)?def)[ \t].*)$",
Thomas Rast80c49c32009-01-17 16:29:48157 /* -- */
158 "[a-zA-Z_][a-zA-Z0-9_]*"
159 "|[-+0-9.e]+[jJlL]?|0[xX]?[0-9a-fA-F]+[lL]?"
Jonathan Nieder664d44e2011-01-11 21:48:50160 "|[-+*/<>%&^|=!]=|//=?|<<=?|>>=?|\\*\\*=?"),
Thomas Rast80c49c32009-01-17 16:29:48161 /* -- */
162PATTERNS("ruby", "^[ \t]*((class|module|def)[ \t].*)$",
163 /* -- */
164 "(@|@@|\\$)?[a-zA-Z_][a-zA-Z0-9_]*"
165 "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+|\\?(\\\\C-)?(\\\\M-)?."
Jonathan Nieder664d44e2011-01-11 21:48:50166 "|//=?|[-+*/<>%&^|=!]=|<<=?|>>=?|===|\\.{1,3}|::|[!=]~"),
Marc-André Lureaud74e7862019-05-16 23:58:15167PATTERNS("rust",
Johannes Sixt33be7b32019-05-30 16:44:35168 "^[\t ]*((pub(\\([^\\)]+\\))?[\t ]+)?((async|const|unsafe|extern([\t ]+\"[^\"]+\"))[\t ]+)?(struct|enum|union|mod|trait|fn|impl)[< \t]+[^;]*)$",
Marc-André Lureaud74e7862019-05-16 23:58:15169 /* -- */
170 "[a-zA-Z_][a-zA-Z0-9_]*"
Johannes Sixt33be7b32019-05-30 16:44:35171 "|[0-9][0-9_a-fA-Fiosuxz]*(\\.([0-9]*[eE][+-]?)?[0-9_fF]*)?"
Marc-André Lureaud74e7862019-05-16 23:58:15172 "|[-+*\\/<>%&^|=!:]=|<<=?|>>=?|&&|\\|\\||->|=>|\\.{2}=|\\.{3}|::"),
Thomas Rast80c49c32009-01-17 16:29:48173PATTERNS("bibtex", "(@[a-zA-Z]{1,}[ \t]*\\{{0,1}[ \t]*[^ \t\"@',\\#}{~%]*).*$",
174 "[={}\"]|[^={}\" \t]+"),
175PATTERNS("tex", "^(\\\\((sub)*section|chapter|part)\\*{0,1}\\{.*)$",
Jonathan Nieder664d44e2011-01-11 21:48:50176 "\\\\[a-zA-Z@]+|\\\\.|[a-zA-Z0-9\x80-\xff]+"),
Thomas Rast80c49c32009-01-17 16:29:48177PATTERNS("cpp",
178 /* Jump targets or access declarations */
Johannes Sixt8a2e8da2014-03-21 21:07:22179 "!^[ \t]*[A-Za-z_][A-Za-z_0-9]*:[[:space:]]*($|/[/*])\n"
180 /* functions/methods, variables, and compounds at top level */
181 "^((::[[:space:]]*)?[A-Za-z_].*)$",
Thomas Rast80c49c32009-01-17 16:29:48182 /* -- */
183 "[a-zA-Z_][a-zA-Z0-9_]*"
Johannes Sixtabf8f982014-03-21 21:07:14184 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lLuU]*"
Johannes Sixt407e07f2014-03-21 21:07:13185 "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->\\*?|\\.\\*"),
Petr Onderkab2212072010-08-16 17:01:02186PATTERNS("csharp",
187 /* Keywords */
188 "!^[ \t]*(do|while|for|if|else|instanceof|new|return|switch|case|throw|catch|using)\n"
189 /* Methods and constructors */
Thomas Levesquea12cec92018-03-08 11:05:32190 "^[ \t]*(((static|public|internal|private|protected|new|virtual|sealed|override|unsafe|async)[ \t]+)*[][<>@.~_[:alnum:]]+[ \t]+[<>@._[:alnum:]]+[ \t]*\\(.*\\))[ \t]*$\n"
Petr Onderkab2212072010-08-16 17:01:02191 /* Properties */
192 "^[ \t]*(((static|public|internal|private|protected|new|virtual|sealed|override|unsafe)[ \t]+)*[][<>@.~_[:alnum:]]+[ \t]+[@._[:alnum:]]+)[ \t]*$\n"
193 /* Type definitions */
194 "^[ \t]*(((static|public|internal|private|protected|new|unsafe|sealed|abstract|partial)[ \t]+)*(class|enum|interface|struct)[ \t]+.*)$\n"
195 /* Namespace */
196 "^[ \t]*(namespace[ \t]+.*)$",
197 /* -- */
198 "[a-zA-Z_][a-zA-Z0-9_]*"
199 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?"
Jonathan Nieder664d44e2011-01-11 21:48:50200 "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->"),
William Duclot0719f3e2016-06-03 12:32:26201IPATTERN("css",
202 "![:;][[:space:]]*$\n"
203 "^[_a-z0-9].*$",
204 /* -- */
205 /*
206 * This regex comes from W3C CSS specs. Should theoretically also
207 * allow ISO 10646 characters U+00A0 and higher,
208 * but they are not handled in this regex.
209 */
210 "-?[_a-zA-Z][-_a-zA-Z0-9]*" /* identifiers */
211 "|-?[0-9]+|\\#[0-9a-fA-F]+" /* numbers */
212),
Jeff King122aa6f2008-10-05 21:43:36213{ "default", NULL, -1, { NULL, 0 } },
Jeff Kingbe58e702008-10-05 21:43:21214};
Thomas Rast80c49c32009-01-17 16:29:48215#undef PATTERNS
Brandon Casey909a5492010-09-10 16:18:14216#undef IPATTERN
Jeff Kingbe58e702008-10-05 21:43:21217
218static struct userdiff_driver driver_true = {
219 "diff=true",
220 NULL,
Jeff King122aa6f2008-10-05 21:43:36221 0,
Jeff Kingbe58e702008-10-05 21:43:21222 { NULL, 0 }
223};
Jeff Kingbe58e702008-10-05 21:43:21224
225static struct userdiff_driver driver_false = {
226 "!diff",
227 NULL,
Jeff King122aa6f2008-10-05 21:43:36228 1,
Jeff Kingbe58e702008-10-05 21:43:21229 { NULL, 0 }
230};
Jeff Kingbe58e702008-10-05 21:43:21231
Jeff Kingf5914f42020-04-10 19:44:28232static struct userdiff_driver *userdiff_find_by_namelen(const char *k, size_t len)
Jeff Kingbe58e702008-10-05 21:43:21233{
234 int i;
235 for (i = 0; i < ndrivers; i++) {
236 struct userdiff_driver *drv = drivers + i;
237 if (!strncmp(drv->name, k, len) && !drv->name[len])
238 return drv;
239 }
240 for (i = 0; i < ARRAY_SIZE(builtin_drivers); i++) {
241 struct userdiff_driver *drv = builtin_drivers + i;
242 if (!strncmp(drv->name, k, len) && !drv->name[len])
243 return drv;
244 }
245 return NULL;
246}
247
Jeff Kingbe58e702008-10-05 21:43:21248static int parse_funcname(struct userdiff_funcname *f, const char *k,
249 const char *v, int cflags)
250{
251 if (git_config_string(&f->pattern, k, v) < 0)
252 return -1;
253 f->cflags = cflags;
Jeff King6680a082012-02-07 18:23:02254 return 0;
Jeff Kingbe58e702008-10-05 21:43:21255}
256
Jeff King122aa6f2008-10-05 21:43:36257static int parse_tristate(int *b, const char *k, const char *v)
258{
259 if (v && !strcasecmp(v, "auto"))
260 *b = -1;
261 else
262 *b = git_config_bool(k, v);
Jeff King6680a082012-02-07 18:23:02263 return 0;
Jeff King122aa6f2008-10-05 21:43:36264}
265
Jeff Kingd9bae1a2010-04-02 00:12:15266static int parse_bool(int *b, const char *k, const char *v)
267{
268 *b = git_config_bool(k, v);
Jeff King6680a082012-02-07 18:23:02269 return 0;
Jeff Kingd9bae1a2010-04-02 00:12:15270}
271
Jeff Kingc7534ef2008-10-26 04:45:55272int userdiff_config(const char *k, const char *v)
Jeff Kingbe58e702008-10-05 21:43:21273{
274 struct userdiff_driver *drv;
Jeff King0a5987f2013-01-23 06:25:07275 const char *name, *type;
Jeff Kingf5914f42020-04-10 19:44:28276 size_t namelen;
Jeff Kingbe58e702008-10-05 21:43:21277
Jeff King0a5987f2013-01-23 06:25:07278 if (parse_config_key(k, "diff", &name, &namelen, &type) || !name)
279 return 0;
280
281 drv = userdiff_find_by_namelen(name, namelen);
282 if (!drv) {
283 ALLOC_GROW(drivers, ndrivers+1, drivers_alloc);
284 drv = &drivers[ndrivers++];
285 memset(drv, 0, sizeof(*drv));
286 drv->name = xmemdupz(name, namelen);
287 drv->binary = -1;
288 }
289
290 if (!strcmp(type, "funcname"))
Jeff Kingbe58e702008-10-05 21:43:21291 return parse_funcname(&drv->funcname, k, v, 0);
Jeff King0a5987f2013-01-23 06:25:07292 if (!strcmp(type, "xfuncname"))
Jeff Kingbe58e702008-10-05 21:43:21293 return parse_funcname(&drv->funcname, k, v, REG_EXTENDED);
Jeff King0a5987f2013-01-23 06:25:07294 if (!strcmp(type, "binary"))
Jeff King122aa6f2008-10-05 21:43:36295 return parse_tristate(&drv->binary, k, v);
Jeff King0a5987f2013-01-23 06:25:07296 if (!strcmp(type, "command"))
Jeff King6680a082012-02-07 18:23:02297 return git_config_string(&drv->external, k, v);
Jeff King0a5987f2013-01-23 06:25:07298 if (!strcmp(type, "textconv"))
Jeff King6680a082012-02-07 18:23:02299 return git_config_string(&drv->textconv, k, v);
Jeff King0a5987f2013-01-23 06:25:07300 if (!strcmp(type, "cachetextconv"))
Jeff Kingd9bae1a2010-04-02 00:12:15301 return parse_bool(&drv->textconv_want_cache, k, v);
Jeff King0a5987f2013-01-23 06:25:07302 if (!strcmp(type, "wordregex"))
Jeff King6680a082012-02-07 18:23:02303 return git_config_string(&drv->word_regex, k, v);
Jeff Kingbe58e702008-10-05 21:43:21304
305 return 0;
306}
307
Nguyễn Thái Ngọc Duy3b335762018-12-09 10:25:21308struct userdiff_driver *userdiff_find_by_name(const char *name)
309{
Jeff Kingbe58e702008-10-05 21:43:21310 int len = strlen(name);
311 return userdiff_find_by_namelen(name, len);
312}
313
Nguyễn Thái Ngọc Duyacd00ea2018-09-21 15:57:33314struct userdiff_driver *userdiff_find_by_path(struct index_state *istate,
315 const char *path)
Jeff Kingbe58e702008-10-05 21:43:21316{
Junio C Hamano2aef63d2017-01-28 02:01:57317 static struct attr_check *check;
Jeff Kingbe58e702008-10-05 21:43:21318
Junio C Hamano2aef63d2017-01-28 02:01:57319 if (!check)
320 check = attr_check_initl("diff", NULL);
Jeff Kingbe58e702008-10-05 21:43:21321 if (!path)
322 return NULL;
Junio C Hamano11877b92018-10-19 04:34:02323 git_check_attr(istate, path, check);
Jeff Kingbe58e702008-10-05 21:43:21324
Junio C Hamano2aef63d2017-01-28 02:01:57325 if (ATTR_TRUE(check->items[0].value))
Jeff Kingbe58e702008-10-05 21:43:21326 return &driver_true;
Junio C Hamano2aef63d2017-01-28 02:01:57327 if (ATTR_FALSE(check->items[0].value))
Jeff Kingbe58e702008-10-05 21:43:21328 return &driver_false;
Junio C Hamano2aef63d2017-01-28 02:01:57329 if (ATTR_UNSET(check->items[0].value))
Jeff Kingbe58e702008-10-05 21:43:21330 return NULL;
Junio C Hamano2aef63d2017-01-28 02:01:57331 return userdiff_find_by_name(check->items[0].value);
Jeff Kingbe58e702008-10-05 21:43:21332}
Jeff King3813e692011-05-23 20:30:14333
Nguyễn Thái Ngọc Duybd7ad452018-11-10 05:49:06334struct userdiff_driver *userdiff_get_textconv(struct repository *r,
335 struct userdiff_driver *driver)
Jeff King3813e692011-05-23 20:30:14336{
337 if (!driver->textconv)
338 return NULL;
339
340 if (driver->textconv_want_cache && !driver->textconv_cache) {
341 struct notes_cache *c = xmalloc(sizeof(*c));
342 struct strbuf name = STRBUF_INIT;
343
344 strbuf_addf(&name, "textconv/%s", driver->name);
Nguyễn Thái Ngọc Duybd7ad452018-11-10 05:49:06345 notes_cache_init(r, c, name.buf, driver->textconv);
Jeff King3813e692011-05-23 20:30:14346 driver->textconv_cache = c;
Rene Scharfe460c7eb2017-08-30 18:20:15347 strbuf_release(&name);
Jeff King3813e692011-05-23 20:30:14348 }
349
350 return driver;
351}