blob: a69241b25ddaff5b61380aa8b451a6fcb833502c [file] [log] [blame]
Jeff Kingd9bae1a2010-04-01 20:12:15 -04001#include "cache.h"
Brandon Williamsb2141fc2017-06-14 11:07:36 -07002#include "config.h"
Jeff Kingbe58e702008-10-05 17:43:21 -04003#include "userdiff.h"
Jeff Kingbe58e702008-10-05 17:43:21 -04004#include "attr.h"
5
6static struct userdiff_driver *drivers;
7static int ndrivers;
8static int drivers_alloc;
9
Boyd Stephen Smith Jrae3b9702009-01-20 22:59:54 -060010#define PATTERNS(name, pattern, word_regex) \
Jonathan Nieder664d44e2011-01-11 15:48:50 -060011 { name, NULL, -1, { pattern, REG_EXTENDED }, \
12 word_regex "|[^[:space:]]|[\xc0-\xff][\x80-\xbf]+" }
Brandon Casey909a5492010-09-10 11:18:14 -050013#define IPATTERN(name, pattern, word_regex) \
Jonathan Nieder664d44e2011-01-11 15:48:50 -060014 { name, NULL, -1, { pattern, REG_EXTENDED | REG_ICASE }, \
15 word_regex "|[^[:space:]]|[\xc0-\xff][\x80-\xbf]+" }
Jeff Kingbe58e702008-10-05 17:43:21 -040016static struct userdiff_driver builtin_drivers[] = {
Adrian Johnsone90d0652012-09-16 13:24:15 +093017IPATTERN("ada",
Adrian Johnson39a87a22014-02-03 22:03:16 +103018 "!^(.*[ \t])?(is[ \t]+new|renames|is[ \t]+separate)([ \t].*)?$\n"
Adrian Johnsone90d0652012-09-16 13:24:15 +093019 "!^[ \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_]*"
Adrian Johnson39a87a22014-02-03 22:03:16 +103024 "|[-+]?[0-9][0-9#_.aAbBcCdDeEfF]*([eE][+-]?[0-9_]+)?"
Adrian Johnsone90d0652012-09-16 13:24:15 +093025 "|=>|\\.\\.|\\*\\*|:=|/=|>=|<=|<<|>>|<>"),
Brandon Casey909a5492010-09-10 11:18:14 -050026IPATTERN("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_]*)?"
Jonathan Nieder664d44e2011-01-11 15:48:50 -060038 "|//|\\*\\*|::|[/<>=]="),
Zoë Blade69f9c872015-07-21 14:22:46 +010039IPATTERN("fountain", "^((\\.[^.]|(int|ext|est|int\\.?/ext|i/e)[. ]).*)$",
40 "[^ \t-]+"),
Alban Gruin1dbf0c02018-03-01 12:19:07 +010041PATTERNS("golang",
42 /* Functions */
43 "^[ \t]*(func[ \t]*.*(\\{[ \t]*)?)\n"
44 /* Structs and interfaces */
45 "^[ \t]*(type[ \t].*(struct|interface)[ \t]*(\\{[ \t]*)?)",
46 /* -- */
47 "[a-zA-Z_][a-zA-Z0-9_]*"
48 "|[-+0-9.eE]+i?|0[xX]?[0-9a-fA-F]+i?"
49 "|[-+*/<>%&^|=!:]=|--|\\+\\+|<<=?|>>=?|&\\^=?|&&|\\|\\||<-|\\.{3}"),
Ilya Kantor9c03cac2017-09-23 10:53:47 +030050PATTERNS("html", "^[ \t]*(<[Hh][1-6]([ \t].*)?>.*)$",
Jonathan Nieder664d44e2011-01-11 15:48:50 -060051 "[^<>= \t]+"),
Thomas Rast80c49c32009-01-17 17:29:48 +010052PATTERNS("java",
Jeff Kingbe58e702008-10-05 17:43:21 -040053 "!^[ \t]*(catch|do|for|if|instanceof|new|return|switch|throw|while)\n"
Paolo Bonzini959e2e62009-06-17 16:26:06 +020054 "^[ \t]*(([A-Za-z_][A-Za-z_0-9]*[ \t]+)+[A-Za-z_][A-Za-z_0-9]*[ \t]*\\([^;]*)$",
55 /* -- */
Thomas Rast80c49c32009-01-17 17:29:48 +010056 "[a-zA-Z_][a-zA-Z0-9_]*"
57 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?"
58 "|[-+*/<>%&^|=!]="
Jonathan Nieder664d44e2011-01-11 15:48:50 -060059 "|--|\\+\\+|<<=?|>>>?=?|&&|\\|\\|"),
Gustaf Hendeby53b10a12011-11-15 21:15:03 +010060PATTERNS("matlab",
61 "^[[:space:]]*((classdef|function)[[:space:]].*)$|^%%[[:space:]].*$",
62 "[a-zA-Z_][a-zA-Z0-9_]*|[-+0-9.e]+|[=~<>]=|\\.[*/\\^']|\\|\\||&&"),
Thomas Rast80c49c32009-01-17 17:29:48 +010063PATTERNS("objc",
Jeff Kingbe58e702008-10-05 17:43:21 -040064 /* Negate C statements that can look like functions */
65 "!^[ \t]*(do|for|if|else|return|switch|while)\n"
66 /* Objective-C methods */
67 "^[ \t]*([-+][ \t]*\\([ \t]*[A-Za-z_][A-Za-z_0-9* \t]*\\)[ \t]*[A-Za-z_].*)$\n"
68 /* C functions */
Paolo Bonzini959e2e62009-06-17 16:26:06 +020069 "^[ \t]*(([A-Za-z_][A-Za-z_0-9]*[ \t]+)+[A-Za-z_][A-Za-z_0-9]*[ \t]*\\([^;]*)$\n"
Jeff Kingbe58e702008-10-05 17:43:21 -040070 /* Objective-C class/protocol definitions */
Thomas Rast80c49c32009-01-17 17:29:48 +010071 "^(@(implementation|interface|protocol)[ \t].*)$",
72 /* -- */
73 "[a-zA-Z_][a-zA-Z0-9_]*"
74 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?"
Jonathan Nieder664d44e2011-01-11 15:48:50 -060075 "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->"),
Thomas Rast80c49c32009-01-17 17:29:48 +010076PATTERNS("pascal",
Alexey Shumkinad5b6942011-01-11 11:53:59 +030077 "^(((class[ \t]+)?(procedure|function)|constructor|destructor|interface|"
Jeff Kingbe58e702008-10-05 17:43:21 -040078 "implementation|initialization|finalization)[ \t]*.*)$"
79 "\n"
Thomas Rast80c49c32009-01-17 17:29:48 +010080 "^(.*=[ \t]*(class|record).*)$",
81 /* -- */
82 "[a-zA-Z_][a-zA-Z0-9_]*"
83 "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+"
Jonathan Nieder664d44e2011-01-11 15:48:50 -060084 "|<>|<=|>=|:=|\\.\\."),
Jonathan Nieder71a5d4b2010-12-26 03:07:31 -060085PATTERNS("perl",
Jonathan Niederea2ca442011-05-21 14:38:26 -050086 "^package .*\n"
87 "^sub [[:alnum:]_':]+[ \t]*"
88 "(\\([^)]*\\)[ \t]*)?" /* prototype */
89 /*
90 * Attributes. A regex can't count nested parentheses,
91 * so just slurp up whatever we see, taking care not
92 * to accept lines like "sub foo; # defined elsewhere".
93 *
94 * An attribute could contain a semicolon, but at that
95 * point it seems reasonable enough to give up.
96 */
97 "(:[^;#]*)?"
98 "(\\{[ \t]*)?" /* brace can come here or on the next line */
99 "(#.*)?$\n" /* comment */
Jonathan Niederf143d9c2011-05-22 12:29:32 -0500100 "^(BEGIN|END|INIT|CHECK|UNITCHECK|AUTOLOAD|DESTROY)[ \t]*"
Jonathan Niederea2ca442011-05-21 14:38:26 -0500101 "(\\{[ \t]*)?" /* brace can come here or on the next line */
102 "(#.*)?$\n"
Jonathan Nieder12f09672011-05-21 14:35:51 -0500103 "^=head[0-9] .*", /* POD */
Jonathan Nieder71a5d4b2010-12-26 03:07:31 -0600104 /* -- */
105 "[[:alpha:]_'][[:alnum:]_']*"
106 "|0[xb]?[0-9a-fA-F_]*"
107 /* taking care not to interpret 3..5 as (3.)(.5) */
108 "|[0-9a-fA-F_]+(\\.[0-9a-fA-F_]+)?([eE][-+]?[0-9_]+)?"
109 "|=>|-[rwxoRWXOezsfdlpSugkbctTBMAC>]|~~|::"
110 "|&&=|\\|\\|=|//=|\\*\\*="
111 "|&&|\\|\\||//|\\+\\+|--|\\*\\*|\\.\\.\\.?"
112 "|[-+*/%.^&<>=!|]="
113 "|=~|!~"
Jonathan Nieder664d44e2011-01-11 15:48:50 -0600114 "|<<|<>|<=>|>>"),
Björn Steinbrink6d2f2082010-05-23 20:05:40 +0200115PATTERNS("php",
116 "^[\t ]*(((public|protected|private|static)[\t ]+)*function.*)$\n"
117 "^[\t ]*(class.*)$",
Thomas Rast80c49c32009-01-17 17:29:48 +0100118 /* -- */
119 "[a-zA-Z_][a-zA-Z0-9_]*"
120 "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+"
Jonathan Nieder664d44e2011-01-11 15:48:50 -0600121 "|[-+*/<>%&^|=!.]=|--|\\+\\+|<<=?|>>=?|===|&&|\\|\\||::|->"),
Thomas Rast80c49c32009-01-17 17:29:48 +0100122PATTERNS("python", "^[ \t]*((class|def)[ \t].*)$",
123 /* -- */
124 "[a-zA-Z_][a-zA-Z0-9_]*"
125 "|[-+0-9.e]+[jJlL]?|0[xX]?[0-9a-fA-F]+[lL]?"
Jonathan Nieder664d44e2011-01-11 15:48:50 -0600126 "|[-+*/<>%&^|=!]=|//=?|<<=?|>>=?|\\*\\*=?"),
Thomas Rast80c49c32009-01-17 17:29:48 +0100127 /* -- */
128PATTERNS("ruby", "^[ \t]*((class|module|def)[ \t].*)$",
129 /* -- */
130 "(@|@@|\\$)?[a-zA-Z_][a-zA-Z0-9_]*"
131 "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+|\\?(\\\\C-)?(\\\\M-)?."
Jonathan Nieder664d44e2011-01-11 15:48:50 -0600132 "|//=?|[-+*/<>%&^|=!]=|<<=?|>>=?|===|\\.{1,3}|::|[!=]~"),
Thomas Rast80c49c32009-01-17 17:29:48 +0100133PATTERNS("bibtex", "(@[a-zA-Z]{1,}[ \t]*\\{{0,1}[ \t]*[^ \t\"@',\\#}{~%]*).*$",
134 "[={}\"]|[^={}\" \t]+"),
135PATTERNS("tex", "^(\\\\((sub)*section|chapter|part)\\*{0,1}\\{.*)$",
Jonathan Nieder664d44e2011-01-11 15:48:50 -0600136 "\\\\[a-zA-Z@]+|\\\\.|[a-zA-Z0-9\x80-\xff]+"),
Thomas Rast80c49c32009-01-17 17:29:48 +0100137PATTERNS("cpp",
138 /* Jump targets or access declarations */
Johannes Sixt8a2e8da2014-03-21 22:07:22 +0100139 "!^[ \t]*[A-Za-z_][A-Za-z_0-9]*:[[:space:]]*($|/[/*])\n"
140 /* functions/methods, variables, and compounds at top level */
141 "^((::[[:space:]]*)?[A-Za-z_].*)$",
Thomas Rast80c49c32009-01-17 17:29:48 +0100142 /* -- */
143 "[a-zA-Z_][a-zA-Z0-9_]*"
Johannes Sixtabf8f982014-03-21 22:07:14 +0100144 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lLuU]*"
Johannes Sixt407e07f2014-03-21 22:07:13 +0100145 "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->\\*?|\\.\\*"),
Petr Onderkab2212072010-08-16 17:01:02 +0000146PATTERNS("csharp",
147 /* Keywords */
148 "!^[ \t]*(do|while|for|if|else|instanceof|new|return|switch|case|throw|catch|using)\n"
149 /* Methods and constructors */
Thomas Levesquea12cec92018-03-08 11:05:32 +0000150 "^[ \t]*(((static|public|internal|private|protected|new|virtual|sealed|override|unsafe|async)[ \t]+)*[][<>@.~_[:alnum:]]+[ \t]+[<>@._[:alnum:]]+[ \t]*\\(.*\\))[ \t]*$\n"
Petr Onderkab2212072010-08-16 17:01:02 +0000151 /* Properties */
152 "^[ \t]*(((static|public|internal|private|protected|new|virtual|sealed|override|unsafe)[ \t]+)*[][<>@.~_[:alnum:]]+[ \t]+[@._[:alnum:]]+)[ \t]*$\n"
153 /* Type definitions */
154 "^[ \t]*(((static|public|internal|private|protected|new|unsafe|sealed|abstract|partial)[ \t]+)*(class|enum|interface|struct)[ \t]+.*)$\n"
155 /* Namespace */
156 "^[ \t]*(namespace[ \t]+.*)$",
157 /* -- */
158 "[a-zA-Z_][a-zA-Z0-9_]*"
159 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?"
Jonathan Nieder664d44e2011-01-11 15:48:50 -0600160 "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->"),
William Duclot0719f3e2016-06-03 14:32:26 +0200161IPATTERN("css",
162 "![:;][[:space:]]*$\n"
163 "^[_a-z0-9].*$",
164 /* -- */
165 /*
166 * This regex comes from W3C CSS specs. Should theoretically also
167 * allow ISO 10646 characters U+00A0 and higher,
168 * but they are not handled in this regex.
169 */
170 "-?[_a-zA-Z][-_a-zA-Z0-9]*" /* identifiers */
171 "|-?[0-9]+|\\#[0-9a-fA-F]+" /* numbers */
172),
Jeff King122aa6f2008-10-05 17:43:36 -0400173{ "default", NULL, -1, { NULL, 0 } },
Jeff Kingbe58e702008-10-05 17:43:21 -0400174};
Thomas Rast80c49c32009-01-17 17:29:48 +0100175#undef PATTERNS
Brandon Casey909a5492010-09-10 11:18:14 -0500176#undef IPATTERN
Jeff Kingbe58e702008-10-05 17:43:21 -0400177
178static struct userdiff_driver driver_true = {
179 "diff=true",
180 NULL,
Jeff King122aa6f2008-10-05 17:43:36 -0400181 0,
Jeff Kingbe58e702008-10-05 17:43:21 -0400182 { NULL, 0 }
183};
Jeff Kingbe58e702008-10-05 17:43:21 -0400184
185static struct userdiff_driver driver_false = {
186 "!diff",
187 NULL,
Jeff King122aa6f2008-10-05 17:43:36 -0400188 1,
Jeff Kingbe58e702008-10-05 17:43:21 -0400189 { NULL, 0 }
190};
Jeff Kingbe58e702008-10-05 17:43:21 -0400191
192static struct userdiff_driver *userdiff_find_by_namelen(const char *k, int len)
193{
194 int i;
195 for (i = 0; i < ndrivers; i++) {
196 struct userdiff_driver *drv = drivers + i;
197 if (!strncmp(drv->name, k, len) && !drv->name[len])
198 return drv;
199 }
200 for (i = 0; i < ARRAY_SIZE(builtin_drivers); i++) {
201 struct userdiff_driver *drv = builtin_drivers + i;
202 if (!strncmp(drv->name, k, len) && !drv->name[len])
203 return drv;
204 }
205 return NULL;
206}
207
Jeff Kingbe58e702008-10-05 17:43:21 -0400208static int parse_funcname(struct userdiff_funcname *f, const char *k,
209 const char *v, int cflags)
210{
211 if (git_config_string(&f->pattern, k, v) < 0)
212 return -1;
213 f->cflags = cflags;
Jeff King6680a082012-02-07 13:23:02 -0500214 return 0;
Jeff Kingbe58e702008-10-05 17:43:21 -0400215}
216
Jeff King122aa6f2008-10-05 17:43:36 -0400217static int parse_tristate(int *b, const char *k, const char *v)
218{
219 if (v && !strcasecmp(v, "auto"))
220 *b = -1;
221 else
222 *b = git_config_bool(k, v);
Jeff King6680a082012-02-07 13:23:02 -0500223 return 0;
Jeff King122aa6f2008-10-05 17:43:36 -0400224}
225
Jeff Kingd9bae1a2010-04-01 20:12:15 -0400226static int parse_bool(int *b, const char *k, const char *v)
227{
228 *b = git_config_bool(k, v);
Jeff King6680a082012-02-07 13:23:02 -0500229 return 0;
Jeff Kingd9bae1a2010-04-01 20:12:15 -0400230}
231
Jeff Kingc7534ef2008-10-26 00:45:55 -0400232int userdiff_config(const char *k, const char *v)
Jeff Kingbe58e702008-10-05 17:43:21 -0400233{
234 struct userdiff_driver *drv;
Jeff King0a5987f2013-01-23 01:25:07 -0500235 const char *name, *type;
236 int namelen;
Jeff Kingbe58e702008-10-05 17:43:21 -0400237
Jeff King0a5987f2013-01-23 01:25:07 -0500238 if (parse_config_key(k, "diff", &name, &namelen, &type) || !name)
239 return 0;
240
241 drv = userdiff_find_by_namelen(name, namelen);
242 if (!drv) {
243 ALLOC_GROW(drivers, ndrivers+1, drivers_alloc);
244 drv = &drivers[ndrivers++];
245 memset(drv, 0, sizeof(*drv));
246 drv->name = xmemdupz(name, namelen);
247 drv->binary = -1;
248 }
249
250 if (!strcmp(type, "funcname"))
Jeff Kingbe58e702008-10-05 17:43:21 -0400251 return parse_funcname(&drv->funcname, k, v, 0);
Jeff King0a5987f2013-01-23 01:25:07 -0500252 if (!strcmp(type, "xfuncname"))
Jeff Kingbe58e702008-10-05 17:43:21 -0400253 return parse_funcname(&drv->funcname, k, v, REG_EXTENDED);
Jeff King0a5987f2013-01-23 01:25:07 -0500254 if (!strcmp(type, "binary"))
Jeff King122aa6f2008-10-05 17:43:36 -0400255 return parse_tristate(&drv->binary, k, v);
Jeff King0a5987f2013-01-23 01:25:07 -0500256 if (!strcmp(type, "command"))
Jeff King6680a082012-02-07 13:23:02 -0500257 return git_config_string(&drv->external, k, v);
Jeff King0a5987f2013-01-23 01:25:07 -0500258 if (!strcmp(type, "textconv"))
Jeff King6680a082012-02-07 13:23:02 -0500259 return git_config_string(&drv->textconv, k, v);
Jeff King0a5987f2013-01-23 01:25:07 -0500260 if (!strcmp(type, "cachetextconv"))
Jeff Kingd9bae1a2010-04-01 20:12:15 -0400261 return parse_bool(&drv->textconv_want_cache, k, v);
Jeff King0a5987f2013-01-23 01:25:07 -0500262 if (!strcmp(type, "wordregex"))
Jeff King6680a082012-02-07 13:23:02 -0500263 return git_config_string(&drv->word_regex, k, v);
Jeff Kingbe58e702008-10-05 17:43:21 -0400264
265 return 0;
266}
267
268struct userdiff_driver *userdiff_find_by_name(const char *name) {
269 int len = strlen(name);
270 return userdiff_find_by_namelen(name, len);
271}
272
273struct userdiff_driver *userdiff_find_by_path(const char *path)
274{
Junio C Hamano2aef63d2017-01-27 18:01:57 -0800275 static struct attr_check *check;
Jeff Kingbe58e702008-10-05 17:43:21 -0400276
Junio C Hamano2aef63d2017-01-27 18:01:57 -0800277 if (!check)
278 check = attr_check_initl("diff", NULL);
Jeff Kingbe58e702008-10-05 17:43:21 -0400279 if (!path)
280 return NULL;
Junio C Hamano2aef63d2017-01-27 18:01:57 -0800281 if (git_check_attr(path, check))
Jeff Kingbe58e702008-10-05 17:43:21 -0400282 return NULL;
283
Junio C Hamano2aef63d2017-01-27 18:01:57 -0800284 if (ATTR_TRUE(check->items[0].value))
Jeff Kingbe58e702008-10-05 17:43:21 -0400285 return &driver_true;
Junio C Hamano2aef63d2017-01-27 18:01:57 -0800286 if (ATTR_FALSE(check->items[0].value))
Jeff Kingbe58e702008-10-05 17:43:21 -0400287 return &driver_false;
Junio C Hamano2aef63d2017-01-27 18:01:57 -0800288 if (ATTR_UNSET(check->items[0].value))
Jeff Kingbe58e702008-10-05 17:43:21 -0400289 return NULL;
Junio C Hamano2aef63d2017-01-27 18:01:57 -0800290 return userdiff_find_by_name(check->items[0].value);
Jeff Kingbe58e702008-10-05 17:43:21 -0400291}
Jeff King3813e692011-05-23 16:30:14 -0400292
293struct userdiff_driver *userdiff_get_textconv(struct userdiff_driver *driver)
294{
295 if (!driver->textconv)
296 return NULL;
297
298 if (driver->textconv_want_cache && !driver->textconv_cache) {
299 struct notes_cache *c = xmalloc(sizeof(*c));
300 struct strbuf name = STRBUF_INIT;
301
302 strbuf_addf(&name, "textconv/%s", driver->name);
303 notes_cache_init(c, name.buf, driver->textconv);
304 driver->textconv_cache = c;
Rene Scharfe460c7eb2017-08-30 20:20:15 +0200305 strbuf_release(&name);
Jeff King3813e692011-05-23 16:30:14 -0400306 }
307
308 return driver;
309}