Skip to content
Newer
Older
100644 176 lines (153 sloc) 4.36 KB
be58e70 @peff diff: unify external diff and funcname parsing code
peff authored
1 #include "userdiff.h"
2 #include "cache.h"
3 #include "attr.h"
4
5 static struct userdiff_driver *drivers;
6 static int ndrivers;
7 static int drivers_alloc;
8
9 #define FUNCNAME(name, pattern) \
122aa6f @peff diff: introduce diff.<driver>.binary
peff authored
10 { name, NULL, -1, { pattern, REG_EXTENDED } }
be58e70 @peff diff: unify external diff and funcname parsing code
peff authored
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}\\{.*)$"),
122aa6f @peff diff: introduce diff.<driver>.binary
peff authored
35 { "default", NULL, -1, { NULL, 0 } },
be58e70 @peff diff: unify external diff and funcname parsing code
peff authored
36 };
37 #undef FUNCNAME
38
39 static struct userdiff_driver driver_true = {
40 "diff=true",
41 NULL,
122aa6f @peff diff: introduce diff.<driver>.binary
peff authored
42 0,
be58e70 @peff diff: unify external diff and funcname parsing code
peff authored
43 { NULL, 0 }
44 };
45
46 static struct userdiff_driver driver_false = {
47 "!diff",
48 NULL,
122aa6f @peff diff: introduce diff.<driver>.binary
peff authored
49 1,
be58e70 @peff diff: unify external diff and funcname parsing code
peff authored
50 { NULL, 0 }
51 };
52
53 static struct userdiff_driver *userdiff_find_by_namelen(const char *k, int len)
54 {
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;
60 }
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;
65 }
66 return NULL;
67 }
68
69 static struct userdiff_driver *parse_driver(const char *var,
70 const char *value, const char *type)
71 {
72 struct userdiff_driver *drv;
73 const char *dot;
74 const char *name;
75 int namelen;
76
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;
84
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);
122aa6f @peff diff: introduce diff.<driver>.binary
peff authored
93 drv->binary = -1;
be58e70 @peff diff: unify external diff and funcname parsing code
peff authored
94 }
95 return drv;
96 }
97
98 static int parse_funcname(struct userdiff_funcname *f, const char *k,
99 const char *v, int cflags)
100 {
101 if (git_config_string(&f->pattern, k, v) < 0)
102 return -1;
103 f->cflags = cflags;
104 return 1;
105 }
106
107 static int parse_string(const char **d, const char *k, const char *v)
108 {
109 if (git_config_string(d, k, v) < 0)
110 return -1;
111 return 1;
112 }
113
122aa6f @peff diff: introduce diff.<driver>.binary
peff authored
114 static int parse_tristate(int *b, const char *k, const char *v)
115 {
116 if (v && !strcasecmp(v, "auto"))
117 *b = -1;
118 else
119 *b = git_config_bool(k, v);
120 return 1;
121 }
122
be58e70 @peff diff: unify external diff and funcname parsing code
peff authored
123 int userdiff_config_basic(const char *k, const char *v)
124 {
125 struct userdiff_driver *drv;
126
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);
122aa6f @peff diff: introduce diff.<driver>.binary
peff authored
131 if ((drv = parse_driver(k, v, "binary")))
132 return parse_tristate(&drv->binary, k, v);
be58e70 @peff diff: unify external diff and funcname parsing code
peff authored
133
134 return 0;
135 }
136
137 int userdiff_config_porcelain(const char *k, const char *v)
138 {
139 struct userdiff_driver *drv;
140
141 if ((drv = parse_driver(k, v, "command")))
142 return parse_string(&drv->external, k, v);
9cb92c3 @peff diff: add filter for converting binary to text
peff authored
143 if ((drv = parse_driver(k, v, "textconv")))
144 return parse_string(&drv->textconv, k, v);
be58e70 @peff diff: unify external diff and funcname parsing code
peff authored
145
146 return 0;
147 }
148
149 struct userdiff_driver *userdiff_find_by_name(const char *name) {
150 int len = strlen(name);
151 return userdiff_find_by_namelen(name, len);
152 }
153
154 struct userdiff_driver *userdiff_find_by_path(const char *path)
155 {
156 static struct git_attr *attr;
157 struct git_attr_check check;
158
159 if (!attr)
160 attr = git_attr("diff", 4);
161 check.attr = attr;
162
163 if (!path)
164 return NULL;
165 if (git_checkattr(path, 1, &check))
166 return NULL;
167
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);
175 }
Something went wrong with that request. Please try again.