Diff patterns for POSIX shells
[git] / userdiff.c
1 #include "cache.h"
2 #include "userdiff.h"
3 #include "attr.h"
4
5 static struct userdiff_driver *drivers;
6 static int ndrivers;
7 static int drivers_alloc;
8
9 #define PATTERNS(name, pattern, word_regex)                     \
10         { name, NULL, -1, { pattern, REG_EXTENDED },            \
11           word_regex "|[^[:space:]]|[\xc0-\xff][\x80-\xbf]+" }
12 #define IPATTERN(name, pattern, word_regex)                     \
13         { name, NULL, -1, { pattern, REG_EXTENDED | REG_ICASE }, \
14           word_regex "|[^[:space:]]|[\xc0-\xff][\x80-\xbf]+" }
15 static struct userdiff_driver builtin_drivers[] = {
16 IPATTERN("ada",
17          "!^(.*[ \t])?(is[ \t]+new|renames|is[ \t]+separate)([ \t].*)?$\n"
18          "!^[ \t]*with[ \t].*$\n"
19          "^[ \t]*((procedure|function)[ \t]+.*)$\n"
20          "^[ \t]*((package|protected|task)[ \t]+.*)$",
21          /* -- */
22          "[a-zA-Z][a-zA-Z0-9_]*"
23          "|[-+]?[0-9][0-9#_.aAbBcCdDeEfF]*([eE][+-]?[0-9_]+)?"
24          "|=>|\\.\\.|\\*\\*|:=|/=|>=|<=|<<|>>|<>"),
25 IPATTERN("fortran",
26          "!^([C*]|[ \t]*!)\n"
27          "!^[ \t]*MODULE[ \t]+PROCEDURE[ \t]\n"
28          "^[ \t]*((END[ \t]+)?(PROGRAM|MODULE|BLOCK[ \t]+DATA"
29                 "|([^'\" \t]+[ \t]+)*(SUBROUTINE|FUNCTION))[ \t]+[A-Z].*)$",
30          /* -- */
31          "[a-zA-Z][a-zA-Z0-9_]*"
32          "|\\.([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])\\."
33          /* numbers and format statements like 2E14.4, or ES12.6, 9X.
34           * Don't worry about format statements without leading digits since
35           * they would have been matched above as a variable anyway. */
36          "|[-+]?[0-9.]+([AaIiDdEeFfLlTtXx][Ss]?[-+]?[0-9.]*)?(_[a-zA-Z0-9][a-zA-Z0-9_]*)?"
37          "|//|\\*\\*|::|[/<>=]="),
38 IPATTERN("fountain", "^((\\.[^.]|(int|ext|est|int\\.?/ext|i/e)[. ]).*)$",
39          "[^ \t-]+"),
40 PATTERNS("html", "^[ \t]*(<[Hh][1-6][ \t].*>.*)$",
41          "[^<>= \t]+"),
42 PATTERNS("java",
43          "!^[ \t]*(catch|do|for|if|instanceof|new|return|switch|throw|while)\n"
44          "^[ \t]*(([A-Za-z_][A-Za-z_0-9]*[ \t]+)+[A-Za-z_][A-Za-z_0-9]*[ \t]*\\([^;]*)$",
45          /* -- */
46          "[a-zA-Z_][a-zA-Z0-9_]*"
47          "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?"
48          "|[-+*/<>%&^|=!]="
49          "|--|\\+\\+|<<=?|>>>?=?|&&|\\|\\|"),
50 PATTERNS("matlab",
51          "^[[:space:]]*((classdef|function)[[:space:]].*)$|^%%[[:space:]].*$",
52          "[a-zA-Z_][a-zA-Z0-9_]*|[-+0-9.e]+|[=~<>]=|\\.[*/\\^']|\\|\\||&&"),
53 PATTERNS("objc",
54          /* Negate C statements that can look like functions */
55          "!^[ \t]*(do|for|if|else|return|switch|while)\n"
56          /* Objective-C methods */
57          "^[ \t]*([-+][ \t]*\\([ \t]*[A-Za-z_][A-Za-z_0-9* \t]*\\)[ \t]*[A-Za-z_].*)$\n"
58          /* C functions */
59          "^[ \t]*(([A-Za-z_][A-Za-z_0-9]*[ \t]+)+[A-Za-z_][A-Za-z_0-9]*[ \t]*\\([^;]*)$\n"
60          /* Objective-C class/protocol definitions */
61          "^(@(implementation|interface|protocol)[ \t].*)$",
62          /* -- */
63          "[a-zA-Z_][a-zA-Z0-9_]*"
64          "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?"
65          "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->"),
66 PATTERNS("pascal",
67          "^(((class[ \t]+)?(procedure|function)|constructor|destructor|interface|"
68                 "implementation|initialization|finalization)[ \t]*.*)$"
69          "\n"
70          "^(.*=[ \t]*(class|record).*)$",
71          /* -- */
72          "[a-zA-Z_][a-zA-Z0-9_]*"
73          "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+"
74          "|<>|<=|>=|:=|\\.\\."),
75 PATTERNS("perl",
76          "^package .*\n"
77          "^sub [[:alnum:]_':]+[ \t]*"
78                 "(\\([^)]*\\)[ \t]*)?" /* prototype */
79                 /*
80                  * Attributes.  A regex can't count nested parentheses,
81                  * so just slurp up whatever we see, taking care not
82                  * to accept lines like "sub foo; # defined elsewhere".
83                  *
84                  * An attribute could contain a semicolon, but at that
85                  * point it seems reasonable enough to give up.
86                  */
87                 "(:[^;#]*)?"
88                 "(\\{[ \t]*)?" /* brace can come here or on the next line */
89                 "(#.*)?$\n" /* comment */
90          "^(BEGIN|END|INIT|CHECK|UNITCHECK|AUTOLOAD|DESTROY)[ \t]*"
91                 "(\\{[ \t]*)?" /* brace can come here or on the next line */
92                 "(#.*)?$\n"
93          "^=head[0-9] .*",      /* POD */
94          /* -- */
95          "[[:alpha:]_'][[:alnum:]_']*"
96          "|0[xb]?[0-9a-fA-F_]*"
97          /* taking care not to interpret 3..5 as (3.)(.5) */
98          "|[0-9a-fA-F_]+(\\.[0-9a-fA-F_]+)?([eE][-+]?[0-9_]+)?"
99          "|=>|-[rwxoRWXOezsfdlpSugkbctTBMAC>]|~~|::"
100          "|&&=|\\|\\|=|//=|\\*\\*="
101          "|&&|\\|\\||//|\\+\\+|--|\\*\\*|\\.\\.\\.?"
102          "|[-+*/%.^&<>=!|]="
103          "|=~|!~"
104          "|<<|<>|<=>|>>"),
105 PATTERNS("php",
106          "^[\t ]*(((public|protected|private|static)[\t ]+)*function.*)$\n"
107          "^[\t ]*(class.*)$",
108          /* -- */
109          "[a-zA-Z_][a-zA-Z0-9_]*"
110          "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+"
111          "|[-+*/<>%&^|=!.]=|--|\\+\\+|<<=?|>>=?|===|&&|\\|\\||::|->"),
112 PATTERNS("python", "^[ \t]*((class|def)[ \t].*)$",
113          /* -- */
114          "[a-zA-Z_][a-zA-Z0-9_]*"
115          "|[-+0-9.e]+[jJlL]?|0[xX]?[0-9a-fA-F]+[lL]?"
116          "|[-+*/<>%&^|=!]=|//=?|<<=?|>>=?|\\*\\*=?"),
117          /* -- */
118 PATTERNS("ruby", "^[ \t]*((class|module|def)[ \t].*)$",
119          /* -- */
120          "(@|@@|\\$)?[a-zA-Z_][a-zA-Z0-9_]*"
121          "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+|\\?(\\\\C-)?(\\\\M-)?."
122          "|//=?|[-+*/<>%&^|=!]=|<<=?|>>=?|===|\\.{1,3}|::|[!=]~"),
123 PATTERNS("shell", "^[ \t]*([a-zA-Z_0-9]+[ \t]*\\(\\)).*",
124          /* -- */
125          "\\$?[a-zA-Z_0-9]+|&&|\\|\\|"),
126 PATTERNS("bibtex", "(@[a-zA-Z]{1,}[ \t]*\\{{0,1}[ \t]*[^ \t\"@',\\#}{~%]*).*$",
127          "[={}\"]|[^={}\" \t]+"),
128 PATTERNS("tex", "^(\\\\((sub)*section|chapter|part)\\*{0,1}\\{.*)$",
129          "\\\\[a-zA-Z@]+|\\\\.|[a-zA-Z0-9\x80-\xff]+"),
130 PATTERNS("cpp",
131          /* Jump targets or access declarations */
132          "!^[ \t]*[A-Za-z_][A-Za-z_0-9]*:[[:space:]]*($|/[/*])\n"
133          /* functions/methods, variables, and compounds at top level */
134          "^((::[[:space:]]*)?[A-Za-z_].*)$",
135          /* -- */
136          "[a-zA-Z_][a-zA-Z0-9_]*"
137          "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lLuU]*"
138          "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->\\*?|\\.\\*"),
139 PATTERNS("csharp",
140          /* Keywords */
141          "!^[ \t]*(do|while|for|if|else|instanceof|new|return|switch|case|throw|catch|using)\n"
142          /* Methods and constructors */
143          "^[ \t]*(((static|public|internal|private|protected|new|virtual|sealed|override|unsafe)[ \t]+)*[][<>@.~_[:alnum:]]+[ \t]+[<>@._[:alnum:]]+[ \t]*\\(.*\\))[ \t]*$\n"
144          /* Properties */
145          "^[ \t]*(((static|public|internal|private|protected|new|virtual|sealed|override|unsafe)[ \t]+)*[][<>@.~_[:alnum:]]+[ \t]+[@._[:alnum:]]+)[ \t]*$\n"
146          /* Type definitions */
147          "^[ \t]*(((static|public|internal|private|protected|new|unsafe|sealed|abstract|partial)[ \t]+)*(class|enum|interface|struct)[ \t]+.*)$\n"
148          /* Namespace */
149          "^[ \t]*(namespace[ \t]+.*)$",
150          /* -- */
151          "[a-zA-Z_][a-zA-Z0-9_]*"
152          "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?"
153          "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->"),
154 IPATTERN("css",
155          "![:;][[:space:]]*$\n"
156          "^[_a-z0-9].*$",
157          /* -- */
158          /*
159           * This regex comes from W3C CSS specs. Should theoretically also
160           * allow ISO 10646 characters U+00A0 and higher,
161           * but they are not handled in this regex.
162           */
163          "-?[_a-zA-Z][-_a-zA-Z0-9]*" /* identifiers */
164          "|-?[0-9]+|\\#[0-9a-fA-F]+" /* numbers */
165 ),
166 { "default", NULL, -1, { NULL, 0 } },
167 };
168 #undef PATTERNS
169 #undef IPATTERN
170
171 static struct userdiff_driver driver_true = {
172         "diff=true",
173         NULL,
174         0,
175         { NULL, 0 }
176 };
177
178 static struct userdiff_driver driver_false = {
179         "!diff",
180         NULL,
181         1,
182         { NULL, 0 }
183 };
184
185 static struct userdiff_driver *userdiff_find_by_namelen(const char *k, int len)
186 {
187         int i;
188         for (i = 0; i < ndrivers; i++) {
189                 struct userdiff_driver *drv = drivers + i;
190                 if (!strncmp(drv->name, k, len) && !drv->name[len])
191                         return drv;
192         }
193         for (i = 0; i < ARRAY_SIZE(builtin_drivers); i++) {
194                 struct userdiff_driver *drv = builtin_drivers + i;
195                 if (!strncmp(drv->name, k, len) && !drv->name[len])
196                         return drv;
197         }
198         return NULL;
199 }
200
201 static int parse_funcname(struct userdiff_funcname *f, const char *k,
202                 const char *v, int cflags)
203 {
204         if (git_config_string(&f->pattern, k, v) < 0)
205                 return -1;
206         f->cflags = cflags;
207         return 0;
208 }
209
210 static int parse_tristate(int *b, const char *k, const char *v)
211 {
212         if (v && !strcasecmp(v, "auto"))
213                 *b = -1;
214         else
215                 *b = git_config_bool(k, v);
216         return 0;
217 }
218
219 static int parse_bool(int *b, const char *k, const char *v)
220 {
221         *b = git_config_bool(k, v);
222         return 0;
223 }
224
225 int userdiff_config(const char *k, const char *v)
226 {
227         struct userdiff_driver *drv;
228         const char *name, *type;
229         int namelen;
230
231         if (parse_config_key(k, "diff", &name, &namelen, &type) || !name)
232                 return 0;
233
234         drv = userdiff_find_by_namelen(name, namelen);
235         if (!drv) {
236                 ALLOC_GROW(drivers, ndrivers+1, drivers_alloc);
237                 drv = &drivers[ndrivers++];
238                 memset(drv, 0, sizeof(*drv));
239                 drv->name = xmemdupz(name, namelen);
240                 drv->binary = -1;
241         }
242
243         if (!strcmp(type, "funcname"))
244                 return parse_funcname(&drv->funcname, k, v, 0);
245         if (!strcmp(type, "xfuncname"))
246                 return parse_funcname(&drv->funcname, k, v, REG_EXTENDED);
247         if (!strcmp(type, "binary"))
248                 return parse_tristate(&drv->binary, k, v);
249         if (!strcmp(type, "command"))
250                 return git_config_string(&drv->external, k, v);
251         if (!strcmp(type, "textconv"))
252                 return git_config_string(&drv->textconv, k, v);
253         if (!strcmp(type, "cachetextconv"))
254                 return parse_bool(&drv->textconv_want_cache, k, v);
255         if (!strcmp(type, "wordregex"))
256                 return git_config_string(&drv->word_regex, k, v);
257
258         return 0;
259 }
260
261 struct userdiff_driver *userdiff_find_by_name(const char *name) {
262         int len = strlen(name);
263         return userdiff_find_by_namelen(name, len);
264 }
265
266 struct userdiff_driver *userdiff_find_by_path(const char *path)
267 {
268         static struct git_attr *attr;
269         struct git_attr_check check;
270
271         if (!attr)
272                 attr = git_attr("diff");
273         check.attr = attr;
274
275         if (!path)
276                 return NULL;
277         if (git_check_attr(path, 1, &check))
278                 return NULL;
279
280         if (ATTR_TRUE(check.value))
281                 return &driver_true;
282         if (ATTR_FALSE(check.value))
283                 return &driver_false;
284         if (ATTR_UNSET(check.value))
285                 return NULL;
286         return userdiff_find_by_name(check.value);
287 }
288
289 struct userdiff_driver *userdiff_get_textconv(struct userdiff_driver *driver)
290 {
291         if (!driver->textconv)
292                 return NULL;
293
294         if (driver->textconv_want_cache && !driver->textconv_cache) {
295                 struct notes_cache *c = xmalloc(sizeof(*c));
296                 struct strbuf name = STRBUF_INIT;
297
298                 strbuf_addf(&name, "textconv/%s", driver->name);
299                 notes_cache_init(c, name.buf, driver->textconv);
300                 driver->textconv_cache = c;
301         }
302
303         return driver;
304 }