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