🌐 AI搜索 & 代理 主页
blob: 2125d6da26dbdc01f7397415ef1f67616e6f2a71 [file] [log] [blame]
Jeff Kingd9bae1a2010-04-02 00:12:151#include "cache.h"
Jeff Kingbe58e702008-10-05 21:43:212#include "userdiff.h"
Jeff Kingbe58e702008-10-05 21:43:213#include "attr.h"
4
5static struct userdiff_driver *drivers;
6static int ndrivers;
7static int drivers_alloc;
8
Boyd Stephen Smith Jrae3b9702009-01-21 04:59:549#define PATTERNS(name, pattern, word_regex) \
Jonathan Nieder664d44e2011-01-11 21:48:5010 { name, NULL, -1, { pattern, REG_EXTENDED }, \
11 word_regex "|[^[:space:]]|[\xc0-\xff][\x80-\xbf]+" }
Brandon Casey909a5492010-09-10 16:18:1412#define IPATTERN(name, pattern, word_regex) \
Jonathan Nieder664d44e2011-01-11 21:48:5013 { name, NULL, -1, { pattern, REG_EXTENDED | REG_ICASE }, \
14 word_regex "|[^[:space:]]|[\xc0-\xff][\x80-\xbf]+" }
Jeff Kingbe58e702008-10-05 21:43:2115static struct userdiff_driver builtin_drivers[] = {
Adrian Johnsone90d0652012-09-16 03:54:1516IPATTERN("ada",
Adrian Johnson39a87a22014-02-03 11:33:1617 "!^(.*[ \t])?(is[ \t]+new|renames|is[ \t]+separate)([ \t].*)?$\n"
Adrian Johnsone90d0652012-09-16 03:54:1518 "!^[ \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_]*"
Adrian Johnson39a87a22014-02-03 11:33:1623 "|[-+]?[0-9][0-9#_.aAbBcCdDeEfF]*([eE][+-]?[0-9_]+)?"
Adrian Johnsone90d0652012-09-16 03:54:1524 "|=>|\\.\\.|\\*\\*|:=|/=|>=|<=|<<|>>|<>"),
Brandon Casey909a5492010-09-10 16:18:1425IPATTERN("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_]*)?"
Jonathan Nieder664d44e2011-01-11 21:48:5037 "|//|\\*\\*|::|[/<>=]="),
Zoë Blade69f9c872015-07-21 13:22:4638IPATTERN("fountain", "^((\\.[^.]|(int|ext|est|int\\.?/ext|i/e)[. ]).*)$",
39 "[^ \t-]+"),
Thomas Rast80c49c32009-01-17 16:29:4840PATTERNS("html", "^[ \t]*(<[Hh][1-6][ \t].*>.*)$",
Jonathan Nieder664d44e2011-01-11 21:48:5041 "[^<>= \t]+"),
Thomas Rast80c49c32009-01-17 16:29:4842PATTERNS("java",
Jeff Kingbe58e702008-10-05 21:43:2143 "!^[ \t]*(catch|do|for|if|instanceof|new|return|switch|throw|while)\n"
Paolo Bonzini959e2e62009-06-17 14:26:0644 "^[ \t]*(([A-Za-z_][A-Za-z_0-9]*[ \t]+)+[A-Za-z_][A-Za-z_0-9]*[ \t]*\\([^;]*)$",
45 /* -- */
Thomas Rast80c49c32009-01-17 16:29:4846 "[a-zA-Z_][a-zA-Z0-9_]*"
47 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?"
48 "|[-+*/<>%&^|=!]="
Jonathan Nieder664d44e2011-01-11 21:48:5049 "|--|\\+\\+|<<=?|>>>?=?|&&|\\|\\|"),
Gustaf Hendeby53b10a12011-11-15 20:15:0350PATTERNS("matlab",
51 "^[[:space:]]*((classdef|function)[[:space:]].*)$|^%%[[:space:]].*$",
52 "[a-zA-Z_][a-zA-Z0-9_]*|[-+0-9.e]+|[=~<>]=|\\.[*/\\^']|\\|\\||&&"),
Thomas Rast80c49c32009-01-17 16:29:4853PATTERNS("objc",
Jeff Kingbe58e702008-10-05 21:43:2154 /* 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 */
Paolo Bonzini959e2e62009-06-17 14:26:0659 "^[ \t]*(([A-Za-z_][A-Za-z_0-9]*[ \t]+)+[A-Za-z_][A-Za-z_0-9]*[ \t]*\\([^;]*)$\n"
Jeff Kingbe58e702008-10-05 21:43:2160 /* Objective-C class/protocol definitions */
Thomas Rast80c49c32009-01-17 16:29:4861 "^(@(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]?"
Jonathan Nieder664d44e2011-01-11 21:48:5065 "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->"),
Thomas Rast80c49c32009-01-17 16:29:4866PATTERNS("pascal",
Alexey Shumkinad5b6942011-01-11 08:53:5967 "^(((class[ \t]+)?(procedure|function)|constructor|destructor|interface|"
Jeff Kingbe58e702008-10-05 21:43:2168 "implementation|initialization|finalization)[ \t]*.*)$"
69 "\n"
Thomas Rast80c49c32009-01-17 16:29:4870 "^(.*=[ \t]*(class|record).*)$",
71 /* -- */
72 "[a-zA-Z_][a-zA-Z0-9_]*"
73 "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+"
Jonathan Nieder664d44e2011-01-11 21:48:5074 "|<>|<=|>=|:=|\\.\\."),
Jonathan Nieder71a5d4b2010-12-26 09:07:3175PATTERNS("perl",
Jonathan Niederea2ca442011-05-21 19:38:2676 "^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 */
Jonathan Niederf143d9c2011-05-22 17:29:3290 "^(BEGIN|END|INIT|CHECK|UNITCHECK|AUTOLOAD|DESTROY)[ \t]*"
Jonathan Niederea2ca442011-05-21 19:38:2691 "(\\{[ \t]*)?" /* brace can come here or on the next line */
92 "(#.*)?$\n"
Jonathan Nieder12f09672011-05-21 19:35:5193 "^=head[0-9] .*", /* POD */
Jonathan Nieder71a5d4b2010-12-26 09:07:3194 /* -- */
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 "|=~|!~"
Jonathan Nieder664d44e2011-01-11 21:48:50104 "|<<|<>|<=>|>>"),
Björn Steinbrink6d2f2082010-05-23 18:05:40105PATTERNS("php",
106 "^[\t ]*(((public|protected|private|static)[\t ]+)*function.*)$\n"
107 "^[\t ]*(class.*)$",
Thomas Rast80c49c32009-01-17 16:29:48108 /* -- */
109 "[a-zA-Z_][a-zA-Z0-9_]*"
110 "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+"
Jonathan Nieder664d44e2011-01-11 21:48:50111 "|[-+*/<>%&^|=!.]=|--|\\+\\+|<<=?|>>=?|===|&&|\\|\\||::|->"),
Thomas Rast80c49c32009-01-17 16:29:48112PATTERNS("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]?"
Jonathan Nieder664d44e2011-01-11 21:48:50116 "|[-+*/<>%&^|=!]=|//=?|<<=?|>>=?|\\*\\*=?"),
Thomas Rast80c49c32009-01-17 16:29:48117 /* -- */
118PATTERNS("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-)?."
Jonathan Nieder664d44e2011-01-11 21:48:50122 "|//=?|[-+*/<>%&^|=!]=|<<=?|>>=?|===|\\.{1,3}|::|[!=]~"),
Thomas Rast80c49c32009-01-17 16:29:48123PATTERNS("bibtex", "(@[a-zA-Z]{1,}[ \t]*\\{{0,1}[ \t]*[^ \t\"@',\\#}{~%]*).*$",
124 "[={}\"]|[^={}\" \t]+"),
125PATTERNS("tex", "^(\\\\((sub)*section|chapter|part)\\*{0,1}\\{.*)$",
Jonathan Nieder664d44e2011-01-11 21:48:50126 "\\\\[a-zA-Z@]+|\\\\.|[a-zA-Z0-9\x80-\xff]+"),
Thomas Rast80c49c32009-01-17 16:29:48127PATTERNS("cpp",
128 /* Jump targets or access declarations */
Johannes Sixt8a2e8da2014-03-21 21:07:22129 "!^[ \t]*[A-Za-z_][A-Za-z_0-9]*:[[:space:]]*($|/[/*])\n"
130 /* functions/methods, variables, and compounds at top level */
131 "^((::[[:space:]]*)?[A-Za-z_].*)$",
Thomas Rast80c49c32009-01-17 16:29:48132 /* -- */
133 "[a-zA-Z_][a-zA-Z0-9_]*"
Johannes Sixtabf8f982014-03-21 21:07:14134 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lLuU]*"
Johannes Sixt407e07f2014-03-21 21:07:13135 "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->\\*?|\\.\\*"),
Petr Onderkab2212072010-08-16 17:01:02136PATTERNS("csharp",
137 /* Keywords */
138 "!^[ \t]*(do|while|for|if|else|instanceof|new|return|switch|case|throw|catch|using)\n"
139 /* Methods and constructors */
140 "^[ \t]*(((static|public|internal|private|protected|new|virtual|sealed|override|unsafe)[ \t]+)*[][<>@.~_[:alnum:]]+[ \t]+[<>@._[:alnum:]]+[ \t]*\\(.*\\))[ \t]*$\n"
141 /* Properties */
142 "^[ \t]*(((static|public|internal|private|protected|new|virtual|sealed|override|unsafe)[ \t]+)*[][<>@.~_[:alnum:]]+[ \t]+[@._[:alnum:]]+)[ \t]*$\n"
143 /* Type definitions */
144 "^[ \t]*(((static|public|internal|private|protected|new|unsafe|sealed|abstract|partial)[ \t]+)*(class|enum|interface|struct)[ \t]+.*)$\n"
145 /* Namespace */
146 "^[ \t]*(namespace[ \t]+.*)$",
147 /* -- */
148 "[a-zA-Z_][a-zA-Z0-9_]*"
149 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?"
Jonathan Nieder664d44e2011-01-11 21:48:50150 "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->"),
William Duclot0719f3e2016-06-03 12:32:26151IPATTERN("css",
152 "![:;][[:space:]]*$\n"
153 "^[_a-z0-9].*$",
154 /* -- */
155 /*
156 * This regex comes from W3C CSS specs. Should theoretically also
157 * allow ISO 10646 characters U+00A0 and higher,
158 * but they are not handled in this regex.
159 */
160 "-?[_a-zA-Z][-_a-zA-Z0-9]*" /* identifiers */
161 "|-?[0-9]+|\\#[0-9a-fA-F]+" /* numbers */
162),
Jeff King122aa6f2008-10-05 21:43:36163{ "default", NULL, -1, { NULL, 0 } },
Jeff Kingbe58e702008-10-05 21:43:21164};
Thomas Rast80c49c32009-01-17 16:29:48165#undef PATTERNS
Brandon Casey909a5492010-09-10 16:18:14166#undef IPATTERN
Jeff Kingbe58e702008-10-05 21:43:21167
168static struct userdiff_driver driver_true = {
169 "diff=true",
170 NULL,
Jeff King122aa6f2008-10-05 21:43:36171 0,
Jeff Kingbe58e702008-10-05 21:43:21172 { NULL, 0 }
173};
Jeff Kingbe58e702008-10-05 21:43:21174
175static struct userdiff_driver driver_false = {
176 "!diff",
177 NULL,
Jeff King122aa6f2008-10-05 21:43:36178 1,
Jeff Kingbe58e702008-10-05 21:43:21179 { NULL, 0 }
180};
Jeff Kingbe58e702008-10-05 21:43:21181
182static struct userdiff_driver *userdiff_find_by_namelen(const char *k, int len)
183{
184 int i;
185 for (i = 0; i < ndrivers; i++) {
186 struct userdiff_driver *drv = drivers + i;
187 if (!strncmp(drv->name, k, len) && !drv->name[len])
188 return drv;
189 }
190 for (i = 0; i < ARRAY_SIZE(builtin_drivers); i++) {
191 struct userdiff_driver *drv = builtin_drivers + i;
192 if (!strncmp(drv->name, k, len) && !drv->name[len])
193 return drv;
194 }
195 return NULL;
196}
197
Jeff Kingbe58e702008-10-05 21:43:21198static int parse_funcname(struct userdiff_funcname *f, const char *k,
199 const char *v, int cflags)
200{
201 if (git_config_string(&f->pattern, k, v) < 0)
202 return -1;
203 f->cflags = cflags;
Jeff King6680a082012-02-07 18:23:02204 return 0;
Jeff Kingbe58e702008-10-05 21:43:21205}
206
Jeff King122aa6f2008-10-05 21:43:36207static int parse_tristate(int *b, const char *k, const char *v)
208{
209 if (v && !strcasecmp(v, "auto"))
210 *b = -1;
211 else
212 *b = git_config_bool(k, v);
Jeff King6680a082012-02-07 18:23:02213 return 0;
Jeff King122aa6f2008-10-05 21:43:36214}
215
Jeff Kingd9bae1a2010-04-02 00:12:15216static int parse_bool(int *b, const char *k, const char *v)
217{
218 *b = git_config_bool(k, v);
Jeff King6680a082012-02-07 18:23:02219 return 0;
Jeff Kingd9bae1a2010-04-02 00:12:15220}
221
Jeff Kingc7534ef2008-10-26 04:45:55222int userdiff_config(const char *k, const char *v)
Jeff Kingbe58e702008-10-05 21:43:21223{
224 struct userdiff_driver *drv;
Jeff King0a5987f2013-01-23 06:25:07225 const char *name, *type;
226 int namelen;
Jeff Kingbe58e702008-10-05 21:43:21227
Jeff King0a5987f2013-01-23 06:25:07228 if (parse_config_key(k, "diff", &name, &namelen, &type) || !name)
229 return 0;
230
231 drv = userdiff_find_by_namelen(name, namelen);
232 if (!drv) {
233 ALLOC_GROW(drivers, ndrivers+1, drivers_alloc);
234 drv = &drivers[ndrivers++];
235 memset(drv, 0, sizeof(*drv));
236 drv->name = xmemdupz(name, namelen);
237 drv->binary = -1;
238 }
239
240 if (!strcmp(type, "funcname"))
Jeff Kingbe58e702008-10-05 21:43:21241 return parse_funcname(&drv->funcname, k, v, 0);
Jeff King0a5987f2013-01-23 06:25:07242 if (!strcmp(type, "xfuncname"))
Jeff Kingbe58e702008-10-05 21:43:21243 return parse_funcname(&drv->funcname, k, v, REG_EXTENDED);
Jeff King0a5987f2013-01-23 06:25:07244 if (!strcmp(type, "binary"))
Jeff King122aa6f2008-10-05 21:43:36245 return parse_tristate(&drv->binary, k, v);
Jeff King0a5987f2013-01-23 06:25:07246 if (!strcmp(type, "command"))
Jeff King6680a082012-02-07 18:23:02247 return git_config_string(&drv->external, k, v);
Jeff King0a5987f2013-01-23 06:25:07248 if (!strcmp(type, "textconv"))
Jeff King6680a082012-02-07 18:23:02249 return git_config_string(&drv->textconv, k, v);
Jeff King0a5987f2013-01-23 06:25:07250 if (!strcmp(type, "cachetextconv"))
Jeff Kingd9bae1a2010-04-02 00:12:15251 return parse_bool(&drv->textconv_want_cache, k, v);
Jeff King0a5987f2013-01-23 06:25:07252 if (!strcmp(type, "wordregex"))
Jeff King6680a082012-02-07 18:23:02253 return git_config_string(&drv->word_regex, k, v);
Jeff Kingbe58e702008-10-05 21:43:21254
255 return 0;
256}
257
258struct userdiff_driver *userdiff_find_by_name(const char *name) {
259 int len = strlen(name);
260 return userdiff_find_by_namelen(name, len);
261}
262
263struct userdiff_driver *userdiff_find_by_path(const char *path)
264{
265 static struct git_attr *attr;
266 struct git_attr_check check;
267
268 if (!attr)
Junio C Hamano7fb0eaa2010-01-17 04:39:59269 attr = git_attr("diff");
Jeff Kingbe58e702008-10-05 21:43:21270 check.attr = attr;
271
272 if (!path)
273 return NULL;
Michael Haggertyd932f4e2011-08-04 04:36:33274 if (git_check_attr(path, 1, &check))
Jeff Kingbe58e702008-10-05 21:43:21275 return NULL;
276
277 if (ATTR_TRUE(check.value))
278 return &driver_true;
279 if (ATTR_FALSE(check.value))
280 return &driver_false;
281 if (ATTR_UNSET(check.value))
282 return NULL;
283 return userdiff_find_by_name(check.value);
284}
Jeff King3813e692011-05-23 20:30:14285
286struct userdiff_driver *userdiff_get_textconv(struct userdiff_driver *driver)
287{
288 if (!driver->textconv)
289 return NULL;
290
291 if (driver->textconv_want_cache && !driver->textconv_cache) {
292 struct notes_cache *c = xmalloc(sizeof(*c));
293 struct strbuf name = STRBUF_INIT;
294
295 strbuf_addf(&name, "textconv/%s", driver->name);
296 notes_cache_init(c, name.buf, driver->textconv);
297 driver->textconv_cache = c;
298 }
299
300 return driver;
301}