5 static struct userdiff_driver *drivers;
7 static int drivers_alloc;
9 #define FUNCNAME(name, pattern) \
10 { name, NULL, -1, { pattern, REG_EXTENDED } }
11 static struct userdiff_driver builtin_drivers[] = {
12 FUNCNAME("html", "^[ \t]*(<[Hh][1-6][ \t].*>.*)$"),
14 "!^[ \t]*(catch|do|for|if|instanceof|new|return|switch|throw|while)\n"
15 "^[ \t]*(([ \t]*[A-Za-z_][A-Za-z_0-9]*){2,}[ \t]*\\([^;]*)$"),
17 /* Negate C statements that can look like functions */
18 "!^[ \t]*(do|for|if|else|return|switch|while)\n"
19 /* Objective-C methods */
20 "^[ \t]*([-+][ \t]*\\([ \t]*[A-Za-z_][A-Za-z_0-9* \t]*\\)[ \t]*[A-Za-z_].*)$\n"
22 "^[ \t]*(([ \t]*[A-Za-z_][A-Za-z_0-9]*){2,}[ \t]*\\([^;]*)$\n"
23 /* Objective-C class/protocol definitions */
24 "^(@(implementation|interface|protocol)[ \t].*)$"),
26 "^((procedure|function|constructor|destructor|interface|"
27 "implementation|initialization|finalization)[ \t]*.*)$"
29 "^(.*=[ \t]*(class|record).*)$"),
30 FUNCNAME("php", "^[\t ]*((function|class).*)"),
31 FUNCNAME("python", "^[ \t]*((class|def)[ \t].*)$"),
32 FUNCNAME("ruby", "^[ \t]*((class|module|def)[ \t].*)$"),
33 FUNCNAME("bibtex", "(@[a-zA-Z]{1,}[ \t]*\\{{0,1}[ \t]*[^ \t\"@',\\#}{~%]*).*$"),
34 FUNCNAME("tex", "^(\\\\((sub)*section|chapter|part)\\*{0,1}\\{.*)$"),
35 { "default", NULL, -1, { NULL, 0 } },
39 static struct userdiff_driver driver_true = {
46 static struct userdiff_driver driver_false = {
53 static struct userdiff_driver *userdiff_find_by_namelen(const char *k, int len)
56 for (i = 0; i < ndrivers; i++) {
57 struct userdiff_driver *drv = drivers + i;
58 if (!strncmp(drv->name, k, len) && !drv->name[len])
61 for (i = 0; i < ARRAY_SIZE(builtin_drivers); i++) {
62 struct userdiff_driver *drv = builtin_drivers + i;
63 if (!strncmp(drv->name, k, len) && !drv->name[len])
69 static struct userdiff_driver *parse_driver(const char *var,
70 const char *value, const char *type)
72 struct userdiff_driver *drv;
77 if (prefixcmp(var, "diff."))
79 dot = strrchr(var, '.');
82 if (strcmp(type, dot+1))
87 drv = userdiff_find_by_namelen(name, namelen);
89 ALLOC_GROW(drivers, ndrivers+1, drivers_alloc);
90 drv = &drivers[ndrivers++];
91 memset(drv, 0, sizeof(*drv));
92 drv->name = xmemdupz(name, namelen);
98 static int parse_funcname(struct userdiff_funcname *f, const char *k,
99 const char *v, int cflags)
101 if (git_config_string(&f->pattern, k, v) < 0)
107 static int parse_string(const char **d, const char *k, const char *v)
109 if (git_config_string(d, k, v) < 0)
114 static int parse_tristate(int *b, const char *k, const char *v)
116 if (v && !strcasecmp(v, "auto"))
119 *b = git_config_bool(k, v);
123 int userdiff_config(const char *k, const char *v)
125 struct userdiff_driver *drv;
127 if ((drv = parse_driver(k, v, "funcname")))
128 return parse_funcname(&drv->funcname, k, v, 0);
129 if ((drv = parse_driver(k, v, "xfuncname")))
130 return parse_funcname(&drv->funcname, k, v, REG_EXTENDED);
131 if ((drv = parse_driver(k, v, "binary")))
132 return parse_tristate(&drv->binary, k, v);
133 if ((drv = parse_driver(k, v, "command")))
134 return parse_string(&drv->external, k, v);
135 if ((drv = parse_driver(k, v, "textconv")))
136 return parse_string(&drv->textconv, k, v);
141 struct userdiff_driver *userdiff_find_by_name(const char *name) {
142 int len = strlen(name);
143 return userdiff_find_by_namelen(name, len);
146 struct userdiff_driver *userdiff_find_by_path(const char *path)
148 static struct git_attr *attr;
149 struct git_attr_check check;
152 attr = git_attr("diff", 4);
157 if (git_checkattr(path, 1, &check))
160 if (ATTR_TRUE(check.value))
162 if (ATTR_FALSE(check.value))
163 return &driver_false;
164 if (ATTR_UNSET(check.value))
166 return userdiff_find_by_name(check.value);