compat/cygwin.c: make runtime detection of lstat/stat lessor impact
[git.git] / userdiff.c
blobd95257ab3bd4fafa59b3ed2aa58c480de274227d
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_basic(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);
134 return 0;
137 int userdiff_config_porcelain(const char *k, const char *v)
139 struct userdiff_driver *drv;
141 if ((drv = parse_driver(k, v, "command")))
142 return parse_string(&drv->external, k, v);
143 if ((drv = parse_driver(k, v, "textconv")))
144 return parse_string(&drv->textconv, k, v);
146 return 0;
149 struct userdiff_driver *userdiff_find_by_name(const char *name) {
150 int len = strlen(name);
151 return userdiff_find_by_namelen(name, len);
154 struct userdiff_driver *userdiff_find_by_path(const char *path)
156 static struct git_attr *attr;
157 struct git_attr_check check;
159 if (!attr)
160 attr = git_attr("diff", 4);
161 check.attr = attr;
163 if (!path)
164 return NULL;
165 if (git_checkattr(path, 1, &check))
166 return NULL;
168 if (ATTR_TRUE(check.value))
169 return &driver_true;
170 if (ATTR_FALSE(check.value))
171 return &driver_false;
172 if (ATTR_UNSET(check.value))
173 return NULL;
174 return userdiff_find_by_name(check.value);