Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 305 lines (282 sloc) 9.78 kb
d9bae1a1 » peff
2010-04-01 diff: cache textconv output
1 #include "cache.h"
be58e70d » peff
2008-10-05 diff: unify external diff and funcname parsing code
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
ae3b970a » stephen-smith
2009-01-20 Change the spelling of "wordregex".
10 #define PATTERNS(name, pattern, word_regex) \
664d44ee » jrn
2011-01-11 userdiff: simplify word-diff safeguard
11 { name, NULL, -1, { pattern, REG_EXTENDED }, \
12 word_regex "|[^[:space:]]|[\xc0-\xff][\x80-\xbf]+" }
909a5494 » drafnel
2010-09-10 userdiff.c: add builtin fortran regex patterns
13 #define IPATTERN(name, pattern, word_regex) \
664d44ee » jrn
2011-01-11 userdiff: simplify word-diff safeguard
14 { name, NULL, -1, { pattern, REG_EXTENDED | REG_ICASE }, \
15 word_regex "|[^[:space:]]|[\xc0-\xff][\x80-\xbf]+" }
be58e70d » peff
2008-10-05 diff: unify external diff and funcname parsing code
16 static struct userdiff_driver builtin_drivers[] = {
e90d065e » adrianjohnson
2012-09-16 Add userdiff patterns for Ada
17 IPATTERN("ada",
18 "!^(.*[ \t])?(is new|renames|is separate)([ \t].*)?$\n"
19 "!^[ \t]*with[ \t].*$\n"
20 "^[ \t]*((procedure|function)[ \t]+.*)$\n"
21 "^[ \t]*((package|protected|task)[ \t]+.*)$",
22 /* -- */
23 "[a-zA-Z][a-zA-Z0-9_]*"
24 "|[0-9][-+0-9#_.eE]"
25 "|=>|\\.\\.|\\*\\*|:=|/=|>=|<=|<<|>>|<>"),
909a5494 » drafnel
2010-09-10 userdiff.c: add builtin fortran regex patterns
26 IPATTERN("fortran",
27 "!^([C*]|[ \t]*!)\n"
28 "!^[ \t]*MODULE[ \t]+PROCEDURE[ \t]\n"
29 "^[ \t]*((END[ \t]+)?(PROGRAM|MODULE|BLOCK[ \t]+DATA"
30 "|([^'\" \t]+[ \t]+)*(SUBROUTINE|FUNCTION))[ \t]+[A-Z].*)$",
31 /* -- */
32 "[a-zA-Z][a-zA-Z0-9_]*"
33 "|\\.([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])\\."
34 /* numbers and format statements like 2E14.4, or ES12.6, 9X.
35 * Don't worry about format statements without leading digits since
36 * they would have been matched above as a variable anyway. */
37 "|[-+]?[0-9.]+([AaIiDdEeFfLlTtXx][Ss]?[-+]?[0-9.]*)?(_[a-zA-Z0-9][a-zA-Z0-9_]*)?"
664d44ee » jrn
2011-01-11 userdiff: simplify word-diff safeguard
38 "|//|\\*\\*|::|[/<>=]="),
80c49c3d » trast
2009-01-17 color-words: make regex configurable via attributes
39 PATTERNS("html", "^[ \t]*(<[Hh][1-6][ \t].*>.*)$",
664d44ee » jrn
2011-01-11 userdiff: simplify word-diff safeguard
40 "[^<>= \t]+"),
80c49c3d » trast
2009-01-17 color-words: make regex configurable via attributes
41 PATTERNS("java",
be58e70d » peff
2008-10-05 diff: unify external diff and funcname parsing code
42 "!^[ \t]*(catch|do|for|if|instanceof|new|return|switch|throw|while)\n"
959e2e64 » bonzini
2009-06-17 avoid exponential regex match for java and objc function names
43 "^[ \t]*(([A-Za-z_][A-Za-z_0-9]*[ \t]+)+[A-Za-z_][A-Za-z_0-9]*[ \t]*\\([^;]*)$",
44 /* -- */
80c49c3d » trast
2009-01-17 color-words: make regex configurable via attributes
45 "[a-zA-Z_][a-zA-Z0-9_]*"
46 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?"
47 "|[-+*/<>%&^|=!]="
664d44ee » jrn
2011-01-11 userdiff: simplify word-diff safeguard
48 "|--|\\+\\+|<<=?|>>>?=?|&&|\\|\\|"),
53b10a14 » Gustaf Hendeby
2011-11-15 Add built-in diff patterns for MATLAB code
49 PATTERNS("matlab",
50 "^[[:space:]]*((classdef|function)[[:space:]].*)$|^%%[[:space:]].*$",
51 "[a-zA-Z_][a-zA-Z0-9_]*|[-+0-9.e]+|[=~<>]=|\\.[*/\\^']|\\|\\||&&"),
80c49c3d » trast
2009-01-17 color-words: make regex configurable via attributes
52 PATTERNS("objc",
be58e70d » peff
2008-10-05 diff: unify external diff and funcname parsing code
53 /* Negate C statements that can look like functions */
54 "!^[ \t]*(do|for|if|else|return|switch|while)\n"
55 /* Objective-C methods */
56 "^[ \t]*([-+][ \t]*\\([ \t]*[A-Za-z_][A-Za-z_0-9* \t]*\\)[ \t]*[A-Za-z_].*)$\n"
57 /* C functions */
959e2e64 » bonzini
2009-06-17 avoid exponential regex match for java and objc function names
58 "^[ \t]*(([A-Za-z_][A-Za-z_0-9]*[ \t]+)+[A-Za-z_][A-Za-z_0-9]*[ \t]*\\([^;]*)$\n"
be58e70d » peff
2008-10-05 diff: unify external diff and funcname parsing code
59 /* Objective-C class/protocol definitions */
80c49c3d » trast
2009-01-17 color-words: make regex configurable via attributes
60 "^(@(implementation|interface|protocol)[ \t].*)$",
61 /* -- */
62 "[a-zA-Z_][a-zA-Z0-9_]*"
63 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?"
664d44ee » jrn
2011-01-11 userdiff: simplify word-diff safeguard
64 "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->"),
80c49c3d » trast
2009-01-17 color-words: make regex configurable via attributes
65 PATTERNS("pascal",
ad5b6942 » Zapped
2011-01-11 userdiff: match Pascal class methods
66 "^(((class[ \t]+)?(procedure|function)|constructor|destructor|interface|"
be58e70d » peff
2008-10-05 diff: unify external diff and funcname parsing code
67 "implementation|initialization|finalization)[ \t]*.*)$"
68 "\n"
80c49c3d » trast
2009-01-17 color-words: make regex configurable via attributes
69 "^(.*=[ \t]*(class|record).*)$",
70 /* -- */
71 "[a-zA-Z_][a-zA-Z0-9_]*"
72 "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+"
664d44ee » jrn
2011-01-11 userdiff: simplify word-diff safeguard
73 "|<>|<=|>=|:=|\\.\\."),
71a5d4bc » jrn
2010-12-26 diff: funcname and word patterns for perl
74 PATTERNS("perl",
ea2ca449 » jrn
2011-05-21 userdiff/perl: catch sub with brace on second line
75 "^package .*\n"
76 "^sub [[:alnum:]_':]+[ \t]*"
77 "(\\([^)]*\\)[ \t]*)?" /* prototype */
78 /*
79 * Attributes. A regex can't count nested parentheses,
80 * so just slurp up whatever we see, taking care not
81 * to accept lines like "sub foo; # defined elsewhere".
82 *
83 * An attribute could contain a semicolon, but at that
84 * point it seems reasonable enough to give up.
85 */
86 "(:[^;#]*)?"
87 "(\\{[ \t]*)?" /* brace can come here or on the next line */
88 "(#.*)?$\n" /* comment */
f143d9c6 » jrn
2011-05-22 userdiff/perl: tighten BEGIN/END block pattern to reject here-doc del…
89 "^(BEGIN|END|INIT|CHECK|UNITCHECK|AUTOLOAD|DESTROY)[ \t]*"
ea2ca449 » jrn
2011-05-21 userdiff/perl: catch sub with brace on second line
90 "(\\{[ \t]*)?" /* brace can come here or on the next line */
91 "(#.*)?$\n"
12f0967a » jrn
2011-05-21 userdiff/perl: match full line of POD headers
92 "^=head[0-9] .*", /* POD */
71a5d4bc » jrn
2010-12-26 diff: funcname and word patterns for perl
93 /* -- */
94 "[[:alpha:]_'][[:alnum:]_']*"
95 "|0[xb]?[0-9a-fA-F_]*"
96 /* taking care not to interpret 3..5 as (3.)(.5) */
97 "|[0-9a-fA-F_]+(\\.[0-9a-fA-F_]+)?([eE][-+]?[0-9_]+)?"
98 "|=>|-[rwxoRWXOezsfdlpSugkbctTBMAC>]|~~|::"
99 "|&&=|\\|\\|=|//=|\\*\\*="
100 "|&&|\\|\\||//|\\+\\+|--|\\*\\*|\\.\\.\\.?"
101 "|[-+*/%.^&<>=!|]="
102 "|=~|!~"
664d44ee » jrn
2011-01-11 userdiff: simplify word-diff safeguard
103 "|<<|<>|<=>|>>"),
6d2f208c » dotdash
2010-05-23 diff: Support visibility modifiers in the PHP hunk header regexp
104 PATTERNS("php",
105 "^[\t ]*(((public|protected|private|static)[\t ]+)*function.*)$\n"
106 "^[\t ]*(class.*)$",
80c49c3d » trast
2009-01-17 color-words: make regex configurable via attributes
107 /* -- */
108 "[a-zA-Z_][a-zA-Z0-9_]*"
109 "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+"
664d44ee » jrn
2011-01-11 userdiff: simplify word-diff safeguard
110 "|[-+*/<>%&^|=!.]=|--|\\+\\+|<<=?|>>=?|===|&&|\\|\\||::|->"),
80c49c3d » trast
2009-01-17 color-words: make regex configurable via attributes
111 PATTERNS("python", "^[ \t]*((class|def)[ \t].*)$",
112 /* -- */
113 "[a-zA-Z_][a-zA-Z0-9_]*"
114 "|[-+0-9.e]+[jJlL]?|0[xX]?[0-9a-fA-F]+[lL]?"
664d44ee » jrn
2011-01-11 userdiff: simplify word-diff safeguard
115 "|[-+*/<>%&^|=!]=|//=?|<<=?|>>=?|\\*\\*=?"),
80c49c3d » trast
2009-01-17 color-words: make regex configurable via attributes
116 /* -- */
117 PATTERNS("ruby", "^[ \t]*((class|module|def)[ \t].*)$",
118 /* -- */
119 "(@|@@|\\$)?[a-zA-Z_][a-zA-Z0-9_]*"
120 "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+|\\?(\\\\C-)?(\\\\M-)?."
664d44ee » jrn
2011-01-11 userdiff: simplify word-diff safeguard
121 "|//=?|[-+*/<>%&^|=!]=|<<=?|>>=?|===|\\.{1,3}|::|[!=]~"),
80c49c3d » trast
2009-01-17 color-words: make regex configurable via attributes
122 PATTERNS("bibtex", "(@[a-zA-Z]{1,}[ \t]*\\{{0,1}[ \t]*[^ \t\"@',\\#}{~%]*).*$",
123 "[={}\"]|[^={}\" \t]+"),
124 PATTERNS("tex", "^(\\\\((sub)*section|chapter|part)\\*{0,1}\\{.*)$",
664d44ee » jrn
2011-01-11 userdiff: simplify word-diff safeguard
125 "\\\\[a-zA-Z@]+|\\\\.|[a-zA-Z0-9\x80-\xff]+"),
80c49c3d » trast
2009-01-17 color-words: make regex configurable via attributes
126 PATTERNS("cpp",
127 /* Jump targets or access declarations */
128 "!^[ \t]*[A-Za-z_][A-Za-z_0-9]*:.*$\n"
129 /* C/++ functions/methods at top level */
37e7793d » trast
2011-12-06 userdiff: allow * between cpp funcname words
130 "^([A-Za-z_][A-Za-z_0-9]*([ \t*]+[A-Za-z_][A-Za-z_0-9]*([ \t]*::[ \t]*[^[:space:]]+)?){1,}[ \t]*\\([^;]*)$\n"
80c49c3d » trast
2009-01-17 color-words: make regex configurable via attributes
131 /* compound type at top level */
132 "^((struct|class|enum)[^;]*)$",
133 /* -- */
134 "[a-zA-Z_][a-zA-Z0-9_]*"
135 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?"
664d44ee » jrn
2011-01-11 userdiff: simplify word-diff safeguard
136 "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->"),
b221207d » svick
2010-08-16 Userdiff patterns for C#
137 PATTERNS("csharp",
138 /* Keywords */
139 "!^[ \t]*(do|while|for|if|else|instanceof|new|return|switch|case|throw|catch|using)\n"
140 /* Methods and constructors */
141 "^[ \t]*(((static|public|internal|private|protected|new|virtual|sealed|override|unsafe)[ \t]+)*[][<>@.~_[:alnum:]]+[ \t]+[<>@._[:alnum:]]+[ \t]*\\(.*\\))[ \t]*$\n"
142 /* Properties */
143 "^[ \t]*(((static|public|internal|private|protected|new|virtual|sealed|override|unsafe)[ \t]+)*[][<>@.~_[:alnum:]]+[ \t]+[@._[:alnum:]]+)[ \t]*$\n"
144 /* Type definitions */
145 "^[ \t]*(((static|public|internal|private|protected|new|unsafe|sealed|abstract|partial)[ \t]+)*(class|enum|interface|struct)[ \t]+.*)$\n"
146 /* Namespace */
147 "^[ \t]*(namespace[ \t]+.*)$",
148 /* -- */
149 "[a-zA-Z_][a-zA-Z0-9_]*"
150 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?"
664d44ee » jrn
2011-01-11 userdiff: simplify word-diff safeguard
151 "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->"),
122aa6f9 » peff
2008-10-05 diff: introduce diff.<driver>.binary
152 { "default", NULL, -1, { NULL, 0 } },
be58e70d » peff
2008-10-05 diff: unify external diff and funcname parsing code
153 };
80c49c3d » trast
2009-01-17 color-words: make regex configurable via attributes
154 #undef PATTERNS
909a5494 » drafnel
2010-09-10 userdiff.c: add builtin fortran regex patterns
155 #undef IPATTERN
be58e70d » peff
2008-10-05 diff: unify external diff and funcname parsing code
156
157 static struct userdiff_driver driver_true = {
158 "diff=true",
159 NULL,
122aa6f9 » peff
2008-10-05 diff: introduce diff.<driver>.binary
160 0,
be58e70d » peff
2008-10-05 diff: unify external diff and funcname parsing code
161 { NULL, 0 }
162 };
163
164 static struct userdiff_driver driver_false = {
165 "!diff",
166 NULL,
122aa6f9 » peff
2008-10-05 diff: introduce diff.<driver>.binary
167 1,
be58e70d » peff
2008-10-05 diff: unify external diff and funcname parsing code
168 { NULL, 0 }
169 };
170
171 static struct userdiff_driver *userdiff_find_by_namelen(const char *k, int len)
172 {
173 int i;
174 for (i = 0; i < ndrivers; i++) {
175 struct userdiff_driver *drv = drivers + i;
176 if (!strncmp(drv->name, k, len) && !drv->name[len])
177 return drv;
178 }
179 for (i = 0; i < ARRAY_SIZE(builtin_drivers); i++) {
180 struct userdiff_driver *drv = builtin_drivers + i;
181 if (!strncmp(drv->name, k, len) && !drv->name[len])
182 return drv;
183 }
184 return NULL;
185 }
186
187 static struct userdiff_driver *parse_driver(const char *var,
188 const char *value, const char *type)
189 {
190 struct userdiff_driver *drv;
191 const char *dot;
192 const char *name;
193 int namelen;
194
195 if (prefixcmp(var, "diff."))
196 return NULL;
197 dot = strrchr(var, '.');
198 if (dot == var + 4)
199 return NULL;
200 if (strcmp(type, dot+1))
201 return NULL;
202
203 name = var + 5;
204 namelen = dot - name;
205 drv = userdiff_find_by_namelen(name, namelen);
206 if (!drv) {
207 ALLOC_GROW(drivers, ndrivers+1, drivers_alloc);
208 drv = &drivers[ndrivers++];
209 memset(drv, 0, sizeof(*drv));
210 drv->name = xmemdupz(name, namelen);
122aa6f9 » peff
2008-10-05 diff: introduce diff.<driver>.binary
211 drv->binary = -1;
be58e70d » peff
2008-10-05 diff: unify external diff and funcname parsing code
212 }
213 return drv;
214 }
215
216 static int parse_funcname(struct userdiff_funcname *f, const char *k,
217 const char *v, int cflags)
218 {
219 if (git_config_string(&f->pattern, k, v) < 0)
220 return -1;
221 f->cflags = cflags;
6680a087 » peff
2012-02-07 drop odd return value semantics from userdiff_config
222 return 0;
be58e70d » peff
2008-10-05 diff: unify external diff and funcname parsing code
223 }
224
122aa6f9 » peff
2008-10-05 diff: introduce diff.<driver>.binary
225 static int parse_tristate(int *b, const char *k, const char *v)
226 {
227 if (v && !strcasecmp(v, "auto"))
228 *b = -1;
229 else
230 *b = git_config_bool(k, v);
6680a087 » peff
2012-02-07 drop odd return value semantics from userdiff_config
231 return 0;
122aa6f9 » peff
2008-10-05 diff: introduce diff.<driver>.binary
232 }
233
d9bae1a1 » peff
2010-04-01 diff: cache textconv output
234 static int parse_bool(int *b, const char *k, const char *v)
235 {
236 *b = git_config_bool(k, v);
6680a087 » peff
2012-02-07 drop odd return value semantics from userdiff_config
237 return 0;
d9bae1a1 » peff
2010-04-01 diff: cache textconv output
238 }
239
c7534ef4 » peff
2008-10-26 userdiff: require explicitly allowing textconv
240 int userdiff_config(const char *k, const char *v)
be58e70d » peff
2008-10-05 diff: unify external diff and funcname parsing code
241 {
242 struct userdiff_driver *drv;
243
244 if ((drv = parse_driver(k, v, "funcname")))
245 return parse_funcname(&drv->funcname, k, v, 0);
246 if ((drv = parse_driver(k, v, "xfuncname")))
247 return parse_funcname(&drv->funcname, k, v, REG_EXTENDED);
122aa6f9 » peff
2008-10-05 diff: introduce diff.<driver>.binary
248 if ((drv = parse_driver(k, v, "binary")))
249 return parse_tristate(&drv->binary, k, v);
be58e70d » peff
2008-10-05 diff: unify external diff and funcname parsing code
250 if ((drv = parse_driver(k, v, "command")))
6680a087 » peff
2012-02-07 drop odd return value semantics from userdiff_config
251 return git_config_string(&drv->external, k, v);
9cb92c39 » peff
2008-10-05 diff: add filter for converting binary to text
252 if ((drv = parse_driver(k, v, "textconv")))
6680a087 » peff
2012-02-07 drop odd return value semantics from userdiff_config
253 return git_config_string(&drv->textconv, k, v);
d9bae1a1 » peff
2010-04-01 diff: cache textconv output
254 if ((drv = parse_driver(k, v, "cachetextconv")))
255 return parse_bool(&drv->textconv_want_cache, k, v);
80c49c3d » trast
2009-01-17 color-words: make regex configurable via attributes
256 if ((drv = parse_driver(k, v, "wordregex")))
6680a087 » peff
2012-02-07 drop odd return value semantics from userdiff_config
257 return git_config_string(&drv->word_regex, k, v);
be58e70d » peff
2008-10-05 diff: unify external diff and funcname parsing code
258
259 return 0;
260 }
261
262 struct userdiff_driver *userdiff_find_by_name(const char *name) {
263 int len = strlen(name);
264 return userdiff_find_by_namelen(name, len);
265 }
266
267 struct userdiff_driver *userdiff_find_by_path(const char *path)
268 {
269 static struct git_attr *attr;
270 struct git_attr_check check;
271
272 if (!attr)
7fb0eaa2 »
2010-01-16 git_attr(): fix function signature
273 attr = git_attr("diff");
be58e70d » peff
2008-10-05 diff: unify external diff and funcname parsing code
274 check.attr = attr;
275
276 if (!path)
277 return NULL;
d932f4eb » mhagger
2011-08-04 Rename git_checkattr() to git_check_attr()
278 if (git_check_attr(path, 1, &check))
be58e70d » peff
2008-10-05 diff: unify external diff and funcname parsing code
279 return NULL;
280
281 if (ATTR_TRUE(check.value))
282 return &driver_true;
283 if (ATTR_FALSE(check.value))
284 return &driver_false;
285 if (ATTR_UNSET(check.value))
286 return NULL;
287 return userdiff_find_by_name(check.value);
288 }
3813e690 » peff
2011-05-23 refactor get_textconv to not require diff_filespec
289
290 struct userdiff_driver *userdiff_get_textconv(struct userdiff_driver *driver)
291 {
292 if (!driver->textconv)
293 return NULL;
294
295 if (driver->textconv_want_cache && !driver->textconv_cache) {
296 struct notes_cache *c = xmalloc(sizeof(*c));
297 struct strbuf name = STRBUF_INIT;
298
299 strbuf_addf(&name, "textconv/%s", driver->name);
300 notes_cache_init(c, name.buf, driver->textconv);
301 driver->textconv_cache = c;
302 }
303
304 return driver;
305 }
Something went wrong with that request. Please try again.