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