blob: 8578cb0d12e59848ebd3d4bff754ada89ec99656 [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]+" }
Junio C Hamanob6029b32021-08-10 15:12:01 -070016
17/*
18 * Built-in drivers for various languages, sorted by their names
19 * (except that the "default" is left at the end).
20 *
21 * When writing or updating patterns, assume that the contents these
22 * patterns are applied to are syntactically correct. The patterns
23 * can be simple without implementing all syntactical corner cases, as
24 * long as they are sufficiently permissive.
25 */
Jeff Kingbe58e702008-10-05 17:43:21 -040026static struct userdiff_driver builtin_drivers[] = {
Adrian Johnsone90d0652012-09-16 13:24:15 +093027IPATTERN("ada",
Adrian Johnson39a87a22014-02-03 22:03:16 +103028 "!^(.*[ \t])?(is[ \t]+new|renames|is[ \t]+separate)([ \t].*)?$\n"
Adrian Johnsone90d0652012-09-16 13:24:15 +093029 "!^[ \t]*with[ \t].*$\n"
30 "^[ \t]*((procedure|function)[ \t]+.*)$\n"
31 "^[ \t]*((package|protected|task)[ \t]+.*)$",
32 /* -- */
33 "[a-zA-Z][a-zA-Z0-9_]*"
Adrian Johnson39a87a22014-02-03 22:03:16 +103034 "|[-+]?[0-9][0-9#_.aAbBcCdDeEfF]*([eE][+-]?[0-9_]+)?"
Adrian Johnsone90d0652012-09-16 13:24:15 +093035 "|=>|\\.\\.|\\*\\*|:=|/=|>=|<=|<<|>>|<>"),
Victor Engmark2ff6c342020-10-22 12:45:08 +130036PATTERNS("bash",
37 /* Optional leading indentation */
38 "^[ \t]*"
39 /* Start of captured text */
40 "("
41 "("
42 /* POSIX identifier with mandatory parentheses */
43 "[a-zA-Z_][a-zA-Z0-9_]*[ \t]*\\([ \t]*\\))"
44 "|"
45 /* Bashism identifier with optional parentheses */
46 "(function[ \t]+[a-zA-Z_][a-zA-Z0-9_]*(([ \t]*\\([ \t]*\\))|([ \t]+))"
47 ")"
48 /* Optional whitespace */
49 "[ \t]*"
50 /* Compound command starting with `{`, `(`, `((` or `[[` */
51 "(\\{|\\(\\(?|\\[\\[)"
52 /* End of captured text */
53 ")",
54 /* -- */
55 /* Characters not in the default $IFS value */
56 "[^ \t]+"),
Ævar Arnfjörð Bjarmason6d1c9c52021-04-08 17:04:17 +020057PATTERNS("bibtex",
58 "(@[a-zA-Z]{1,}[ \t]*\\{{0,1}[ \t]*[^ \t\"@',\\#}{~%]*).*$",
59 /* -- */
Ævar Arnfjörð Bjarmasonddd164d2021-04-08 17:04:16 +020060 "[={}\"]|[^={}\" \t]+"),
61PATTERNS("cpp",
62 /* Jump targets or access declarations */
63 "!^[ \t]*[A-Za-z_][A-Za-z_0-9]*:[[:space:]]*($|/[/*])\n"
64 /* functions/methods, variables, and compounds at top level */
65 "^((::[[:space:]]*)?[A-Za-z_].*)$",
66 /* -- */
Johannes Sixt350b87c2021-10-08 19:09:55 +000067 /* identifiers and keywords */
Ævar Arnfjörð Bjarmasonddd164d2021-04-08 17:04:16 +020068 "[a-zA-Z_][a-zA-Z0-9_]*"
Johannes Sixt350b87c2021-10-08 19:09:55 +000069 /* decimal and octal integers as well as floatingpoint numbers */
Johannes Sixt386076e2021-10-24 11:56:43 +020070 "|[0-9][0-9.]*([Ee][-+]?[0-9]+)?[fFlLuU]*"
Johannes Sixt350b87c2021-10-08 19:09:55 +000071 /* hexadecimal and binary integers */
Johannes Sixt386076e2021-10-24 11:56:43 +020072 "|0[xXbB][0-9a-fA-F]+[lLuU]*"
Johannes Sixt350b87c2021-10-08 19:09:55 +000073 /* floatingpoint numbers that begin with a decimal point */
Johannes Sixt386076e2021-10-24 11:56:43 +020074 "|\\.[0-9][0-9]*([Ee][-+]?[0-9]+)?[fFlL]?"
Johannes Sixtc4fdba32021-10-10 17:03:04 +000075 "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->\\*?|\\.\\*|<=>"),
Ævar Arnfjörð Bjarmasonddd164d2021-04-08 17:04:16 +020076PATTERNS("csharp",
77 /* Keywords */
78 "!^[ \t]*(do|while|for|if|else|instanceof|new|return|switch|case|throw|catch|using)\n"
79 /* Methods and constructors */
80 "^[ \t]*(((static|public|internal|private|protected|new|virtual|sealed|override|unsafe|async)[ \t]+)*[][<>@.~_[:alnum:]]+[ \t]+[<>@._[:alnum:]]+[ \t]*\\(.*\\))[ \t]*$\n"
81 /* Properties */
82 "^[ \t]*(((static|public|internal|private|protected|new|virtual|sealed|override|unsafe)[ \t]+)*[][<>@.~_[:alnum:]]+[ \t]+[@._[:alnum:]]+)[ \t]*$\n"
83 /* Type definitions */
Julian Verdurmenc4e31782021-03-02 00:58:09 +000084 "^[ \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 +020085 /* Namespace */
86 "^[ \t]*(namespace[ \t]+.*)$",
87 /* -- */
88 "[a-zA-Z_][a-zA-Z0-9_]*"
89 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?"
90 "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->"),
91IPATTERN("css",
92 "![:;][[:space:]]*$\n"
93 "^[:[@.#]?[_a-z0-9].*$",
94 /* -- */
95 /*
96 * This regex comes from W3C CSS specs. Should theoretically also
97 * allow ISO 10646 characters U+00A0 and higher,
98 * but they are not handled in this regex.
99 */
100 "-?[_a-zA-Z][-_a-zA-Z0-9]*" /* identifiers */
101 "|-?[0-9]+|\\#[0-9a-fA-F]+" /* numbers */
102),
Stephen Boyd3c817602019-08-19 14:22:43 -0700103PATTERNS("dts",
104 "!;\n"
Stephen Boyd8da56a42019-10-20 11:52:30 -0700105 "!=\n"
Stephen Boyd3c817602019-08-19 14:22:43 -0700106 /* lines beginning with a word optionally preceded by '&' or the root */
Stephen Boyd8da56a42019-10-20 11:52:30 -0700107 "^[ \t]*((/[ \t]*\\{|&?[a-zA-Z_]).*)",
Stephen Boyd3c817602019-08-19 14:22:43 -0700108 /* -- */
109 /* Property names and math operators */
110 "[a-zA-Z0-9,._+?#-]+"
111 "|[-+*/%&^|!~]|>>|<<|&&|\\|\\|"),
Łukasz Niemiera8072002019-11-08 22:38:24 +0100112PATTERNS("elixir",
113 "^[ \t]*((def(macro|module|impl|protocol|p)?|test)[ \t].*)$",
Ed Masted1b13842019-12-13 17:55:35 +0000114 /* -- */
Łukasz Niemiera8072002019-11-08 22:38:24 +0100115 /* Atoms, names, and module attributes */
Ed Masted1b13842019-12-13 17:55:35 +0000116 "[@:]?[a-zA-Z0-9@_?!]+"
Łukasz Niemiera8072002019-11-08 22:38:24 +0100117 /* Numbers with specific base */
118 "|[-+]?0[xob][0-9a-fA-F]+"
119 /* Numbers */
120 "|[-+]?[0-9][0-9_.]*([eE][-+]?[0-9_]+)?"
121 /* Operators and atoms that represent them */
122 "|:?(\\+\\+|--|\\.\\.|~~~|<>|\\^\\^\\^|<?\\|>|<<<?|>?>>|<<?~|~>?>|<~>|<=|>=|===?|!==?|=~|&&&?|\\|\\|\\|?|=>|<-|\\\\\\\\|->)"
123 /* Not real operators, but should be grouped */
124 "|:?%[A-Za-z0-9_.]\\{\\}?"),
Brandon Casey909a5492010-09-10 11:18:14 -0500125IPATTERN("fortran",
Philippe Blainb79e6922020-08-12 22:30:28 +0000126 /* Don't match comment lines */
Brandon Casey909a5492010-09-10 11:18:14 -0500127 "!^([C*]|[ \t]*!)\n"
Philippe Blainb79e6922020-08-12 22:30:28 +0000128 /* Don't match 'module procedure' lines */
Brandon Casey909a5492010-09-10 11:18:14 -0500129 "!^[ \t]*MODULE[ \t]+PROCEDURE[ \t]\n"
Philippe Blainb79e6922020-08-12 22:30:28 +0000130 /* Program, module, block data */
Brandon Casey909a5492010-09-10 11:18:14 -0500131 "^[ \t]*((END[ \t]+)?(PROGRAM|MODULE|BLOCK[ \t]+DATA"
Philippe Blainb79e6922020-08-12 22:30:28 +0000132 /* Subroutines and functions */
Philippe Blain75c3b6b2020-08-12 22:30:29 +0000133 "|([^!'\" \t]+[ \t]+)*(SUBROUTINE|FUNCTION))[ \t]+[A-Z].*)$",
Brandon Casey909a5492010-09-10 11:18:14 -0500134 /* -- */
135 "[a-zA-Z][a-zA-Z0-9_]*"
136 "|\\.([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])\\."
137 /* numbers and format statements like 2E14.4, or ES12.6, 9X.
138 * Don't worry about format statements without leading digits since
139 * they would have been matched above as a variable anyway. */
140 "|[-+]?[0-9.]+([AaIiDdEeFfLlTtXx][Ss]?[-+]?[0-9.]*)?(_[a-zA-Z0-9][a-zA-Z0-9_]*)?"
Jonathan Nieder664d44e2011-01-11 15:48:50 -0600141 "|//|\\*\\*|::|[/<>=]="),
Ævar Arnfjörð Bjarmason6d1c9c52021-04-08 17:04:17 +0200142IPATTERN("fountain",
143 "^((\\.[^.]|(int|ext|est|int\\.?/ext|i/e)[. ]).*)$",
144 /* -- */
Zoë Blade69f9c872015-07-21 14:22:46 +0100145 "[^ \t-]+"),
Alban Gruin1dbf0c02018-03-01 12:19:07 +0100146PATTERNS("golang",
147 /* Functions */
148 "^[ \t]*(func[ \t]*.*(\\{[ \t]*)?)\n"
149 /* Structs and interfaces */
150 "^[ \t]*(type[ \t].*(struct|interface)[ \t]*(\\{[ \t]*)?)",
151 /* -- */
152 "[a-zA-Z_][a-zA-Z0-9_]*"
153 "|[-+0-9.eE]+i?|0[xX]?[0-9a-fA-F]+i?"
154 "|[-+*/<>%&^|=!:]=|--|\\+\\+|<<=?|>>=?|&\\^=?|&&|\\|\\||<-|\\.{3}"),
Ævar Arnfjörð Bjarmason6d1c9c52021-04-08 17:04:17 +0200155PATTERNS("html",
156 "^[ \t]*(<[Hh][1-6]([ \t].*)?>.*)$",
157 /* -- */
Jonathan Nieder664d44e2011-01-11 15:48:50 -0600158 "[^<>= \t]+"),
Thomas Rast80c49c32009-01-17 17:29:48 +0100159PATTERNS("java",
Jeff Kingbe58e702008-10-05 17:43:21 -0400160 "!^[ \t]*(catch|do|for|if|instanceof|new|return|switch|throw|while)\n"
Tassilo Horna8cbc892021-08-11 19:51:04 +0200161 /* Class, enum, and interface declarations */
162 "^[ \t]*(([a-z]+[ \t]+)*(class|enum|interface)[ \t]+[A-Za-z][A-Za-z0-9_$]*[ \t]+.*)$\n"
163 /* Method definitions; note that constructor signatures are not */
164 /* matched because they are indistinguishable from method calls. */
165 "^[ \t]*(([A-Za-z_<>&][][?&<>.,A-Za-z_0-9]*[ \t]+)+[A-Za-z_][A-Za-z_0-9]*[ \t]*\\([^;]*)$",
Paolo Bonzini959e2e62009-06-17 16:26:06 +0200166 /* -- */
Thomas Rast80c49c32009-01-17 17:29:48 +0100167 "[a-zA-Z_][a-zA-Z0-9_]*"
168 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?"
169 "|[-+*/<>%&^|=!]="
Jonathan Nieder664d44e2011-01-11 15:48:50 -0600170 "|--|\\+\\+|<<=?|>>>?=?|&&|\\|\\|"),
Ash Holland09dad922020-05-02 14:15:43 +0100171PATTERNS("markdown",
172 "^ {0,3}#{1,6}[ \t].*",
Ævar Arnfjörð Bjarmason6d1c9c52021-04-08 17:04:17 +0200173 /* -- */
Ash Holland09dad922020-05-02 14:15:43 +0100174 "[^<>= \t]+"),
Gustaf Hendeby53b10a12011-11-15 21:15:03 +0100175PATTERNS("matlab",
Boxuan Li2731a782019-05-30 00:15:39 +0800176 /*
177 * Octave pattern is mostly the same as matlab, except that '%%%' and
Boxuan Li91bf3822019-05-18 11:46:23 +0800178 * '##' can also be used to begin code sections, in addition to '%%'
Boxuan Li2731a782019-05-30 00:15:39 +0800179 * that is understood by both.
180 */
Boxuan Li91bf3822019-05-18 11:46:23 +0800181 "^[[:space:]]*((classdef|function)[[:space:]].*)$|^(%%%?|##)[[:space:]].*$",
Ævar Arnfjörð Bjarmason6d1c9c52021-04-08 17:04:17 +0200182 /* -- */
Gustaf Hendeby53b10a12011-11-15 21:15:03 +0100183 "[a-zA-Z_][a-zA-Z0-9_]*|[-+0-9.e]+|[=~<>]=|\\.[*/\\^']|\\|\\||&&"),
Thomas Rast80c49c32009-01-17 17:29:48 +0100184PATTERNS("objc",
Jeff Kingbe58e702008-10-05 17:43:21 -0400185 /* Negate C statements that can look like functions */
186 "!^[ \t]*(do|for|if|else|return|switch|while)\n"
187 /* Objective-C methods */
188 "^[ \t]*([-+][ \t]*\\([ \t]*[A-Za-z_][A-Za-z_0-9* \t]*\\)[ \t]*[A-Za-z_].*)$\n"
189 /* C functions */
Paolo Bonzini959e2e62009-06-17 16:26:06 +0200190 "^[ \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 -0400191 /* Objective-C class/protocol definitions */
Thomas Rast80c49c32009-01-17 17:29:48 +0100192 "^(@(implementation|interface|protocol)[ \t].*)$",
193 /* -- */
194 "[a-zA-Z_][a-zA-Z0-9_]*"
195 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?"
Jonathan Nieder664d44e2011-01-11 15:48:50 -0600196 "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->"),
Thomas Rast80c49c32009-01-17 17:29:48 +0100197PATTERNS("pascal",
Ævar Arnfjörð Bjarmason82512e02021-04-08 17:04:18 +0200198 "^(((class[ \t]+)?(procedure|function)|constructor|destructor|interface"
199 "|implementation|initialization|finalization)[ \t]*.*)$\n"
Thomas Rast80c49c32009-01-17 17:29:48 +0100200 "^(.*=[ \t]*(class|record).*)$",
201 /* -- */
202 "[a-zA-Z_][a-zA-Z0-9_]*"
203 "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+"
Jonathan Nieder664d44e2011-01-11 15:48:50 -0600204 "|<>|<=|>=|:=|\\.\\."),
Jonathan Nieder71a5d4b2010-12-26 03:07:31 -0600205PATTERNS("perl",
Jonathan Niederea2ca442011-05-21 14:38:26 -0500206 "^package .*\n"
207 "^sub [[:alnum:]_':]+[ \t]*"
208 "(\\([^)]*\\)[ \t]*)?" /* prototype */
209 /*
210 * Attributes. A regex can't count nested parentheses,
211 * so just slurp up whatever we see, taking care not
212 * to accept lines like "sub foo; # defined elsewhere".
213 *
214 * An attribute could contain a semicolon, but at that
215 * point it seems reasonable enough to give up.
216 */
217 "(:[^;#]*)?"
218 "(\\{[ \t]*)?" /* brace can come here or on the next line */
219 "(#.*)?$\n" /* comment */
Jonathan Niederf143d9c2011-05-22 12:29:32 -0500220 "^(BEGIN|END|INIT|CHECK|UNITCHECK|AUTOLOAD|DESTROY)[ \t]*"
Jonathan Niederea2ca442011-05-21 14:38:26 -0500221 "(\\{[ \t]*)?" /* brace can come here or on the next line */
222 "(#.*)?$\n"
Jonathan Nieder12f09672011-05-21 14:35:51 -0500223 "^=head[0-9] .*", /* POD */
Jonathan Nieder71a5d4b2010-12-26 03:07:31 -0600224 /* -- */
225 "[[:alpha:]_'][[:alnum:]_']*"
226 "|0[xb]?[0-9a-fA-F_]*"
227 /* taking care not to interpret 3..5 as (3.)(.5) */
228 "|[0-9a-fA-F_]+(\\.[0-9a-fA-F_]+)?([eE][-+]?[0-9_]+)?"
229 "|=>|-[rwxoRWXOezsfdlpSugkbctTBMAC>]|~~|::"
230 "|&&=|\\|\\|=|//=|\\*\\*="
231 "|&&|\\|\\||//|\\+\\+|--|\\*\\*|\\.\\.\\.?"
232 "|[-+*/%.^&<>=!|]="
233 "|=~|!~"
Jonathan Nieder664d44e2011-01-11 15:48:50 -0600234 "|<<|<>|<=>|>>"),
Björn Steinbrink6d2f2082010-05-23 20:05:40 +0200235PATTERNS("php",
Javier Spagnolettiaff92822020-10-07 03:38:18 +0000236 "^[\t ]*(((public|protected|private|static|abstract|final)[\t ]+)*function.*)$\n"
USAMI Kenta2c7f3aa2021-08-31 06:01:25 +0000237 "^[\t ]*((((final|abstract)[\t ]+)?class|enum|interface|trait).*)$",
Thomas Rast80c49c32009-01-17 17:29:48 +0100238 /* -- */
239 "[a-zA-Z_][a-zA-Z0-9_]*"
240 "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+"
Jonathan Nieder664d44e2011-01-11 15:48:50 -0600241 "|[-+*/<>%&^|=!.]=|--|\\+\\+|<<=?|>>=?|===|&&|\\|\\||::|->"),
Ævar Arnfjörð Bjarmason6d1c9c52021-04-08 17:04:17 +0200242PATTERNS("python",
243 "^[ \t]*((class|(async[ \t]+)?def)[ \t].*)$",
Thomas Rast80c49c32009-01-17 17:29:48 +0100244 /* -- */
245 "[a-zA-Z_][a-zA-Z0-9_]*"
246 "|[-+0-9.e]+[jJlL]?|0[xX]?[0-9a-fA-F]+[lL]?"
Jonathan Nieder664d44e2011-01-11 15:48:50 -0600247 "|[-+*/<>%&^|=!]=|//=?|<<=?|>>=?|\\*\\*=?"),
Thomas Rast80c49c32009-01-17 17:29:48 +0100248 /* -- */
Ævar Arnfjörð Bjarmason6d1c9c52021-04-08 17:04:17 +0200249PATTERNS("ruby",
250 "^[ \t]*((class|module|def)[ \t].*)$",
Thomas Rast80c49c32009-01-17 17:29:48 +0100251 /* -- */
252 "(@|@@|\\$)?[a-zA-Z_][a-zA-Z0-9_]*"
253 "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+|\\?(\\\\C-)?(\\\\M-)?."
Jonathan Nieder664d44e2011-01-11 15:48:50 -0600254 "|//=?|[-+*/<>%&^|=!]=|<<=?|>>=?|===|\\.{1,3}|::|[!=]~"),
Marc-André Lureaud74e7862019-05-17 01:58:15 +0200255PATTERNS("rust",
Konrad Borowskia04c7e02020-10-07 13:26:11 +0000256 "^[\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 +0200257 /* -- */
258 "[a-zA-Z_][a-zA-Z0-9_]*"
Johannes Sixt33be7b32019-05-30 18:44:35 +0200259 "|[0-9][0-9_a-fA-Fiosuxz]*(\\.([0-9]*[eE][+-]?)?[0-9_fF]*)?"
Marc-André Lureaud74e7862019-05-17 01:58:15 +0200260 "|[-+*\\/<>%&^|=!:]=|<<=?|>>=?|&&|\\|\\||->|=>|\\.{2}=|\\.{3}|::"),
Atharva Raykara4373902021-04-08 14:44:43 +0530261PATTERNS("scheme",
262 "^[\t ]*(\\(((define|def(struct|syntax|class|method|rules|record|proto|alias)?)[-*/ \t]|(library|module|struct|class)[*+ \t]).*)$",
263 /*
264 * R7RS valid identifiers include any sequence enclosed
265 * within vertical lines having no backslashes
266 */
267 "\\|([^\\\\]*)\\|"
268 /* All other words should be delimited by spaces or parentheses */
269 "|([^][)(}{[ \t])+"),
Thomas Rast80c49c32009-01-17 17:29:48 +0100270PATTERNS("tex", "^(\\\\((sub)*section|chapter|part)\\*{0,1}\\{.*)$",
Jonathan Nieder664d44e2011-01-11 15:48:50 -0600271 "\\\\[a-zA-Z@]+|\\\\.|[a-zA-Z0-9\x80-\xff]+"),
Jeff King122aa6f2008-10-05 17:43:36 -0400272{ "default", NULL, -1, { NULL, 0 } },
Jeff Kingbe58e702008-10-05 17:43:21 -0400273};
Thomas Rast80c49c32009-01-17 17:29:48 +0100274#undef PATTERNS
Brandon Casey909a5492010-09-10 11:18:14 -0500275#undef IPATTERN
Jeff Kingbe58e702008-10-05 17:43:21 -0400276
277static struct userdiff_driver driver_true = {
278 "diff=true",
279 NULL,
Jeff King122aa6f2008-10-05 17:43:36 -0400280 0,
Jeff Kingbe58e702008-10-05 17:43:21 -0400281 { NULL, 0 }
282};
Jeff Kingbe58e702008-10-05 17:43:21 -0400283
284static struct userdiff_driver driver_false = {
285 "!diff",
286 NULL,
Jeff King122aa6f2008-10-05 17:43:36 -0400287 1,
Jeff Kingbe58e702008-10-05 17:43:21 -0400288 { NULL, 0 }
289};
Jeff Kingbe58e702008-10-05 17:43:21 -0400290
Ævar Arnfjörð Bjarmasonf12fa9e2021-04-08 17:04:19 +0200291struct find_by_namelen_data {
292 const char *name;
293 size_t len;
294 struct userdiff_driver *driver;
295};
296
297static int userdiff_find_by_namelen_cb(struct userdiff_driver *driver,
298 enum userdiff_driver_type type, void *priv)
Jeff Kingbe58e702008-10-05 17:43:21 -0400299{
Ævar Arnfjörð Bjarmasonf12fa9e2021-04-08 17:04:19 +0200300 struct find_by_namelen_data *cb_data = priv;
301
302 if (!strncmp(driver->name, cb_data->name, cb_data->len) &&
303 !driver->name[cb_data->len]) {
304 cb_data->driver = driver;
305 return 1; /* tell the caller to stop iterating */
Jeff Kingbe58e702008-10-05 17:43:21 -0400306 }
Ævar Arnfjörð Bjarmasonf12fa9e2021-04-08 17:04:19 +0200307 return 0;
308}
309
310static struct userdiff_driver *userdiff_find_by_namelen(const char *name, size_t len)
311{
312 struct find_by_namelen_data udcbdata = {
313 .name = name,
314 .len = len,
315 };
316 for_each_userdiff_driver(userdiff_find_by_namelen_cb, &udcbdata);
317 return udcbdata.driver;
Jeff Kingbe58e702008-10-05 17:43:21 -0400318}
319
Jeff Kingbe58e702008-10-05 17:43:21 -0400320static int parse_funcname(struct userdiff_funcname *f, const char *k,
321 const char *v, int cflags)
322{
323 if (git_config_string(&f->pattern, k, v) < 0)
324 return -1;
325 f->cflags = cflags;
Jeff King6680a082012-02-07 13:23:02 -0500326 return 0;
Jeff Kingbe58e702008-10-05 17:43:21 -0400327}
328
Jeff King122aa6f2008-10-05 17:43:36 -0400329static int parse_tristate(int *b, const char *k, const char *v)
330{
331 if (v && !strcasecmp(v, "auto"))
332 *b = -1;
333 else
334 *b = git_config_bool(k, v);
Jeff King6680a082012-02-07 13:23:02 -0500335 return 0;
Jeff King122aa6f2008-10-05 17:43:36 -0400336}
337
Jeff Kingd9bae1a2010-04-01 20:12:15 -0400338static int parse_bool(int *b, const char *k, const char *v)
339{
340 *b = git_config_bool(k, v);
Jeff King6680a082012-02-07 13:23:02 -0500341 return 0;
Jeff Kingd9bae1a2010-04-01 20:12:15 -0400342}
343
Jeff Kingc7534ef2008-10-26 00:45:55 -0400344int userdiff_config(const char *k, const char *v)
Jeff Kingbe58e702008-10-05 17:43:21 -0400345{
346 struct userdiff_driver *drv;
Jeff King0a5987f2013-01-23 01:25:07 -0500347 const char *name, *type;
Jeff Kingf5914f42020-04-10 15:44:28 -0400348 size_t namelen;
Jeff Kingbe58e702008-10-05 17:43:21 -0400349
Jeff King0a5987f2013-01-23 01:25:07 -0500350 if (parse_config_key(k, "diff", &name, &namelen, &type) || !name)
351 return 0;
352
353 drv = userdiff_find_by_namelen(name, namelen);
354 if (!drv) {
355 ALLOC_GROW(drivers, ndrivers+1, drivers_alloc);
356 drv = &drivers[ndrivers++];
357 memset(drv, 0, sizeof(*drv));
358 drv->name = xmemdupz(name, namelen);
359 drv->binary = -1;
360 }
361
362 if (!strcmp(type, "funcname"))
Jeff Kingbe58e702008-10-05 17:43:21 -0400363 return parse_funcname(&drv->funcname, k, v, 0);
Jeff King0a5987f2013-01-23 01:25:07 -0500364 if (!strcmp(type, "xfuncname"))
Jeff Kingbe58e702008-10-05 17:43:21 -0400365 return parse_funcname(&drv->funcname, k, v, REG_EXTENDED);
Jeff King0a5987f2013-01-23 01:25:07 -0500366 if (!strcmp(type, "binary"))
Jeff King122aa6f2008-10-05 17:43:36 -0400367 return parse_tristate(&drv->binary, k, v);
Jeff King0a5987f2013-01-23 01:25:07 -0500368 if (!strcmp(type, "command"))
Jeff King6680a082012-02-07 13:23:02 -0500369 return git_config_string(&drv->external, k, v);
Jeff King0a5987f2013-01-23 01:25:07 -0500370 if (!strcmp(type, "textconv"))
Jeff King6680a082012-02-07 13:23:02 -0500371 return git_config_string(&drv->textconv, k, v);
Jeff King0a5987f2013-01-23 01:25:07 -0500372 if (!strcmp(type, "cachetextconv"))
Jeff Kingd9bae1a2010-04-01 20:12:15 -0400373 return parse_bool(&drv->textconv_want_cache, k, v);
Jeff King0a5987f2013-01-23 01:25:07 -0500374 if (!strcmp(type, "wordregex"))
Jeff King6680a082012-02-07 13:23:02 -0500375 return git_config_string(&drv->word_regex, k, v);
Jeff Kingbe58e702008-10-05 17:43:21 -0400376
377 return 0;
378}
379
Nguyễn Thái Ngọc Duy3b335762018-12-09 11:25:21 +0100380struct userdiff_driver *userdiff_find_by_name(const char *name)
381{
Jeff Kingbe58e702008-10-05 17:43:21 -0400382 int len = strlen(name);
383 return userdiff_find_by_namelen(name, len);
384}
385
Nguyễn Thái Ngọc Duyacd00ea2018-09-21 17:57:33 +0200386struct userdiff_driver *userdiff_find_by_path(struct index_state *istate,
387 const char *path)
Jeff Kingbe58e702008-10-05 17:43:21 -0400388{
Junio C Hamano2aef63d2017-01-27 18:01:57 -0800389 static struct attr_check *check;
Jeff Kingbe58e702008-10-05 17:43:21 -0400390
Junio C Hamano2aef63d2017-01-27 18:01:57 -0800391 if (!check)
392 check = attr_check_initl("diff", NULL);
Jeff Kingbe58e702008-10-05 17:43:21 -0400393 if (!path)
394 return NULL;
Junio C Hamano11877b92018-10-19 13:34:02 +0900395 git_check_attr(istate, path, check);
Jeff Kingbe58e702008-10-05 17:43:21 -0400396
Junio C Hamano2aef63d2017-01-27 18:01:57 -0800397 if (ATTR_TRUE(check->items[0].value))
Jeff Kingbe58e702008-10-05 17:43:21 -0400398 return &driver_true;
Junio C Hamano2aef63d2017-01-27 18:01:57 -0800399 if (ATTR_FALSE(check->items[0].value))
Jeff Kingbe58e702008-10-05 17:43:21 -0400400 return &driver_false;
Junio C Hamano2aef63d2017-01-27 18:01:57 -0800401 if (ATTR_UNSET(check->items[0].value))
Jeff Kingbe58e702008-10-05 17:43:21 -0400402 return NULL;
Junio C Hamano2aef63d2017-01-27 18:01:57 -0800403 return userdiff_find_by_name(check->items[0].value);
Jeff Kingbe58e702008-10-05 17:43:21 -0400404}
Jeff King3813e692011-05-23 16:30:14 -0400405
Nguyễn Thái Ngọc Duybd7ad452018-11-10 06:49:06 +0100406struct userdiff_driver *userdiff_get_textconv(struct repository *r,
407 struct userdiff_driver *driver)
Jeff King3813e692011-05-23 16:30:14 -0400408{
409 if (!driver->textconv)
410 return NULL;
411
412 if (driver->textconv_want_cache && !driver->textconv_cache) {
413 struct notes_cache *c = xmalloc(sizeof(*c));
414 struct strbuf name = STRBUF_INIT;
415
416 strbuf_addf(&name, "textconv/%s", driver->name);
Nguyễn Thái Ngọc Duybd7ad452018-11-10 06:49:06 +0100417 notes_cache_init(r, c, name.buf, driver->textconv);
Jeff King3813e692011-05-23 16:30:14 -0400418 driver->textconv_cache = c;
Rene Scharfe460c7eb2017-08-30 20:20:15 +0200419 strbuf_release(&name);
Jeff King3813e692011-05-23 16:30:14 -0400420 }
421
422 return driver;
423}
Ævar Arnfjörð Bjarmasonf12fa9e2021-04-08 17:04:19 +0200424
425static int for_each_userdiff_driver_list(each_userdiff_driver_fn fn,
426 enum userdiff_driver_type type, void *cb_data,
427 struct userdiff_driver *drv,
428 int drv_size)
429{
430 int i;
431 int ret;
432 for (i = 0; i < drv_size; i++) {
433 struct userdiff_driver *item = drv + i;
434 if ((ret = fn(item, type, cb_data)))
435 return ret;
436 }
437 return 0;
438}
439
440int for_each_userdiff_driver(each_userdiff_driver_fn fn, void *cb_data)
441{
442 int ret;
443
444 ret = for_each_userdiff_driver_list(fn, USERDIFF_DRIVER_TYPE_CUSTOM,
445 cb_data, drivers, ndrivers);
446 if (ret)
447 return ret;
448
449 ret = for_each_userdiff_driver_list(fn, USERDIFF_DRIVER_TYPE_BUILTIN,
450 cb_data, builtin_drivers,
451 ARRAY_SIZE(builtin_drivers));
452 if (ret)
453 return ret;
454
455 return 0;
456}