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[] = {
19 "!^[ \t]*MODULE[ \t]+PROCEDURE[ \t]\n"
20 "^[ \t]*((END[ \t]+)?(PROGRAM|MODULE|BLOCK[ \t]+DATA"
21 "|([^'\" \t]+[ \t]+)*(SUBROUTINE|FUNCTION))[ \t]+[A-Z].*)$",
23 "[a-zA-Z][a-zA-Z0-9_]*"
24 "|\\.([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])\\."
25 /* numbers and format statements like 2E14.4, or ES12.6, 9X.
26 * Don't worry about format statements without leading digits since
27 * they would have been matched above as a variable anyway. */
28 "|[-+]?[0-9.]+([AaIiDdEeFfLlTtXx][Ss]?[-+]?[0-9.]*)?(_[a-zA-Z0-9][a-zA-Z0-9_]*)?"
29 "|//|\\*\\*|::|[/<>=]="),
30 PATTERNS("html", "^[ \t]*(<[Hh][1-6][ \t].*>.*)$",
33 "!^[ \t]*(catch|do|for|if|instanceof|new|return|switch|throw|while)\n"
34 "^[ \t]*(([A-Za-z_][A-Za-z_0-9]*[ \t]+)+[A-Za-z_][A-Za-z_0-9]*[ \t]*\\([^;]*)$",
36 "[a-zA-Z_][a-zA-Z0-9_]*"
37 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?"
39 "|--|\\+\\+|<<=?|>>>?=?|&&|\\|\\|"),
41 "^[[:space:]]*((classdef|function)[[:space:]].*)$|^%%[[:space:]].*$",
42 "[a-zA-Z_][a-zA-Z0-9_]*|[-+0-9.e]+|[=~<>]=|\\.[*/\\^']|\\|\\||&&"),
44 /* Negate C statements that can look like functions */
45 "!^[ \t]*(do|for|if|else|return|switch|while)\n"
46 /* Objective-C methods */
47 "^[ \t]*([-+][ \t]*\\([ \t]*[A-Za-z_][A-Za-z_0-9* \t]*\\)[ \t]*[A-Za-z_].*)$\n"
49 "^[ \t]*(([A-Za-z_][A-Za-z_0-9]*[ \t]+)+[A-Za-z_][A-Za-z_0-9]*[ \t]*\\([^;]*)$\n"
50 /* Objective-C class/protocol definitions */
51 "^(@(implementation|interface|protocol)[ \t].*)$",
53 "[a-zA-Z_][a-zA-Z0-9_]*"
54 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?"
55 "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->"),
57 "^(((class[ \t]+)?(procedure|function)|constructor|destructor|interface|"
58 "implementation|initialization|finalization)[ \t]*.*)$"
60 "^(.*=[ \t]*(class|record).*)$",
62 "[a-zA-Z_][a-zA-Z0-9_]*"
63 "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+"
64 "|<>|<=|>=|:=|\\.\\."),
67 "^sub [[:alnum:]_':]+[ \t]*"
68 "(\\([^)]*\\)[ \t]*)?" /* prototype */
70 * Attributes. A regex can't count nested parentheses,
71 * so just slurp up whatever we see, taking care not
72 * to accept lines like "sub foo; # defined elsewhere".
74 * An attribute could contain a semicolon, but at that
75 * point it seems reasonable enough to give up.
78 "(\\{[ \t]*)?" /* brace can come here or on the next line */
79 "(#.*)?$\n" /* comment */
80 "^(BEGIN|END|INIT|CHECK|UNITCHECK|AUTOLOAD|DESTROY)[ \t]*"
81 "(\\{[ \t]*)?" /* brace can come here or on the next line */
83 "^=head[0-9] .*", /* POD */
85 "[[:alpha:]_'][[:alnum:]_']*"
86 "|0[xb]?[0-9a-fA-F_]*"
87 /* taking care not to interpret 3..5 as (3.)(.5) */
88 "|[0-9a-fA-F_]+(\\.[0-9a-fA-F_]+)?([eE][-+]?[0-9_]+)?"
89 "|=>|-[rwxoRWXOezsfdlpSugkbctTBMAC>]|~~|::"
90 "|&&=|\\|\\|=|//=|\\*\\*="
91 "|&&|\\|\\||//|\\+\\+|--|\\*\\*|\\.\\.\\.?"
96 "^[\t ]*(((public|protected|private|static)[\t ]+)*function.*)$\n"
99 "[a-zA-Z_][a-zA-Z0-9_]*"
100 "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+"
101 "|[-+*/<>%&^|=!.]=|--|\\+\\+|<<=?|>>=?|===|&&|\\|\\||::|->"),
102 PATTERNS("python", "^[ \t]*((class|def)[ \t].*)$",
104 "[a-zA-Z_][a-zA-Z0-9_]*"
105 "|[-+0-9.e]+[jJlL]?|0[xX]?[0-9a-fA-F]+[lL]?"
106 "|[-+*/<>%&^|=!]=|//=?|<<=?|>>=?|\\*\\*=?"),
108 PATTERNS("ruby", "^[ \t]*((class|module|def)[ \t].*)$",
110 "(@|@@|\\$)?[a-zA-Z_][a-zA-Z0-9_]*"
111 "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+|\\?(\\\\C-)?(\\\\M-)?."
112 "|//=?|[-+*/<>%&^|=!]=|<<=?|>>=?|===|\\.{1,3}|::|[!=]~"),
113 PATTERNS("bibtex", "(@[a-zA-Z]{1,}[ \t]*\\{{0,1}[ \t]*[^ \t\"@',\\#}{~%]*).*$",
114 "[={}\"]|[^={}\" \t]+"),
115 PATTERNS("tex", "^(\\\\((sub)*section|chapter|part)\\*{0,1}\\{.*)$",
116 "\\\\[a-zA-Z@]+|\\\\.|[a-zA-Z0-9\x80-\xff]+"),
118 /* Jump targets or access declarations */
119 "!^[ \t]*[A-Za-z_][A-Za-z_0-9]*:.*$\n"
120 /* C/++ functions/methods at top level */
121 "^([A-Za-z_][A-Za-z_0-9]*([ \t*]+[A-Za-z_][A-Za-z_0-9]*([ \t]*::[ \t]*[^[:space:]]+)?){1,}[ \t]*\\([^;]*)$\n"
122 /* compound type at top level */
123 "^((struct|class|enum)[^;]*)$",
125 "[a-zA-Z_][a-zA-Z0-9_]*"
126 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?"
127 "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->"),
130 "!^[ \t]*(do|while|for|if|else|instanceof|new|return|switch|case|throw|catch|using)\n"
131 /* Methods and constructors */
132 "^[ \t]*(((static|public|internal|private|protected|new|virtual|sealed|override|unsafe)[ \t]+)*[][<>@.~_[:alnum:]]+[ \t]+[<>@._[:alnum:]]+[ \t]*\\(.*\\))[ \t]*$\n"
134 "^[ \t]*(((static|public|internal|private|protected|new|virtual|sealed|override|unsafe)[ \t]+)*[][<>@.~_[:alnum:]]+[ \t]+[@._[:alnum:]]+)[ \t]*$\n"
135 /* Type definitions */
136 "^[ \t]*(((static|public|internal|private|protected|new|unsafe|sealed|abstract|partial)[ \t]+)*(class|enum|interface|struct)[ \t]+.*)$\n"
138 "^[ \t]*(namespace[ \t]+.*)$",
140 "[a-zA-Z_][a-zA-Z0-9_]*"
141 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?"
142 "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->"),
143 { "default", NULL, -1, { NULL, 0 } },
148 static struct userdiff_driver driver_true = {
155 static struct userdiff_driver driver_false = {
162 static struct userdiff_driver *userdiff_find_by_namelen(const char *k, int len)
165 for (i = 0; i < ndrivers; i++) {
166 struct userdiff_driver *drv = drivers + i;
167 if (!strncmp(drv->name, k, len) && !drv->name[len])
170 for (i = 0; i < ARRAY_SIZE(builtin_drivers); i++) {
171 struct userdiff_driver *drv = builtin_drivers + i;
172 if (!strncmp(drv->name, k, len) && !drv->name[len])
178 static struct userdiff_driver *parse_driver(const char *var,
179 const char *value, const char *type)
181 struct userdiff_driver *drv;
186 if (prefixcmp(var, "diff."))
188 dot = strrchr(var, '.');
191 if (strcmp(type, dot+1))
195 namelen = dot - name;
196 drv = userdiff_find_by_namelen(name, namelen);
198 ALLOC_GROW(drivers, ndrivers+1, drivers_alloc);
199 drv = &drivers[ndrivers++];
200 memset(drv, 0, sizeof(*drv));
201 drv->name = xmemdupz(name, namelen);
207 static int parse_funcname(struct userdiff_funcname *f, const char *k,
208 const char *v, int cflags)
210 if (git_config_string(&f->pattern, k, v) < 0)
216 static int parse_tristate(int *b, const char *k, const char *v)
218 if (v && !strcasecmp(v, "auto"))
221 *b = git_config_bool(k, v);
225 static int parse_bool(int *b, const char *k, const char *v)
227 *b = git_config_bool(k, v);
231 int userdiff_config(const char *k, const char *v)
233 struct userdiff_driver *drv;
235 if ((drv = parse_driver(k, v, "funcname")))
236 return parse_funcname(&drv->funcname, k, v, 0);
237 if ((drv = parse_driver(k, v, "xfuncname")))
238 return parse_funcname(&drv->funcname, k, v, REG_EXTENDED);
239 if ((drv = parse_driver(k, v, "binary")))
240 return parse_tristate(&drv->binary, k, v);
241 if ((drv = parse_driver(k, v, "command")))
242 return git_config_string(&drv->external, k, v);
243 if ((drv = parse_driver(k, v, "textconv")))
244 return git_config_string(&drv->textconv, k, v);
245 if ((drv = parse_driver(k, v, "cachetextconv")))
246 return parse_bool(&drv->textconv_want_cache, k, v);
247 if ((drv = parse_driver(k, v, "wordregex")))
248 return git_config_string(&drv->word_regex, k, v);
253 struct userdiff_driver *userdiff_find_by_name(const char *name) {
254 int len = strlen(name);
255 return userdiff_find_by_namelen(name, len);
258 struct userdiff_driver *userdiff_find_by_path(const char *path)
260 static struct git_attr *attr;
261 struct git_attr_check check;
264 attr = git_attr("diff");
269 if (git_check_attr(path, 1, &check))
272 if (ATTR_TRUE(check.value))
274 if (ATTR_FALSE(check.value))
275 return &driver_false;
276 if (ATTR_UNSET(check.value))
278 return userdiff_find_by_name(check.value);
281 struct userdiff_driver *userdiff_get_textconv(struct userdiff_driver *driver)
283 if (!driver->textconv)
286 if (driver->textconv_want_cache && !driver->textconv_cache) {
287 struct notes_cache *c = xmalloc(sizeof(*c));
288 struct strbuf name = STRBUF_INIT;
290 strbuf_addf(&name, "textconv/%s", driver->name);
291 notes_cache_init(c, name.buf, driver->textconv);
292 driver->textconv_cache = c;