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