GIT 1.6.1.2
[4msysgit-hv.git] / userdiff.c
blob3681062ebfef85af08d71ed6e1ff734804906d6a
1 #include "userdiff.h"
2 #include "cache.h"
3 #include "attr.h"
5 static struct userdiff_driver *drivers;
6 static int ndrivers;
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].*>.*)$"),
13 FUNCNAME("java",
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]*\\([^;]*)$"),
16 FUNCNAME("objc",
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"
21 /* C functions */
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].*)$"),
25 FUNCNAME("pascal",
26 "^((procedure|function|constructor|destructor|interface|"
27 "implementation|initialization|finalization)[ \t]*.*)$"
28 "\n"
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 } },
37 #undef FUNCNAME
39 static struct userdiff_driver driver_true = {
40 "diff=true",
41 NULL,
43 { NULL, 0 }
46 static struct userdiff_driver driver_false = {
47 "!diff",
48 NULL,
50 { NULL, 0 }
53 static struct userdiff_driver *userdiff_find_by_namelen(const char *k, int len)
55 int i;
56 for (i = 0; i < ndrivers; i++) {
57 struct userdiff_driver *drv = drivers + i;
58 if (!strncmp(drv->name, k, len) && !drv->name[len])
59 return drv;
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])
64 return drv;
66 return NULL;
69 static struct userdiff_driver *parse_driver(const char *var,
70 const char *value, const char *type)
72 struct userdiff_driver *drv;
73 const char *dot;
74 const char *name;
75 int namelen;
77 if (prefixcmp(var, "diff."))
78 return NULL;
79 dot = strrchr(var, '.');
80 if (dot == var + 4)
81 return NULL;
82 if (strcmp(type, dot+1))
83 return NULL;
85 name = var + 5;
86 namelen = dot - name;
87 drv = userdiff_find_by_namelen(name, namelen);
88 if (!drv) {
89 ALLOC_GROW(drivers, ndrivers+1, drivers_alloc);
90 drv = &drivers[ndrivers++];
91 memset(drv, 0, sizeof(*drv));
92 drv->name = xmemdupz(name, namelen);
93 drv->binary = -1;
95 return drv;
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)
102 return -1;
103 f->cflags = cflags;
104 return 1;
107 static int parse_string(const char **d, const char *k, const char *v)
109 if (git_config_string(d, k, v) < 0)
110 return -1;
111 return 1;
114 static int parse_tristate(int *b, const char *k, const char *v)
116 if (v && !strcasecmp(v, "auto"))
117 *b = -1;
118 else
119 *b = git_config_bool(k, v);
120 return 1;
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);
138 return 0;
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;
151 if (!attr)
152 attr = git_attr("diff", 4);
153 check.attr = attr;
155 if (!path)
156 return NULL;
157 if (git_checkattr(path, 1, &check))
158 return NULL;
160 if (ATTR_TRUE(check.value))
161 return &driver_true;
162 if (ATTR_FALSE(check.value))
163 return &driver_false;
164 if (ATTR_UNSET(check.value))
165 return NULL;
166 return userdiff_find_by_name(check.value);