blob: 82bc76b910ad1ef98d0ca51669de1ca1ca7861ef [file] [log] [blame]
Elijah Newren36bf1952023-02-24 00:09:24 +00001#include "git-compat-util.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"
Elijah Newren36bf1952023-02-24 00:09:24 +00005#include "strbuf.h"
Jeff Kingaffe3552024-02-26 05:27:29 -05006#include "environment.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]+", \
René Scharfebe391442023-04-06 22:19:11 +020020 .word_regex_multi_byte = wrx "|[^[:space:]]", \
Ævar Arnfjörð Bjarmason2dd75f12022-02-24 10:33:03 +010021}
22#define IPATTERN(lang, rx, wrx) { \
23 .name = lang, \
24 .binary = -1, \
25 .funcname = { \
26 .pattern = rx, \
27 .cflags = REG_EXTENDED | REG_ICASE, \
28 }, \
29 .word_regex = wrx "|[^[:space:]]|[\xc0-\xff][\x80-\xbf]+", \
René Scharfebe391442023-04-06 22:19:11 +020030 .word_regex_multi_byte = wrx "|[^[:space:]]", \
Ævar Arnfjörð Bjarmason2dd75f12022-02-24 10:33:03 +010031}
Junio C Hamanob6029b32021-08-10 15:12:01 -070032
33/*
34 * Built-in drivers for various languages, sorted by their names
35 * (except that the "default" is left at the end).
36 *
37 * When writing or updating patterns, assume that the contents these
38 * patterns are applied to are syntactically correct. The patterns
39 * can be simple without implementing all syntactical corner cases, as
40 * long as they are sufficiently permissive.
41 */
Jeff Kingbe58e702008-10-05 17:43:21 -040042static struct userdiff_driver builtin_drivers[] = {
Adrian Johnsone90d0652012-09-16 13:24:15 +093043IPATTERN("ada",
Adrian Johnson39a87a22014-02-03 22:03:16 +103044 "!^(.*[ \t])?(is[ \t]+new|renames|is[ \t]+separate)([ \t].*)?$\n"
Adrian Johnsone90d0652012-09-16 13:24:15 +093045 "!^[ \t]*with[ \t].*$\n"
46 "^[ \t]*((procedure|function)[ \t]+.*)$\n"
47 "^[ \t]*((package|protected|task)[ \t]+.*)$",
48 /* -- */
49 "[a-zA-Z][a-zA-Z0-9_]*"
Adrian Johnson39a87a22014-02-03 22:03:16 +103050 "|[-+]?[0-9][0-9#_.aAbBcCdDeEfF]*([eE][+-]?[0-9_]+)?"
Adrian Johnsone90d0652012-09-16 13:24:15 +093051 "|=>|\\.\\.|\\*\\*|:=|/=|>=|<=|<<|>>|<>"),
Victor Engmark2ff6c342020-10-22 12:45:08 +130052PATTERNS("bash",
53 /* Optional leading indentation */
54 "^[ \t]*"
55 /* Start of captured text */
56 "("
57 "("
58 /* POSIX identifier with mandatory parentheses */
59 "[a-zA-Z_][a-zA-Z0-9_]*[ \t]*\\([ \t]*\\))"
60 "|"
61 /* Bashism identifier with optional parentheses */
62 "(function[ \t]+[a-zA-Z_][a-zA-Z0-9_]*(([ \t]*\\([ \t]*\\))|([ \t]+))"
63 ")"
64 /* Optional whitespace */
65 "[ \t]*"
66 /* Compound command starting with `{`, `(`, `((` or `[[` */
67 "(\\{|\\(\\(?|\\[\\[)"
68 /* End of captured text */
69 ")",
70 /* -- */
71 /* Characters not in the default $IFS value */
72 "[^ \t]+"),
Ævar Arnfjörð Bjarmason6d1c9c52021-04-08 17:04:17 +020073PATTERNS("bibtex",
74 "(@[a-zA-Z]{1,}[ \t]*\\{{0,1}[ \t]*[^ \t\"@',\\#}{~%]*).*$",
75 /* -- */
Ævar Arnfjörð Bjarmasonddd164d2021-04-08 17:04:16 +020076 "[={}\"]|[^={}\" \t]+"),
77PATTERNS("cpp",
78 /* Jump targets or access declarations */
79 "!^[ \t]*[A-Za-z_][A-Za-z_0-9]*:[[:space:]]*($|/[/*])\n"
80 /* functions/methods, variables, and compounds at top level */
81 "^((::[[:space:]]*)?[A-Za-z_].*)$",
82 /* -- */
Johannes Sixt350b87c2021-10-08 19:09:55 +000083 /* identifiers and keywords */
Ævar Arnfjörð Bjarmasonddd164d2021-04-08 17:04:16 +020084 "[a-zA-Z_][a-zA-Z0-9_]*"
Johannes Sixt350b87c2021-10-08 19:09:55 +000085 /* decimal and octal integers as well as floatingpoint numbers */
Johannes Sixt386076e2021-10-24 11:56:43 +020086 "|[0-9][0-9.]*([Ee][-+]?[0-9]+)?[fFlLuU]*"
Johannes Sixt350b87c2021-10-08 19:09:55 +000087 /* hexadecimal and binary integers */
Johannes Sixt386076e2021-10-24 11:56:43 +020088 "|0[xXbB][0-9a-fA-F]+[lLuU]*"
Johannes Sixt350b87c2021-10-08 19:09:55 +000089 /* floatingpoint numbers that begin with a decimal point */
Johannes Sixt386076e2021-10-24 11:56:43 +020090 "|\\.[0-9][0-9]*([Ee][-+]?[0-9]+)?[fFlL]?"
Johannes Sixtc4fdba32021-10-10 17:03:04 +000091 "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->\\*?|\\.\\*|<=>"),
Ævar Arnfjörð Bjarmasonddd164d2021-04-08 17:04:16 +020092PATTERNS("csharp",
Steven Jeurisec0e3072024-04-03 21:42:44 +000093 /*
94 * Jump over reserved keywords which are illegal method names, but which
95 * can be followed by parentheses without special characters in between,
96 * making them look like methods.
97 */
98 "!(^|[ \t]+)" /* Start of line or whitespace. */
99 "(do|while|for|foreach|if|else|new|default|return|switch|case|throw"
100 "|catch|using|lock|fixed)"
101 "([ \t(]+|$)\n" /* Whitespace, "(", or end of line. */
102 /*
103 * Methods/constructors:
104 * The strategy is to identify a minimum of two groups (any combination
105 * of keywords/type/name) before the opening parenthesis, and without
106 * final unexpected characters, normally only used in ordinary statements.
107 */
108 "^[ \t]*" /* Remove leading whitespace. */
109 "(" /* Start chunk header capture. */
110 "(" /* First group. */
111 "[][[:alnum:]@_.]" /* Name. */
112 "(<[][[:alnum:]@_, \t<>]+>)?" /* Optional generic parameters. */
113 ")+"
114 "([ \t]+" /* Subsequent groups, prepended with space. */
115 "([][[:alnum:]@_.](<[][[:alnum:]@_, \t<>]+>)?)+"
116 ")+"
117 "[ \t]*" /* Optional space before parameters start. */
118 "\\(" /* Start of method parameters. */
119 "[^;]*" /* Allow complex parameters, but exclude statements (;). */
120 ")$\n" /* Close chunk header capture. */
121 /*
122 * Properties:
123 * As with methods, expect a minimum of two groups. But, more trivial than
124 * methods, the vast majority of properties long enough to be worth
125 * showing a chunk header for don't include "=:;,()" on the line they are
126 * defined, since they don't have a parameter list.
127 */
128 "^[ \t]*("
129 "([][[:alnum:]@_.](<[][[:alnum:]@_, \t<>]+>)?)+"
130 "([ \t]+"
131 "([][[:alnum:]@_.](<[][[:alnum:]@_, \t<>]+>)?)+"
132 ")+" /* Up to here, same as methods regex. */
133 "[^;=:,()]*" /* Compared to methods, no parameter list allowed. */
134 ")$\n"
Ævar Arnfjörð Bjarmasonddd164d2021-04-08 17:04:16 +0200135 /* Type definitions */
Julian Verdurmenc4e31782021-03-02 00:58:09 +0000136 "^[ \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 +0200137 /* Namespace */
138 "^[ \t]*(namespace[ \t]+.*)$",
139 /* -- */
140 "[a-zA-Z_][a-zA-Z0-9_]*"
141 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?"
142 "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->"),
143IPATTERN("css",
144 "![:;][[:space:]]*$\n"
145 "^[:[@.#]?[_a-z0-9].*$",
146 /* -- */
147 /*
148 * This regex comes from W3C CSS specs. Should theoretically also
149 * allow ISO 10646 characters U+00A0 and higher,
150 * but they are not handled in this regex.
151 */
152 "-?[_a-zA-Z][-_a-zA-Z0-9]*" /* identifiers */
153 "|-?[0-9]+|\\#[0-9a-fA-F]+" /* numbers */
154),
Stephen Boyd3c817602019-08-19 14:22:43 -0700155PATTERNS("dts",
156 "!;\n"
Stephen Boyd8da56a42019-10-20 11:52:30 -0700157 "!=\n"
Stephen Boyd3c817602019-08-19 14:22:43 -0700158 /* lines beginning with a word optionally preceded by '&' or the root */
Stephen Boyd8da56a42019-10-20 11:52:30 -0700159 "^[ \t]*((/[ \t]*\\{|&?[a-zA-Z_]).*)",
Stephen Boyd3c817602019-08-19 14:22:43 -0700160 /* -- */
161 /* Property names and math operators */
162 "[a-zA-Z0-9,._+?#-]+"
163 "|[-+*/%&^|!~]|>>|<<|&&|\\|\\|"),
Łukasz Niemiera8072002019-11-08 22:38:24 +0100164PATTERNS("elixir",
165 "^[ \t]*((def(macro|module|impl|protocol|p)?|test)[ \t].*)$",
Ed Masted1b13842019-12-13 17:55:35 +0000166 /* -- */
Łukasz Niemiera8072002019-11-08 22:38:24 +0100167 /* Atoms, names, and module attributes */
Ed Masted1b13842019-12-13 17:55:35 +0000168 "[@:]?[a-zA-Z0-9@_?!]+"
Łukasz Niemiera8072002019-11-08 22:38:24 +0100169 /* Numbers with specific base */
170 "|[-+]?0[xob][0-9a-fA-F]+"
171 /* Numbers */
172 "|[-+]?[0-9][0-9_.]*([eE][-+]?[0-9_]+)?"
173 /* Operators and atoms that represent them */
174 "|:?(\\+\\+|--|\\.\\.|~~~|<>|\\^\\^\\^|<?\\|>|<<<?|>?>>|<<?~|~>?>|<~>|<=|>=|===?|!==?|=~|&&&?|\\|\\|\\|?|=>|<-|\\\\\\\\|->)"
175 /* Not real operators, but should be grouped */
176 "|:?%[A-Za-z0-9_.]\\{\\}?"),
Brandon Casey909a5492010-09-10 11:18:14 -0500177IPATTERN("fortran",
Philippe Blainb79e6922020-08-12 22:30:28 +0000178 /* Don't match comment lines */
Brandon Casey909a5492010-09-10 11:18:14 -0500179 "!^([C*]|[ \t]*!)\n"
Philippe Blainb79e6922020-08-12 22:30:28 +0000180 /* Don't match 'module procedure' lines */
Brandon Casey909a5492010-09-10 11:18:14 -0500181 "!^[ \t]*MODULE[ \t]+PROCEDURE[ \t]\n"
Philippe Blainb79e6922020-08-12 22:30:28 +0000182 /* Program, module, block data */
Brandon Casey909a5492010-09-10 11:18:14 -0500183 "^[ \t]*((END[ \t]+)?(PROGRAM|MODULE|BLOCK[ \t]+DATA"
Philippe Blainb79e6922020-08-12 22:30:28 +0000184 /* Subroutines and functions */
Philippe Blain75c3b6b2020-08-12 22:30:29 +0000185 "|([^!'\" \t]+[ \t]+)*(SUBROUTINE|FUNCTION))[ \t]+[A-Z].*)$",
Brandon Casey909a5492010-09-10 11:18:14 -0500186 /* -- */
187 "[a-zA-Z][a-zA-Z0-9_]*"
188 "|\\.([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])\\."
189 /* numbers and format statements like 2E14.4, or ES12.6, 9X.
190 * Don't worry about format statements without leading digits since
191 * they would have been matched above as a variable anyway. */
192 "|[-+]?[0-9.]+([AaIiDdEeFfLlTtXx][Ss]?[-+]?[0-9.]*)?(_[a-zA-Z0-9][a-zA-Z0-9_]*)?"
Jonathan Nieder664d44e2011-01-11 15:48:50 -0600193 "|//|\\*\\*|::|[/<>=]="),
Ævar Arnfjörð Bjarmason6d1c9c52021-04-08 17:04:17 +0200194IPATTERN("fountain",
195 "^((\\.[^.]|(int|ext|est|int\\.?/ext|i/e)[. ]).*)$",
196 /* -- */
Zoë Blade69f9c872015-07-21 14:22:46 +0100197 "[^ \t-]+"),
Alban Gruin1dbf0c02018-03-01 12:19:07 +0100198PATTERNS("golang",
199 /* Functions */
200 "^[ \t]*(func[ \t]*.*(\\{[ \t]*)?)\n"
201 /* Structs and interfaces */
202 "^[ \t]*(type[ \t].*(struct|interface)[ \t]*(\\{[ \t]*)?)",
203 /* -- */
204 "[a-zA-Z_][a-zA-Z0-9_]*"
205 "|[-+0-9.eE]+i?|0[xX]?[0-9a-fA-F]+i?"
206 "|[-+*/<>%&^|=!:]=|--|\\+\\+|<<=?|>>=?|&\\^=?|&&|\\|\\||<-|\\.{3}"),
Ævar Arnfjörð Bjarmason6d1c9c52021-04-08 17:04:17 +0200207PATTERNS("html",
208 "^[ \t]*(<[Hh][1-6]([ \t].*)?>.*)$",
209 /* -- */
Jonathan Nieder664d44e2011-01-11 15:48:50 -0600210 "[^<>= \t]+"),
Thomas Rast80c49c32009-01-17 17:29:48 +0100211PATTERNS("java",
Jeff Kingbe58e702008-10-05 17:43:21 -0400212 "!^[ \t]*(catch|do|for|if|instanceof|new|return|switch|throw|while)\n"
Andrei Rybak575e6fc2023-02-08 00:42:58 +0100213 /* Class, enum, interface, and record declarations */
Andrei Rybak93d52ed2023-02-08 00:42:59 +0100214 "^[ \t]*(([a-z-]+[ \t]+)*(class|enum|interface|record)[ \t]+.*)$\n"
Tassilo Horna8cbc892021-08-11 19:51:04 +0200215 /* Method definitions; note that constructor signatures are not */
216 /* matched because they are indistinguishable from method calls. */
217 "^[ \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 +0200218 /* -- */
Thomas Rast80c49c32009-01-17 17:29:48 +0100219 "[a-zA-Z_][a-zA-Z0-9_]*"
220 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?"
221 "|[-+*/<>%&^|=!]="
Jonathan Nieder664d44e2011-01-11 15:48:50 -0600222 "|--|\\+\\+|<<=?|>>>?=?|&&|\\|\\|"),
Jaydeep P Das09188ed2022-03-12 10:18:32 +0530223PATTERNS("kotlin",
224 "^[ \t]*(([a-z]+[ \t]+)*(fun|class|interface)[ \t]+.*)$",
225 /* -- */
226 "[a-zA-Z_][a-zA-Z0-9_]*"
227 /* hexadecimal and binary numbers */
228 "|0[xXbB][0-9a-fA-F_]+[lLuU]*"
229 /* integers and floats */
230 "|[0-9][0-9_]*([.][0-9_]*)?([Ee][-+]?[0-9]+)?[fFlLuU]*"
231 /* floating point numbers beginning with decimal point */
232 "|[.][0-9][0-9_]*([Ee][-+]?[0-9]+)?[fFlLuU]?"
233 /* unary and binary operators */
234 "|[-+*/<>%&^|=!]==?|--|\\+\\+|<<=|>>=|&&|\\|\\||->|\\.\\*|!!|[?:.][.:]"),
Ash Holland09dad922020-05-02 14:15:43 +0100235PATTERNS("markdown",
236 "^ {0,3}#{1,6}[ \t].*",
Ævar Arnfjörð Bjarmason6d1c9c52021-04-08 17:04:17 +0200237 /* -- */
Ash Holland09dad922020-05-02 14:15:43 +0100238 "[^<>= \t]+"),
Gustaf Hendeby53b10a12011-11-15 21:15:03 +0100239PATTERNS("matlab",
Boxuan Li2731a782019-05-30 00:15:39 +0800240 /*
241 * Octave pattern is mostly the same as matlab, except that '%%%' and
Boxuan Li91bf3822019-05-18 11:46:23 +0800242 * '##' can also be used to begin code sections, in addition to '%%'
Boxuan Li2731a782019-05-30 00:15:39 +0800243 * that is understood by both.
244 */
Boxuan Li91bf3822019-05-18 11:46:23 +0800245 "^[[:space:]]*((classdef|function)[[:space:]].*)$|^(%%%?|##)[[:space:]].*$",
Ævar Arnfjörð Bjarmason6d1c9c52021-04-08 17:04:17 +0200246 /* -- */
Gustaf Hendeby53b10a12011-11-15 21:15:03 +0100247 "[a-zA-Z_][a-zA-Z0-9_]*|[-+0-9.e]+|[=~<>]=|\\.[*/\\^']|\\|\\||&&"),
Thomas Rast80c49c32009-01-17 17:29:48 +0100248PATTERNS("objc",
Jeff Kingbe58e702008-10-05 17:43:21 -0400249 /* Negate C statements that can look like functions */
250 "!^[ \t]*(do|for|if|else|return|switch|while)\n"
251 /* Objective-C methods */
252 "^[ \t]*([-+][ \t]*\\([ \t]*[A-Za-z_][A-Za-z_0-9* \t]*\\)[ \t]*[A-Za-z_].*)$\n"
253 /* C functions */
Paolo Bonzini959e2e62009-06-17 16:26:06 +0200254 "^[ \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 -0400255 /* Objective-C class/protocol definitions */
Thomas Rast80c49c32009-01-17 17:29:48 +0100256 "^(@(implementation|interface|protocol)[ \t].*)$",
257 /* -- */
258 "[a-zA-Z_][a-zA-Z0-9_]*"
259 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?"
Jonathan Nieder664d44e2011-01-11 15:48:50 -0600260 "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->"),
Thomas Rast80c49c32009-01-17 17:29:48 +0100261PATTERNS("pascal",
Ævar Arnfjörð Bjarmason82512e02021-04-08 17:04:18 +0200262 "^(((class[ \t]+)?(procedure|function)|constructor|destructor|interface"
263 "|implementation|initialization|finalization)[ \t]*.*)$\n"
Thomas Rast80c49c32009-01-17 17:29:48 +0100264 "^(.*=[ \t]*(class|record).*)$",
265 /* -- */
266 "[a-zA-Z_][a-zA-Z0-9_]*"
267 "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+"
Jonathan Nieder664d44e2011-01-11 15:48:50 -0600268 "|<>|<=|>=|:=|\\.\\."),
Jonathan Nieder71a5d4b2010-12-26 03:07:31 -0600269PATTERNS("perl",
Jonathan Niederea2ca442011-05-21 14:38:26 -0500270 "^package .*\n"
271 "^sub [[:alnum:]_':]+[ \t]*"
272 "(\\([^)]*\\)[ \t]*)?" /* prototype */
273 /*
274 * Attributes. A regex can't count nested parentheses,
275 * so just slurp up whatever we see, taking care not
276 * to accept lines like "sub foo; # defined elsewhere".
277 *
278 * An attribute could contain a semicolon, but at that
279 * point it seems reasonable enough to give up.
280 */
281 "(:[^;#]*)?"
282 "(\\{[ \t]*)?" /* brace can come here or on the next line */
283 "(#.*)?$\n" /* comment */
Jonathan Niederf143d9c2011-05-22 12:29:32 -0500284 "^(BEGIN|END|INIT|CHECK|UNITCHECK|AUTOLOAD|DESTROY)[ \t]*"
Jonathan Niederea2ca442011-05-21 14:38:26 -0500285 "(\\{[ \t]*)?" /* brace can come here or on the next line */
286 "(#.*)?$\n"
Jonathan Nieder12f09672011-05-21 14:35:51 -0500287 "^=head[0-9] .*", /* POD */
Jonathan Nieder71a5d4b2010-12-26 03:07:31 -0600288 /* -- */
289 "[[:alpha:]_'][[:alnum:]_']*"
290 "|0[xb]?[0-9a-fA-F_]*"
291 /* taking care not to interpret 3..5 as (3.)(.5) */
292 "|[0-9a-fA-F_]+(\\.[0-9a-fA-F_]+)?([eE][-+]?[0-9_]+)?"
293 "|=>|-[rwxoRWXOezsfdlpSugkbctTBMAC>]|~~|::"
294 "|&&=|\\|\\|=|//=|\\*\\*="
295 "|&&|\\|\\||//|\\+\\+|--|\\*\\*|\\.\\.\\.?"
296 "|[-+*/%.^&<>=!|]="
297 "|=~|!~"
Jonathan Nieder664d44e2011-01-11 15:48:50 -0600298 "|<<|<>|<=>|>>"),
Björn Steinbrink6d2f2082010-05-23 20:05:40 +0200299PATTERNS("php",
Javier Spagnolettiaff92822020-10-07 03:38:18 +0000300 "^[\t ]*(((public|protected|private|static|abstract|final)[\t ]+)*function.*)$\n"
USAMI Kenta2c7f3aa2021-08-31 06:01:25 +0000301 "^[\t ]*((((final|abstract)[\t ]+)?class|enum|interface|trait).*)$",
Thomas Rast80c49c32009-01-17 17:29:48 +0100302 /* -- */
303 "[a-zA-Z_][a-zA-Z0-9_]*"
304 "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+"
Jonathan Nieder664d44e2011-01-11 15:48:50 -0600305 "|[-+*/<>%&^|=!.]=|--|\\+\\+|<<=?|>>=?|===|&&|\\|\\||::|->"),
Ævar Arnfjörð Bjarmason6d1c9c52021-04-08 17:04:17 +0200306PATTERNS("python",
307 "^[ \t]*((class|(async[ \t]+)?def)[ \t].*)$",
Thomas Rast80c49c32009-01-17 17:29:48 +0100308 /* -- */
309 "[a-zA-Z_][a-zA-Z0-9_]*"
310 "|[-+0-9.e]+[jJlL]?|0[xX]?[0-9a-fA-F]+[lL]?"
Jonathan Nieder664d44e2011-01-11 15:48:50 -0600311 "|[-+*/<>%&^|=!]=|//=?|<<=?|>>=?|\\*\\*=?"),
Thomas Rast80c49c32009-01-17 17:29:48 +0100312 /* -- */
Ævar Arnfjörð Bjarmason6d1c9c52021-04-08 17:04:17 +0200313PATTERNS("ruby",
314 "^[ \t]*((class|module|def)[ \t].*)$",
Thomas Rast80c49c32009-01-17 17:29:48 +0100315 /* -- */
316 "(@|@@|\\$)?[a-zA-Z_][a-zA-Z0-9_]*"
317 "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+|\\?(\\\\C-)?(\\\\M-)?."
Jonathan Nieder664d44e2011-01-11 15:48:50 -0600318 "|//=?|[-+*/<>%&^|=!]=|<<=?|>>=?|===|\\.{1,3}|::|[!=]~"),
Marc-André Lureaud74e7862019-05-17 01:58:15 +0200319PATTERNS("rust",
Konrad Borowskia04c7e02020-10-07 13:26:11 +0000320 "^[\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 +0200321 /* -- */
322 "[a-zA-Z_][a-zA-Z0-9_]*"
Johannes Sixt33be7b32019-05-30 18:44:35 +0200323 "|[0-9][0-9_a-fA-Fiosuxz]*(\\.([0-9]*[eE][+-]?)?[0-9_fF]*)?"
Marc-André Lureaud74e7862019-05-17 01:58:15 +0200324 "|[-+*\\/<>%&^|=!:]=|<<=?|>>=?|&&|\\|\\||->|=>|\\.{2}=|\\.{3}|::"),
Atharva Raykara4373902021-04-08 14:44:43 +0530325PATTERNS("scheme",
326 "^[\t ]*(\\(((define|def(struct|syntax|class|method|rules|record|proto|alias)?)[-*/ \t]|(library|module|struct|class)[*+ \t]).*)$",
327 /*
328 * R7RS valid identifiers include any sequence enclosed
329 * within vertical lines having no backslashes
330 */
331 "\\|([^\\\\]*)\\|"
332 /* All other words should be delimited by spaces or parentheses */
333 "|([^][)(}{[ \t])+"),
Thomas Rast80c49c32009-01-17 17:29:48 +0100334PATTERNS("tex", "^(\\\\((sub)*section|chapter|part)\\*{0,1}\\{.*)$",
René Scharfebe391442023-04-06 22:19:11 +0200335 "\\\\[a-zA-Z@]+|\\\\.|([a-zA-Z0-9]|[^\x01-\x7f])+"),
John Caia4cf9002023-02-20 21:04:42 +0000336{ "default", NULL, NULL, -1, { NULL, 0 } },
Jeff Kingbe58e702008-10-05 17:43:21 -0400337};
Thomas Rast80c49c32009-01-17 17:29:48 +0100338#undef PATTERNS
Brandon Casey909a5492010-09-10 11:18:14 -0500339#undef IPATTERN
Jeff Kingbe58e702008-10-05 17:43:21 -0400340
341static struct userdiff_driver driver_true = {
Ævar Arnfjörð Bjarmason2dd75f12022-02-24 10:33:03 +0100342 .name = "diff=true",
343 .binary = 0,
Jeff Kingbe58e702008-10-05 17:43:21 -0400344};
Jeff Kingbe58e702008-10-05 17:43:21 -0400345
346static struct userdiff_driver driver_false = {
Ævar Arnfjörð Bjarmason2dd75f12022-02-24 10:33:03 +0100347 .name = "!diff",
348 .binary = 1,
Jeff Kingbe58e702008-10-05 17:43:21 -0400349};
Jeff Kingbe58e702008-10-05 17:43:21 -0400350
Ævar Arnfjörð Bjarmasonf12fa9e2021-04-08 17:04:19 +0200351struct find_by_namelen_data {
352 const char *name;
353 size_t len;
354 struct userdiff_driver *driver;
355};
356
357static int userdiff_find_by_namelen_cb(struct userdiff_driver *driver,
Jeff Kingc25d9e52022-12-13 06:16:57 -0500358 enum userdiff_driver_type type UNUSED,
359 void *priv)
Jeff Kingbe58e702008-10-05 17:43:21 -0400360{
Ævar Arnfjörð Bjarmasonf12fa9e2021-04-08 17:04:19 +0200361 struct find_by_namelen_data *cb_data = priv;
362
René Scharfef0e578c2024-02-10 08:43:01 +0100363 if (!xstrncmpz(driver->name, cb_data->name, cb_data->len)) {
Ævar Arnfjörð Bjarmasonf12fa9e2021-04-08 17:04:19 +0200364 cb_data->driver = driver;
365 return 1; /* tell the caller to stop iterating */
Jeff Kingbe58e702008-10-05 17:43:21 -0400366 }
Ævar Arnfjörð Bjarmasonf12fa9e2021-04-08 17:04:19 +0200367 return 0;
368}
369
René Scharfebe391442023-04-06 22:19:11 +0200370static int regexec_supports_multi_byte_chars(void)
371{
372 static const char not_space[] = "[^[:space:]]";
373 static const char utf8_multi_byte_char[] = "\xc2\xa3";
374 regex_t re;
375 regmatch_t match;
376 static int result = -1;
377
378 if (result != -1)
379 return result;
380 if (regcomp(&re, not_space, REG_EXTENDED))
381 BUG("invalid regular expression: %s", not_space);
382 result = !regexec(&re, utf8_multi_byte_char, 1, &match, 0) &&
383 match.rm_so == 0 &&
384 match.rm_eo == strlen(utf8_multi_byte_char);
385 regfree(&re);
386 return result;
387}
388
Ævar Arnfjörð Bjarmasonf12fa9e2021-04-08 17:04:19 +0200389static struct userdiff_driver *userdiff_find_by_namelen(const char *name, size_t len)
390{
391 struct find_by_namelen_data udcbdata = {
392 .name = name,
393 .len = len,
394 };
395 for_each_userdiff_driver(userdiff_find_by_namelen_cb, &udcbdata);
396 return udcbdata.driver;
Jeff Kingbe58e702008-10-05 17:43:21 -0400397}
398
Jeff Kingbe58e702008-10-05 17:43:21 -0400399static int parse_funcname(struct userdiff_funcname *f, const char *k,
400 const char *v, int cflags)
401{
402 if (git_config_string(&f->pattern, k, v) < 0)
403 return -1;
404 f->cflags = cflags;
Jeff King6680a082012-02-07 13:23:02 -0500405 return 0;
Jeff Kingbe58e702008-10-05 17:43:21 -0400406}
407
Jeff King122aa6f2008-10-05 17:43:36 -0400408static int parse_tristate(int *b, const char *k, const char *v)
409{
410 if (v && !strcasecmp(v, "auto"))
411 *b = -1;
412 else
413 *b = git_config_bool(k, v);
Jeff King6680a082012-02-07 13:23:02 -0500414 return 0;
Jeff King122aa6f2008-10-05 17:43:36 -0400415}
416
Jeff Kingd9bae1a2010-04-01 20:12:15 -0400417static int parse_bool(int *b, const char *k, const char *v)
418{
419 *b = git_config_bool(k, v);
Jeff King6680a082012-02-07 13:23:02 -0500420 return 0;
Jeff Kingd9bae1a2010-04-01 20:12:15 -0400421}
422
Jeff Kingc7534ef2008-10-26 00:45:55 -0400423int userdiff_config(const char *k, const char *v)
Jeff Kingbe58e702008-10-05 17:43:21 -0400424{
425 struct userdiff_driver *drv;
Jeff King0a5987f2013-01-23 01:25:07 -0500426 const char *name, *type;
Jeff Kingf5914f42020-04-10 15:44:28 -0400427 size_t namelen;
Jeff Kingbe58e702008-10-05 17:43:21 -0400428
Jeff King0a5987f2013-01-23 01:25:07 -0500429 if (parse_config_key(k, "diff", &name, &namelen, &type) || !name)
430 return 0;
431
432 drv = userdiff_find_by_namelen(name, namelen);
433 if (!drv) {
434 ALLOC_GROW(drivers, ndrivers+1, drivers_alloc);
435 drv = &drivers[ndrivers++];
436 memset(drv, 0, sizeof(*drv));
437 drv->name = xmemdupz(name, namelen);
438 drv->binary = -1;
439 }
440
441 if (!strcmp(type, "funcname"))
Jeff Kingbe58e702008-10-05 17:43:21 -0400442 return parse_funcname(&drv->funcname, k, v, 0);
Jeff King0a5987f2013-01-23 01:25:07 -0500443 if (!strcmp(type, "xfuncname"))
Jeff Kingbe58e702008-10-05 17:43:21 -0400444 return parse_funcname(&drv->funcname, k, v, REG_EXTENDED);
Jeff King0a5987f2013-01-23 01:25:07 -0500445 if (!strcmp(type, "binary"))
Jeff King122aa6f2008-10-05 17:43:36 -0400446 return parse_tristate(&drv->binary, k, v);
Jeff King0a5987f2013-01-23 01:25:07 -0500447 if (!strcmp(type, "command"))
Jeff King6680a082012-02-07 13:23:02 -0500448 return git_config_string(&drv->external, k, v);
Jeff King0a5987f2013-01-23 01:25:07 -0500449 if (!strcmp(type, "textconv"))
Jeff King6680a082012-02-07 13:23:02 -0500450 return git_config_string(&drv->textconv, k, v);
Jeff King0a5987f2013-01-23 01:25:07 -0500451 if (!strcmp(type, "cachetextconv"))
Jeff Kingd9bae1a2010-04-01 20:12:15 -0400452 return parse_bool(&drv->textconv_want_cache, k, v);
Jeff King0a5987f2013-01-23 01:25:07 -0500453 if (!strcmp(type, "wordregex"))
Jeff King6680a082012-02-07 13:23:02 -0500454 return git_config_string(&drv->word_regex, k, v);
John Caia4cf9002023-02-20 21:04:42 +0000455 if (!strcmp(type, "algorithm"))
456 return git_config_string(&drv->algorithm, k, v);
Jeff Kingbe58e702008-10-05 17:43:21 -0400457
458 return 0;
459}
460
Nguyễn Thái Ngọc Duy3b335762018-12-09 11:25:21 +0100461struct userdiff_driver *userdiff_find_by_name(const char *name)
462{
Jeff Kingbe58e702008-10-05 17:43:21 -0400463 int len = strlen(name);
René Scharfebe391442023-04-06 22:19:11 +0200464 struct userdiff_driver *driver = userdiff_find_by_namelen(name, len);
465 if (driver && driver->word_regex_multi_byte) {
466 if (regexec_supports_multi_byte_chars())
467 driver->word_regex = driver->word_regex_multi_byte;
468 driver->word_regex_multi_byte = NULL;
469 }
470 return driver;
Jeff Kingbe58e702008-10-05 17:43:21 -0400471}
472
Nguyễn Thái Ngọc Duyacd00ea2018-09-21 17:57:33 +0200473struct userdiff_driver *userdiff_find_by_path(struct index_state *istate,
474 const char *path)
Jeff Kingbe58e702008-10-05 17:43:21 -0400475{
Junio C Hamano2aef63d2017-01-27 18:01:57 -0800476 static struct attr_check *check;
Jeff Kingbe58e702008-10-05 17:43:21 -0400477
Junio C Hamano2aef63d2017-01-27 18:01:57 -0800478 if (!check)
479 check = attr_check_initl("diff", NULL);
Jeff Kingbe58e702008-10-05 17:43:21 -0400480 if (!path)
481 return NULL;
John Cai44451a22023-05-06 04:15:29 +0000482 git_check_attr(istate, path, check);
Jeff Kingbe58e702008-10-05 17:43:21 -0400483
Junio C Hamano2aef63d2017-01-27 18:01:57 -0800484 if (ATTR_TRUE(check->items[0].value))
Jeff Kingbe58e702008-10-05 17:43:21 -0400485 return &driver_true;
Junio C Hamano2aef63d2017-01-27 18:01:57 -0800486 if (ATTR_FALSE(check->items[0].value))
Jeff Kingbe58e702008-10-05 17:43:21 -0400487 return &driver_false;
Junio C Hamano2aef63d2017-01-27 18:01:57 -0800488 if (ATTR_UNSET(check->items[0].value))
Jeff Kingbe58e702008-10-05 17:43:21 -0400489 return NULL;
Junio C Hamano2aef63d2017-01-27 18:01:57 -0800490 return userdiff_find_by_name(check->items[0].value);
Jeff Kingbe58e702008-10-05 17:43:21 -0400491}
Jeff King3813e692011-05-23 16:30:14 -0400492
Nguyễn Thái Ngọc Duybd7ad452018-11-10 06:49:06 +0100493struct userdiff_driver *userdiff_get_textconv(struct repository *r,
494 struct userdiff_driver *driver)
Jeff King3813e692011-05-23 16:30:14 -0400495{
496 if (!driver->textconv)
497 return NULL;
498
Jeff Kingaffe3552024-02-26 05:27:29 -0500499 if (driver->textconv_want_cache && !driver->textconv_cache &&
500 have_git_dir()) {
Jeff King3813e692011-05-23 16:30:14 -0400501 struct notes_cache *c = xmalloc(sizeof(*c));
502 struct strbuf name = STRBUF_INIT;
503
504 strbuf_addf(&name, "textconv/%s", driver->name);
Nguyễn Thái Ngọc Duybd7ad452018-11-10 06:49:06 +0100505 notes_cache_init(r, c, name.buf, driver->textconv);
Jeff King3813e692011-05-23 16:30:14 -0400506 driver->textconv_cache = c;
Rene Scharfe460c7eb2017-08-30 20:20:15 +0200507 strbuf_release(&name);
Jeff King3813e692011-05-23 16:30:14 -0400508 }
509
510 return driver;
511}
Ævar Arnfjörð Bjarmasonf12fa9e2021-04-08 17:04:19 +0200512
513static int for_each_userdiff_driver_list(each_userdiff_driver_fn fn,
514 enum userdiff_driver_type type, void *cb_data,
515 struct userdiff_driver *drv,
516 int drv_size)
517{
518 int i;
519 int ret;
520 for (i = 0; i < drv_size; i++) {
521 struct userdiff_driver *item = drv + i;
522 if ((ret = fn(item, type, cb_data)))
523 return ret;
524 }
525 return 0;
526}
527
528int for_each_userdiff_driver(each_userdiff_driver_fn fn, void *cb_data)
529{
530 int ret;
531
532 ret = for_each_userdiff_driver_list(fn, USERDIFF_DRIVER_TYPE_CUSTOM,
533 cb_data, drivers, ndrivers);
534 if (ret)
535 return ret;
536
537 ret = for_each_userdiff_driver_list(fn, USERDIFF_DRIVER_TYPE_BUILTIN,
538 cb_data, builtin_drivers,
539 ARRAY_SIZE(builtin_drivers));
540 if (ret)
541 return ret;
542
543 return 0;
544}