blob: efbe05e5a5b6f1aa9a6b92365a4f987f6078dc04 [file] [log] [blame]
Jeff Kingd9bae1a2010-04-01 20:12:15 -04001#include "cache.h"
Brandon Williamsb2141fc2017-06-14 11:07:36 -07002#include "config.h"
Jeff Kingbe58e702008-10-05 17:43:21 -04003#include "userdiff.h"
Jeff Kingbe58e702008-10-05 17:43:21 -04004#include "attr.h"
5
6static struct userdiff_driver *drivers;
7static int ndrivers;
8static int drivers_alloc;
9
Boyd Stephen Smith Jrae3b9702009-01-20 22:59:54 -060010#define PATTERNS(name, pattern, word_regex) \
Jonathan Nieder664d44e2011-01-11 15:48:50 -060011 { name, NULL, -1, { pattern, REG_EXTENDED }, \
12 word_regex "|[^[:space:]]|[\xc0-\xff][\x80-\xbf]+" }
Brandon Casey909a5492010-09-10 11:18:14 -050013#define IPATTERN(name, pattern, word_regex) \
Jonathan Nieder664d44e2011-01-11 15:48:50 -060014 { name, NULL, -1, { pattern, REG_EXTENDED | REG_ICASE }, \
15 word_regex "|[^[:space:]]|[\xc0-\xff][\x80-\xbf]+" }
Jeff Kingbe58e702008-10-05 17:43:21 -040016static struct userdiff_driver builtin_drivers[] = {
Adrian Johnsone90d0652012-09-16 13:24:15 +093017IPATTERN("ada",
Adrian Johnson39a87a22014-02-03 22:03:16 +103018 "!^(.*[ \t])?(is[ \t]+new|renames|is[ \t]+separate)([ \t].*)?$\n"
Adrian Johnsone90d0652012-09-16 13:24:15 +093019 "!^[ \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 22:03:16 +103024 "|[-+]?[0-9][0-9#_.aAbBcCdDeEfF]*([eE][+-]?[0-9_]+)?"
Adrian Johnsone90d0652012-09-16 13:24:15 +093025 "|=>|\\.\\.|\\*\\*|:=|/=|>=|<=|<<|>>|<>"),
Stephen Boyd3c817602019-08-19 14:22:43 -070026PATTERNS("dts",
27 "!;\n"
Stephen Boyd8da56a42019-10-20 11:52:30 -070028 "!=\n"
Stephen Boyd3c817602019-08-19 14:22:43 -070029 /* lines beginning with a word optionally preceded by '&' or the root */
Stephen Boyd8da56a42019-10-20 11:52:30 -070030 "^[ \t]*((/[ \t]*\\{|&?[a-zA-Z_]).*)",
Stephen Boyd3c817602019-08-19 14:22:43 -070031 /* -- */
32 /* Property names and math operators */
33 "[a-zA-Z0-9,._+?#-]+"
34 "|[-+*/%&^|!~]|>>|<<|&&|\\|\\|"),
Łukasz Niemiera8072002019-11-08 22:38:24 +010035PATTERNS("elixir",
36 "^[ \t]*((def(macro|module|impl|protocol|p)?|test)[ \t].*)$",
Ed Masted1b13842019-12-13 17:55:35 +000037 /* -- */
Łukasz Niemiera8072002019-11-08 22:38:24 +010038 /* Atoms, names, and module attributes */
Ed Masted1b13842019-12-13 17:55:35 +000039 "[@:]?[a-zA-Z0-9@_?!]+"
Łukasz Niemiera8072002019-11-08 22:38:24 +010040 /* 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 11:18:14 -050048IPATTERN("fortran",
49 "!^([C*]|[ \t]*!)\n"
50 "!^[ \t]*MODULE[ \t]+PROCEDURE[ \t]\n"
51 "^[ \t]*((END[ \t]+)?(PROGRAM|MODULE|BLOCK[ \t]+DATA"
52 "|([^'\" \t]+[ \t]+)*(SUBROUTINE|FUNCTION))[ \t]+[A-Z].*)$",
53 /* -- */
54 "[a-zA-Z][a-zA-Z0-9_]*"
55 "|\\.([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])\\."
56 /* numbers and format statements like 2E14.4, or ES12.6, 9X.
57 * Don't worry about format statements without leading digits since
58 * they would have been matched above as a variable anyway. */
59 "|[-+]?[0-9.]+([AaIiDdEeFfLlTtXx][Ss]?[-+]?[0-9.]*)?(_[a-zA-Z0-9][a-zA-Z0-9_]*)?"
Jonathan Nieder664d44e2011-01-11 15:48:50 -060060 "|//|\\*\\*|::|[/<>=]="),
Zoë Blade69f9c872015-07-21 14:22:46 +010061IPATTERN("fountain", "^((\\.[^.]|(int|ext|est|int\\.?/ext|i/e)[. ]).*)$",
62 "[^ \t-]+"),
Alban Gruin1dbf0c02018-03-01 12:19:07 +010063PATTERNS("golang",
64 /* Functions */
65 "^[ \t]*(func[ \t]*.*(\\{[ \t]*)?)\n"
66 /* Structs and interfaces */
67 "^[ \t]*(type[ \t].*(struct|interface)[ \t]*(\\{[ \t]*)?)",
68 /* -- */
69 "[a-zA-Z_][a-zA-Z0-9_]*"
70 "|[-+0-9.eE]+i?|0[xX]?[0-9a-fA-F]+i?"
71 "|[-+*/<>%&^|=!:]=|--|\\+\\+|<<=?|>>=?|&\\^=?|&&|\\|\\||<-|\\.{3}"),
Ilya Kantor9c03cac2017-09-23 10:53:47 +030072PATTERNS("html", "^[ \t]*(<[Hh][1-6]([ \t].*)?>.*)$",
Jonathan Nieder664d44e2011-01-11 15:48:50 -060073 "[^<>= \t]+"),
Thomas Rast80c49c32009-01-17 17:29:48 +010074PATTERNS("java",
Jeff Kingbe58e702008-10-05 17:43:21 -040075 "!^[ \t]*(catch|do|for|if|instanceof|new|return|switch|throw|while)\n"
Paolo Bonzini959e2e62009-06-17 16:26:06 +020076 "^[ \t]*(([A-Za-z_][A-Za-z_0-9]*[ \t]+)+[A-Za-z_][A-Za-z_0-9]*[ \t]*\\([^;]*)$",
77 /* -- */
Thomas Rast80c49c32009-01-17 17:29:48 +010078 "[a-zA-Z_][a-zA-Z0-9_]*"
79 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?"
80 "|[-+*/<>%&^|=!]="
Jonathan Nieder664d44e2011-01-11 15:48:50 -060081 "|--|\\+\\+|<<=?|>>>?=?|&&|\\|\\|"),
Gustaf Hendeby53b10a12011-11-15 21:15:03 +010082PATTERNS("matlab",
Boxuan Li2731a782019-05-30 00:15:39 +080083 /*
84 * Octave pattern is mostly the same as matlab, except that '%%%' and
Boxuan Li91bf3822019-05-18 11:46:23 +080085 * '##' can also be used to begin code sections, in addition to '%%'
Boxuan Li2731a782019-05-30 00:15:39 +080086 * that is understood by both.
87 */
Boxuan Li91bf3822019-05-18 11:46:23 +080088 "^[[:space:]]*((classdef|function)[[:space:]].*)$|^(%%%?|##)[[:space:]].*$",
Gustaf Hendeby53b10a12011-11-15 21:15:03 +010089 "[a-zA-Z_][a-zA-Z0-9_]*|[-+0-9.e]+|[=~<>]=|\\.[*/\\^']|\\|\\||&&"),
Thomas Rast80c49c32009-01-17 17:29:48 +010090PATTERNS("objc",
Jeff Kingbe58e702008-10-05 17:43:21 -040091 /* Negate C statements that can look like functions */
92 "!^[ \t]*(do|for|if|else|return|switch|while)\n"
93 /* Objective-C methods */
94 "^[ \t]*([-+][ \t]*\\([ \t]*[A-Za-z_][A-Za-z_0-9* \t]*\\)[ \t]*[A-Za-z_].*)$\n"
95 /* C functions */
Paolo Bonzini959e2e62009-06-17 16:26:06 +020096 "^[ \t]*(([A-Za-z_][A-Za-z_0-9]*[ \t]+)+[A-Za-z_][A-Za-z_0-9]*[ \t]*\\([^;]*)$\n"
Jeff Kingbe58e702008-10-05 17:43:21 -040097 /* Objective-C class/protocol definitions */
Thomas Rast80c49c32009-01-17 17:29:48 +010098 "^(@(implementation|interface|protocol)[ \t].*)$",
99 /* -- */
100 "[a-zA-Z_][a-zA-Z0-9_]*"
101 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?"
Jonathan Nieder664d44e2011-01-11 15:48:50 -0600102 "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->"),
Thomas Rast80c49c32009-01-17 17:29:48 +0100103PATTERNS("pascal",
Alexey Shumkinad5b6942011-01-11 11:53:59 +0300104 "^(((class[ \t]+)?(procedure|function)|constructor|destructor|interface|"
Jeff Kingbe58e702008-10-05 17:43:21 -0400105 "implementation|initialization|finalization)[ \t]*.*)$"
106 "\n"
Thomas Rast80c49c32009-01-17 17:29:48 +0100107 "^(.*=[ \t]*(class|record).*)$",
108 /* -- */
109 "[a-zA-Z_][a-zA-Z0-9_]*"
110 "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+"
Jonathan Nieder664d44e2011-01-11 15:48:50 -0600111 "|<>|<=|>=|:=|\\.\\."),
Jonathan Nieder71a5d4b2010-12-26 03:07:31 -0600112PATTERNS("perl",
Jonathan Niederea2ca442011-05-21 14:38:26 -0500113 "^package .*\n"
114 "^sub [[:alnum:]_':]+[ \t]*"
115 "(\\([^)]*\\)[ \t]*)?" /* prototype */
116 /*
117 * Attributes. A regex can't count nested parentheses,
118 * so just slurp up whatever we see, taking care not
119 * to accept lines like "sub foo; # defined elsewhere".
120 *
121 * An attribute could contain a semicolon, but at that
122 * point it seems reasonable enough to give up.
123 */
124 "(:[^;#]*)?"
125 "(\\{[ \t]*)?" /* brace can come here or on the next line */
126 "(#.*)?$\n" /* comment */
Jonathan Niederf143d9c2011-05-22 12:29:32 -0500127 "^(BEGIN|END|INIT|CHECK|UNITCHECK|AUTOLOAD|DESTROY)[ \t]*"
Jonathan Niederea2ca442011-05-21 14:38:26 -0500128 "(\\{[ \t]*)?" /* brace can come here or on the next line */
129 "(#.*)?$\n"
Jonathan Nieder12f09672011-05-21 14:35:51 -0500130 "^=head[0-9] .*", /* POD */
Jonathan Nieder71a5d4b2010-12-26 03:07:31 -0600131 /* -- */
132 "[[:alpha:]_'][[:alnum:]_']*"
133 "|0[xb]?[0-9a-fA-F_]*"
134 /* taking care not to interpret 3..5 as (3.)(.5) */
135 "|[0-9a-fA-F_]+(\\.[0-9a-fA-F_]+)?([eE][-+]?[0-9_]+)?"
136 "|=>|-[rwxoRWXOezsfdlpSugkbctTBMAC>]|~~|::"
137 "|&&=|\\|\\|=|//=|\\*\\*="
138 "|&&|\\|\\||//|\\+\\+|--|\\*\\*|\\.\\.\\.?"
139 "|[-+*/%.^&<>=!|]="
140 "|=~|!~"
Jonathan Nieder664d44e2011-01-11 15:48:50 -0600141 "|<<|<>|<=>|>>"),
Björn Steinbrink6d2f2082010-05-23 20:05:40 +0200142PATTERNS("php",
143 "^[\t ]*(((public|protected|private|static)[\t ]+)*function.*)$\n"
Kana Natsuno1ab63162018-07-03 22:15:40 +0900144 "^[\t ]*((((final|abstract)[\t ]+)?class|interface|trait).*)$",
Thomas Rast80c49c32009-01-17 17:29:48 +0100145 /* -- */
146 "[a-zA-Z_][a-zA-Z0-9_]*"
147 "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+"
Jonathan Nieder664d44e2011-01-11 15:48:50 -0600148 "|[-+*/<>%&^|=!.]=|--|\\+\\+|<<=?|>>=?|===|&&|\\|\\||::|->"),
Josh Holland077a1fd2019-11-19 15:08:10 +0000149PATTERNS("python", "^[ \t]*((class|(async[ \t]+)?def)[ \t].*)$",
Thomas Rast80c49c32009-01-17 17:29:48 +0100150 /* -- */
151 "[a-zA-Z_][a-zA-Z0-9_]*"
152 "|[-+0-9.e]+[jJlL]?|0[xX]?[0-9a-fA-F]+[lL]?"
Jonathan Nieder664d44e2011-01-11 15:48:50 -0600153 "|[-+*/<>%&^|=!]=|//=?|<<=?|>>=?|\\*\\*=?"),
Thomas Rast80c49c32009-01-17 17:29:48 +0100154 /* -- */
155PATTERNS("ruby", "^[ \t]*((class|module|def)[ \t].*)$",
156 /* -- */
157 "(@|@@|\\$)?[a-zA-Z_][a-zA-Z0-9_]*"
158 "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+|\\?(\\\\C-)?(\\\\M-)?."
Jonathan Nieder664d44e2011-01-11 15:48:50 -0600159 "|//=?|[-+*/<>%&^|=!]=|<<=?|>>=?|===|\\.{1,3}|::|[!=]~"),
Marc-André Lureaud74e7862019-05-17 01:58:15 +0200160PATTERNS("rust",
Johannes Sixt33be7b32019-05-30 18:44:35 +0200161 "^[\t ]*((pub(\\([^\\)]+\\))?[\t ]+)?((async|const|unsafe|extern([\t ]+\"[^\"]+\"))[\t ]+)?(struct|enum|union|mod|trait|fn|impl)[< \t]+[^;]*)$",
Marc-André Lureaud74e7862019-05-17 01:58:15 +0200162 /* -- */
163 "[a-zA-Z_][a-zA-Z0-9_]*"
Johannes Sixt33be7b32019-05-30 18:44:35 +0200164 "|[0-9][0-9_a-fA-Fiosuxz]*(\\.([0-9]*[eE][+-]?)?[0-9_fF]*)?"
Marc-André Lureaud74e7862019-05-17 01:58:15 +0200165 "|[-+*\\/<>%&^|=!:]=|<<=?|>>=?|&&|\\|\\||->|=>|\\.{2}=|\\.{3}|::"),
Thomas Rast80c49c32009-01-17 17:29:48 +0100166PATTERNS("bibtex", "(@[a-zA-Z]{1,}[ \t]*\\{{0,1}[ \t]*[^ \t\"@',\\#}{~%]*).*$",
167 "[={}\"]|[^={}\" \t]+"),
168PATTERNS("tex", "^(\\\\((sub)*section|chapter|part)\\*{0,1}\\{.*)$",
Jonathan Nieder664d44e2011-01-11 15:48:50 -0600169 "\\\\[a-zA-Z@]+|\\\\.|[a-zA-Z0-9\x80-\xff]+"),
Thomas Rast80c49c32009-01-17 17:29:48 +0100170PATTERNS("cpp",
171 /* Jump targets or access declarations */
Johannes Sixt8a2e8da2014-03-21 22:07:22 +0100172 "!^[ \t]*[A-Za-z_][A-Za-z_0-9]*:[[:space:]]*($|/[/*])\n"
173 /* functions/methods, variables, and compounds at top level */
174 "^((::[[:space:]]*)?[A-Za-z_].*)$",
Thomas Rast80c49c32009-01-17 17:29:48 +0100175 /* -- */
176 "[a-zA-Z_][a-zA-Z0-9_]*"
Johannes Sixtabf8f982014-03-21 22:07:14 +0100177 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lLuU]*"
Johannes Sixt407e07f2014-03-21 22:07:13 +0100178 "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->\\*?|\\.\\*"),
Petr Onderkab2212072010-08-16 17:01:02 +0000179PATTERNS("csharp",
180 /* Keywords */
181 "!^[ \t]*(do|while|for|if|else|instanceof|new|return|switch|case|throw|catch|using)\n"
182 /* Methods and constructors */
Thomas Levesquea12cec92018-03-08 11:05:32 +0000183 "^[ \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:02 +0000184 /* Properties */
185 "^[ \t]*(((static|public|internal|private|protected|new|virtual|sealed|override|unsafe)[ \t]+)*[][<>@.~_[:alnum:]]+[ \t]+[@._[:alnum:]]+)[ \t]*$\n"
186 /* Type definitions */
187 "^[ \t]*(((static|public|internal|private|protected|new|unsafe|sealed|abstract|partial)[ \t]+)*(class|enum|interface|struct)[ \t]+.*)$\n"
188 /* Namespace */
189 "^[ \t]*(namespace[ \t]+.*)$",
190 /* -- */
191 "[a-zA-Z_][a-zA-Z0-9_]*"
192 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?"
Jonathan Nieder664d44e2011-01-11 15:48:50 -0600193 "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->"),
William Duclot0719f3e2016-06-03 14:32:26 +0200194IPATTERN("css",
195 "![:;][[:space:]]*$\n"
196 "^[_a-z0-9].*$",
197 /* -- */
198 /*
199 * This regex comes from W3C CSS specs. Should theoretically also
200 * allow ISO 10646 characters U+00A0 and higher,
201 * but they are not handled in this regex.
202 */
203 "-?[_a-zA-Z][-_a-zA-Z0-9]*" /* identifiers */
204 "|-?[0-9]+|\\#[0-9a-fA-F]+" /* numbers */
205),
Jeff King122aa6f2008-10-05 17:43:36 -0400206{ "default", NULL, -1, { NULL, 0 } },
Jeff Kingbe58e702008-10-05 17:43:21 -0400207};
Thomas Rast80c49c32009-01-17 17:29:48 +0100208#undef PATTERNS
Brandon Casey909a5492010-09-10 11:18:14 -0500209#undef IPATTERN
Jeff Kingbe58e702008-10-05 17:43:21 -0400210
211static struct userdiff_driver driver_true = {
212 "diff=true",
213 NULL,
Jeff King122aa6f2008-10-05 17:43:36 -0400214 0,
Jeff Kingbe58e702008-10-05 17:43:21 -0400215 { NULL, 0 }
216};
Jeff Kingbe58e702008-10-05 17:43:21 -0400217
218static struct userdiff_driver driver_false = {
219 "!diff",
220 NULL,
Jeff King122aa6f2008-10-05 17:43:36 -0400221 1,
Jeff Kingbe58e702008-10-05 17:43:21 -0400222 { NULL, 0 }
223};
Jeff Kingbe58e702008-10-05 17:43:21 -0400224
225static struct userdiff_driver *userdiff_find_by_namelen(const char *k, int len)
226{
227 int i;
228 for (i = 0; i < ndrivers; i++) {
229 struct userdiff_driver *drv = drivers + i;
230 if (!strncmp(drv->name, k, len) && !drv->name[len])
231 return drv;
232 }
233 for (i = 0; i < ARRAY_SIZE(builtin_drivers); i++) {
234 struct userdiff_driver *drv = builtin_drivers + i;
235 if (!strncmp(drv->name, k, len) && !drv->name[len])
236 return drv;
237 }
238 return NULL;
239}
240
Jeff Kingbe58e702008-10-05 17:43:21 -0400241static int parse_funcname(struct userdiff_funcname *f, const char *k,
242 const char *v, int cflags)
243{
244 if (git_config_string(&f->pattern, k, v) < 0)
245 return -1;
246 f->cflags = cflags;
Jeff King6680a082012-02-07 13:23:02 -0500247 return 0;
Jeff Kingbe58e702008-10-05 17:43:21 -0400248}
249
Jeff King122aa6f2008-10-05 17:43:36 -0400250static int parse_tristate(int *b, const char *k, const char *v)
251{
252 if (v && !strcasecmp(v, "auto"))
253 *b = -1;
254 else
255 *b = git_config_bool(k, v);
Jeff King6680a082012-02-07 13:23:02 -0500256 return 0;
Jeff King122aa6f2008-10-05 17:43:36 -0400257}
258
Jeff Kingd9bae1a2010-04-01 20:12:15 -0400259static int parse_bool(int *b, const char *k, const char *v)
260{
261 *b = git_config_bool(k, v);
Jeff King6680a082012-02-07 13:23:02 -0500262 return 0;
Jeff Kingd9bae1a2010-04-01 20:12:15 -0400263}
264
Jeff Kingc7534ef2008-10-26 00:45:55 -0400265int userdiff_config(const char *k, const char *v)
Jeff Kingbe58e702008-10-05 17:43:21 -0400266{
267 struct userdiff_driver *drv;
Jeff King0a5987f2013-01-23 01:25:07 -0500268 const char *name, *type;
269 int namelen;
Jeff Kingbe58e702008-10-05 17:43:21 -0400270
Jeff King0a5987f2013-01-23 01:25:07 -0500271 if (parse_config_key(k, "diff", &name, &namelen, &type) || !name)
272 return 0;
273
274 drv = userdiff_find_by_namelen(name, namelen);
275 if (!drv) {
276 ALLOC_GROW(drivers, ndrivers+1, drivers_alloc);
277 drv = &drivers[ndrivers++];
278 memset(drv, 0, sizeof(*drv));
279 drv->name = xmemdupz(name, namelen);
280 drv->binary = -1;
281 }
282
283 if (!strcmp(type, "funcname"))
Jeff Kingbe58e702008-10-05 17:43:21 -0400284 return parse_funcname(&drv->funcname, k, v, 0);
Jeff King0a5987f2013-01-23 01:25:07 -0500285 if (!strcmp(type, "xfuncname"))
Jeff Kingbe58e702008-10-05 17:43:21 -0400286 return parse_funcname(&drv->funcname, k, v, REG_EXTENDED);
Jeff King0a5987f2013-01-23 01:25:07 -0500287 if (!strcmp(type, "binary"))
Jeff King122aa6f2008-10-05 17:43:36 -0400288 return parse_tristate(&drv->binary, k, v);
Jeff King0a5987f2013-01-23 01:25:07 -0500289 if (!strcmp(type, "command"))
Jeff King6680a082012-02-07 13:23:02 -0500290 return git_config_string(&drv->external, k, v);
Jeff King0a5987f2013-01-23 01:25:07 -0500291 if (!strcmp(type, "textconv"))
Jeff King6680a082012-02-07 13:23:02 -0500292 return git_config_string(&drv->textconv, k, v);
Jeff King0a5987f2013-01-23 01:25:07 -0500293 if (!strcmp(type, "cachetextconv"))
Jeff Kingd9bae1a2010-04-01 20:12:15 -0400294 return parse_bool(&drv->textconv_want_cache, k, v);
Jeff King0a5987f2013-01-23 01:25:07 -0500295 if (!strcmp(type, "wordregex"))
Jeff King6680a082012-02-07 13:23:02 -0500296 return git_config_string(&drv->word_regex, k, v);
Jeff Kingbe58e702008-10-05 17:43:21 -0400297
298 return 0;
299}
300
Nguyễn Thái Ngọc Duy3b335762018-12-09 11:25:21 +0100301struct userdiff_driver *userdiff_find_by_name(const char *name)
302{
Jeff Kingbe58e702008-10-05 17:43:21 -0400303 int len = strlen(name);
304 return userdiff_find_by_namelen(name, len);
305}
306
Nguyễn Thái Ngọc Duyacd00ea2018-09-21 17:57:33 +0200307struct userdiff_driver *userdiff_find_by_path(struct index_state *istate,
308 const char *path)
Jeff Kingbe58e702008-10-05 17:43:21 -0400309{
Junio C Hamano2aef63d2017-01-27 18:01:57 -0800310 static struct attr_check *check;
Jeff Kingbe58e702008-10-05 17:43:21 -0400311
Junio C Hamano2aef63d2017-01-27 18:01:57 -0800312 if (!check)
313 check = attr_check_initl("diff", NULL);
Jeff Kingbe58e702008-10-05 17:43:21 -0400314 if (!path)
315 return NULL;
Junio C Hamano11877b92018-10-19 13:34:02 +0900316 git_check_attr(istate, path, check);
Jeff Kingbe58e702008-10-05 17:43:21 -0400317
Junio C Hamano2aef63d2017-01-27 18:01:57 -0800318 if (ATTR_TRUE(check->items[0].value))
Jeff Kingbe58e702008-10-05 17:43:21 -0400319 return &driver_true;
Junio C Hamano2aef63d2017-01-27 18:01:57 -0800320 if (ATTR_FALSE(check->items[0].value))
Jeff Kingbe58e702008-10-05 17:43:21 -0400321 return &driver_false;
Junio C Hamano2aef63d2017-01-27 18:01:57 -0800322 if (ATTR_UNSET(check->items[0].value))
Jeff Kingbe58e702008-10-05 17:43:21 -0400323 return NULL;
Junio C Hamano2aef63d2017-01-27 18:01:57 -0800324 return userdiff_find_by_name(check->items[0].value);
Jeff Kingbe58e702008-10-05 17:43:21 -0400325}
Jeff King3813e692011-05-23 16:30:14 -0400326
Nguyễn Thái Ngọc Duybd7ad452018-11-10 06:49:06 +0100327struct userdiff_driver *userdiff_get_textconv(struct repository *r,
328 struct userdiff_driver *driver)
Jeff King3813e692011-05-23 16:30:14 -0400329{
330 if (!driver->textconv)
331 return NULL;
332
333 if (driver->textconv_want_cache && !driver->textconv_cache) {
334 struct notes_cache *c = xmalloc(sizeof(*c));
335 struct strbuf name = STRBUF_INIT;
336
337 strbuf_addf(&name, "textconv/%s", driver->name);
Nguyễn Thái Ngọc Duybd7ad452018-11-10 06:49:06 +0100338 notes_cache_init(r, c, name.buf, driver->textconv);
Jeff King3813e692011-05-23 16:30:14 -0400339 driver->textconv_cache = c;
Rene Scharfe460c7eb2017-08-30 20:20:15 +0200340 strbuf_release(&name);
Jeff King3813e692011-05-23 16:30:14 -0400341 }
342
343 return driver;
344}