Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 261 lines (240 sloc) 8.216 kb
d9bae1a @peff diff: cache textconv output
peff authored
1 #include "cache.h"
be58e70 @peff diff: unify external diff and funcname parsing code
peff authored
2 #include "userdiff.h"
3 #include "cache.h"
4 #include "attr.h"
5
6 static struct userdiff_driver *drivers;
7 static int ndrivers;
8 static int drivers_alloc;
9
ae3b970 @stephen-smith Change the spelling of "wordregex".
stephen-smith authored
10 #define PATTERNS(name, pattern, word_regex) \
11 { name, NULL, -1, { pattern, REG_EXTENDED }, word_regex }
909a549 @drafnel userdiff.c: add builtin fortran regex patterns
drafnel authored
12 #define IPATTERN(name, pattern, word_regex) \
13 { name, NULL, -1, { pattern, REG_EXTENDED | REG_ICASE }, word_regex }
be58e70 @peff diff: unify external diff and funcname parsing code
peff authored
14 static struct userdiff_driver builtin_drivers[] = {
909a549 @drafnel userdiff.c: add builtin fortran regex patterns
drafnel authored
15 IPATTERN("fortran",
16 "!^([C*]|[ \t]*!)\n"
17 "!^[ \t]*MODULE[ \t]+PROCEDURE[ \t]\n"
18 "^[ \t]*((END[ \t]+)?(PROGRAM|MODULE|BLOCK[ \t]+DATA"
19 "|([^'\" \t]+[ \t]+)*(SUBROUTINE|FUNCTION))[ \t]+[A-Z].*)$",
20 /* -- */
21 "[a-zA-Z][a-zA-Z0-9_]*"
22 "|\\.([Ee][Qq]|[Nn][Ee]|[Gg][TtEe]|[Ll][TtEe]|[Tt][Rr][Uu][Ee]|[Ff][Aa][Ll][Ss][Ee]|[Aa][Nn][Dd]|[Oo][Rr]|[Nn]?[Ee][Qq][Vv]|[Nn][Oo][Tt])\\."
23 /* numbers and format statements like 2E14.4, or ES12.6, 9X.
24 * Don't worry about format statements without leading digits since
25 * they would have been matched above as a variable anyway. */
26 "|[-+]?[0-9.]+([AaIiDdEeFfLlTtXx][Ss]?[-+]?[0-9.]*)?(_[a-zA-Z0-9][a-zA-Z0-9_]*)?"
27 "|//|\\*\\*|::|[/<>=]="
28 "|[^[:space:]]|[\x80-\xff]+"),
80c49c3 @trast color-words: make regex configurable via attributes
trast authored
29 PATTERNS("html", "^[ \t]*(<[Hh][1-6][ \t].*>.*)$",
30 "[^<>= \t]+|[^[:space:]]|[\x80-\xff]+"),
31 PATTERNS("java",
be58e70 @peff diff: unify external diff and funcname parsing code
peff authored
32 "!^[ \t]*(catch|do|for|if|instanceof|new|return|switch|throw|while)\n"
959e2e6 @bonzini avoid exponential regex match for java and objc function names
bonzini authored
33 "^[ \t]*(([A-Za-z_][A-Za-z_0-9]*[ \t]+)+[A-Za-z_][A-Za-z_0-9]*[ \t]*\\([^;]*)$",
34 /* -- */
80c49c3 @trast color-words: make regex configurable via attributes
trast authored
35 "[a-zA-Z_][a-zA-Z0-9_]*"
36 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?"
37 "|[-+*/<>%&^|=!]="
38 "|--|\\+\\+|<<=?|>>>?=?|&&|\\|\\|"
39 "|[^[:space:]]|[\x80-\xff]+"),
40 PATTERNS("objc",
be58e70 @peff diff: unify external diff and funcname parsing code
peff authored
41 /* Negate C statements that can look like functions */
42 "!^[ \t]*(do|for|if|else|return|switch|while)\n"
43 /* Objective-C methods */
44 "^[ \t]*([-+][ \t]*\\([ \t]*[A-Za-z_][A-Za-z_0-9* \t]*\\)[ \t]*[A-Za-z_].*)$\n"
45 /* C functions */
959e2e6 @bonzini avoid exponential regex match for java and objc function names
bonzini authored
46 "^[ \t]*(([A-Za-z_][A-Za-z_0-9]*[ \t]+)+[A-Za-z_][A-Za-z_0-9]*[ \t]*\\([^;]*)$\n"
be58e70 @peff diff: unify external diff and funcname parsing code
peff authored
47 /* Objective-C class/protocol definitions */
80c49c3 @trast color-words: make regex configurable via attributes
trast authored
48 "^(@(implementation|interface|protocol)[ \t].*)$",
49 /* -- */
50 "[a-zA-Z_][a-zA-Z0-9_]*"
51 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?"
52 "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->"
53 "|[^[:space:]]|[\x80-\xff]+"),
54 PATTERNS("pascal",
ad5b694 @Zapped userdiff: match Pascal class methods
Zapped authored
55 "^(((class[ \t]+)?(procedure|function)|constructor|destructor|interface|"
be58e70 @peff diff: unify external diff and funcname parsing code
peff authored
56 "implementation|initialization|finalization)[ \t]*.*)$"
57 "\n"
80c49c3 @trast color-words: make regex configurable via attributes
trast authored
58 "^(.*=[ \t]*(class|record).*)$",
59 /* -- */
60 "[a-zA-Z_][a-zA-Z0-9_]*"
61 "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+"
62 "|<>|<=|>=|:=|\\.\\."
63 "|[^[:space:]]|[\x80-\xff]+"),
6d2f208 @dotdash diff: Support visibility modifiers in the PHP hunk header regexp
dotdash authored
64 PATTERNS("php",
65 "^[\t ]*(((public|protected|private|static)[\t ]+)*function.*)$\n"
66 "^[\t ]*(class.*)$",
80c49c3 @trast color-words: make regex configurable via attributes
trast authored
67 /* -- */
68 "[a-zA-Z_][a-zA-Z0-9_]*"
69 "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+"
70 "|[-+*/<>%&^|=!.]=|--|\\+\\+|<<=?|>>=?|===|&&|\\|\\||::|->"
71 "|[^[:space:]]|[\x80-\xff]+"),
72 PATTERNS("python", "^[ \t]*((class|def)[ \t].*)$",
73 /* -- */
74 "[a-zA-Z_][a-zA-Z0-9_]*"
75 "|[-+0-9.e]+[jJlL]?|0[xX]?[0-9a-fA-F]+[lL]?"
76 "|[-+*/<>%&^|=!]=|//=?|<<=?|>>=?|\\*\\*=?"
b34f69f @trast userdiff: fix typo in ruby and python word regexes
trast authored
77 "|[^[:space:]]|[\x80-\xff]+"),
80c49c3 @trast color-words: make regex configurable via attributes
trast authored
78 /* -- */
79 PATTERNS("ruby", "^[ \t]*((class|module|def)[ \t].*)$",
80 /* -- */
81 "(@|@@|\\$)?[a-zA-Z_][a-zA-Z0-9_]*"
82 "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+|\\?(\\\\C-)?(\\\\M-)?."
83 "|//=?|[-+*/<>%&^|=!]=|<<=?|>>=?|===|\\.{1,3}|::|[!=]~"
b34f69f @trast userdiff: fix typo in ruby and python word regexes
trast authored
84 "|[^[:space:]]|[\x80-\xff]+"),
80c49c3 @trast color-words: make regex configurable via attributes
trast authored
85 PATTERNS("bibtex", "(@[a-zA-Z]{1,}[ \t]*\\{{0,1}[ \t]*[^ \t\"@',\\#}{~%]*).*$",
86 "[={}\"]|[^={}\" \t]+"),
87 PATTERNS("tex", "^(\\\\((sub)*section|chapter|part)\\*{0,1}\\{.*)$",
88 "\\\\[a-zA-Z@]+|\\\\.|[a-zA-Z0-9\x80-\xff]+|[^[:space:]]"),
89 PATTERNS("cpp",
90 /* Jump targets or access declarations */
91 "!^[ \t]*[A-Za-z_][A-Za-z_0-9]*:.*$\n"
92 /* C/++ functions/methods at top level */
93 "^([A-Za-z_][A-Za-z_0-9]*([ \t]+[A-Za-z_][A-Za-z_0-9]*([ \t]*::[ \t]*[^[:space:]]+)?){1,}[ \t]*\\([^;]*)$\n"
94 /* compound type at top level */
95 "^((struct|class|enum)[^;]*)$",
96 /* -- */
97 "[a-zA-Z_][a-zA-Z0-9_]*"
98 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?"
99 "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->"
100 "|[^[:space:]]|[\x80-\xff]+"),
b221207 @svick Userdiff patterns for C#
svick authored
101 PATTERNS("csharp",
102 /* Keywords */
103 "!^[ \t]*(do|while|for|if|else|instanceof|new|return|switch|case|throw|catch|using)\n"
104 /* Methods and constructors */
105 "^[ \t]*(((static|public|internal|private|protected|new|virtual|sealed|override|unsafe)[ \t]+)*[][<>@.~_[:alnum:]]+[ \t]+[<>@._[:alnum:]]+[ \t]*\\(.*\\))[ \t]*$\n"
106 /* Properties */
107 "^[ \t]*(((static|public|internal|private|protected|new|virtual|sealed|override|unsafe)[ \t]+)*[][<>@.~_[:alnum:]]+[ \t]+[@._[:alnum:]]+)[ \t]*$\n"
108 /* Type definitions */
109 "^[ \t]*(((static|public|internal|private|protected|new|unsafe|sealed|abstract|partial)[ \t]+)*(class|enum|interface|struct)[ \t]+.*)$\n"
110 /* Namespace */
111 "^[ \t]*(namespace[ \t]+.*)$",
112 /* -- */
113 "[a-zA-Z_][a-zA-Z0-9_]*"
114 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?"
115 "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->"
116 "|[^[:space:]]|[\x80-\xff]+"),
122aa6f @peff diff: introduce diff.<driver>.binary
peff authored
117 { "default", NULL, -1, { NULL, 0 } },
be58e70 @peff diff: unify external diff and funcname parsing code
peff authored
118 };
80c49c3 @trast color-words: make regex configurable via attributes
trast authored
119 #undef PATTERNS
909a549 @drafnel userdiff.c: add builtin fortran regex patterns
drafnel authored
120 #undef IPATTERN
be58e70 @peff diff: unify external diff and funcname parsing code
peff authored
121
122 static struct userdiff_driver driver_true = {
123 "diff=true",
124 NULL,
122aa6f @peff diff: introduce diff.<driver>.binary
peff authored
125 0,
be58e70 @peff diff: unify external diff and funcname parsing code
peff authored
126 { NULL, 0 }
127 };
128
129 static struct userdiff_driver driver_false = {
130 "!diff",
131 NULL,
122aa6f @peff diff: introduce diff.<driver>.binary
peff authored
132 1,
be58e70 @peff diff: unify external diff and funcname parsing code
peff authored
133 { NULL, 0 }
134 };
135
136 static struct userdiff_driver *userdiff_find_by_namelen(const char *k, int len)
137 {
138 int i;
139 for (i = 0; i < ndrivers; i++) {
140 struct userdiff_driver *drv = drivers + i;
141 if (!strncmp(drv->name, k, len) && !drv->name[len])
142 return drv;
143 }
144 for (i = 0; i < ARRAY_SIZE(builtin_drivers); i++) {
145 struct userdiff_driver *drv = builtin_drivers + i;
146 if (!strncmp(drv->name, k, len) && !drv->name[len])
147 return drv;
148 }
149 return NULL;
150 }
151
152 static struct userdiff_driver *parse_driver(const char *var,
153 const char *value, const char *type)
154 {
155 struct userdiff_driver *drv;
156 const char *dot;
157 const char *name;
158 int namelen;
159
160 if (prefixcmp(var, "diff."))
161 return NULL;
162 dot = strrchr(var, '.');
163 if (dot == var + 4)
164 return NULL;
165 if (strcmp(type, dot+1))
166 return NULL;
167
168 name = var + 5;
169 namelen = dot - name;
170 drv = userdiff_find_by_namelen(name, namelen);
171 if (!drv) {
172 ALLOC_GROW(drivers, ndrivers+1, drivers_alloc);
173 drv = &drivers[ndrivers++];
174 memset(drv, 0, sizeof(*drv));
175 drv->name = xmemdupz(name, namelen);
122aa6f @peff diff: introduce diff.<driver>.binary
peff authored
176 drv->binary = -1;
be58e70 @peff diff: unify external diff and funcname parsing code
peff authored
177 }
178 return drv;
179 }
180
181 static int parse_funcname(struct userdiff_funcname *f, const char *k,
182 const char *v, int cflags)
183 {
184 if (git_config_string(&f->pattern, k, v) < 0)
185 return -1;
186 f->cflags = cflags;
187 return 1;
188 }
189
190 static int parse_string(const char **d, const char *k, const char *v)
191 {
192 if (git_config_string(d, k, v) < 0)
193 return -1;
194 return 1;
195 }
196
122aa6f @peff diff: introduce diff.<driver>.binary
peff authored
197 static int parse_tristate(int *b, const char *k, const char *v)
198 {
199 if (v && !strcasecmp(v, "auto"))
200 *b = -1;
201 else
202 *b = git_config_bool(k, v);
203 return 1;
204 }
205
d9bae1a @peff diff: cache textconv output
peff authored
206 static int parse_bool(int *b, const char *k, const char *v)
207 {
208 *b = git_config_bool(k, v);
209 return 1;
210 }
211
c7534ef @peff userdiff: require explicitly allowing textconv
peff authored
212 int userdiff_config(const char *k, const char *v)
be58e70 @peff diff: unify external diff and funcname parsing code
peff authored
213 {
214 struct userdiff_driver *drv;
215
216 if ((drv = parse_driver(k, v, "funcname")))
217 return parse_funcname(&drv->funcname, k, v, 0);
218 if ((drv = parse_driver(k, v, "xfuncname")))
219 return parse_funcname(&drv->funcname, k, v, REG_EXTENDED);
122aa6f @peff diff: introduce diff.<driver>.binary
peff authored
220 if ((drv = parse_driver(k, v, "binary")))
221 return parse_tristate(&drv->binary, k, v);
be58e70 @peff diff: unify external diff and funcname parsing code
peff authored
222 if ((drv = parse_driver(k, v, "command")))
223 return parse_string(&drv->external, k, v);
9cb92c3 @peff diff: add filter for converting binary to text
peff authored
224 if ((drv = parse_driver(k, v, "textconv")))
225 return parse_string(&drv->textconv, k, v);
d9bae1a @peff diff: cache textconv output
peff authored
226 if ((drv = parse_driver(k, v, "cachetextconv")))
227 return parse_bool(&drv->textconv_want_cache, k, v);
80c49c3 @trast color-words: make regex configurable via attributes
trast authored
228 if ((drv = parse_driver(k, v, "wordregex")))
229 return parse_string(&drv->word_regex, k, v);
be58e70 @peff diff: unify external diff and funcname parsing code
peff authored
230
231 return 0;
232 }
233
234 struct userdiff_driver *userdiff_find_by_name(const char *name) {
235 int len = strlen(name);
236 return userdiff_find_by_namelen(name, len);
237 }
238
239 struct userdiff_driver *userdiff_find_by_path(const char *path)
240 {
241 static struct git_attr *attr;
242 struct git_attr_check check;
243
244 if (!attr)
7fb0eaa @gitster git_attr(): fix function signature
gitster authored
245 attr = git_attr("diff");
be58e70 @peff diff: unify external diff and funcname parsing code
peff authored
246 check.attr = attr;
247
248 if (!path)
249 return NULL;
250 if (git_checkattr(path, 1, &check))
251 return NULL;
252
253 if (ATTR_TRUE(check.value))
254 return &driver_true;
255 if (ATTR_FALSE(check.value))
256 return &driver_false;
257 if (ATTR_UNSET(check.value))
258 return NULL;
259 return userdiff_find_by_name(check.value);
260 }
Something went wrong with that request. Please try again.