blob: d9b2ba752f0885240ea1089ea90525de2cedf40c [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 "|=>|\\.\\.|\\*\\*|:=|/=|>=|<=|<<|>>|<>"),
Victor Engmark2ff6c342020-10-22 12:45:08 +130026PATTERNS("bash",
27 /* Optional leading indentation */
28 "^[ \t]*"
29 /* Start of captured text */
30 "("
31 "("
32 /* POSIX identifier with mandatory parentheses */
33 "[a-zA-Z_][a-zA-Z0-9_]*[ \t]*\\([ \t]*\\))"
34 "|"
35 /* Bashism identifier with optional parentheses */
36 "(function[ \t]+[a-zA-Z_][a-zA-Z0-9_]*(([ \t]*\\([ \t]*\\))|([ \t]+))"
37 ")"
38 /* Optional whitespace */
39 "[ \t]*"
40 /* Compound command starting with `{`, `(`, `((` or `[[` */
41 "(\\{|\\(\\(?|\\[\\[)"
42 /* End of captured text */
43 ")",
44 /* -- */
45 /* Characters not in the default $IFS value */
46 "[^ \t]+"),
Ævar Arnfjörð Bjarmason6d1c9c52021-04-08 17:04:17 +020047PATTERNS("bibtex",
48 "(@[a-zA-Z]{1,}[ \t]*\\{{0,1}[ \t]*[^ \t\"@',\\#}{~%]*).*$",
49 /* -- */
Ævar Arnfjörð Bjarmasonddd164d2021-04-08 17:04:16 +020050 "[={}\"]|[^={}\" \t]+"),
51PATTERNS("cpp",
52 /* Jump targets or access declarations */
53 "!^[ \t]*[A-Za-z_][A-Za-z_0-9]*:[[:space:]]*($|/[/*])\n"
54 /* functions/methods, variables, and compounds at top level */
55 "^((::[[:space:]]*)?[A-Za-z_].*)$",
56 /* -- */
57 "[a-zA-Z_][a-zA-Z0-9_]*"
58 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lLuU]*"
59 "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->\\*?|\\.\\*"),
60PATTERNS("csharp",
61 /* Keywords */
62 "!^[ \t]*(do|while|for|if|else|instanceof|new|return|switch|case|throw|catch|using)\n"
63 /* Methods and constructors */
64 "^[ \t]*(((static|public|internal|private|protected|new|virtual|sealed|override|unsafe|async)[ \t]+)*[][<>@.~_[:alnum:]]+[ \t]+[<>@._[:alnum:]]+[ \t]*\\(.*\\))[ \t]*$\n"
65 /* Properties */
66 "^[ \t]*(((static|public|internal|private|protected|new|virtual|sealed|override|unsafe)[ \t]+)*[][<>@.~_[:alnum:]]+[ \t]+[@._[:alnum:]]+)[ \t]*$\n"
67 /* Type definitions */
Julian Verdurmenc4e31782021-03-02 00:58:09 +000068 "^[ \t]*(((static|public|internal|private|protected|new|unsafe|sealed|abstract|partial)[ \t]+)*(class|enum|interface|struct|record)[ \t]+.*)$\n"
Ævar Arnfjörð Bjarmasonddd164d2021-04-08 17:04:16 +020069 /* Namespace */
70 "^[ \t]*(namespace[ \t]+.*)$",
71 /* -- */
72 "[a-zA-Z_][a-zA-Z0-9_]*"
73 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?"
74 "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->"),
75IPATTERN("css",
76 "![:;][[:space:]]*$\n"
77 "^[:[@.#]?[_a-z0-9].*$",
78 /* -- */
79 /*
80 * This regex comes from W3C CSS specs. Should theoretically also
81 * allow ISO 10646 characters U+00A0 and higher,
82 * but they are not handled in this regex.
83 */
84 "-?[_a-zA-Z][-_a-zA-Z0-9]*" /* identifiers */
85 "|-?[0-9]+|\\#[0-9a-fA-F]+" /* numbers */
86),
Stephen Boyd3c817602019-08-19 14:22:43 -070087PATTERNS("dts",
88 "!;\n"
Stephen Boyd8da56a42019-10-20 11:52:30 -070089 "!=\n"
Stephen Boyd3c817602019-08-19 14:22:43 -070090 /* lines beginning with a word optionally preceded by '&' or the root */
Stephen Boyd8da56a42019-10-20 11:52:30 -070091 "^[ \t]*((/[ \t]*\\{|&?[a-zA-Z_]).*)",
Stephen Boyd3c817602019-08-19 14:22:43 -070092 /* -- */
93 /* Property names and math operators */
94 "[a-zA-Z0-9,._+?#-]+"
95 "|[-+*/%&^|!~]|>>|<<|&&|\\|\\|"),
Łukasz Niemiera8072002019-11-08 22:38:24 +010096PATTERNS("elixir",
97 "^[ \t]*((def(macro|module|impl|protocol|p)?|test)[ \t].*)$",
Ed Masted1b13842019-12-13 17:55:35 +000098 /* -- */
Łukasz Niemiera8072002019-11-08 22:38:24 +010099 /* Atoms, names, and module attributes */
Ed Masted1b13842019-12-13 17:55:35 +0000100 "[@:]?[a-zA-Z0-9@_?!]+"
Łukasz Niemiera8072002019-11-08 22:38:24 +0100101 /* Numbers with specific base */
102 "|[-+]?0[xob][0-9a-fA-F]+"
103 /* Numbers */
104 "|[-+]?[0-9][0-9_.]*([eE][-+]?[0-9_]+)?"
105 /* Operators and atoms that represent them */
106 "|:?(\\+\\+|--|\\.\\.|~~~|<>|\\^\\^\\^|<?\\|>|<<<?|>?>>|<<?~|~>?>|<~>|<=|>=|===?|!==?|=~|&&&?|\\|\\|\\|?|=>|<-|\\\\\\\\|->)"
107 /* Not real operators, but should be grouped */
108 "|:?%[A-Za-z0-9_.]\\{\\}?"),
Brandon Casey909a5492010-09-10 11:18:14 -0500109IPATTERN("fortran",
Philippe Blainb79e6922020-08-12 22:30:28 +0000110 /* Don't match comment lines */
Brandon Casey909a5492010-09-10 11:18:14 -0500111 "!^([C*]|[ \t]*!)\n"
Philippe Blainb79e6922020-08-12 22:30:28 +0000112 /* Don't match 'module procedure' lines */
Brandon Casey909a5492010-09-10 11:18:14 -0500113 "!^[ \t]*MODULE[ \t]+PROCEDURE[ \t]\n"
Philippe Blainb79e6922020-08-12 22:30:28 +0000114 /* Program, module, block data */
Brandon Casey909a5492010-09-10 11:18:14 -0500115 "^[ \t]*((END[ \t]+)?(PROGRAM|MODULE|BLOCK[ \t]+DATA"
Philippe Blainb79e6922020-08-12 22:30:28 +0000116 /* Subroutines and functions */
Philippe Blain75c3b6b2020-08-12 22:30:29 +0000117 "|([^!'\" \t]+[ \t]+)*(SUBROUTINE|FUNCTION))[ \t]+[A-Z].*)$",
Brandon Casey909a5492010-09-10 11:18:14 -0500118 /* -- */
119 "[a-zA-Z][a-zA-Z0-9_]*"
120 "|\\.([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])\\."
121 /* numbers and format statements like 2E14.4, or ES12.6, 9X.
122 * Don't worry about format statements without leading digits since
123 * they would have been matched above as a variable anyway. */
124 "|[-+]?[0-9.]+([AaIiDdEeFfLlTtXx][Ss]?[-+]?[0-9.]*)?(_[a-zA-Z0-9][a-zA-Z0-9_]*)?"
Jonathan Nieder664d44e2011-01-11 15:48:50 -0600125 "|//|\\*\\*|::|[/<>=]="),
Ævar Arnfjörð Bjarmason6d1c9c52021-04-08 17:04:17 +0200126IPATTERN("fountain",
127 "^((\\.[^.]|(int|ext|est|int\\.?/ext|i/e)[. ]).*)$",
128 /* -- */
Zoë Blade69f9c872015-07-21 14:22:46 +0100129 "[^ \t-]+"),
Alban Gruin1dbf0c02018-03-01 12:19:07 +0100130PATTERNS("golang",
131 /* Functions */
132 "^[ \t]*(func[ \t]*.*(\\{[ \t]*)?)\n"
133 /* Structs and interfaces */
134 "^[ \t]*(type[ \t].*(struct|interface)[ \t]*(\\{[ \t]*)?)",
135 /* -- */
136 "[a-zA-Z_][a-zA-Z0-9_]*"
137 "|[-+0-9.eE]+i?|0[xX]?[0-9a-fA-F]+i?"
138 "|[-+*/<>%&^|=!:]=|--|\\+\\+|<<=?|>>=?|&\\^=?|&&|\\|\\||<-|\\.{3}"),
Ævar Arnfjörð Bjarmason6d1c9c52021-04-08 17:04:17 +0200139PATTERNS("html",
140 "^[ \t]*(<[Hh][1-6]([ \t].*)?>.*)$",
141 /* -- */
Jonathan Nieder664d44e2011-01-11 15:48:50 -0600142 "[^<>= \t]+"),
Thomas Rast80c49c32009-01-17 17:29:48 +0100143PATTERNS("java",
Jeff Kingbe58e702008-10-05 17:43:21 -0400144 "!^[ \t]*(catch|do|for|if|instanceof|new|return|switch|throw|while)\n"
Paolo Bonzini959e2e62009-06-17 16:26:06 +0200145 "^[ \t]*(([A-Za-z_][A-Za-z_0-9]*[ \t]+)+[A-Za-z_][A-Za-z_0-9]*[ \t]*\\([^;]*)$",
146 /* -- */
Thomas Rast80c49c32009-01-17 17:29:48 +0100147 "[a-zA-Z_][a-zA-Z0-9_]*"
148 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?"
149 "|[-+*/<>%&^|=!]="
Jonathan Nieder664d44e2011-01-11 15:48:50 -0600150 "|--|\\+\\+|<<=?|>>>?=?|&&|\\|\\|"),
Ash Holland09dad922020-05-02 14:15:43 +0100151PATTERNS("markdown",
152 "^ {0,3}#{1,6}[ \t].*",
Ævar Arnfjörð Bjarmason6d1c9c52021-04-08 17:04:17 +0200153 /* -- */
Ash Holland09dad922020-05-02 14:15:43 +0100154 "[^<>= \t]+"),
Gustaf Hendeby53b10a12011-11-15 21:15:03 +0100155PATTERNS("matlab",
Boxuan Li2731a782019-05-30 00:15:39 +0800156 /*
157 * Octave pattern is mostly the same as matlab, except that '%%%' and
Boxuan Li91bf3822019-05-18 11:46:23 +0800158 * '##' can also be used to begin code sections, in addition to '%%'
Boxuan Li2731a782019-05-30 00:15:39 +0800159 * that is understood by both.
160 */
Boxuan Li91bf3822019-05-18 11:46:23 +0800161 "^[[:space:]]*((classdef|function)[[:space:]].*)$|^(%%%?|##)[[:space:]].*$",
Ævar Arnfjörð Bjarmason6d1c9c52021-04-08 17:04:17 +0200162 /* -- */
Gustaf Hendeby53b10a12011-11-15 21:15:03 +0100163 "[a-zA-Z_][a-zA-Z0-9_]*|[-+0-9.e]+|[=~<>]=|\\.[*/\\^']|\\|\\||&&"),
Thomas Rast80c49c32009-01-17 17:29:48 +0100164PATTERNS("objc",
Jeff Kingbe58e702008-10-05 17:43:21 -0400165 /* Negate C statements that can look like functions */
166 "!^[ \t]*(do|for|if|else|return|switch|while)\n"
167 /* Objective-C methods */
168 "^[ \t]*([-+][ \t]*\\([ \t]*[A-Za-z_][A-Za-z_0-9* \t]*\\)[ \t]*[A-Za-z_].*)$\n"
169 /* C functions */
Paolo Bonzini959e2e62009-06-17 16:26:06 +0200170 "^[ \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 -0400171 /* Objective-C class/protocol definitions */
Thomas Rast80c49c32009-01-17 17:29:48 +0100172 "^(@(implementation|interface|protocol)[ \t].*)$",
173 /* -- */
174 "[a-zA-Z_][a-zA-Z0-9_]*"
175 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?"
Jonathan Nieder664d44e2011-01-11 15:48:50 -0600176 "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->"),
Thomas Rast80c49c32009-01-17 17:29:48 +0100177PATTERNS("pascal",
Ævar Arnfjörð Bjarmason82512e02021-04-08 17:04:18 +0200178 "^(((class[ \t]+)?(procedure|function)|constructor|destructor|interface"
179 "|implementation|initialization|finalization)[ \t]*.*)$\n"
Thomas Rast80c49c32009-01-17 17:29:48 +0100180 "^(.*=[ \t]*(class|record).*)$",
181 /* -- */
182 "[a-zA-Z_][a-zA-Z0-9_]*"
183 "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+"
Jonathan Nieder664d44e2011-01-11 15:48:50 -0600184 "|<>|<=|>=|:=|\\.\\."),
Jonathan Nieder71a5d4b2010-12-26 03:07:31 -0600185PATTERNS("perl",
Jonathan Niederea2ca442011-05-21 14:38:26 -0500186 "^package .*\n"
187 "^sub [[:alnum:]_':]+[ \t]*"
188 "(\\([^)]*\\)[ \t]*)?" /* prototype */
189 /*
190 * Attributes. A regex can't count nested parentheses,
191 * so just slurp up whatever we see, taking care not
192 * to accept lines like "sub foo; # defined elsewhere".
193 *
194 * An attribute could contain a semicolon, but at that
195 * point it seems reasonable enough to give up.
196 */
197 "(:[^;#]*)?"
198 "(\\{[ \t]*)?" /* brace can come here or on the next line */
199 "(#.*)?$\n" /* comment */
Jonathan Niederf143d9c2011-05-22 12:29:32 -0500200 "^(BEGIN|END|INIT|CHECK|UNITCHECK|AUTOLOAD|DESTROY)[ \t]*"
Jonathan Niederea2ca442011-05-21 14:38:26 -0500201 "(\\{[ \t]*)?" /* brace can come here or on the next line */
202 "(#.*)?$\n"
Jonathan Nieder12f09672011-05-21 14:35:51 -0500203 "^=head[0-9] .*", /* POD */
Jonathan Nieder71a5d4b2010-12-26 03:07:31 -0600204 /* -- */
205 "[[:alpha:]_'][[:alnum:]_']*"
206 "|0[xb]?[0-9a-fA-F_]*"
207 /* taking care not to interpret 3..5 as (3.)(.5) */
208 "|[0-9a-fA-F_]+(\\.[0-9a-fA-F_]+)?([eE][-+]?[0-9_]+)?"
209 "|=>|-[rwxoRWXOezsfdlpSugkbctTBMAC>]|~~|::"
210 "|&&=|\\|\\|=|//=|\\*\\*="
211 "|&&|\\|\\||//|\\+\\+|--|\\*\\*|\\.\\.\\.?"
212 "|[-+*/%.^&<>=!|]="
213 "|=~|!~"
Jonathan Nieder664d44e2011-01-11 15:48:50 -0600214 "|<<|<>|<=>|>>"),
Björn Steinbrink6d2f2082010-05-23 20:05:40 +0200215PATTERNS("php",
Javier Spagnolettiaff92822020-10-07 03:38:18 +0000216 "^[\t ]*(((public|protected|private|static|abstract|final)[\t ]+)*function.*)$\n"
Kana Natsuno1ab63162018-07-03 22:15:40 +0900217 "^[\t ]*((((final|abstract)[\t ]+)?class|interface|trait).*)$",
Thomas Rast80c49c32009-01-17 17:29:48 +0100218 /* -- */
219 "[a-zA-Z_][a-zA-Z0-9_]*"
220 "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+"
Jonathan Nieder664d44e2011-01-11 15:48:50 -0600221 "|[-+*/<>%&^|=!.]=|--|\\+\\+|<<=?|>>=?|===|&&|\\|\\||::|->"),
Ævar Arnfjörð Bjarmason6d1c9c52021-04-08 17:04:17 +0200222PATTERNS("python",
223 "^[ \t]*((class|(async[ \t]+)?def)[ \t].*)$",
Thomas Rast80c49c32009-01-17 17:29:48 +0100224 /* -- */
225 "[a-zA-Z_][a-zA-Z0-9_]*"
226 "|[-+0-9.e]+[jJlL]?|0[xX]?[0-9a-fA-F]+[lL]?"
Jonathan Nieder664d44e2011-01-11 15:48:50 -0600227 "|[-+*/<>%&^|=!]=|//=?|<<=?|>>=?|\\*\\*=?"),
Thomas Rast80c49c32009-01-17 17:29:48 +0100228 /* -- */
Ævar Arnfjörð Bjarmason6d1c9c52021-04-08 17:04:17 +0200229PATTERNS("ruby",
230 "^[ \t]*((class|module|def)[ \t].*)$",
Thomas Rast80c49c32009-01-17 17:29:48 +0100231 /* -- */
232 "(@|@@|\\$)?[a-zA-Z_][a-zA-Z0-9_]*"
233 "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+|\\?(\\\\C-)?(\\\\M-)?."
Jonathan Nieder664d44e2011-01-11 15:48:50 -0600234 "|//=?|[-+*/<>%&^|=!]=|<<=?|>>=?|===|\\.{1,3}|::|[!=]~"),
Marc-André Lureaud74e7862019-05-17 01:58:15 +0200235PATTERNS("rust",
Konrad Borowskia04c7e02020-10-07 13:26:11 +0000236 "^[\t ]*((pub(\\([^\\)]+\\))?[\t ]+)?((async|const|unsafe|extern([\t ]+\"[^\"]+\"))[\t ]+)?(struct|enum|union|mod|trait|fn|impl|macro_rules!)[< \t]+[^;]*)$",
Marc-André Lureaud74e7862019-05-17 01:58:15 +0200237 /* -- */
238 "[a-zA-Z_][a-zA-Z0-9_]*"
Johannes Sixt33be7b32019-05-30 18:44:35 +0200239 "|[0-9][0-9_a-fA-Fiosuxz]*(\\.([0-9]*[eE][+-]?)?[0-9_fF]*)?"
Marc-André Lureaud74e7862019-05-17 01:58:15 +0200240 "|[-+*\\/<>%&^|=!:]=|<<=?|>>=?|&&|\\|\\||->|=>|\\.{2}=|\\.{3}|::"),
Atharva Raykara4373902021-04-08 14:44:43 +0530241PATTERNS("scheme",
242 "^[\t ]*(\\(((define|def(struct|syntax|class|method|rules|record|proto|alias)?)[-*/ \t]|(library|module|struct|class)[*+ \t]).*)$",
243 /*
244 * R7RS valid identifiers include any sequence enclosed
245 * within vertical lines having no backslashes
246 */
247 "\\|([^\\\\]*)\\|"
248 /* All other words should be delimited by spaces or parentheses */
249 "|([^][)(}{[ \t])+"),
Thomas Rast80c49c32009-01-17 17:29:48 +0100250PATTERNS("tex", "^(\\\\((sub)*section|chapter|part)\\*{0,1}\\{.*)$",
Jonathan Nieder664d44e2011-01-11 15:48:50 -0600251 "\\\\[a-zA-Z@]+|\\\\.|[a-zA-Z0-9\x80-\xff]+"),
Jeff King122aa6f2008-10-05 17:43:36 -0400252{ "default", NULL, -1, { NULL, 0 } },
Jeff Kingbe58e702008-10-05 17:43:21 -0400253};
Thomas Rast80c49c32009-01-17 17:29:48 +0100254#undef PATTERNS
Brandon Casey909a5492010-09-10 11:18:14 -0500255#undef IPATTERN
Jeff Kingbe58e702008-10-05 17:43:21 -0400256
257static struct userdiff_driver driver_true = {
258 "diff=true",
259 NULL,
Jeff King122aa6f2008-10-05 17:43:36 -0400260 0,
Jeff Kingbe58e702008-10-05 17:43:21 -0400261 { NULL, 0 }
262};
Jeff Kingbe58e702008-10-05 17:43:21 -0400263
264static struct userdiff_driver driver_false = {
265 "!diff",
266 NULL,
Jeff King122aa6f2008-10-05 17:43:36 -0400267 1,
Jeff Kingbe58e702008-10-05 17:43:21 -0400268 { NULL, 0 }
269};
Jeff Kingbe58e702008-10-05 17:43:21 -0400270
Ævar Arnfjörð Bjarmasonf12fa9e2021-04-08 17:04:19 +0200271struct find_by_namelen_data {
272 const char *name;
273 size_t len;
274 struct userdiff_driver *driver;
275};
276
277static int userdiff_find_by_namelen_cb(struct userdiff_driver *driver,
278 enum userdiff_driver_type type, void *priv)
Jeff Kingbe58e702008-10-05 17:43:21 -0400279{
Ævar Arnfjörð Bjarmasonf12fa9e2021-04-08 17:04:19 +0200280 struct find_by_namelen_data *cb_data = priv;
281
282 if (!strncmp(driver->name, cb_data->name, cb_data->len) &&
283 !driver->name[cb_data->len]) {
284 cb_data->driver = driver;
285 return 1; /* tell the caller to stop iterating */
Jeff Kingbe58e702008-10-05 17:43:21 -0400286 }
Ævar Arnfjörð Bjarmasonf12fa9e2021-04-08 17:04:19 +0200287 return 0;
288}
289
290static struct userdiff_driver *userdiff_find_by_namelen(const char *name, size_t len)
291{
292 struct find_by_namelen_data udcbdata = {
293 .name = name,
294 .len = len,
295 };
296 for_each_userdiff_driver(userdiff_find_by_namelen_cb, &udcbdata);
297 return udcbdata.driver;
Jeff Kingbe58e702008-10-05 17:43:21 -0400298}
299
Jeff Kingbe58e702008-10-05 17:43:21 -0400300static int parse_funcname(struct userdiff_funcname *f, const char *k,
301 const char *v, int cflags)
302{
303 if (git_config_string(&f->pattern, k, v) < 0)
304 return -1;
305 f->cflags = cflags;
Jeff King6680a082012-02-07 13:23:02 -0500306 return 0;
Jeff Kingbe58e702008-10-05 17:43:21 -0400307}
308
Jeff King122aa6f2008-10-05 17:43:36 -0400309static int parse_tristate(int *b, const char *k, const char *v)
310{
311 if (v && !strcasecmp(v, "auto"))
312 *b = -1;
313 else
314 *b = git_config_bool(k, v);
Jeff King6680a082012-02-07 13:23:02 -0500315 return 0;
Jeff King122aa6f2008-10-05 17:43:36 -0400316}
317
Jeff Kingd9bae1a2010-04-01 20:12:15 -0400318static int parse_bool(int *b, const char *k, const char *v)
319{
320 *b = git_config_bool(k, v);
Jeff King6680a082012-02-07 13:23:02 -0500321 return 0;
Jeff Kingd9bae1a2010-04-01 20:12:15 -0400322}
323
Jeff Kingc7534ef2008-10-26 00:45:55 -0400324int userdiff_config(const char *k, const char *v)
Jeff Kingbe58e702008-10-05 17:43:21 -0400325{
326 struct userdiff_driver *drv;
Jeff King0a5987f2013-01-23 01:25:07 -0500327 const char *name, *type;
Jeff Kingf5914f42020-04-10 15:44:28 -0400328 size_t namelen;
Jeff Kingbe58e702008-10-05 17:43:21 -0400329
Jeff King0a5987f2013-01-23 01:25:07 -0500330 if (parse_config_key(k, "diff", &name, &namelen, &type) || !name)
331 return 0;
332
333 drv = userdiff_find_by_namelen(name, namelen);
334 if (!drv) {
335 ALLOC_GROW(drivers, ndrivers+1, drivers_alloc);
336 drv = &drivers[ndrivers++];
337 memset(drv, 0, sizeof(*drv));
338 drv->name = xmemdupz(name, namelen);
339 drv->binary = -1;
340 }
341
342 if (!strcmp(type, "funcname"))
Jeff Kingbe58e702008-10-05 17:43:21 -0400343 return parse_funcname(&drv->funcname, k, v, 0);
Jeff King0a5987f2013-01-23 01:25:07 -0500344 if (!strcmp(type, "xfuncname"))
Jeff Kingbe58e702008-10-05 17:43:21 -0400345 return parse_funcname(&drv->funcname, k, v, REG_EXTENDED);
Jeff King0a5987f2013-01-23 01:25:07 -0500346 if (!strcmp(type, "binary"))
Jeff King122aa6f2008-10-05 17:43:36 -0400347 return parse_tristate(&drv->binary, k, v);
Jeff King0a5987f2013-01-23 01:25:07 -0500348 if (!strcmp(type, "command"))
Jeff King6680a082012-02-07 13:23:02 -0500349 return git_config_string(&drv->external, k, v);
Jeff King0a5987f2013-01-23 01:25:07 -0500350 if (!strcmp(type, "textconv"))
Jeff King6680a082012-02-07 13:23:02 -0500351 return git_config_string(&drv->textconv, k, v);
Jeff King0a5987f2013-01-23 01:25:07 -0500352 if (!strcmp(type, "cachetextconv"))
Jeff Kingd9bae1a2010-04-01 20:12:15 -0400353 return parse_bool(&drv->textconv_want_cache, k, v);
Jeff King0a5987f2013-01-23 01:25:07 -0500354 if (!strcmp(type, "wordregex"))
Jeff King6680a082012-02-07 13:23:02 -0500355 return git_config_string(&drv->word_regex, k, v);
Jeff Kingbe58e702008-10-05 17:43:21 -0400356
357 return 0;
358}
359
Nguyễn Thái Ngọc Duy3b335762018-12-09 11:25:21 +0100360struct userdiff_driver *userdiff_find_by_name(const char *name)
361{
Jeff Kingbe58e702008-10-05 17:43:21 -0400362 int len = strlen(name);
363 return userdiff_find_by_namelen(name, len);
364}
365
Nguyễn Thái Ngọc Duyacd00ea2018-09-21 17:57:33 +0200366struct userdiff_driver *userdiff_find_by_path(struct index_state *istate,
367 const char *path)
Jeff Kingbe58e702008-10-05 17:43:21 -0400368{
Junio C Hamano2aef63d2017-01-27 18:01:57 -0800369 static struct attr_check *check;
Jeff Kingbe58e702008-10-05 17:43:21 -0400370
Junio C Hamano2aef63d2017-01-27 18:01:57 -0800371 if (!check)
372 check = attr_check_initl("diff", NULL);
Jeff Kingbe58e702008-10-05 17:43:21 -0400373 if (!path)
374 return NULL;
Junio C Hamano11877b92018-10-19 13:34:02 +0900375 git_check_attr(istate, path, check);
Jeff Kingbe58e702008-10-05 17:43:21 -0400376
Junio C Hamano2aef63d2017-01-27 18:01:57 -0800377 if (ATTR_TRUE(check->items[0].value))
Jeff Kingbe58e702008-10-05 17:43:21 -0400378 return &driver_true;
Junio C Hamano2aef63d2017-01-27 18:01:57 -0800379 if (ATTR_FALSE(check->items[0].value))
Jeff Kingbe58e702008-10-05 17:43:21 -0400380 return &driver_false;
Junio C Hamano2aef63d2017-01-27 18:01:57 -0800381 if (ATTR_UNSET(check->items[0].value))
Jeff Kingbe58e702008-10-05 17:43:21 -0400382 return NULL;
Junio C Hamano2aef63d2017-01-27 18:01:57 -0800383 return userdiff_find_by_name(check->items[0].value);
Jeff Kingbe58e702008-10-05 17:43:21 -0400384}
Jeff King3813e692011-05-23 16:30:14 -0400385
Nguyễn Thái Ngọc Duybd7ad452018-11-10 06:49:06 +0100386struct userdiff_driver *userdiff_get_textconv(struct repository *r,
387 struct userdiff_driver *driver)
Jeff King3813e692011-05-23 16:30:14 -0400388{
389 if (!driver->textconv)
390 return NULL;
391
392 if (driver->textconv_want_cache && !driver->textconv_cache) {
393 struct notes_cache *c = xmalloc(sizeof(*c));
394 struct strbuf name = STRBUF_INIT;
395
396 strbuf_addf(&name, "textconv/%s", driver->name);
Nguyễn Thái Ngọc Duybd7ad452018-11-10 06:49:06 +0100397 notes_cache_init(r, c, name.buf, driver->textconv);
Jeff King3813e692011-05-23 16:30:14 -0400398 driver->textconv_cache = c;
Rene Scharfe460c7eb2017-08-30 20:20:15 +0200399 strbuf_release(&name);
Jeff King3813e692011-05-23 16:30:14 -0400400 }
401
402 return driver;
403}
Ævar Arnfjörð Bjarmasonf12fa9e2021-04-08 17:04:19 +0200404
405static int for_each_userdiff_driver_list(each_userdiff_driver_fn fn,
406 enum userdiff_driver_type type, void *cb_data,
407 struct userdiff_driver *drv,
408 int drv_size)
409{
410 int i;
411 int ret;
412 for (i = 0; i < drv_size; i++) {
413 struct userdiff_driver *item = drv + i;
414 if ((ret = fn(item, type, cb_data)))
415 return ret;
416 }
417 return 0;
418}
419
420int for_each_userdiff_driver(each_userdiff_driver_fn fn, void *cb_data)
421{
422 int ret;
423
424 ret = for_each_userdiff_driver_list(fn, USERDIFF_DRIVER_TYPE_CUSTOM,
425 cb_data, drivers, ndrivers);
426 if (ret)
427 return ret;
428
429 ret = for_each_userdiff_driver_list(fn, USERDIFF_DRIVER_TYPE_BUILTIN,
430 cb_data, builtin_drivers,
431 ARRAY_SIZE(builtin_drivers));
432 if (ret)
433 return ret;
434
435 return 0;
436}