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 "|=>|\\.\\.|\\*\\*|:=|/=|>=|<=|<<|>>|<>"),
29 /* lines beginning with a word optionally preceded by '&' or the root */
30 "^[ \t]*((/[ \t]*\\{|&?[a-zA-Z_]).*)",
32 /* Property names and math operators */
34 "|[-+*/%&^|!~]|>>|<<|&&|\\|\\|"),
36 "^[ \t]*((def(macro|module|impl|protocol|p)?|test)[ \t].*)$",
38 /* Atoms, names, and module attributes */
39 "[@:]?[a-zA-Z0-9@_?!]+"
40 /* Numbers with specific base */
41 "|[-+]?0[xob][0-9a-fA-F]+"
43 "|[-+]?[0-9][0-9_.]*([eE][-+]?[0-9_]+)?"
44 /* Operators and atoms that represent them */
45 "|:?(\\+\\+|--|\\.\\.|~~~|<>|\\^\\^\\^|<?\\|>|<<<?|>?>>|<<?~|~>?>|<~>|<=|>=|===?|!==?|=~|&&&?|\\|\\|\\|?|=>|<-|\\\\\\\\|->)"
46 /* Not real operators, but should be grouped */
47 "|:?%[A-Za-z0-9_.]\\{\\}?"),
50 "!^[ \t]*MODULE[ \t]+PROCEDURE[ \t]\n"
51 "^[ \t]*((END[ \t]+)?(PROGRAM|MODULE|BLOCK[ \t]+DATA"
52 "|([^'\" \t]+[ \t]+)*(SUBROUTINE|FUNCTION))[ \t]+[A-Z].*)$",
54 "[a-zA-Z][a-zA-Z0-9_]*"
55 "|\\.([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])\\."
56 /* numbers and format statements like 2E14.4, or ES12.6, 9X.
57 * Don't worry about format statements without leading digits since
58 * they would have been matched above as a variable anyway. */
59 "|[-+]?[0-9.]+([AaIiDdEeFfLlTtXx][Ss]?[-+]?[0-9.]*)?(_[a-zA-Z0-9][a-zA-Z0-9_]*)?"
60 "|//|\\*\\*|::|[/<>=]="),
61 IPATTERN("fountain", "^((\\.[^.]|(int|ext|est|int\\.?/ext|i/e)[. ]).*)$",
65 "^[ \t]*(func[ \t]*.*(\\{[ \t]*)?)\n"
66 /* Structs and interfaces */
67 "^[ \t]*(type[ \t].*(struct|interface)[ \t]*(\\{[ \t]*)?)",
69 "[a-zA-Z_][a-zA-Z0-9_]*"
70 "|[-+0-9.eE]+i?|0[xX]?[0-9a-fA-F]+i?"
71 "|[-+*/<>%&^|=!:]=|--|\\+\\+|<<=?|>>=?|&\\^=?|&&|\\|\\||<-|\\.{3}"),
72 PATTERNS("html", "^[ \t]*(<[Hh][1-6]([ \t].*)?>.*)$",
75 "!^[ \t]*(catch|do|for|if|instanceof|new|return|switch|throw|while)\n"
76 "^[ \t]*(([A-Za-z_][A-Za-z_0-9]*[ \t]+)+[A-Za-z_][A-Za-z_0-9]*[ \t]*\\([^;]*)$",
78 "[a-zA-Z_][a-zA-Z0-9_]*"
79 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?"
81 "|--|\\+\\+|<<=?|>>>?=?|&&|\\|\\|"),
83 "^ {0,3}#{1,6}[ \t].*",
87 * Octave pattern is mostly the same as matlab, except that '%%%' and
88 * '##' can also be used to begin code sections, in addition to '%%'
89 * that is understood by both.
91 "^[[:space:]]*((classdef|function)[[:space:]].*)$|^(%%%?|##)[[:space:]].*$",
92 "[a-zA-Z_][a-zA-Z0-9_]*|[-+0-9.e]+|[=~<>]=|\\.[*/\\^']|\\|\\||&&"),
94 /* Negate C statements that can look like functions */
95 "!^[ \t]*(do|for|if|else|return|switch|while)\n"
96 /* Objective-C methods */
97 "^[ \t]*([-+][ \t]*\\([ \t]*[A-Za-z_][A-Za-z_0-9* \t]*\\)[ \t]*[A-Za-z_].*)$\n"
99 "^[ \t]*(([A-Za-z_][A-Za-z_0-9]*[ \t]+)+[A-Za-z_][A-Za-z_0-9]*[ \t]*\\([^;]*)$\n"
100 /* Objective-C class/protocol definitions */
101 "^(@(implementation|interface|protocol)[ \t].*)$",
103 "[a-zA-Z_][a-zA-Z0-9_]*"
104 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?"
105 "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->"),
107 "^(((class[ \t]+)?(procedure|function)|constructor|destructor|interface|"
108 "implementation|initialization|finalization)[ \t]*.*)$"
110 "^(.*=[ \t]*(class|record).*)$",
112 "[a-zA-Z_][a-zA-Z0-9_]*"
113 "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+"
114 "|<>|<=|>=|:=|\\.\\."),
117 "^sub [[:alnum:]_':]+[ \t]*"
118 "(\\([^)]*\\)[ \t]*)?" /* prototype */
120 * Attributes. A regex can't count nested parentheses,
121 * so just slurp up whatever we see, taking care not
122 * to accept lines like "sub foo; # defined elsewhere".
124 * An attribute could contain a semicolon, but at that
125 * point it seems reasonable enough to give up.
128 "(\\{[ \t]*)?" /* brace can come here or on the next line */
129 "(#.*)?$\n" /* comment */
130 "^(BEGIN|END|INIT|CHECK|UNITCHECK|AUTOLOAD|DESTROY)[ \t]*"
131 "(\\{[ \t]*)?" /* brace can come here or on the next line */
133 "^=head[0-9] .*", /* POD */
135 "[[:alpha:]_'][[:alnum:]_']*"
136 "|0[xb]?[0-9a-fA-F_]*"
137 /* taking care not to interpret 3..5 as (3.)(.5) */
138 "|[0-9a-fA-F_]+(\\.[0-9a-fA-F_]+)?([eE][-+]?[0-9_]+)?"
139 "|=>|-[rwxoRWXOezsfdlpSugkbctTBMAC>]|~~|::"
140 "|&&=|\\|\\|=|//=|\\*\\*="
141 "|&&|\\|\\||//|\\+\\+|--|\\*\\*|\\.\\.\\.?"
146 "^[\t ]*(((public|protected|private|static)[\t ]+)*function.*)$\n"
147 "^[\t ]*((((final|abstract)[\t ]+)?class|interface|trait).*)$",
149 "[a-zA-Z_][a-zA-Z0-9_]*"
150 "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+"
151 "|[-+*/<>%&^|=!.]=|--|\\+\\+|<<=?|>>=?|===|&&|\\|\\||::|->"),
152 PATTERNS("python", "^[ \t]*((class|(async[ \t]+)?def)[ \t].*)$",
154 "[a-zA-Z_][a-zA-Z0-9_]*"
155 "|[-+0-9.e]+[jJlL]?|0[xX]?[0-9a-fA-F]+[lL]?"
156 "|[-+*/<>%&^|=!]=|//=?|<<=?|>>=?|\\*\\*=?"),
158 PATTERNS("ruby", "^[ \t]*((class|module|def)[ \t].*)$",
160 "(@|@@|\\$)?[a-zA-Z_][a-zA-Z0-9_]*"
161 "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+|\\?(\\\\C-)?(\\\\M-)?."
162 "|//=?|[-+*/<>%&^|=!]=|<<=?|>>=?|===|\\.{1,3}|::|[!=]~"),
164 "^[\t ]*((pub(\\([^\\)]+\\))?[\t ]+)?((async|const|unsafe|extern([\t ]+\"[^\"]+\"))[\t ]+)?(struct|enum|union|mod|trait|fn|impl)[< \t]+[^;]*)$",
166 "[a-zA-Z_][a-zA-Z0-9_]*"
167 "|[0-9][0-9_a-fA-Fiosuxz]*(\\.([0-9]*[eE][+-]?)?[0-9_fF]*)?"
168 "|[-+*\\/<>%&^|=!:]=|<<=?|>>=?|&&|\\|\\||->|=>|\\.{2}=|\\.{3}|::"),
169 PATTERNS("bibtex", "(@[a-zA-Z]{1,}[ \t]*\\{{0,1}[ \t]*[^ \t\"@',\\#}{~%]*).*$",
170 "[={}\"]|[^={}\" \t]+"),
171 PATTERNS("tex", "^(\\\\((sub)*section|chapter|part)\\*{0,1}\\{.*)$",
172 "\\\\[a-zA-Z@]+|\\\\.|[a-zA-Z0-9\x80-\xff]+"),
174 /* Jump targets or access declarations */
175 "!^[ \t]*[A-Za-z_][A-Za-z_0-9]*:[[:space:]]*($|/[/*])\n"
176 /* functions/methods, variables, and compounds at top level */
177 "^((::[[:space:]]*)?[A-Za-z_].*)$",
179 "[a-zA-Z_][a-zA-Z0-9_]*"
180 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lLuU]*"
181 "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->\\*?|\\.\\*"),
184 "!^[ \t]*(do|while|for|if|else|instanceof|new|return|switch|case|throw|catch|using)\n"
185 /* Methods and constructors */
186 "^[ \t]*(((static|public|internal|private|protected|new|virtual|sealed|override|unsafe|async)[ \t]+)*[][<>@.~_[:alnum:]]+[ \t]+[<>@._[:alnum:]]+[ \t]*\\(.*\\))[ \t]*$\n"
188 "^[ \t]*(((static|public|internal|private|protected|new|virtual|sealed|override|unsafe)[ \t]+)*[][<>@.~_[:alnum:]]+[ \t]+[@._[:alnum:]]+)[ \t]*$\n"
189 /* Type definitions */
190 "^[ \t]*(((static|public|internal|private|protected|new|unsafe|sealed|abstract|partial)[ \t]+)*(class|enum|interface|struct)[ \t]+.*)$\n"
192 "^[ \t]*(namespace[ \t]+.*)$",
194 "[a-zA-Z_][a-zA-Z0-9_]*"
195 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?"
196 "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->"),
198 "![:;][[:space:]]*$\n"
202 * This regex comes from W3C CSS specs. Should theoretically also
203 * allow ISO 10646 characters U+00A0 and higher,
204 * but they are not handled in this regex.
206 "-?[_a-zA-Z][-_a-zA-Z0-9]*" /* identifiers */
207 "|-?[0-9]+|\\#[0-9a-fA-F]+" /* numbers */
209 { "default", NULL, -1, { NULL, 0 } },
214 static struct userdiff_driver driver_true = {
221 static struct userdiff_driver driver_false = {
228 static struct userdiff_driver *userdiff_find_by_namelen(const char *k, size_t len)
231 for (i = 0; i < ndrivers; i++) {
232 struct userdiff_driver *drv = drivers + i;
233 if (!strncmp(drv->name, k, len) && !drv->name[len])
236 for (i = 0; i < ARRAY_SIZE(builtin_drivers); i++) {
237 struct userdiff_driver *drv = builtin_drivers + i;
238 if (!strncmp(drv->name, k, len) && !drv->name[len])
244 static int parse_funcname(struct userdiff_funcname *f, const char *k,
245 const char *v, int cflags)
247 if (git_config_string(&f->pattern, k, v) < 0)
253 static int parse_tristate(int *b, const char *k, const char *v)
255 if (v && !strcasecmp(v, "auto"))
258 *b = git_config_bool(k, v);
262 static int parse_bool(int *b, const char *k, const char *v)
264 *b = git_config_bool(k, v);
268 int userdiff_config(const char *k, const char *v)
270 struct userdiff_driver *drv;
271 const char *name, *type;
274 if (parse_config_key(k, "diff", &name, &namelen, &type) || !name)
277 drv = userdiff_find_by_namelen(name, namelen);
279 ALLOC_GROW(drivers, ndrivers+1, drivers_alloc);
280 drv = &drivers[ndrivers++];
281 memset(drv, 0, sizeof(*drv));
282 drv->name = xmemdupz(name, namelen);
286 if (!strcmp(type, "funcname"))
287 return parse_funcname(&drv->funcname, k, v, 0);
288 if (!strcmp(type, "xfuncname"))
289 return parse_funcname(&drv->funcname, k, v, REG_EXTENDED);
290 if (!strcmp(type, "binary"))
291 return parse_tristate(&drv->binary, k, v);
292 if (!strcmp(type, "command"))
293 return git_config_string(&drv->external, k, v);
294 if (!strcmp(type, "textconv"))
295 return git_config_string(&drv->textconv, k, v);
296 if (!strcmp(type, "cachetextconv"))
297 return parse_bool(&drv->textconv_want_cache, k, v);
298 if (!strcmp(type, "wordregex"))
299 return git_config_string(&drv->word_regex, k, v);
304 struct userdiff_driver *userdiff_find_by_name(const char *name)
306 int len = strlen(name);
307 return userdiff_find_by_namelen(name, len);
310 struct userdiff_driver *userdiff_find_by_path(struct index_state *istate,
313 static struct attr_check *check;
316 check = attr_check_initl("diff", NULL);
319 git_check_attr(istate, path, check);
321 if (ATTR_TRUE(check->items[0].value))
323 if (ATTR_FALSE(check->items[0].value))
324 return &driver_false;
325 if (ATTR_UNSET(check->items[0].value))
327 return userdiff_find_by_name(check->items[0].value);
330 struct userdiff_driver *userdiff_get_textconv(struct repository *r,
331 struct userdiff_driver *driver)
333 if (!driver->textconv)
336 if (driver->textconv_want_cache && !driver->textconv_cache) {
337 struct notes_cache *c = xmalloc(sizeof(*c));
338 struct strbuf name = STRBUF_INIT;
340 strbuf_addf(&name, "textconv/%s", driver->name);
341 notes_cache_init(r, c, name.buf, driver->textconv);
342 driver->textconv_cache = c;
343 strbuf_release(&name);