blob: 09203fbc35453e50836e82987dc6e73466aefb27 [file] [log] [blame]
Elijah Newren36bf1952023-02-24 00:09:24 +00001#include "git-compat-util.h"
2#include "alloc.h"
Brandon Williamsb2141fc2017-06-14 11:07:36 -07003#include "config.h"
Jeff Kingbe58e702008-10-05 17:43:21 -04004#include "userdiff.h"
Jeff Kingbe58e702008-10-05 17:43:21 -04005#include "attr.h"
Elijah Newren36bf1952023-02-24 00:09:24 +00006#include "strbuf.h"
Jeff Kingbe58e702008-10-05 17:43:21 -04007
8static struct userdiff_driver *drivers;
9static int ndrivers;
10static int drivers_alloc;
11
Ævar Arnfjörð Bjarmason2dd75f12022-02-24 10:33:03 +010012#define PATTERNS(lang, rx, wrx) { \
13 .name = lang, \
14 .binary = -1, \
15 .funcname = { \
16 .pattern = rx, \
17 .cflags = REG_EXTENDED, \
18 }, \
19 .word_regex = wrx "|[^[:space:]]|[\xc0-\xff][\x80-\xbf]+", \
20}
21#define IPATTERN(lang, rx, wrx) { \
22 .name = lang, \
23 .binary = -1, \
24 .funcname = { \
25 .pattern = rx, \
26 .cflags = REG_EXTENDED | REG_ICASE, \
27 }, \
28 .word_regex = wrx "|[^[:space:]]|[\xc0-\xff][\x80-\xbf]+", \
29}
Junio C Hamanob6029b32021-08-10 15:12:01 -070030
31/*
32 * Built-in drivers for various languages, sorted by their names
33 * (except that the "default" is left at the end).
34 *
35 * When writing or updating patterns, assume that the contents these
36 * patterns are applied to are syntactically correct. The patterns
37 * can be simple without implementing all syntactical corner cases, as
38 * long as they are sufficiently permissive.
39 */
Jeff Kingbe58e702008-10-05 17:43:21 -040040static struct userdiff_driver builtin_drivers[] = {
Adrian Johnsone90d0652012-09-16 13:24:15 +093041IPATTERN("ada",
Adrian Johnson39a87a22014-02-03 22:03:16 +103042 "!^(.*[ \t])?(is[ \t]+new|renames|is[ \t]+separate)([ \t].*)?$\n"
Adrian Johnsone90d0652012-09-16 13:24:15 +093043 "!^[ \t]*with[ \t].*$\n"
44 "^[ \t]*((procedure|function)[ \t]+.*)$\n"
45 "^[ \t]*((package|protected|task)[ \t]+.*)$",
46 /* -- */
47 "[a-zA-Z][a-zA-Z0-9_]*"
Adrian Johnson39a87a22014-02-03 22:03:16 +103048 "|[-+]?[0-9][0-9#_.aAbBcCdDeEfF]*([eE][+-]?[0-9_]+)?"
Adrian Johnsone90d0652012-09-16 13:24:15 +093049 "|=>|\\.\\.|\\*\\*|:=|/=|>=|<=|<<|>>|<>"),
Victor Engmark2ff6c342020-10-22 12:45:08 +130050PATTERNS("bash",
51 /* Optional leading indentation */
52 "^[ \t]*"
53 /* Start of captured text */
54 "("
55 "("
56 /* POSIX identifier with mandatory parentheses */
57 "[a-zA-Z_][a-zA-Z0-9_]*[ \t]*\\([ \t]*\\))"
58 "|"
59 /* Bashism identifier with optional parentheses */
60 "(function[ \t]+[a-zA-Z_][a-zA-Z0-9_]*(([ \t]*\\([ \t]*\\))|([ \t]+))"
61 ")"
62 /* Optional whitespace */
63 "[ \t]*"
64 /* Compound command starting with `{`, `(`, `((` or `[[` */
65 "(\\{|\\(\\(?|\\[\\[)"
66 /* End of captured text */
67 ")",
68 /* -- */
69 /* Characters not in the default $IFS value */
70 "[^ \t]+"),
Ævar Arnfjörð Bjarmason6d1c9c52021-04-08 17:04:17 +020071PATTERNS("bibtex",
72 "(@[a-zA-Z]{1,}[ \t]*\\{{0,1}[ \t]*[^ \t\"@',\\#}{~%]*).*$",
73 /* -- */
Ævar Arnfjörð Bjarmasonddd164d2021-04-08 17:04:16 +020074 "[={}\"]|[^={}\" \t]+"),
75PATTERNS("cpp",
76 /* Jump targets or access declarations */
77 "!^[ \t]*[A-Za-z_][A-Za-z_0-9]*:[[:space:]]*($|/[/*])\n"
78 /* functions/methods, variables, and compounds at top level */
79 "^((::[[:space:]]*)?[A-Za-z_].*)$",
80 /* -- */
Johannes Sixt350b87c2021-10-08 19:09:55 +000081 /* identifiers and keywords */
Ævar Arnfjörð Bjarmasonddd164d2021-04-08 17:04:16 +020082 "[a-zA-Z_][a-zA-Z0-9_]*"
Johannes Sixt350b87c2021-10-08 19:09:55 +000083 /* decimal and octal integers as well as floatingpoint numbers */
Johannes Sixt386076e2021-10-24 11:56:43 +020084 "|[0-9][0-9.]*([Ee][-+]?[0-9]+)?[fFlLuU]*"
Johannes Sixt350b87c2021-10-08 19:09:55 +000085 /* hexadecimal and binary integers */
Johannes Sixt386076e2021-10-24 11:56:43 +020086 "|0[xXbB][0-9a-fA-F]+[lLuU]*"
Johannes Sixt350b87c2021-10-08 19:09:55 +000087 /* floatingpoint numbers that begin with a decimal point */
Johannes Sixt386076e2021-10-24 11:56:43 +020088 "|\\.[0-9][0-9]*([Ee][-+]?[0-9]+)?[fFlL]?"
Johannes Sixtc4fdba32021-10-10 17:03:04 +000089 "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->\\*?|\\.\\*|<=>"),
Ævar Arnfjörð Bjarmasonddd164d2021-04-08 17:04:16 +020090PATTERNS("csharp",
91 /* Keywords */
92 "!^[ \t]*(do|while|for|if|else|instanceof|new|return|switch|case|throw|catch|using)\n"
93 /* Methods and constructors */
94 "^[ \t]*(((static|public|internal|private|protected|new|virtual|sealed|override|unsafe|async)[ \t]+)*[][<>@.~_[:alnum:]]+[ \t]+[<>@._[:alnum:]]+[ \t]*\\(.*\\))[ \t]*$\n"
95 /* Properties */
96 "^[ \t]*(((static|public|internal|private|protected|new|virtual|sealed|override|unsafe)[ \t]+)*[][<>@.~_[:alnum:]]+[ \t]+[@._[:alnum:]]+)[ \t]*$\n"
97 /* Type definitions */
Julian Verdurmenc4e31782021-03-02 00:58:09 +000098 "^[ \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 +020099 /* Namespace */
100 "^[ \t]*(namespace[ \t]+.*)$",
101 /* -- */
102 "[a-zA-Z_][a-zA-Z0-9_]*"
103 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?"
104 "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->"),
105IPATTERN("css",
106 "![:;][[:space:]]*$\n"
107 "^[:[@.#]?[_a-z0-9].*$",
108 /* -- */
109 /*
110 * This regex comes from W3C CSS specs. Should theoretically also
111 * allow ISO 10646 characters U+00A0 and higher,
112 * but they are not handled in this regex.
113 */
114 "-?[_a-zA-Z][-_a-zA-Z0-9]*" /* identifiers */
115 "|-?[0-9]+|\\#[0-9a-fA-F]+" /* numbers */
116),
Stephen Boyd3c817602019-08-19 14:22:43 -0700117PATTERNS("dts",
118 "!;\n"
Stephen Boyd8da56a42019-10-20 11:52:30 -0700119 "!=\n"
Stephen Boyd3c817602019-08-19 14:22:43 -0700120 /* lines beginning with a word optionally preceded by '&' or the root */
Stephen Boyd8da56a42019-10-20 11:52:30 -0700121 "^[ \t]*((/[ \t]*\\{|&?[a-zA-Z_]).*)",
Stephen Boyd3c817602019-08-19 14:22:43 -0700122 /* -- */
123 /* Property names and math operators */
124 "[a-zA-Z0-9,._+?#-]+"
125 "|[-+*/%&^|!~]|>>|<<|&&|\\|\\|"),
Łukasz Niemiera8072002019-11-08 22:38:24 +0100126PATTERNS("elixir",
127 "^[ \t]*((def(macro|module|impl|protocol|p)?|test)[ \t].*)$",
Ed Masted1b13842019-12-13 17:55:35 +0000128 /* -- */
Łukasz Niemiera8072002019-11-08 22:38:24 +0100129 /* Atoms, names, and module attributes */
Ed Masted1b13842019-12-13 17:55:35 +0000130 "[@:]?[a-zA-Z0-9@_?!]+"
Łukasz Niemiera8072002019-11-08 22:38:24 +0100131 /* Numbers with specific base */
132 "|[-+]?0[xob][0-9a-fA-F]+"
133 /* Numbers */
134 "|[-+]?[0-9][0-9_.]*([eE][-+]?[0-9_]+)?"
135 /* Operators and atoms that represent them */
136 "|:?(\\+\\+|--|\\.\\.|~~~|<>|\\^\\^\\^|<?\\|>|<<<?|>?>>|<<?~|~>?>|<~>|<=|>=|===?|!==?|=~|&&&?|\\|\\|\\|?|=>|<-|\\\\\\\\|->)"
137 /* Not real operators, but should be grouped */
138 "|:?%[A-Za-z0-9_.]\\{\\}?"),
Brandon Casey909a5492010-09-10 11:18:14 -0500139IPATTERN("fortran",
Philippe Blainb79e6922020-08-12 22:30:28 +0000140 /* Don't match comment lines */
Brandon Casey909a5492010-09-10 11:18:14 -0500141 "!^([C*]|[ \t]*!)\n"
Philippe Blainb79e6922020-08-12 22:30:28 +0000142 /* Don't match 'module procedure' lines */
Brandon Casey909a5492010-09-10 11:18:14 -0500143 "!^[ \t]*MODULE[ \t]+PROCEDURE[ \t]\n"
Philippe Blainb79e6922020-08-12 22:30:28 +0000144 /* Program, module, block data */
Brandon Casey909a5492010-09-10 11:18:14 -0500145 "^[ \t]*((END[ \t]+)?(PROGRAM|MODULE|BLOCK[ \t]+DATA"
Philippe Blainb79e6922020-08-12 22:30:28 +0000146 /* Subroutines and functions */
Philippe Blain75c3b6b2020-08-12 22:30:29 +0000147 "|([^!'\" \t]+[ \t]+)*(SUBROUTINE|FUNCTION))[ \t]+[A-Z].*)$",
Brandon Casey909a5492010-09-10 11:18:14 -0500148 /* -- */
149 "[a-zA-Z][a-zA-Z0-9_]*"
150 "|\\.([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])\\."
151 /* numbers and format statements like 2E14.4, or ES12.6, 9X.
152 * Don't worry about format statements without leading digits since
153 * they would have been matched above as a variable anyway. */
154 "|[-+]?[0-9.]+([AaIiDdEeFfLlTtXx][Ss]?[-+]?[0-9.]*)?(_[a-zA-Z0-9][a-zA-Z0-9_]*)?"
Jonathan Nieder664d44e2011-01-11 15:48:50 -0600155 "|//|\\*\\*|::|[/<>=]="),
Ævar Arnfjörð Bjarmason6d1c9c52021-04-08 17:04:17 +0200156IPATTERN("fountain",
157 "^((\\.[^.]|(int|ext|est|int\\.?/ext|i/e)[. ]).*)$",
158 /* -- */
Zoë Blade69f9c872015-07-21 14:22:46 +0100159 "[^ \t-]+"),
Alban Gruin1dbf0c02018-03-01 12:19:07 +0100160PATTERNS("golang",
161 /* Functions */
162 "^[ \t]*(func[ \t]*.*(\\{[ \t]*)?)\n"
163 /* Structs and interfaces */
164 "^[ \t]*(type[ \t].*(struct|interface)[ \t]*(\\{[ \t]*)?)",
165 /* -- */
166 "[a-zA-Z_][a-zA-Z0-9_]*"
167 "|[-+0-9.eE]+i?|0[xX]?[0-9a-fA-F]+i?"
168 "|[-+*/<>%&^|=!:]=|--|\\+\\+|<<=?|>>=?|&\\^=?|&&|\\|\\||<-|\\.{3}"),
Ævar Arnfjörð Bjarmason6d1c9c52021-04-08 17:04:17 +0200169PATTERNS("html",
170 "^[ \t]*(<[Hh][1-6]([ \t].*)?>.*)$",
171 /* -- */
Jonathan Nieder664d44e2011-01-11 15:48:50 -0600172 "[^<>= \t]+"),
Thomas Rast80c49c32009-01-17 17:29:48 +0100173PATTERNS("java",
Jeff Kingbe58e702008-10-05 17:43:21 -0400174 "!^[ \t]*(catch|do|for|if|instanceof|new|return|switch|throw|while)\n"
Andrei Rybak575e6fc2023-02-08 00:42:58 +0100175 /* Class, enum, interface, and record declarations */
Andrei Rybak93d52ed2023-02-08 00:42:59 +0100176 "^[ \t]*(([a-z-]+[ \t]+)*(class|enum|interface|record)[ \t]+.*)$\n"
Tassilo Horna8cbc892021-08-11 19:51:04 +0200177 /* Method definitions; note that constructor signatures are not */
178 /* matched because they are indistinguishable from method calls. */
179 "^[ \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 +0200180 /* -- */
Thomas Rast80c49c32009-01-17 17:29:48 +0100181 "[a-zA-Z_][a-zA-Z0-9_]*"
182 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?"
183 "|[-+*/<>%&^|=!]="
Jonathan Nieder664d44e2011-01-11 15:48:50 -0600184 "|--|\\+\\+|<<=?|>>>?=?|&&|\\|\\|"),
Jaydeep P Das09188ed2022-03-12 10:18:32 +0530185PATTERNS("kotlin",
186 "^[ \t]*(([a-z]+[ \t]+)*(fun|class|interface)[ \t]+.*)$",
187 /* -- */
188 "[a-zA-Z_][a-zA-Z0-9_]*"
189 /* hexadecimal and binary numbers */
190 "|0[xXbB][0-9a-fA-F_]+[lLuU]*"
191 /* integers and floats */
192 "|[0-9][0-9_]*([.][0-9_]*)?([Ee][-+]?[0-9]+)?[fFlLuU]*"
193 /* floating point numbers beginning with decimal point */
194 "|[.][0-9][0-9_]*([Ee][-+]?[0-9]+)?[fFlLuU]?"
195 /* unary and binary operators */
196 "|[-+*/<>%&^|=!]==?|--|\\+\\+|<<=|>>=|&&|\\|\\||->|\\.\\*|!!|[?:.][.:]"),
Ash Holland09dad922020-05-02 14:15:43 +0100197PATTERNS("markdown",
198 "^ {0,3}#{1,6}[ \t].*",
Ævar Arnfjörð Bjarmason6d1c9c52021-04-08 17:04:17 +0200199 /* -- */
Ash Holland09dad922020-05-02 14:15:43 +0100200 "[^<>= \t]+"),
Gustaf Hendeby53b10a12011-11-15 21:15:03 +0100201PATTERNS("matlab",
Boxuan Li2731a782019-05-30 00:15:39 +0800202 /*
203 * Octave pattern is mostly the same as matlab, except that '%%%' and
Boxuan Li91bf3822019-05-18 11:46:23 +0800204 * '##' can also be used to begin code sections, in addition to '%%'
Boxuan Li2731a782019-05-30 00:15:39 +0800205 * that is understood by both.
206 */
Boxuan Li91bf3822019-05-18 11:46:23 +0800207 "^[[:space:]]*((classdef|function)[[:space:]].*)$|^(%%%?|##)[[:space:]].*$",
Ævar Arnfjörð Bjarmason6d1c9c52021-04-08 17:04:17 +0200208 /* -- */
Gustaf Hendeby53b10a12011-11-15 21:15:03 +0100209 "[a-zA-Z_][a-zA-Z0-9_]*|[-+0-9.e]+|[=~<>]=|\\.[*/\\^']|\\|\\||&&"),
Thomas Rast80c49c32009-01-17 17:29:48 +0100210PATTERNS("objc",
Jeff Kingbe58e702008-10-05 17:43:21 -0400211 /* Negate C statements that can look like functions */
212 "!^[ \t]*(do|for|if|else|return|switch|while)\n"
213 /* Objective-C methods */
214 "^[ \t]*([-+][ \t]*\\([ \t]*[A-Za-z_][A-Za-z_0-9* \t]*\\)[ \t]*[A-Za-z_].*)$\n"
215 /* C functions */
Paolo Bonzini959e2e62009-06-17 16:26:06 +0200216 "^[ \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 -0400217 /* Objective-C class/protocol definitions */
Thomas Rast80c49c32009-01-17 17:29:48 +0100218 "^(@(implementation|interface|protocol)[ \t].*)$",
219 /* -- */
220 "[a-zA-Z_][a-zA-Z0-9_]*"
221 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?"
Jonathan Nieder664d44e2011-01-11 15:48:50 -0600222 "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->"),
Thomas Rast80c49c32009-01-17 17:29:48 +0100223PATTERNS("pascal",
Ævar Arnfjörð Bjarmason82512e02021-04-08 17:04:18 +0200224 "^(((class[ \t]+)?(procedure|function)|constructor|destructor|interface"
225 "|implementation|initialization|finalization)[ \t]*.*)$\n"
Thomas Rast80c49c32009-01-17 17:29:48 +0100226 "^(.*=[ \t]*(class|record).*)$",
227 /* -- */
228 "[a-zA-Z_][a-zA-Z0-9_]*"
229 "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+"
Jonathan Nieder664d44e2011-01-11 15:48:50 -0600230 "|<>|<=|>=|:=|\\.\\."),
Jonathan Nieder71a5d4b2010-12-26 03:07:31 -0600231PATTERNS("perl",
Jonathan Niederea2ca442011-05-21 14:38:26 -0500232 "^package .*\n"
233 "^sub [[:alnum:]_':]+[ \t]*"
234 "(\\([^)]*\\)[ \t]*)?" /* prototype */
235 /*
236 * Attributes. A regex can't count nested parentheses,
237 * so just slurp up whatever we see, taking care not
238 * to accept lines like "sub foo; # defined elsewhere".
239 *
240 * An attribute could contain a semicolon, but at that
241 * point it seems reasonable enough to give up.
242 */
243 "(:[^;#]*)?"
244 "(\\{[ \t]*)?" /* brace can come here or on the next line */
245 "(#.*)?$\n" /* comment */
Jonathan Niederf143d9c2011-05-22 12:29:32 -0500246 "^(BEGIN|END|INIT|CHECK|UNITCHECK|AUTOLOAD|DESTROY)[ \t]*"
Jonathan Niederea2ca442011-05-21 14:38:26 -0500247 "(\\{[ \t]*)?" /* brace can come here or on the next line */
248 "(#.*)?$\n"
Jonathan Nieder12f09672011-05-21 14:35:51 -0500249 "^=head[0-9] .*", /* POD */
Jonathan Nieder71a5d4b2010-12-26 03:07:31 -0600250 /* -- */
251 "[[:alpha:]_'][[:alnum:]_']*"
252 "|0[xb]?[0-9a-fA-F_]*"
253 /* taking care not to interpret 3..5 as (3.)(.5) */
254 "|[0-9a-fA-F_]+(\\.[0-9a-fA-F_]+)?([eE][-+]?[0-9_]+)?"
255 "|=>|-[rwxoRWXOezsfdlpSugkbctTBMAC>]|~~|::"
256 "|&&=|\\|\\|=|//=|\\*\\*="
257 "|&&|\\|\\||//|\\+\\+|--|\\*\\*|\\.\\.\\.?"
258 "|[-+*/%.^&<>=!|]="
259 "|=~|!~"
Jonathan Nieder664d44e2011-01-11 15:48:50 -0600260 "|<<|<>|<=>|>>"),
Björn Steinbrink6d2f2082010-05-23 20:05:40 +0200261PATTERNS("php",
Javier Spagnolettiaff92822020-10-07 03:38:18 +0000262 "^[\t ]*(((public|protected|private|static|abstract|final)[\t ]+)*function.*)$\n"
USAMI Kenta2c7f3aa2021-08-31 06:01:25 +0000263 "^[\t ]*((((final|abstract)[\t ]+)?class|enum|interface|trait).*)$",
Thomas Rast80c49c32009-01-17 17:29:48 +0100264 /* -- */
265 "[a-zA-Z_][a-zA-Z0-9_]*"
266 "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+"
Jonathan Nieder664d44e2011-01-11 15:48:50 -0600267 "|[-+*/<>%&^|=!.]=|--|\\+\\+|<<=?|>>=?|===|&&|\\|\\||::|->"),
Ævar Arnfjörð Bjarmason6d1c9c52021-04-08 17:04:17 +0200268PATTERNS("python",
269 "^[ \t]*((class|(async[ \t]+)?def)[ \t].*)$",
Thomas Rast80c49c32009-01-17 17:29:48 +0100270 /* -- */
271 "[a-zA-Z_][a-zA-Z0-9_]*"
272 "|[-+0-9.e]+[jJlL]?|0[xX]?[0-9a-fA-F]+[lL]?"
Jonathan Nieder664d44e2011-01-11 15:48:50 -0600273 "|[-+*/<>%&^|=!]=|//=?|<<=?|>>=?|\\*\\*=?"),
Thomas Rast80c49c32009-01-17 17:29:48 +0100274 /* -- */
Ævar Arnfjörð Bjarmason6d1c9c52021-04-08 17:04:17 +0200275PATTERNS("ruby",
276 "^[ \t]*((class|module|def)[ \t].*)$",
Thomas Rast80c49c32009-01-17 17:29:48 +0100277 /* -- */
278 "(@|@@|\\$)?[a-zA-Z_][a-zA-Z0-9_]*"
279 "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+|\\?(\\\\C-)?(\\\\M-)?."
Jonathan Nieder664d44e2011-01-11 15:48:50 -0600280 "|//=?|[-+*/<>%&^|=!]=|<<=?|>>=?|===|\\.{1,3}|::|[!=]~"),
Marc-André Lureaud74e7862019-05-17 01:58:15 +0200281PATTERNS("rust",
Konrad Borowskia04c7e02020-10-07 13:26:11 +0000282 "^[\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 +0200283 /* -- */
284 "[a-zA-Z_][a-zA-Z0-9_]*"
Johannes Sixt33be7b32019-05-30 18:44:35 +0200285 "|[0-9][0-9_a-fA-Fiosuxz]*(\\.([0-9]*[eE][+-]?)?[0-9_fF]*)?"
Marc-André Lureaud74e7862019-05-17 01:58:15 +0200286 "|[-+*\\/<>%&^|=!:]=|<<=?|>>=?|&&|\\|\\||->|=>|\\.{2}=|\\.{3}|::"),
Atharva Raykara4373902021-04-08 14:44:43 +0530287PATTERNS("scheme",
288 "^[\t ]*(\\(((define|def(struct|syntax|class|method|rules|record|proto|alias)?)[-*/ \t]|(library|module|struct|class)[*+ \t]).*)$",
289 /*
290 * R7RS valid identifiers include any sequence enclosed
291 * within vertical lines having no backslashes
292 */
293 "\\|([^\\\\]*)\\|"
294 /* All other words should be delimited by spaces or parentheses */
295 "|([^][)(}{[ \t])+"),
Thomas Rast80c49c32009-01-17 17:29:48 +0100296PATTERNS("tex", "^(\\\\((sub)*section|chapter|part)\\*{0,1}\\{.*)$",
Jonathan Nieder664d44e2011-01-11 15:48:50 -0600297 "\\\\[a-zA-Z@]+|\\\\.|[a-zA-Z0-9\x80-\xff]+"),
John Caia4cf9002023-02-20 21:04:42 +0000298{ "default", NULL, NULL, -1, { NULL, 0 } },
Jeff Kingbe58e702008-10-05 17:43:21 -0400299};
Thomas Rast80c49c32009-01-17 17:29:48 +0100300#undef PATTERNS
Brandon Casey909a5492010-09-10 11:18:14 -0500301#undef IPATTERN
Jeff Kingbe58e702008-10-05 17:43:21 -0400302
303static struct userdiff_driver driver_true = {
Ævar Arnfjörð Bjarmason2dd75f12022-02-24 10:33:03 +0100304 .name = "diff=true",
305 .binary = 0,
Jeff Kingbe58e702008-10-05 17:43:21 -0400306};
Jeff Kingbe58e702008-10-05 17:43:21 -0400307
308static struct userdiff_driver driver_false = {
Ævar Arnfjörð Bjarmason2dd75f12022-02-24 10:33:03 +0100309 .name = "!diff",
310 .binary = 1,
Jeff Kingbe58e702008-10-05 17:43:21 -0400311};
Jeff Kingbe58e702008-10-05 17:43:21 -0400312
Ævar Arnfjörð Bjarmasonf12fa9e2021-04-08 17:04:19 +0200313struct find_by_namelen_data {
314 const char *name;
315 size_t len;
316 struct userdiff_driver *driver;
317};
318
319static int userdiff_find_by_namelen_cb(struct userdiff_driver *driver,
Jeff Kingc25d9e52022-12-13 06:16:57 -0500320 enum userdiff_driver_type type UNUSED,
321 void *priv)
Jeff Kingbe58e702008-10-05 17:43:21 -0400322{
Ævar Arnfjörð Bjarmasonf12fa9e2021-04-08 17:04:19 +0200323 struct find_by_namelen_data *cb_data = priv;
324
325 if (!strncmp(driver->name, cb_data->name, cb_data->len) &&
326 !driver->name[cb_data->len]) {
327 cb_data->driver = driver;
328 return 1; /* tell the caller to stop iterating */
Jeff Kingbe58e702008-10-05 17:43:21 -0400329 }
Ævar Arnfjörð Bjarmasonf12fa9e2021-04-08 17:04:19 +0200330 return 0;
331}
332
333static struct userdiff_driver *userdiff_find_by_namelen(const char *name, size_t len)
334{
335 struct find_by_namelen_data udcbdata = {
336 .name = name,
337 .len = len,
338 };
339 for_each_userdiff_driver(userdiff_find_by_namelen_cb, &udcbdata);
340 return udcbdata.driver;
Jeff Kingbe58e702008-10-05 17:43:21 -0400341}
342
Jeff Kingbe58e702008-10-05 17:43:21 -0400343static int parse_funcname(struct userdiff_funcname *f, const char *k,
344 const char *v, int cflags)
345{
346 if (git_config_string(&f->pattern, k, v) < 0)
347 return -1;
348 f->cflags = cflags;
Jeff King6680a082012-02-07 13:23:02 -0500349 return 0;
Jeff Kingbe58e702008-10-05 17:43:21 -0400350}
351
Jeff King122aa6f2008-10-05 17:43:36 -0400352static int parse_tristate(int *b, const char *k, const char *v)
353{
354 if (v && !strcasecmp(v, "auto"))
355 *b = -1;
356 else
357 *b = git_config_bool(k, v);
Jeff King6680a082012-02-07 13:23:02 -0500358 return 0;
Jeff King122aa6f2008-10-05 17:43:36 -0400359}
360
Jeff Kingd9bae1a2010-04-01 20:12:15 -0400361static int parse_bool(int *b, const char *k, const char *v)
362{
363 *b = git_config_bool(k, v);
Jeff King6680a082012-02-07 13:23:02 -0500364 return 0;
Jeff Kingd9bae1a2010-04-01 20:12:15 -0400365}
366
Jeff Kingc7534ef2008-10-26 00:45:55 -0400367int userdiff_config(const char *k, const char *v)
Jeff Kingbe58e702008-10-05 17:43:21 -0400368{
369 struct userdiff_driver *drv;
Jeff King0a5987f2013-01-23 01:25:07 -0500370 const char *name, *type;
Jeff Kingf5914f42020-04-10 15:44:28 -0400371 size_t namelen;
Jeff Kingbe58e702008-10-05 17:43:21 -0400372
Jeff King0a5987f2013-01-23 01:25:07 -0500373 if (parse_config_key(k, "diff", &name, &namelen, &type) || !name)
374 return 0;
375
376 drv = userdiff_find_by_namelen(name, namelen);
377 if (!drv) {
378 ALLOC_GROW(drivers, ndrivers+1, drivers_alloc);
379 drv = &drivers[ndrivers++];
380 memset(drv, 0, sizeof(*drv));
381 drv->name = xmemdupz(name, namelen);
382 drv->binary = -1;
383 }
384
385 if (!strcmp(type, "funcname"))
Jeff Kingbe58e702008-10-05 17:43:21 -0400386 return parse_funcname(&drv->funcname, k, v, 0);
Jeff King0a5987f2013-01-23 01:25:07 -0500387 if (!strcmp(type, "xfuncname"))
Jeff Kingbe58e702008-10-05 17:43:21 -0400388 return parse_funcname(&drv->funcname, k, v, REG_EXTENDED);
Jeff King0a5987f2013-01-23 01:25:07 -0500389 if (!strcmp(type, "binary"))
Jeff King122aa6f2008-10-05 17:43:36 -0400390 return parse_tristate(&drv->binary, k, v);
Jeff King0a5987f2013-01-23 01:25:07 -0500391 if (!strcmp(type, "command"))
Jeff King6680a082012-02-07 13:23:02 -0500392 return git_config_string(&drv->external, k, v);
Jeff King0a5987f2013-01-23 01:25:07 -0500393 if (!strcmp(type, "textconv"))
Jeff King6680a082012-02-07 13:23:02 -0500394 return git_config_string(&drv->textconv, k, v);
Jeff King0a5987f2013-01-23 01:25:07 -0500395 if (!strcmp(type, "cachetextconv"))
Jeff Kingd9bae1a2010-04-01 20:12:15 -0400396 return parse_bool(&drv->textconv_want_cache, k, v);
Jeff King0a5987f2013-01-23 01:25:07 -0500397 if (!strcmp(type, "wordregex"))
Jeff King6680a082012-02-07 13:23:02 -0500398 return git_config_string(&drv->word_regex, k, v);
John Caia4cf9002023-02-20 21:04:42 +0000399 if (!strcmp(type, "algorithm"))
400 return git_config_string(&drv->algorithm, k, v);
Jeff Kingbe58e702008-10-05 17:43:21 -0400401
402 return 0;
403}
404
Nguyễn Thái Ngọc Duy3b335762018-12-09 11:25:21 +0100405struct userdiff_driver *userdiff_find_by_name(const char *name)
406{
Jeff Kingbe58e702008-10-05 17:43:21 -0400407 int len = strlen(name);
408 return userdiff_find_by_namelen(name, len);
409}
410
Nguyễn Thái Ngọc Duyacd00ea2018-09-21 17:57:33 +0200411struct userdiff_driver *userdiff_find_by_path(struct index_state *istate,
412 const char *path)
Jeff Kingbe58e702008-10-05 17:43:21 -0400413{
Junio C Hamano2aef63d2017-01-27 18:01:57 -0800414 static struct attr_check *check;
Jeff Kingbe58e702008-10-05 17:43:21 -0400415
Junio C Hamano2aef63d2017-01-27 18:01:57 -0800416 if (!check)
417 check = attr_check_initl("diff", NULL);
Jeff Kingbe58e702008-10-05 17:43:21 -0400418 if (!path)
419 return NULL;
Karthik Nayak47cfc9b2023-01-14 09:30:38 +0100420 git_check_attr(istate, NULL, path, check);
Jeff Kingbe58e702008-10-05 17:43:21 -0400421
Junio C Hamano2aef63d2017-01-27 18:01:57 -0800422 if (ATTR_TRUE(check->items[0].value))
Jeff Kingbe58e702008-10-05 17:43:21 -0400423 return &driver_true;
Junio C Hamano2aef63d2017-01-27 18:01:57 -0800424 if (ATTR_FALSE(check->items[0].value))
Jeff Kingbe58e702008-10-05 17:43:21 -0400425 return &driver_false;
Junio C Hamano2aef63d2017-01-27 18:01:57 -0800426 if (ATTR_UNSET(check->items[0].value))
Jeff Kingbe58e702008-10-05 17:43:21 -0400427 return NULL;
Junio C Hamano2aef63d2017-01-27 18:01:57 -0800428 return userdiff_find_by_name(check->items[0].value);
Jeff Kingbe58e702008-10-05 17:43:21 -0400429}
Jeff King3813e692011-05-23 16:30:14 -0400430
Nguyễn Thái Ngọc Duybd7ad452018-11-10 06:49:06 +0100431struct userdiff_driver *userdiff_get_textconv(struct repository *r,
432 struct userdiff_driver *driver)
Jeff King3813e692011-05-23 16:30:14 -0400433{
434 if (!driver->textconv)
435 return NULL;
436
437 if (driver->textconv_want_cache && !driver->textconv_cache) {
438 struct notes_cache *c = xmalloc(sizeof(*c));
439 struct strbuf name = STRBUF_INIT;
440
441 strbuf_addf(&name, "textconv/%s", driver->name);
Nguyễn Thái Ngọc Duybd7ad452018-11-10 06:49:06 +0100442 notes_cache_init(r, c, name.buf, driver->textconv);
Jeff King3813e692011-05-23 16:30:14 -0400443 driver->textconv_cache = c;
Rene Scharfe460c7eb2017-08-30 20:20:15 +0200444 strbuf_release(&name);
Jeff King3813e692011-05-23 16:30:14 -0400445 }
446
447 return driver;
448}
Ævar Arnfjörð Bjarmasonf12fa9e2021-04-08 17:04:19 +0200449
450static int for_each_userdiff_driver_list(each_userdiff_driver_fn fn,
451 enum userdiff_driver_type type, void *cb_data,
452 struct userdiff_driver *drv,
453 int drv_size)
454{
455 int i;
456 int ret;
457 for (i = 0; i < drv_size; i++) {
458 struct userdiff_driver *item = drv + i;
459 if ((ret = fn(item, type, cb_data)))
460 return ret;
461 }
462 return 0;
463}
464
465int for_each_userdiff_driver(each_userdiff_driver_fn fn, void *cb_data)
466{
467 int ret;
468
469 ret = for_each_userdiff_driver_list(fn, USERDIFF_DRIVER_TYPE_CUSTOM,
470 cb_data, drivers, ndrivers);
471 if (ret)
472 return ret;
473
474 ret = for_each_userdiff_driver_list(fn, USERDIFF_DRIVER_TYPE_BUILTIN,
475 cb_data, builtin_drivers,
476 ARRAY_SIZE(builtin_drivers));
477 if (ret)
478 return ret;
479
480 return 0;
481}