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 "|=>|\\.\\.|\\*\\*|:=|/=|>=|<=|<<|>>|<>"),
28 "!^[ \t]*MODULE[ \t]+PROCEDURE[ \t]\n"
29 "^[ \t]*((END[ \t]+)?(PROGRAM|MODULE|BLOCK[ \t]+DATA"
30 "|([^'\" \t]+[ \t]+)*(SUBROUTINE|FUNCTION))[ \t]+[A-Z].*)$",
32 "[a-zA-Z][a-zA-Z0-9_]*"
33 "|\\.([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])\\."
34 /* numbers and format statements like 2E14.4, or ES12.6, 9X.
35 * Don't worry about format statements without leading digits since
36 * they would have been matched above as a variable anyway. */
37 "|[-+]?[0-9.]+([AaIiDdEeFfLlTtXx][Ss]?[-+]?[0-9.]*)?(_[a-zA-Z0-9][a-zA-Z0-9_]*)?"
38 "|//|\\*\\*|::|[/<>=]="),
39 IPATTERN("fountain", "^((\\.[^.]|(int|ext|est|int\\.?/ext|i/e)[. ]).*)$",
43 "^[ \t]*(func[ \t]*.*(\\{[ \t]*)?)\n"
44 /* Structs and interfaces */
45 "^[ \t]*(type[ \t].*(struct|interface)[ \t]*(\\{[ \t]*)?)",
47 "[a-zA-Z_][a-zA-Z0-9_]*"
48 "|[-+0-9.eE]+i?|0[xX]?[0-9a-fA-F]+i?"
49 "|[-+*/<>%&^|=!:]=|--|\\+\\+|<<=?|>>=?|&\\^=?|&&|\\|\\||<-|\\.{3}"),
50 PATTERNS("html", "^[ \t]*(<[Hh][1-6]([ \t].*)?>.*)$",
53 "!^[ \t]*(catch|do|for|if|instanceof|new|return|switch|throw|while)\n"
54 "^[ \t]*(([A-Za-z_][A-Za-z_0-9]*[ \t]+)+[A-Za-z_][A-Za-z_0-9]*[ \t]*\\([^;]*)$",
56 "[a-zA-Z_][a-zA-Z0-9_]*"
57 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?"
59 "|--|\\+\\+|<<=?|>>>?=?|&&|\\|\\|"),
61 "^[[:space:]]*((classdef|function)[[:space:]].*)$|^%%[[:space:]].*$",
62 "[a-zA-Z_][a-zA-Z0-9_]*|[-+0-9.e]+|[=~<>]=|\\.[*/\\^']|\\|\\||&&"),
64 /* Negate C statements that can look like functions */
65 "!^[ \t]*(do|for|if|else|return|switch|while)\n"
66 /* Objective-C methods */
67 "^[ \t]*([-+][ \t]*\\([ \t]*[A-Za-z_][A-Za-z_0-9* \t]*\\)[ \t]*[A-Za-z_].*)$\n"
69 "^[ \t]*(([A-Za-z_][A-Za-z_0-9]*[ \t]+)+[A-Za-z_][A-Za-z_0-9]*[ \t]*\\([^;]*)$\n"
70 /* Objective-C class/protocol definitions */
71 "^(@(implementation|interface|protocol)[ \t].*)$",
73 "[a-zA-Z_][a-zA-Z0-9_]*"
74 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?"
75 "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->"),
77 "^(((class[ \t]+)?(procedure|function)|constructor|destructor|interface|"
78 "implementation|initialization|finalization)[ \t]*.*)$"
80 "^(.*=[ \t]*(class|record).*)$",
82 "[a-zA-Z_][a-zA-Z0-9_]*"
83 "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+"
84 "|<>|<=|>=|:=|\\.\\."),
87 "^sub [[:alnum:]_':]+[ \t]*"
88 "(\\([^)]*\\)[ \t]*)?" /* prototype */
90 * Attributes. A regex can't count nested parentheses,
91 * so just slurp up whatever we see, taking care not
92 * to accept lines like "sub foo; # defined elsewhere".
94 * An attribute could contain a semicolon, but at that
95 * point it seems reasonable enough to give up.
98 "(\\{[ \t]*)?" /* brace can come here or on the next line */
99 "(#.*)?$\n" /* comment */
100 "^(BEGIN|END|INIT|CHECK|UNITCHECK|AUTOLOAD|DESTROY)[ \t]*"
101 "(\\{[ \t]*)?" /* brace can come here or on the next line */
103 "^=head[0-9] .*", /* POD */
105 "[[:alpha:]_'][[:alnum:]_']*"
106 "|0[xb]?[0-9a-fA-F_]*"
107 /* taking care not to interpret 3..5 as (3.)(.5) */
108 "|[0-9a-fA-F_]+(\\.[0-9a-fA-F_]+)?([eE][-+]?[0-9_]+)?"
109 "|=>|-[rwxoRWXOezsfdlpSugkbctTBMAC>]|~~|::"
110 "|&&=|\\|\\|=|//=|\\*\\*="
111 "|&&|\\|\\||//|\\+\\+|--|\\*\\*|\\.\\.\\.?"
116 "^[\t ]*(((public|protected|private|static)[\t ]+)*function.*)$\n"
117 "^[\t ]*((((final|abstract)[\t ]+)?class|interface|trait).*)$",
119 "[a-zA-Z_][a-zA-Z0-9_]*"
120 "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+"
121 "|[-+*/<>%&^|=!.]=|--|\\+\\+|<<=?|>>=?|===|&&|\\|\\||::|->"),
122 PATTERNS("python", "^[ \t]*((class|def)[ \t].*)$",
124 "[a-zA-Z_][a-zA-Z0-9_]*"
125 "|[-+0-9.e]+[jJlL]?|0[xX]?[0-9a-fA-F]+[lL]?"
126 "|[-+*/<>%&^|=!]=|//=?|<<=?|>>=?|\\*\\*=?"),
128 PATTERNS("ruby", "^[ \t]*((class|module|def)[ \t].*)$",
130 "(@|@@|\\$)?[a-zA-Z_][a-zA-Z0-9_]*"
131 "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+|\\?(\\\\C-)?(\\\\M-)?."
132 "|//=?|[-+*/<>%&^|=!]=|<<=?|>>=?|===|\\.{1,3}|::|[!=]~"),
133 PATTERNS("bibtex", "(@[a-zA-Z]{1,}[ \t]*\\{{0,1}[ \t]*[^ \t\"@',\\#}{~%]*).*$",
134 "[={}\"]|[^={}\" \t]+"),
135 PATTERNS("tex", "^(\\\\((sub)*section|chapter|part)\\*{0,1}\\{.*)$",
136 "\\\\[a-zA-Z@]+|\\\\.|[a-zA-Z0-9\x80-\xff]+"),
138 /* Jump targets or access declarations */
139 "!^[ \t]*[A-Za-z_][A-Za-z_0-9]*:[[:space:]]*($|/[/*])\n"
140 /* functions/methods, variables, and compounds at top level */
141 "^((::[[:space:]]*)?[A-Za-z_].*)$",
143 "[a-zA-Z_][a-zA-Z0-9_]*"
144 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lLuU]*"
145 "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->\\*?|\\.\\*"),
148 "!^[ \t]*(do|while|for|if|else|instanceof|new|return|switch|case|throw|catch|using)\n"
149 /* Methods and constructors */
150 "^[ \t]*(((static|public|internal|private|protected|new|virtual|sealed|override|unsafe|async)[ \t]+)*[][<>@.~_[:alnum:]]+[ \t]+[<>@._[:alnum:]]+[ \t]*\\(.*\\))[ \t]*$\n"
152 "^[ \t]*(((static|public|internal|private|protected|new|virtual|sealed|override|unsafe)[ \t]+)*[][<>@.~_[:alnum:]]+[ \t]+[@._[:alnum:]]+)[ \t]*$\n"
153 /* Type definitions */
154 "^[ \t]*(((static|public|internal|private|protected|new|unsafe|sealed|abstract|partial)[ \t]+)*(class|enum|interface|struct)[ \t]+.*)$\n"
156 "^[ \t]*(namespace[ \t]+.*)$",
158 "[a-zA-Z_][a-zA-Z0-9_]*"
159 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?"
160 "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->"),
162 "![:;][[:space:]]*$\n"
166 * This regex comes from W3C CSS specs. Should theoretically also
167 * allow ISO 10646 characters U+00A0 and higher,
168 * but they are not handled in this regex.
170 "-?[_a-zA-Z][-_a-zA-Z0-9]*" /* identifiers */
171 "|-?[0-9]+|\\#[0-9a-fA-F]+" /* numbers */
173 { "default", NULL, -1, { NULL, 0 } },
178 static struct userdiff_driver driver_true = {
185 static struct userdiff_driver driver_false = {
192 static struct userdiff_driver *userdiff_find_by_namelen(const char *k, int len)
195 for (i = 0; i < ndrivers; i++) {
196 struct userdiff_driver *drv = drivers + i;
197 if (!strncmp(drv->name, k, len) && !drv->name[len])
200 for (i = 0; i < ARRAY_SIZE(builtin_drivers); i++) {
201 struct userdiff_driver *drv = builtin_drivers + i;
202 if (!strncmp(drv->name, k, len) && !drv->name[len])
208 static int parse_funcname(struct userdiff_funcname *f, const char *k,
209 const char *v, int cflags)
211 if (git_config_string(&f->pattern, k, v) < 0)
217 static int parse_tristate(int *b, const char *k, const char *v)
219 if (v && !strcasecmp(v, "auto"))
222 *b = git_config_bool(k, v);
226 static int parse_bool(int *b, const char *k, const char *v)
228 *b = git_config_bool(k, v);
232 int userdiff_config(const char *k, const char *v)
234 struct userdiff_driver *drv;
235 const char *name, *type;
238 if (parse_config_key(k, "diff", &name, &namelen, &type) || !name)
241 drv = userdiff_find_by_namelen(name, namelen);
243 ALLOC_GROW(drivers, ndrivers+1, drivers_alloc);
244 drv = &drivers[ndrivers++];
245 memset(drv, 0, sizeof(*drv));
246 drv->name = xmemdupz(name, namelen);
250 if (!strcmp(type, "funcname"))
251 return parse_funcname(&drv->funcname, k, v, 0);
252 if (!strcmp(type, "xfuncname"))
253 return parse_funcname(&drv->funcname, k, v, REG_EXTENDED);
254 if (!strcmp(type, "binary"))
255 return parse_tristate(&drv->binary, k, v);
256 if (!strcmp(type, "command"))
257 return git_config_string(&drv->external, k, v);
258 if (!strcmp(type, "textconv"))
259 return git_config_string(&drv->textconv, k, v);
260 if (!strcmp(type, "cachetextconv"))
261 return parse_bool(&drv->textconv_want_cache, k, v);
262 if (!strcmp(type, "wordregex"))
263 return git_config_string(&drv->word_regex, k, v);
268 struct userdiff_driver *userdiff_find_by_name(const char *name) {
269 int len = strlen(name);
270 return userdiff_find_by_namelen(name, len);
273 struct userdiff_driver *userdiff_find_by_path(struct index_state *istate,
276 static struct attr_check *check;
279 check = attr_check_initl("diff", NULL);
282 git_check_attr(istate, path, check);
284 if (ATTR_TRUE(check->items[0].value))
286 if (ATTR_FALSE(check->items[0].value))
287 return &driver_false;
288 if (ATTR_UNSET(check->items[0].value))
290 return userdiff_find_by_name(check->items[0].value);
293 struct userdiff_driver *userdiff_get_textconv(struct repository *r,
294 struct userdiff_driver *driver)
296 if (!driver->textconv)
299 if (driver->textconv_want_cache && !driver->textconv_cache) {
300 struct notes_cache *c = xmalloc(sizeof(*c));
301 struct strbuf name = STRBUF_INIT;
303 strbuf_addf(&name, "textconv/%s", driver->name);
304 notes_cache_init(r, c, name.buf, driver->textconv);
305 driver->textconv_cache = c;
306 strbuf_release(&name);