Merge branch 'ar/config-from-command-line'
[git] / userdiff.c
1 #include "cache.h"
2 #include "userdiff.h"
3 #include "cache.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 }, word_regex }
12 static struct userdiff_driver builtin_drivers[] = {
13 PATTERNS("html", "^[ \t]*(<[Hh][1-6][ \t].*>.*)$",
14          "[^<>= \t]+|[^[:space:]]|[\x80-\xff]+"),
15 PATTERNS("java",
16          "!^[ \t]*(catch|do|for|if|instanceof|new|return|switch|throw|while)\n"
17          "^[ \t]*(([A-Za-z_][A-Za-z_0-9]*[ \t]+)+[A-Za-z_][A-Za-z_0-9]*[ \t]*\\([^;]*)$",
18          /* -- */
19          "[a-zA-Z_][a-zA-Z0-9_]*"
20          "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?"
21          "|[-+*/<>%&^|=!]="
22          "|--|\\+\\+|<<=?|>>>?=?|&&|\\|\\|"
23          "|[^[:space:]]|[\x80-\xff]+"),
24 PATTERNS("objc",
25          /* Negate C statements that can look like functions */
26          "!^[ \t]*(do|for|if|else|return|switch|while)\n"
27          /* Objective-C methods */
28          "^[ \t]*([-+][ \t]*\\([ \t]*[A-Za-z_][A-Za-z_0-9* \t]*\\)[ \t]*[A-Za-z_].*)$\n"
29          /* C functions */
30          "^[ \t]*(([A-Za-z_][A-Za-z_0-9]*[ \t]+)+[A-Za-z_][A-Za-z_0-9]*[ \t]*\\([^;]*)$\n"
31          /* Objective-C class/protocol definitions */
32          "^(@(implementation|interface|protocol)[ \t].*)$",
33          /* -- */
34          "[a-zA-Z_][a-zA-Z0-9_]*"
35          "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?"
36          "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->"
37          "|[^[:space:]]|[\x80-\xff]+"),
38 PATTERNS("pascal",
39          "^((procedure|function|constructor|destructor|interface|"
40                 "implementation|initialization|finalization)[ \t]*.*)$"
41          "\n"
42          "^(.*=[ \t]*(class|record).*)$",
43          /* -- */
44          "[a-zA-Z_][a-zA-Z0-9_]*"
45          "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+"
46          "|<>|<=|>=|:=|\\.\\."
47          "|[^[:space:]]|[\x80-\xff]+"),
48 PATTERNS("php", "^[\t ]*((function|class).*)",
49          /* -- */
50          "[a-zA-Z_][a-zA-Z0-9_]*"
51          "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+"
52          "|[-+*/<>%&^|=!.]=|--|\\+\\+|<<=?|>>=?|===|&&|\\|\\||::|->"
53          "|[^[:space:]]|[\x80-\xff]+"),
54 PATTERNS("python", "^[ \t]*((class|def)[ \t].*)$",
55          /* -- */
56          "[a-zA-Z_][a-zA-Z0-9_]*"
57          "|[-+0-9.e]+[jJlL]?|0[xX]?[0-9a-fA-F]+[lL]?"
58          "|[-+*/<>%&^|=!]=|//=?|<<=?|>>=?|\\*\\*=?"
59          "|[^[:space:]|[\x80-\xff]+"),
60          /* -- */
61 PATTERNS("ruby", "^[ \t]*((class|module|def)[ \t].*)$",
62          /* -- */
63          "(@|@@|\\$)?[a-zA-Z_][a-zA-Z0-9_]*"
64          "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+|\\?(\\\\C-)?(\\\\M-)?."
65          "|//=?|[-+*/<>%&^|=!]=|<<=?|>>=?|===|\\.{1,3}|::|[!=]~"
66          "|[^[:space:]|[\x80-\xff]+"),
67 PATTERNS("bibtex", "(@[a-zA-Z]{1,}[ \t]*\\{{0,1}[ \t]*[^ \t\"@',\\#}{~%]*).*$",
68          "[={}\"]|[^={}\" \t]+"),
69 PATTERNS("tex", "^(\\\\((sub)*section|chapter|part)\\*{0,1}\\{.*)$",
70          "\\\\[a-zA-Z@]+|\\\\.|[a-zA-Z0-9\x80-\xff]+|[^[:space:]]"),
71 PATTERNS("cpp",
72          /* Jump targets or access declarations */
73          "!^[ \t]*[A-Za-z_][A-Za-z_0-9]*:.*$\n"
74          /* C/++ functions/methods at top level */
75          "^([A-Za-z_][A-Za-z_0-9]*([ \t]+[A-Za-z_][A-Za-z_0-9]*([ \t]*::[ \t]*[^[:space:]]+)?){1,}[ \t]*\\([^;]*)$\n"
76          /* compound type at top level */
77          "^((struct|class|enum)[^;]*)$",
78          /* -- */
79          "[a-zA-Z_][a-zA-Z0-9_]*"
80          "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?"
81          "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->"
82          "|[^[:space:]]|[\x80-\xff]+"),
83 { "default", NULL, -1, { NULL, 0 } },
84 };
85 #undef PATTERNS
86
87 static struct userdiff_driver driver_true = {
88         "diff=true",
89         NULL,
90         0,
91         { NULL, 0 }
92 };
93
94 static struct userdiff_driver driver_false = {
95         "!diff",
96         NULL,
97         1,
98         { NULL, 0 }
99 };
100
101 static struct userdiff_driver *userdiff_find_by_namelen(const char *k, int len)
102 {
103         int i;
104         for (i = 0; i < ndrivers; i++) {
105                 struct userdiff_driver *drv = drivers + i;
106                 if (!strncmp(drv->name, k, len) && !drv->name[len])
107                         return drv;
108         }
109         for (i = 0; i < ARRAY_SIZE(builtin_drivers); i++) {
110                 struct userdiff_driver *drv = builtin_drivers + i;
111                 if (!strncmp(drv->name, k, len) && !drv->name[len])
112                         return drv;
113         }
114         return NULL;
115 }
116
117 static struct userdiff_driver *parse_driver(const char *var,
118                 const char *value, const char *type)
119 {
120         struct userdiff_driver *drv;
121         const char *dot;
122         const char *name;
123         int namelen;
124
125         if (prefixcmp(var, "diff."))
126                 return NULL;
127         dot = strrchr(var, '.');
128         if (dot == var + 4)
129                 return NULL;
130         if (strcmp(type, dot+1))
131                 return NULL;
132
133         name = var + 5;
134         namelen = dot - name;
135         drv = userdiff_find_by_namelen(name, namelen);
136         if (!drv) {
137                 ALLOC_GROW(drivers, ndrivers+1, drivers_alloc);
138                 drv = &drivers[ndrivers++];
139                 memset(drv, 0, sizeof(*drv));
140                 drv->name = xmemdupz(name, namelen);
141                 drv->binary = -1;
142         }
143         return drv;
144 }
145
146 static int parse_funcname(struct userdiff_funcname *f, const char *k,
147                 const char *v, int cflags)
148 {
149         if (git_config_string(&f->pattern, k, v) < 0)
150                 return -1;
151         f->cflags = cflags;
152         return 1;
153 }
154
155 static int parse_string(const char **d, const char *k, const char *v)
156 {
157         if (git_config_string(d, k, v) < 0)
158                 return -1;
159         return 1;
160 }
161
162 static int parse_tristate(int *b, const char *k, const char *v)
163 {
164         if (v && !strcasecmp(v, "auto"))
165                 *b = -1;
166         else
167                 *b = git_config_bool(k, v);
168         return 1;
169 }
170
171 static int parse_bool(int *b, const char *k, const char *v)
172 {
173         *b = git_config_bool(k, v);
174         return 1;
175 }
176
177 int userdiff_config(const char *k, const char *v)
178 {
179         struct userdiff_driver *drv;
180
181         if ((drv = parse_driver(k, v, "funcname")))
182                 return parse_funcname(&drv->funcname, k, v, 0);
183         if ((drv = parse_driver(k, v, "xfuncname")))
184                 return parse_funcname(&drv->funcname, k, v, REG_EXTENDED);
185         if ((drv = parse_driver(k, v, "binary")))
186                 return parse_tristate(&drv->binary, k, v);
187         if ((drv = parse_driver(k, v, "command")))
188                 return parse_string(&drv->external, k, v);
189         if ((drv = parse_driver(k, v, "textconv")))
190                 return parse_string(&drv->textconv, k, v);
191         if ((drv = parse_driver(k, v, "cachetextconv")))
192                 return parse_bool(&drv->textconv_want_cache, k, v);
193         if ((drv = parse_driver(k, v, "wordregex")))
194                 return parse_string(&drv->word_regex, k, v);
195
196         return 0;
197 }
198
199 struct userdiff_driver *userdiff_find_by_name(const char *name) {
200         int len = strlen(name);
201         return userdiff_find_by_namelen(name, len);
202 }
203
204 struct userdiff_driver *userdiff_find_by_path(const char *path)
205 {
206         static struct git_attr *attr;
207         struct git_attr_check check;
208
209         if (!attr)
210                 attr = git_attr("diff");
211         check.attr = attr;
212
213         if (!path)
214                 return NULL;
215         if (git_checkattr(path, 1, &check))
216                 return NULL;
217
218         if (ATTR_TRUE(check.value))
219                 return &driver_true;
220         if (ATTR_FALSE(check.value))
221                 return &driver_false;
222         if (ATTR_UNSET(check.value))
223                 return NULL;
224         return userdiff_find_by_name(check.value);
225 }