]> git.ipfire.org Git - thirdparty/git.git/blame - userdiff.c
is_ntfs_dotgit(): only verify the leading segment
[thirdparty/git.git] / userdiff.c
CommitLineData
d9bae1a1 1#include "cache.h"
b2141fc1 2#include "config.h"
be58e70d 3#include "userdiff.h"
be58e70d
JK
4#include "attr.h"
5
6static struct userdiff_driver *drivers;
7static int ndrivers;
8static int drivers_alloc;
9
ae3b970a 10#define PATTERNS(name, pattern, word_regex) \
664d44ee
JN
11 { name, NULL, -1, { pattern, REG_EXTENDED }, \
12 word_regex "|[^[:space:]]|[\xc0-\xff][\x80-\xbf]+" }
909a5494 13#define IPATTERN(name, pattern, word_regex) \
664d44ee
JN
14 { name, NULL, -1, { pattern, REG_EXTENDED | REG_ICASE }, \
15 word_regex "|[^[:space:]]|[\xc0-\xff][\x80-\xbf]+" }
be58e70d 16static struct userdiff_driver builtin_drivers[] = {
e90d065e 17IPATTERN("ada",
39a87a29 18 "!^(.*[ \t])?(is[ \t]+new|renames|is[ \t]+separate)([ \t].*)?$\n"
e90d065e
AJ
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_]*"
39a87a29 24 "|[-+]?[0-9][0-9#_.aAbBcCdDeEfF]*([eE][+-]?[0-9_]+)?"
e90d065e 25 "|=>|\\.\\.|\\*\\*|:=|/=|>=|<=|<<|>>|<>"),
909a5494
BC
26IPATTERN("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 38 "|//|\\*\\*|::|[/<>=]="),
69f9c87d
ZB
39IPATTERN("fountain", "^((\\.[^.]|(int|ext|est|int\\.?/ext|i/e)[. ]).*)$",
40 "[^ \t-]+"),
9c03caca 41PATTERNS("html", "^[ \t]*(<[Hh][1-6]([ \t].*)?>.*)$",
664d44ee 42 "[^<>= \t]+"),
80c49c3d 43PATTERNS("java",
be58e70d 44 "!^[ \t]*(catch|do|for|if|instanceof|new|return|switch|throw|while)\n"
959e2e64
PB
45 "^[ \t]*(([A-Za-z_][A-Za-z_0-9]*[ \t]+)+[A-Za-z_][A-Za-z_0-9]*[ \t]*\\([^;]*)$",
46 /* -- */
80c49c3d
TR
47 "[a-zA-Z_][a-zA-Z0-9_]*"
48 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?"
49 "|[-+*/<>%&^|=!]="
664d44ee 50 "|--|\\+\\+|<<=?|>>>?=?|&&|\\|\\|"),
53b10a14
GH
51PATTERNS("matlab",
52 "^[[:space:]]*((classdef|function)[[:space:]].*)$|^%%[[:space:]].*$",
53 "[a-zA-Z_][a-zA-Z0-9_]*|[-+0-9.e]+|[=~<>]=|\\.[*/\\^']|\\|\\||&&"),
80c49c3d 54PATTERNS("objc",
be58e70d
JK
55 /* Negate C statements that can look like functions */
56 "!^[ \t]*(do|for|if|else|return|switch|while)\n"
57 /* Objective-C methods */
58 "^[ \t]*([-+][ \t]*\\([ \t]*[A-Za-z_][A-Za-z_0-9* \t]*\\)[ \t]*[A-Za-z_].*)$\n"
59 /* C functions */
959e2e64 60 "^[ \t]*(([A-Za-z_][A-Za-z_0-9]*[ \t]+)+[A-Za-z_][A-Za-z_0-9]*[ \t]*\\([^;]*)$\n"
be58e70d 61 /* Objective-C class/protocol definitions */
80c49c3d
TR
62 "^(@(implementation|interface|protocol)[ \t].*)$",
63 /* -- */
64 "[a-zA-Z_][a-zA-Z0-9_]*"
65 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?"
664d44ee 66 "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->"),
80c49c3d 67PATTERNS("pascal",
ad5b6942 68 "^(((class[ \t]+)?(procedure|function)|constructor|destructor|interface|"
be58e70d
JK
69 "implementation|initialization|finalization)[ \t]*.*)$"
70 "\n"
80c49c3d
TR
71 "^(.*=[ \t]*(class|record).*)$",
72 /* -- */
73 "[a-zA-Z_][a-zA-Z0-9_]*"
74 "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+"
664d44ee 75 "|<>|<=|>=|:=|\\.\\."),
71a5d4bc 76PATTERNS("perl",
ea2ca449
JN
77 "^package .*\n"
78 "^sub [[:alnum:]_':]+[ \t]*"
79 "(\\([^)]*\\)[ \t]*)?" /* prototype */
80 /*
81 * Attributes. A regex can't count nested parentheses,
82 * so just slurp up whatever we see, taking care not
83 * to accept lines like "sub foo; # defined elsewhere".
84 *
85 * An attribute could contain a semicolon, but at that
86 * point it seems reasonable enough to give up.
87 */
88 "(:[^;#]*)?"
89 "(\\{[ \t]*)?" /* brace can come here or on the next line */
90 "(#.*)?$\n" /* comment */
f143d9c6 91 "^(BEGIN|END|INIT|CHECK|UNITCHECK|AUTOLOAD|DESTROY)[ \t]*"
ea2ca449
JN
92 "(\\{[ \t]*)?" /* brace can come here or on the next line */
93 "(#.*)?$\n"
12f0967a 94 "^=head[0-9] .*", /* POD */
71a5d4bc
JN
95 /* -- */
96 "[[:alpha:]_'][[:alnum:]_']*"
97 "|0[xb]?[0-9a-fA-F_]*"
98 /* taking care not to interpret 3..5 as (3.)(.5) */
99 "|[0-9a-fA-F_]+(\\.[0-9a-fA-F_]+)?([eE][-+]?[0-9_]+)?"
100 "|=>|-[rwxoRWXOezsfdlpSugkbctTBMAC>]|~~|::"
101 "|&&=|\\|\\|=|//=|\\*\\*="
102 "|&&|\\|\\||//|\\+\\+|--|\\*\\*|\\.\\.\\.?"
103 "|[-+*/%.^&<>=!|]="
104 "|=~|!~"
664d44ee 105 "|<<|<>|<=>|>>"),
6d2f208c
BS
106PATTERNS("php",
107 "^[\t ]*(((public|protected|private|static)[\t ]+)*function.*)$\n"
108 "^[\t ]*(class.*)$",
80c49c3d
TR
109 /* -- */
110 "[a-zA-Z_][a-zA-Z0-9_]*"
111 "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+"
664d44ee 112 "|[-+*/<>%&^|=!.]=|--|\\+\\+|<<=?|>>=?|===|&&|\\|\\||::|->"),
80c49c3d
TR
113PATTERNS("python", "^[ \t]*((class|def)[ \t].*)$",
114 /* -- */
115 "[a-zA-Z_][a-zA-Z0-9_]*"
116 "|[-+0-9.e]+[jJlL]?|0[xX]?[0-9a-fA-F]+[lL]?"
664d44ee 117 "|[-+*/<>%&^|=!]=|//=?|<<=?|>>=?|\\*\\*=?"),
80c49c3d
TR
118 /* -- */
119PATTERNS("ruby", "^[ \t]*((class|module|def)[ \t].*)$",
120 /* -- */
121 "(@|@@|\\$)?[a-zA-Z_][a-zA-Z0-9_]*"
122 "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+|\\?(\\\\C-)?(\\\\M-)?."
664d44ee 123 "|//=?|[-+*/<>%&^|=!]=|<<=?|>>=?|===|\\.{1,3}|::|[!=]~"),
80c49c3d
TR
124PATTERNS("bibtex", "(@[a-zA-Z]{1,}[ \t]*\\{{0,1}[ \t]*[^ \t\"@',\\#}{~%]*).*$",
125 "[={}\"]|[^={}\" \t]+"),
126PATTERNS("tex", "^(\\\\((sub)*section|chapter|part)\\*{0,1}\\{.*)$",
664d44ee 127 "\\\\[a-zA-Z@]+|\\\\.|[a-zA-Z0-9\x80-\xff]+"),
80c49c3d
TR
128PATTERNS("cpp",
129 /* Jump targets or access declarations */
8a2e8da3
JS
130 "!^[ \t]*[A-Za-z_][A-Za-z_0-9]*:[[:space:]]*($|/[/*])\n"
131 /* functions/methods, variables, and compounds at top level */
132 "^((::[[:space:]]*)?[A-Za-z_].*)$",
80c49c3d
TR
133 /* -- */
134 "[a-zA-Z_][a-zA-Z0-9_]*"
abf8f986 135 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lLuU]*"
407e07f2 136 "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->\\*?|\\.\\*"),
b221207d
PO
137PATTERNS("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 151 "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->"),
0719f3ee
WD
152IPATTERN("css",
153 "![:;][[:space:]]*$\n"
154 "^[_a-z0-9].*$",
155 /* -- */
156 /*
157 * This regex comes from W3C CSS specs. Should theoretically also
158 * allow ISO 10646 characters U+00A0 and higher,
159 * but they are not handled in this regex.
160 */
161 "-?[_a-zA-Z][-_a-zA-Z0-9]*" /* identifiers */
162 "|-?[0-9]+|\\#[0-9a-fA-F]+" /* numbers */
163),
122aa6f9 164{ "default", NULL, -1, { NULL, 0 } },
be58e70d 165};
80c49c3d 166#undef PATTERNS
909a5494 167#undef IPATTERN
be58e70d
JK
168
169static struct userdiff_driver driver_true = {
170 "diff=true",
171 NULL,
122aa6f9 172 0,
be58e70d
JK
173 { NULL, 0 }
174};
be58e70d
JK
175
176static struct userdiff_driver driver_false = {
177 "!diff",
178 NULL,
122aa6f9 179 1,
be58e70d
JK
180 { NULL, 0 }
181};
be58e70d
JK
182
183static struct userdiff_driver *userdiff_find_by_namelen(const char *k, int len)
184{
185 int i;
186 for (i = 0; i < ndrivers; i++) {
187 struct userdiff_driver *drv = drivers + i;
188 if (!strncmp(drv->name, k, len) && !drv->name[len])
189 return drv;
190 }
191 for (i = 0; i < ARRAY_SIZE(builtin_drivers); i++) {
192 struct userdiff_driver *drv = builtin_drivers + i;
193 if (!strncmp(drv->name, k, len) && !drv->name[len])
194 return drv;
195 }
196 return NULL;
197}
198
be58e70d
JK
199static int parse_funcname(struct userdiff_funcname *f, const char *k,
200 const char *v, int cflags)
201{
202 if (git_config_string(&f->pattern, k, v) < 0)
203 return -1;
204 f->cflags = cflags;
6680a087 205 return 0;
be58e70d
JK
206}
207
122aa6f9
JK
208static int parse_tristate(int *b, const char *k, const char *v)
209{
210 if (v && !strcasecmp(v, "auto"))
211 *b = -1;
212 else
213 *b = git_config_bool(k, v);
6680a087 214 return 0;
122aa6f9
JK
215}
216
d9bae1a1
JK
217static int parse_bool(int *b, const char *k, const char *v)
218{
219 *b = git_config_bool(k, v);
6680a087 220 return 0;
d9bae1a1
JK
221}
222
c7534ef4 223int userdiff_config(const char *k, const char *v)
be58e70d
JK
224{
225 struct userdiff_driver *drv;
0a5987fe
JK
226 const char *name, *type;
227 int namelen;
228
229 if (parse_config_key(k, "diff", &name, &namelen, &type) || !name)
230 return 0;
231
232 drv = userdiff_find_by_namelen(name, namelen);
233 if (!drv) {
234 ALLOC_GROW(drivers, ndrivers+1, drivers_alloc);
235 drv = &drivers[ndrivers++];
236 memset(drv, 0, sizeof(*drv));
237 drv->name = xmemdupz(name, namelen);
238 drv->binary = -1;
239 }
be58e70d 240
0a5987fe 241 if (!strcmp(type, "funcname"))
be58e70d 242 return parse_funcname(&drv->funcname, k, v, 0);
0a5987fe 243 if (!strcmp(type, "xfuncname"))
be58e70d 244 return parse_funcname(&drv->funcname, k, v, REG_EXTENDED);
0a5987fe 245 if (!strcmp(type, "binary"))
122aa6f9 246 return parse_tristate(&drv->binary, k, v);
0a5987fe 247 if (!strcmp(type, "command"))
6680a087 248 return git_config_string(&drv->external, k, v);
0a5987fe 249 if (!strcmp(type, "textconv"))
6680a087 250 return git_config_string(&drv->textconv, k, v);
0a5987fe 251 if (!strcmp(type, "cachetextconv"))
d9bae1a1 252 return parse_bool(&drv->textconv_want_cache, k, v);
0a5987fe 253 if (!strcmp(type, "wordregex"))
6680a087 254 return git_config_string(&drv->word_regex, k, v);
be58e70d
JK
255
256 return 0;
257}
258
259struct userdiff_driver *userdiff_find_by_name(const char *name) {
260 int len = strlen(name);
261 return userdiff_find_by_namelen(name, len);
262}
263
264struct userdiff_driver *userdiff_find_by_path(const char *path)
265{
2aef63d3 266 static struct attr_check *check;
be58e70d 267
2aef63d3
JH
268 if (!check)
269 check = attr_check_initl("diff", NULL);
be58e70d
JK
270 if (!path)
271 return NULL;
2aef63d3 272 if (git_check_attr(path, check))
be58e70d
JK
273 return NULL;
274
2aef63d3 275 if (ATTR_TRUE(check->items[0].value))
be58e70d 276 return &driver_true;
2aef63d3 277 if (ATTR_FALSE(check->items[0].value))
be58e70d 278 return &driver_false;
2aef63d3 279 if (ATTR_UNSET(check->items[0].value))
be58e70d 280 return NULL;
2aef63d3 281 return userdiff_find_by_name(check->items[0].value);
be58e70d 282}
3813e690
JK
283
284struct userdiff_driver *userdiff_get_textconv(struct userdiff_driver *driver)
285{
286 if (!driver->textconv)
287 return NULL;
288
289 if (driver->textconv_want_cache && !driver->textconv_cache) {
290 struct notes_cache *c = xmalloc(sizeof(*c));
291 struct strbuf name = STRBUF_INIT;
292
293 strbuf_addf(&name, "textconv/%s", driver->name);
294 notes_cache_init(c, name.buf, driver->textconv);
295 driver->textconv_cache = c;
296 }
297
298 return driver;
299}