diff: unify external diff and funcname parsing code
[git/git.git] / userdiff.c
CommitLineData
be58e70d
JK
1#include "userdiff.h"
2#include "cache.h"
3#include "attr.h"
4
5static struct userdiff_driver *drivers;
6static int ndrivers;
7static int drivers_alloc;
8
9#define FUNCNAME(name, pattern) \
10 { name, NULL, { pattern, REG_EXTENDED } }
11static struct userdiff_driver builtin_drivers[] = {
12FUNCNAME("html", "^[ \t]*(<[Hh][1-6][ \t].*>.*)$"),
13FUNCNAME("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]*\\([^;]*)$"),
16FUNCNAME("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].*)$"),
25FUNCNAME("pascal",
26 "^((procedure|function|constructor|destructor|interface|"
27 "implementation|initialization|finalization)[ \t]*.*)$"
28 "\n"
29 "^(.*=[ \t]*(class|record).*)$"),
30FUNCNAME("php", "^[\t ]*((function|class).*)"),
31FUNCNAME("python", "^[ \t]*((class|def)[ \t].*)$"),
32FUNCNAME("ruby", "^[ \t]*((class|module|def)[ \t].*)$"),
33FUNCNAME("bibtex", "(@[a-zA-Z]{1,}[ \t]*\\{{0,1}[ \t]*[^ \t\"@',\\#}{~%]*).*$"),
34FUNCNAME("tex", "^(\\\\((sub)*section|chapter|part)\\*{0,1}\\{.*)$"),
35};
36#undef FUNCNAME
37
38static struct userdiff_driver driver_true = {
39 "diff=true",
40 NULL,
41 { NULL, 0 }
42};
43struct userdiff_driver *USERDIFF_ATTR_TRUE = &driver_true;
44
45static struct userdiff_driver driver_false = {
46 "!diff",
47 NULL,
48 { NULL, 0 }
49};
50struct userdiff_driver *USERDIFF_ATTR_FALSE = &driver_false;
51
52static struct userdiff_driver *userdiff_find_by_namelen(const char *k, int len)
53{
54 int i;
55 for (i = 0; i < ndrivers; i++) {
56 struct userdiff_driver *drv = drivers + i;
57 if (!strncmp(drv->name, k, len) && !drv->name[len])
58 return drv;
59 }
60 for (i = 0; i < ARRAY_SIZE(builtin_drivers); i++) {
61 struct userdiff_driver *drv = builtin_drivers + i;
62 if (!strncmp(drv->name, k, len) && !drv->name[len])
63 return drv;
64 }
65 return NULL;
66}
67
68static struct userdiff_driver *parse_driver(const char *var,
69 const char *value, const char *type)
70{
71 struct userdiff_driver *drv;
72 const char *dot;
73 const char *name;
74 int namelen;
75
76 if (prefixcmp(var, "diff."))
77 return NULL;
78 dot = strrchr(var, '.');
79 if (dot == var + 4)
80 return NULL;
81 if (strcmp(type, dot+1))
82 return NULL;
83
84 name = var + 5;
85 namelen = dot - name;
86 drv = userdiff_find_by_namelen(name, namelen);
87 if (!drv) {
88 ALLOC_GROW(drivers, ndrivers+1, drivers_alloc);
89 drv = &drivers[ndrivers++];
90 memset(drv, 0, sizeof(*drv));
91 drv->name = xmemdupz(name, namelen);
92 }
93 return drv;
94}
95
96static int parse_funcname(struct userdiff_funcname *f, const char *k,
97 const char *v, int cflags)
98{
99 if (git_config_string(&f->pattern, k, v) < 0)
100 return -1;
101 f->cflags = cflags;
102 return 1;
103}
104
105static int parse_string(const char **d, const char *k, const char *v)
106{
107 if (git_config_string(d, k, v) < 0)
108 return -1;
109 return 1;
110}
111
112int userdiff_config_basic(const char *k, const char *v)
113{
114 struct userdiff_driver *drv;
115
116 if ((drv = parse_driver(k, v, "funcname")))
117 return parse_funcname(&drv->funcname, k, v, 0);
118 if ((drv = parse_driver(k, v, "xfuncname")))
119 return parse_funcname(&drv->funcname, k, v, REG_EXTENDED);
120
121 return 0;
122}
123
124int userdiff_config_porcelain(const char *k, const char *v)
125{
126 struct userdiff_driver *drv;
127
128 if ((drv = parse_driver(k, v, "command")))
129 return parse_string(&drv->external, k, v);
130
131 return 0;
132}
133
134struct userdiff_driver *userdiff_find_by_name(const char *name) {
135 int len = strlen(name);
136 return userdiff_find_by_namelen(name, len);
137}
138
139struct userdiff_driver *userdiff_find_by_path(const char *path)
140{
141 static struct git_attr *attr;
142 struct git_attr_check check;
143
144 if (!attr)
145 attr = git_attr("diff", 4);
146 check.attr = attr;
147
148 if (!path)
149 return NULL;
150 if (git_checkattr(path, 1, &check))
151 return NULL;
152
153 if (ATTR_TRUE(check.value))
154 return &driver_true;
155 if (ATTR_FALSE(check.value))
156 return &driver_false;
157 if (ATTR_UNSET(check.value))
158 return NULL;
159 return userdiff_find_by_name(check.value);
160}