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