Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

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