5 static struct userdiff_driver *drivers;
7 static int drivers_alloc;
9 #define PATTERNS(name, pattern, word_regex) \
10 { name, NULL, -1, { pattern, REG_EXTENDED }, word_regex }
11 static struct userdiff_driver builtin_drivers[] = {
12 PATTERNS("html", "^[ \t]*(<[Hh][1-6][ \t].*>.*)$",
13 "[^<>= \t]+|[^[:space:]]|[\x80-\xff]+"),
15 "!^[ \t]*(catch|do|for|if|instanceof|new|return|switch|throw|while)\n"
16 "^[ \t]*(([ \t]*[A-Za-z_][A-Za-z_0-9]*){2,}[ \t]*\\([^;]*)$",
17 "[a-zA-Z_][a-zA-Z0-9_]*"
18 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?"
20 "|--|\\+\\+|<<=?|>>>?=?|&&|\\|\\|"
21 "|[^[:space:]]|[\x80-\xff]+"),
23 /* Negate C statements that can look like functions */
24 "!^[ \t]*(do|for|if|else|return|switch|while)\n"
25 /* Objective-C methods */
26 "^[ \t]*([-+][ \t]*\\([ \t]*[A-Za-z_][A-Za-z_0-9* \t]*\\)[ \t]*[A-Za-z_].*)$\n"
28 "^[ \t]*(([ \t]*[A-Za-z_][A-Za-z_0-9]*){2,}[ \t]*\\([^;]*)$\n"
29 /* Objective-C class/protocol definitions */
30 "^(@(implementation|interface|protocol)[ \t].*)$",
32 "[a-zA-Z_][a-zA-Z0-9_]*"
33 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?"
34 "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->"
35 "|[^[:space:]]|[\x80-\xff]+"),
37 "^((procedure|function|constructor|destructor|interface|"
38 "implementation|initialization|finalization)[ \t]*.*)$"
40 "^(.*=[ \t]*(class|record).*)$",
42 "[a-zA-Z_][a-zA-Z0-9_]*"
43 "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+"
45 "|[^[:space:]]|[\x80-\xff]+"),
46 PATTERNS("php", "^[\t ]*((function|class).*)",
48 "[a-zA-Z_][a-zA-Z0-9_]*"
49 "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+"
50 "|[-+*/<>%&^|=!.]=|--|\\+\\+|<<=?|>>=?|===|&&|\\|\\||::|->"
51 "|[^[:space:]]|[\x80-\xff]+"),
52 PATTERNS("python", "^[ \t]*((class|def)[ \t].*)$",
54 "[a-zA-Z_][a-zA-Z0-9_]*"
55 "|[-+0-9.e]+[jJlL]?|0[xX]?[0-9a-fA-F]+[lL]?"
56 "|[-+*/<>%&^|=!]=|//=?|<<=?|>>=?|\\*\\*=?"
57 "|[^[:space:]|[\x80-\xff]+"),
59 PATTERNS("ruby", "^[ \t]*((class|module|def)[ \t].*)$",
61 "(@|@@|\\$)?[a-zA-Z_][a-zA-Z0-9_]*"
62 "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+|\\?(\\\\C-)?(\\\\M-)?."
63 "|//=?|[-+*/<>%&^|=!]=|<<=?|>>=?|===|\\.{1,3}|::|[!=]~"
64 "|[^[:space:]|[\x80-\xff]+"),
65 PATTERNS("bibtex", "(@[a-zA-Z]{1,}[ \t]*\\{{0,1}[ \t]*[^ \t\"@',\\#}{~%]*).*$",
66 "[={}\"]|[^={}\" \t]+"),
67 PATTERNS("tex", "^(\\\\((sub)*section|chapter|part)\\*{0,1}\\{.*)$",
68 "\\\\[a-zA-Z@]+|\\\\.|[a-zA-Z0-9\x80-\xff]+|[^[:space:]]"),
70 /* Jump targets or access declarations */
71 "!^[ \t]*[A-Za-z_][A-Za-z_0-9]*:.*$\n"
72 /* C/++ functions/methods at top level */
73 "^([A-Za-z_][A-Za-z_0-9]*([ \t]+[A-Za-z_][A-Za-z_0-9]*([ \t]*::[ \t]*[^[:space:]]+)?){1,}[ \t]*\\([^;]*)$\n"
74 /* compound type at top level */
75 "^((struct|class|enum)[^;]*)$",
77 "[a-zA-Z_][a-zA-Z0-9_]*"
78 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?"
79 "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->"
80 "|[^[:space:]]|[\x80-\xff]+"),
81 { "default", NULL, -1, { NULL, 0 } },
85 static struct userdiff_driver driver_true = {
92 static struct userdiff_driver driver_false = {
99 static struct userdiff_driver *userdiff_find_by_namelen(const char *k, int len)
102 for (i = 0; i < ndrivers; i++) {
103 struct userdiff_driver *drv = drivers + i;
104 if (!strncmp(drv->name, k, len) && !drv->name[len])
107 for (i = 0; i < ARRAY_SIZE(builtin_drivers); i++) {
108 struct userdiff_driver *drv = builtin_drivers + i;
109 if (!strncmp(drv->name, k, len) && !drv->name[len])
115 static struct userdiff_driver *parse_driver(const char *var,
116 const char *value, const char *type)
118 struct userdiff_driver *drv;
123 if (prefixcmp(var, "diff."))
125 dot = strrchr(var, '.');
128 if (strcmp(type, dot+1))
132 namelen = dot - name;
133 drv = userdiff_find_by_namelen(name, namelen);
135 ALLOC_GROW(drivers, ndrivers+1, drivers_alloc);
136 drv = &drivers[ndrivers++];
137 memset(drv, 0, sizeof(*drv));
138 drv->name = xmemdupz(name, namelen);
144 static int parse_funcname(struct userdiff_funcname *f, const char *k,
145 const char *v, int cflags)
147 if (git_config_string(&f->pattern, k, v) < 0)
153 static int parse_string(const char **d, const char *k, const char *v)
155 if (git_config_string(d, k, v) < 0)
160 static int parse_tristate(int *b, const char *k, const char *v)
162 if (v && !strcasecmp(v, "auto"))
165 *b = git_config_bool(k, v);
169 int userdiff_config(const char *k, const char *v)
171 struct userdiff_driver *drv;
173 if ((drv = parse_driver(k, v, "funcname")))
174 return parse_funcname(&drv->funcname, k, v, 0);
175 if ((drv = parse_driver(k, v, "xfuncname")))
176 return parse_funcname(&drv->funcname, k, v, REG_EXTENDED);
177 if ((drv = parse_driver(k, v, "binary")))
178 return parse_tristate(&drv->binary, k, v);
179 if ((drv = parse_driver(k, v, "command")))
180 return parse_string(&drv->external, k, v);
181 if ((drv = parse_driver(k, v, "textconv")))
182 return parse_string(&drv->textconv, k, v);
183 if ((drv = parse_driver(k, v, "wordregex")))
184 return parse_string(&drv->word_regex, k, v);
189 struct userdiff_driver *userdiff_find_by_name(const char *name) {
190 int len = strlen(name);
191 return userdiff_find_by_namelen(name, len);
194 struct userdiff_driver *userdiff_find_by_path(const char *path)
196 static struct git_attr *attr;
197 struct git_attr_check check;
200 attr = git_attr("diff", 4);
205 if (git_checkattr(path, 1, &check))
208 if (ATTR_TRUE(check.value))
210 if (ATTR_FALSE(check.value))
211 return &driver_false;
212 if (ATTR_UNSET(check.value))
214 return userdiff_find_by_name(check.value);