6 static struct userdiff_driver *drivers;
8 static int drivers_alloc;
10 #define PATTERNS(name, pattern, word_regex) \
11 { name, NULL, -1, { pattern, REG_EXTENDED }, \
12 word_regex "|[^[:space:]]|[\xc0-\xff][\x80-\xbf]+" }
13 #define IPATTERN(name, pattern, word_regex) \
14 { name, NULL, -1, { pattern, REG_EXTENDED | REG_ICASE }, \
15 word_regex "|[^[:space:]]|[\xc0-\xff][\x80-\xbf]+" }
16 static struct userdiff_driver builtin_drivers[] = {
18 "!^(.*[ \t])?(is[ \t]+new|renames|is[ \t]+separate)([ \t].*)?$\n"
19 "!^[ \t]*with[ \t].*$\n"
20 "^[ \t]*((procedure|function)[ \t]+.*)$\n"
21 "^[ \t]*((package|protected|task)[ \t]+.*)$",
23 "[a-zA-Z][a-zA-Z0-9_]*"
24 "|[-+]?[0-9][0-9#_.aAbBcCdDeEfF]*([eE][+-]?[0-9_]+)?"
25 "|=>|\\.\\.|\\*\\*|:=|/=|>=|<=|<<|>>|<>"),
27 /* Optional leading indentation */
29 /* Start of captured text */
32 /* POSIX identifier with mandatory parentheses */
33 "[a-zA-Z_][a-zA-Z0-9_]*[ \t]*\\([ \t]*\\))"
35 /* Bashism identifier with optional parentheses */
36 "(function[ \t]+[a-zA-Z_][a-zA-Z0-9_]*(([ \t]*\\([ \t]*\\))|([ \t]+))"
38 /* Optional whitespace */
40 /* Compound command starting with `{`, `(`, `((` or `[[` */
41 "(\\{|\\(\\(?|\\[\\[)"
42 /* End of captured text */
45 /* Characters not in the default $IFS value */
50 /* lines beginning with a word optionally preceded by '&' or the root */
51 "^[ \t]*((/[ \t]*\\{|&?[a-zA-Z_]).*)",
53 /* Property names and math operators */
55 "|[-+*/%&^|!~]|>>|<<|&&|\\|\\|"),
57 "^[ \t]*((def(macro|module|impl|protocol|p)?|test)[ \t].*)$",
59 /* Atoms, names, and module attributes */
60 "[@:]?[a-zA-Z0-9@_?!]+"
61 /* Numbers with specific base */
62 "|[-+]?0[xob][0-9a-fA-F]+"
64 "|[-+]?[0-9][0-9_.]*([eE][-+]?[0-9_]+)?"
65 /* Operators and atoms that represent them */
66 "|:?(\\+\\+|--|\\.\\.|~~~|<>|\\^\\^\\^|<?\\|>|<<<?|>?>>|<<?~|~>?>|<~>|<=|>=|===?|!==?|=~|&&&?|\\|\\|\\|?|=>|<-|\\\\\\\\|->)"
67 /* Not real operators, but should be grouped */
68 "|:?%[A-Za-z0-9_.]\\{\\}?"),
70 /* Don't match comment lines */
72 /* Don't match 'module procedure' lines */
73 "!^[ \t]*MODULE[ \t]+PROCEDURE[ \t]\n"
74 /* Program, module, block data */
75 "^[ \t]*((END[ \t]+)?(PROGRAM|MODULE|BLOCK[ \t]+DATA"
76 /* Subroutines and functions */
77 "|([^!'\" \t]+[ \t]+)*(SUBROUTINE|FUNCTION))[ \t]+[A-Z].*)$",
79 "[a-zA-Z][a-zA-Z0-9_]*"
80 "|\\.([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])\\."
81 /* numbers and format statements like 2E14.4, or ES12.6, 9X.
82 * Don't worry about format statements without leading digits since
83 * they would have been matched above as a variable anyway. */
84 "|[-+]?[0-9.]+([AaIiDdEeFfLlTtXx][Ss]?[-+]?[0-9.]*)?(_[a-zA-Z0-9][a-zA-Z0-9_]*)?"
85 "|//|\\*\\*|::|[/<>=]="),
86 IPATTERN("fountain", "^((\\.[^.]|(int|ext|est|int\\.?/ext|i/e)[. ]).*)$",
90 "^[ \t]*(func[ \t]*.*(\\{[ \t]*)?)\n"
91 /* Structs and interfaces */
92 "^[ \t]*(type[ \t].*(struct|interface)[ \t]*(\\{[ \t]*)?)",
94 "[a-zA-Z_][a-zA-Z0-9_]*"
95 "|[-+0-9.eE]+i?|0[xX]?[0-9a-fA-F]+i?"
96 "|[-+*/<>%&^|=!:]=|--|\\+\\+|<<=?|>>=?|&\\^=?|&&|\\|\\||<-|\\.{3}"),
97 PATTERNS("html", "^[ \t]*(<[Hh][1-6]([ \t].*)?>.*)$",
100 "!^[ \t]*(catch|do|for|if|instanceof|new|return|switch|throw|while)\n"
101 "^[ \t]*(([A-Za-z_][A-Za-z_0-9]*[ \t]+)+[A-Za-z_][A-Za-z_0-9]*[ \t]*\\([^;]*)$",
103 "[a-zA-Z_][a-zA-Z0-9_]*"
104 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?"
106 "|--|\\+\\+|<<=?|>>>?=?|&&|\\|\\|"),
108 "^ {0,3}#{1,6}[ \t].*",
112 * Octave pattern is mostly the same as matlab, except that '%%%' and
113 * '##' can also be used to begin code sections, in addition to '%%'
114 * that is understood by both.
116 "^[[:space:]]*((classdef|function)[[:space:]].*)$|^(%%%?|##)[[:space:]].*$",
117 "[a-zA-Z_][a-zA-Z0-9_]*|[-+0-9.e]+|[=~<>]=|\\.[*/\\^']|\\|\\||&&"),
119 /* Negate C statements that can look like functions */
120 "!^[ \t]*(do|for|if|else|return|switch|while)\n"
121 /* Objective-C methods */
122 "^[ \t]*([-+][ \t]*\\([ \t]*[A-Za-z_][A-Za-z_0-9* \t]*\\)[ \t]*[A-Za-z_].*)$\n"
124 "^[ \t]*(([A-Za-z_][A-Za-z_0-9]*[ \t]+)+[A-Za-z_][A-Za-z_0-9]*[ \t]*\\([^;]*)$\n"
125 /* Objective-C class/protocol definitions */
126 "^(@(implementation|interface|protocol)[ \t].*)$",
128 "[a-zA-Z_][a-zA-Z0-9_]*"
129 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?"
130 "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->"),
132 "^(((class[ \t]+)?(procedure|function)|constructor|destructor|interface|"
133 "implementation|initialization|finalization)[ \t]*.*)$"
135 "^(.*=[ \t]*(class|record).*)$",
137 "[a-zA-Z_][a-zA-Z0-9_]*"
138 "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+"
139 "|<>|<=|>=|:=|\\.\\."),
142 "^sub [[:alnum:]_':]+[ \t]*"
143 "(\\([^)]*\\)[ \t]*)?" /* prototype */
145 * Attributes. A regex can't count nested parentheses,
146 * so just slurp up whatever we see, taking care not
147 * to accept lines like "sub foo; # defined elsewhere".
149 * An attribute could contain a semicolon, but at that
150 * point it seems reasonable enough to give up.
153 "(\\{[ \t]*)?" /* brace can come here or on the next line */
154 "(#.*)?$\n" /* comment */
155 "^(BEGIN|END|INIT|CHECK|UNITCHECK|AUTOLOAD|DESTROY)[ \t]*"
156 "(\\{[ \t]*)?" /* brace can come here or on the next line */
158 "^=head[0-9] .*", /* POD */
160 "[[:alpha:]_'][[:alnum:]_']*"
161 "|0[xb]?[0-9a-fA-F_]*"
162 /* taking care not to interpret 3..5 as (3.)(.5) */
163 "|[0-9a-fA-F_]+(\\.[0-9a-fA-F_]+)?([eE][-+]?[0-9_]+)?"
164 "|=>|-[rwxoRWXOezsfdlpSugkbctTBMAC>]|~~|::"
165 "|&&=|\\|\\|=|//=|\\*\\*="
166 "|&&|\\|\\||//|\\+\\+|--|\\*\\*|\\.\\.\\.?"
171 "^[\t ]*(((public|protected|private|static|abstract|final)[\t ]+)*function.*)$\n"
172 "^[\t ]*((((final|abstract)[\t ]+)?class|interface|trait).*)$",
174 "[a-zA-Z_][a-zA-Z0-9_]*"
175 "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+"
176 "|[-+*/<>%&^|=!.]=|--|\\+\\+|<<=?|>>=?|===|&&|\\|\\||::|->"),
177 PATTERNS("python", "^[ \t]*((class|(async[ \t]+)?def)[ \t].*)$",
179 "[a-zA-Z_][a-zA-Z0-9_]*"
180 "|[-+0-9.e]+[jJlL]?|0[xX]?[0-9a-fA-F]+[lL]?"
181 "|[-+*/<>%&^|=!]=|//=?|<<=?|>>=?|\\*\\*=?"),
183 PATTERNS("ruby", "^[ \t]*((class|module|def)[ \t].*)$",
185 "(@|@@|\\$)?[a-zA-Z_][a-zA-Z0-9_]*"
186 "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+|\\?(\\\\C-)?(\\\\M-)?."
187 "|//=?|[-+*/<>%&^|=!]=|<<=?|>>=?|===|\\.{1,3}|::|[!=]~"),
189 "^[\t ]*((pub(\\([^\\)]+\\))?[\t ]+)?((async|const|unsafe|extern([\t ]+\"[^\"]+\"))[\t ]+)?(struct|enum|union|mod|trait|fn|impl|macro_rules!)[< \t]+[^;]*)$",
191 "[a-zA-Z_][a-zA-Z0-9_]*"
192 "|[0-9][0-9_a-fA-Fiosuxz]*(\\.([0-9]*[eE][+-]?)?[0-9_fF]*)?"
193 "|[-+*\\/<>%&^|=!:]=|<<=?|>>=?|&&|\\|\\||->|=>|\\.{2}=|\\.{3}|::"),
195 "^[\t ]*(\\(((define|def(struct|syntax|class|method|rules|record|proto|alias)?)[-*/ \t]|(library|module|struct|class)[*+ \t]).*)$",
197 * R7RS valid identifiers include any sequence enclosed
198 * within vertical lines having no backslashes
201 /* All other words should be delimited by spaces or parentheses */
202 "|([^][)(}{[ \t])+"),
203 PATTERNS("bibtex", "(@[a-zA-Z]{1,}[ \t]*\\{{0,1}[ \t]*[^ \t\"@',\\#}{~%]*).*$",
204 "[={}\"]|[^={}\" \t]+"),
205 PATTERNS("tex", "^(\\\\((sub)*section|chapter|part)\\*{0,1}\\{.*)$",
206 "\\\\[a-zA-Z@]+|\\\\.|[a-zA-Z0-9\x80-\xff]+"),
208 /* Jump targets or access declarations */
209 "!^[ \t]*[A-Za-z_][A-Za-z_0-9]*:[[:space:]]*($|/[/*])\n"
210 /* functions/methods, variables, and compounds at top level */
211 "^((::[[:space:]]*)?[A-Za-z_].*)$",
213 "[a-zA-Z_][a-zA-Z0-9_]*"
214 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lLuU]*"
215 "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->\\*?|\\.\\*"),
218 "!^[ \t]*(do|while|for|if|else|instanceof|new|return|switch|case|throw|catch|using)\n"
219 /* Methods and constructors */
220 "^[ \t]*(((static|public|internal|private|protected|new|virtual|sealed|override|unsafe|async)[ \t]+)*[][<>@.~_[:alnum:]]+[ \t]+[<>@._[:alnum:]]+[ \t]*\\(.*\\))[ \t]*$\n"
222 "^[ \t]*(((static|public|internal|private|protected|new|virtual|sealed|override|unsafe)[ \t]+)*[][<>@.~_[:alnum:]]+[ \t]+[@._[:alnum:]]+)[ \t]*$\n"
223 /* Type definitions */
224 "^[ \t]*(((static|public|internal|private|protected|new|unsafe|sealed|abstract|partial)[ \t]+)*(class|enum|interface|struct)[ \t]+.*)$\n"
226 "^[ \t]*(namespace[ \t]+.*)$",
228 "[a-zA-Z_][a-zA-Z0-9_]*"
229 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?"
230 "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->"),
232 "![:;][[:space:]]*$\n"
233 "^[:[@.#]?[_a-z0-9].*$",
236 * This regex comes from W3C CSS specs. Should theoretically also
237 * allow ISO 10646 characters U+00A0 and higher,
238 * but they are not handled in this regex.
240 "-?[_a-zA-Z][-_a-zA-Z0-9]*" /* identifiers */
241 "|-?[0-9]+|\\#[0-9a-fA-F]+" /* numbers */
243 { "default", NULL, -1, { NULL, 0 } },
248 static struct userdiff_driver driver_true = {
255 static struct userdiff_driver driver_false = {
262 static struct userdiff_driver *userdiff_find_by_namelen(const char *k, size_t len)
265 for (i = 0; i < ndrivers; i++) {
266 struct userdiff_driver *drv = drivers + i;
267 if (!strncmp(drv->name, k, len) && !drv->name[len])
270 for (i = 0; i < ARRAY_SIZE(builtin_drivers); i++) {
271 struct userdiff_driver *drv = builtin_drivers + i;
272 if (!strncmp(drv->name, k, len) && !drv->name[len])
278 static int parse_funcname(struct userdiff_funcname *f, const char *k,
279 const char *v, int cflags)
281 if (git_config_string(&f->pattern, k, v) < 0)
287 static int parse_tristate(int *b, const char *k, const char *v)
289 if (v && !strcasecmp(v, "auto"))
292 *b = git_config_bool(k, v);
296 static int parse_bool(int *b, const char *k, const char *v)
298 *b = git_config_bool(k, v);
302 int userdiff_config(const char *k, const char *v)
304 struct userdiff_driver *drv;
305 const char *name, *type;
308 if (parse_config_key(k, "diff", &name, &namelen, &type) || !name)
311 drv = userdiff_find_by_namelen(name, namelen);
313 ALLOC_GROW(drivers, ndrivers+1, drivers_alloc);
314 drv = &drivers[ndrivers++];
315 memset(drv, 0, sizeof(*drv));
316 drv->name = xmemdupz(name, namelen);
320 if (!strcmp(type, "funcname"))
321 return parse_funcname(&drv->funcname, k, v, 0);
322 if (!strcmp(type, "xfuncname"))
323 return parse_funcname(&drv->funcname, k, v, REG_EXTENDED);
324 if (!strcmp(type, "binary"))
325 return parse_tristate(&drv->binary, k, v);
326 if (!strcmp(type, "command"))
327 return git_config_string(&drv->external, k, v);
328 if (!strcmp(type, "textconv"))
329 return git_config_string(&drv->textconv, k, v);
330 if (!strcmp(type, "cachetextconv"))
331 return parse_bool(&drv->textconv_want_cache, k, v);
332 if (!strcmp(type, "wordregex"))
333 return git_config_string(&drv->word_regex, k, v);
338 struct userdiff_driver *userdiff_find_by_name(const char *name)
340 int len = strlen(name);
341 return userdiff_find_by_namelen(name, len);
344 struct userdiff_driver *userdiff_find_by_path(struct index_state *istate,
347 static struct attr_check *check;
350 check = attr_check_initl("diff", NULL);
353 git_check_attr(istate, path, check);
355 if (ATTR_TRUE(check->items[0].value))
357 if (ATTR_FALSE(check->items[0].value))
358 return &driver_false;
359 if (ATTR_UNSET(check->items[0].value))
361 return userdiff_find_by_name(check->items[0].value);
364 struct userdiff_driver *userdiff_get_textconv(struct repository *r,
365 struct userdiff_driver *driver)
367 if (!driver->textconv)
370 if (driver->textconv_want_cache && !driver->textconv_cache) {
371 struct notes_cache *c = xmalloc(sizeof(*c));
372 struct strbuf name = STRBUF_INIT;
374 strbuf_addf(&name, "textconv/%s", driver->name);
375 notes_cache_init(r, c, name.buf, driver->textconv);
376 driver->textconv_cache = c;
377 strbuf_release(&name);