attr.c: refactoring
[git/git.git] / attr.c
CommitLineData
d0bfd026
JH
1#include "cache.h"
2#include "attr.h"
3
a5e92abd
JH
4const char git_attr__true[] = "(builtin)true";
5const char git_attr__false[] = "\0(builtin)false";
6static const char git_attr__unknown[] = "(builtin)unknown";
7#define ATTR__TRUE git_attr__true
8#define ATTR__FALSE git_attr__false
9#define ATTR__UNSET NULL
10#define ATTR__UNKNOWN git_attr__unknown
515106fa 11
d0bfd026
JH
12/*
13 * The basic design decision here is that we are not going to have
14 * insanely large number of attributes.
15 *
16 * This is a randomly chosen prime.
17 */
18#define HASHSIZE 257
19
20#ifndef DEBUG_ATTR
21#define DEBUG_ATTR 0
22#endif
23
24struct git_attr {
25 struct git_attr *next;
26 unsigned h;
f48fd688 27 int attr_nr;
d0bfd026
JH
28 char name[FLEX_ARRAY];
29};
f48fd688 30static int attr_nr;
d0bfd026 31
f48fd688 32static struct git_attr_check *check_all_attr;
d0bfd026
JH
33static struct git_attr *(git_attr_hash[HASHSIZE]);
34
35static unsigned hash_name(const char *name, int namelen)
36{
37 unsigned val = 0;
38 unsigned char c;
39
40 while (namelen--) {
41 c = *name++;
42 val = ((val << 7) | (val >> 22)) ^ c;
43 }
44 return val;
45}
46
e4aee10a
JH
47static int invalid_attr_name(const char *name, int namelen)
48{
49 /*
50 * Attribute name cannot begin with '-' and from
51 * [-A-Za-z0-9_.]. We'd specifically exclude '=' for now,
52 * as we might later want to allow non-binary value for
53 * attributes, e.g. "*.svg merge=special-merge-program-for-svg"
54 */
55 if (*name == '-')
56 return -1;
57 while (namelen--) {
58 char ch = *name++;
59 if (! (ch == '-' || ch == '.' || ch == '_' ||
60 ('0' <= ch && ch <= '9') ||
61 ('a' <= ch && ch <= 'z') ||
62 ('A' <= ch && ch <= 'Z')) )
63 return -1;
64 }
65 return 0;
66}
67
d0bfd026
JH
68struct git_attr *git_attr(const char *name, int len)
69{
70 unsigned hval = hash_name(name, len);
71 unsigned pos = hval % HASHSIZE;
72 struct git_attr *a;
73
74 for (a = git_attr_hash[pos]; a; a = a->next) {
75 if (a->h == hval &&
76 !memcmp(a->name, name, len) && !a->name[len])
77 return a;
78 }
79
e4aee10a
JH
80 if (invalid_attr_name(name, len))
81 return NULL;
82
d0bfd026
JH
83 a = xmalloc(sizeof(*a) + len + 1);
84 memcpy(a->name, name, len);
85 a->name[len] = 0;
86 a->h = hval;
87 a->next = git_attr_hash[pos];
f48fd688 88 a->attr_nr = attr_nr++;
d0bfd026 89 git_attr_hash[pos] = a;
f48fd688
JH
90
91 check_all_attr = xrealloc(check_all_attr,
92 sizeof(*check_all_attr) * attr_nr);
93 check_all_attr[a->attr_nr].attr = a;
515106fa 94 check_all_attr[a->attr_nr].value = ATTR__UNKNOWN;
d0bfd026
JH
95 return a;
96}
97
98/*
99 * .gitattributes file is one line per record, each of which is
100 *
101 * (1) glob pattern.
102 * (2) whitespace
103 * (3) whitespace separated list of attribute names, each of which
515106fa
JH
104 * could be prefixed with '-' to mean "set to false", '!' to mean
105 * "unset".
d0bfd026
JH
106 */
107
515106fa 108/* What does a matched pattern decide? */
d0bfd026 109struct attr_state {
d0bfd026 110 struct git_attr *attr;
a5e92abd 111 const char *setto;
d0bfd026
JH
112};
113
114struct match_attr {
f48fd688
JH
115 union {
116 char *pattern;
117 struct git_attr *attr;
118 } u;
119 char is_macro;
d0bfd026
JH
120 unsigned num_attr;
121 struct attr_state state[FLEX_ARRAY];
122};
123
124static const char blank[] = " \t\r\n";
125
515106fa
JH
126static const char *parse_attr(const char *src, int lineno, const char *cp,
127 int *num_attr, struct match_attr *res)
128{
129 const char *ep, *equals;
130 int len;
131
132 ep = cp + strcspn(cp, blank);
133 equals = strchr(cp, '=');
134 if (equals && ep < equals)
135 equals = NULL;
136 if (equals)
137 len = equals - cp;
138 else
139 len = ep - cp;
140 if (!res) {
141 if (*cp == '-' || *cp == '!') {
142 cp++;
143 len--;
144 }
145 if (invalid_attr_name(cp, len)) {
146 fprintf(stderr,
147 "%.*s is not a valid attribute name: %s:%d\n",
148 len, cp, src, lineno);
149 return NULL;
150 }
151 } else {
152 struct attr_state *e;
153
154 e = &(res->state[*num_attr]);
155 if (*cp == '-' || *cp == '!') {
156 e->setto = (*cp == '-') ? ATTR__FALSE : ATTR__UNSET;
157 cp++;
158 len--;
159 }
160 else if (!equals)
161 e->setto = ATTR__TRUE;
162 else {
163 char *value;
164 int vallen = ep - equals;
165 value = xmalloc(vallen);
166 memcpy(value, equals+1, vallen-1);
167 value[vallen-1] = 0;
168 e->setto = value;
169 }
170 e->attr = git_attr(cp, len);
171 }
172 (*num_attr)++;
173 return ep + strspn(ep, blank);
174}
175
f48fd688
JH
176static struct match_attr *parse_attr_line(const char *line, const char *src,
177 int lineno, int macro_ok)
d0bfd026
JH
178{
179 int namelen;
180 int num_attr;
181 const char *cp, *name;
515106fa 182 struct match_attr *res = NULL;
d0bfd026 183 int pass;
f48fd688 184 int is_macro;
d0bfd026
JH
185
186 cp = line + strspn(line, blank);
187 if (!*cp || *cp == '#')
188 return NULL;
189 name = cp;
190 namelen = strcspn(name, blank);
f48fd688
JH
191 if (strlen(ATTRIBUTE_MACRO_PREFIX) < namelen &&
192 !prefixcmp(name, ATTRIBUTE_MACRO_PREFIX)) {
193 if (!macro_ok) {
194 fprintf(stderr, "%s not allowed: %s:%d\n",
195 name, src, lineno);
196 return NULL;
197 }
198 is_macro = 1;
199 name += strlen(ATTRIBUTE_MACRO_PREFIX);
200 name += strspn(name, blank);
201 namelen = strcspn(name, blank);
e4aee10a
JH
202 if (invalid_attr_name(name, namelen)) {
203 fprintf(stderr,
204 "%.*s is not a valid attribute name: %s:%d\n",
205 namelen, name, src, lineno);
206 return NULL;
207 }
f48fd688
JH
208 }
209 else
210 is_macro = 0;
d0bfd026
JH
211
212 for (pass = 0; pass < 2; pass++) {
213 /* pass 0 counts and allocates, pass 1 fills */
214 num_attr = 0;
215 cp = name + namelen;
216 cp = cp + strspn(cp, blank);
515106fa
JH
217 while (*cp)
218 cp = parse_attr(src, lineno, cp, &num_attr, res);
d0bfd026
JH
219 if (pass)
220 break;
221 res = xcalloc(1,
222 sizeof(*res) +
223 sizeof(struct attr_state) * num_attr +
f48fd688 224 (is_macro ? 0 : namelen + 1));
515106fa 225 if (is_macro)
f48fd688
JH
226 res->u.attr = git_attr(name, namelen);
227 else {
228 res->u.pattern = (char*)&(res->state[num_attr]);
229 memcpy(res->u.pattern, name, namelen);
230 res->u.pattern[namelen] = 0;
231 }
232 res->is_macro = is_macro;
d0bfd026
JH
233 res->num_attr = num_attr;
234 }
235 return res;
236}
237
238/*
239 * Like info/exclude and .gitignore, the attribute information can
240 * come from many places.
241 *
242 * (1) .gitattribute file of the same directory;
515106fa
JH
243 * (2) .gitattribute file of the parent directory if (1) does not have
244 * any match; this goes recursively upwards, just like .gitignore.
245 * (3) $GIT_DIR/info/attributes, which overrides both of the above.
d0bfd026
JH
246 *
247 * In the same file, later entries override the earlier match, so in the
248 * global list, we would have entries from info/attributes the earliest
249 * (reading the file from top to bottom), .gitattribute of the root
250 * directory (again, reading the file from top to bottom) down to the
251 * current directory, and then scan the list backwards to find the first match.
252 * This is exactly the same as what excluded() does in dir.c to deal with
253 * .gitignore
254 */
255
256static struct attr_stack {
257 struct attr_stack *prev;
258 char *origin;
259 unsigned num_matches;
a4413118 260 unsigned alloc;
d0bfd026
JH
261 struct match_attr **attrs;
262} *attr_stack;
263
264static void free_attr_elem(struct attr_stack *e)
265{
266 int i;
267 free(e->origin);
515106fa
JH
268 for (i = 0; i < e->num_matches; i++) {
269 struct match_attr *a = e->attrs[i];
270 int j;
271 for (j = 0; j < a->num_attr; j++) {
a5e92abd 272 const char *setto = a->state[j].setto;
515106fa
JH
273 if (setto == ATTR__TRUE ||
274 setto == ATTR__FALSE ||
275 setto == ATTR__UNSET ||
276 setto == ATTR__UNKNOWN)
277 ;
278 else
a5e92abd 279 free((char*) setto);
515106fa
JH
280 }
281 free(a);
282 }
d0bfd026
JH
283 free(e);
284}
285
286static const char *builtin_attr[] = {
e4aee10a 287 "[attr]binary -diff -crlf",
d0bfd026
JH
288 NULL,
289};
290
a4413118
JH
291static void handle_attr_line(struct attr_stack *res,
292 const char *line,
293 const char *src,
294 int lineno,
295 int macro_ok)
296{
297 struct match_attr *a;
298
299 a = parse_attr_line(line, src, lineno, macro_ok);
300 if (!a)
301 return;
302 if (res->alloc <= res->num_matches) {
303 res->alloc = alloc_nr(res->num_matches);
304 res->attrs = xrealloc(res->attrs,
305 sizeof(struct match_attr *) *
306 res->alloc);
307 }
308 res->attrs[res->num_matches++] = a;
309}
310
d0bfd026
JH
311static struct attr_stack *read_attr_from_array(const char **list)
312{
313 struct attr_stack *res;
314 const char *line;
f48fd688 315 int lineno = 0;
d0bfd026
JH
316
317 res = xcalloc(1, sizeof(*res));
a4413118
JH
318 while ((line = *(list++)) != NULL)
319 handle_attr_line(res, line, "[builtin]", ++lineno, 1);
d0bfd026
JH
320 return res;
321}
322
f48fd688 323static struct attr_stack *read_attr_from_file(const char *path, int macro_ok)
d0bfd026 324{
a4413118 325 FILE *fp = fopen(path, "r");
d0bfd026
JH
326 struct attr_stack *res;
327 char buf[2048];
f48fd688 328 int lineno = 0;
d0bfd026 329
d0bfd026 330 if (!fp)
a4413118
JH
331 return NULL;
332 res = xcalloc(1, sizeof(*res));
333 while (fgets(buf, sizeof(buf), fp))
334 handle_attr_line(res, buf, path, ++lineno, macro_ok);
335 fclose(fp);
336 return res;
337}
d0bfd026 338
a4413118
JH
339static struct attr_stack *read_attr(const char *path, int macro_ok)
340{
341 struct attr_stack *res;
f48fd688 342
a4413118
JH
343 res = read_attr_from_file(path, macro_ok);
344 if (!res)
345 res = xcalloc(1, sizeof(*res));
d0bfd026
JH
346 return res;
347}
348
349#if DEBUG_ATTR
350static void debug_info(const char *what, struct attr_stack *elem)
351{
352 fprintf(stderr, "%s: %s\n", what, elem->origin ? elem->origin : "()");
353}
515106fa 354static void debug_set(const char *what, const char *match, struct git_attr *attr, void *v)
f48fd688 355{
515106fa
JH
356 const char *value = v;
357
358 if (ATTR_TRUE(value))
359 value = "set";
360 else if (ATTR_FALSE(value))
361 value = "unset";
362 else if (ATTR_UNSET(value))
363 value = "unspecified";
364
365 fprintf(stderr, "%s: %s => %s (%s)\n",
366 what, attr->name, (char *) value, match);
f48fd688 367}
d0bfd026
JH
368#define debug_push(a) debug_info("push", (a))
369#define debug_pop(a) debug_info("pop", (a))
370#else
371#define debug_push(a) do { ; } while (0)
372#define debug_pop(a) do { ; } while (0)
f48fd688 373#define debug_set(a,b,c,d) do { ; } while (0)
d0bfd026
JH
374#endif
375
f48fd688
JH
376static void bootstrap_attr_stack(void)
377{
378 if (!attr_stack) {
379 struct attr_stack *elem;
380
381 elem = read_attr_from_array(builtin_attr);
382 elem->origin = NULL;
383 elem->prev = attr_stack;
384 attr_stack = elem;
385
a4413118 386 elem = read_attr(GITATTRIBUTES_FILE, 1);
f48fd688
JH
387 elem->origin = strdup("");
388 elem->prev = attr_stack;
389 attr_stack = elem;
390 debug_push(elem);
391
392 elem = read_attr_from_file(git_path(INFOATTRIBUTES_FILE), 1);
a4413118
JH
393 if (!elem)
394 elem = xcalloc(1, sizeof(*elem));
f48fd688
JH
395 elem->origin = NULL;
396 elem->prev = attr_stack;
397 attr_stack = elem;
398 }
399}
400
d0bfd026
JH
401static void prepare_attr_stack(const char *path, int dirlen)
402{
403 struct attr_stack *elem, *info;
404 int len;
405 char pathbuf[PATH_MAX];
406
407 /*
408 * At the bottom of the attribute stack is the built-in
409 * set of attribute definitions. Then, contents from
410 * .gitattribute files from directories closer to the
411 * root to the ones in deeper directories are pushed
412 * to the stack. Finally, at the very top of the stack
413 * we always keep the contents of $GIT_DIR/info/attributes.
414 *
415 * When checking, we use entries from near the top of the
416 * stack, preferring $GIT_DIR/info/attributes, then
417 * .gitattributes in deeper directories to shallower ones,
418 * and finally use the built-in set as the default.
419 */
f48fd688
JH
420 if (!attr_stack)
421 bootstrap_attr_stack();
d0bfd026
JH
422
423 /*
424 * Pop the "info" one that is always at the top of the stack.
425 */
426 info = attr_stack;
427 attr_stack = info->prev;
428
429 /*
430 * Pop the ones from directories that are not the prefix of
431 * the path we are checking.
432 */
433 while (attr_stack && attr_stack->origin) {
434 int namelen = strlen(attr_stack->origin);
435
436 elem = attr_stack;
437 if (namelen <= dirlen &&
438 !strncmp(elem->origin, path, namelen))
439 break;
440
441 debug_pop(elem);
442 attr_stack = elem->prev;
443 free_attr_elem(elem);
444 }
445
446 /*
447 * Read from parent directories and push them down
448 */
449 while (1) {
450 char *cp;
451
452 len = strlen(attr_stack->origin);
453 if (dirlen <= len)
454 break;
455 memcpy(pathbuf, path, dirlen);
456 memcpy(pathbuf + dirlen, "/", 2);
457 cp = strchr(pathbuf + len + 1, '/');
458 strcpy(cp + 1, GITATTRIBUTES_FILE);
a4413118 459 elem = read_attr(pathbuf, 0);
d0bfd026
JH
460 *cp = '\0';
461 elem->origin = strdup(pathbuf);
462 elem->prev = attr_stack;
463 attr_stack = elem;
464 debug_push(elem);
465 }
466
467 /*
468 * Finally push the "info" one at the top of the stack.
469 */
470 info->prev = attr_stack;
471 attr_stack = info;
472}
473
474static int path_matches(const char *pathname, int pathlen,
475 const char *pattern,
476 const char *base, int baselen)
477{
478 if (!strchr(pattern, '/')) {
479 /* match basename */
480 const char *basename = strrchr(pathname, '/');
481 basename = basename ? basename + 1 : pathname;
482 return (fnmatch(pattern, basename, 0) == 0);
483 }
484 /*
485 * match with FNM_PATHNAME; the pattern has base implicitly
486 * in front of it.
487 */
488 if (*pattern == '/')
489 pattern++;
490 if (pathlen < baselen ||
491 (baselen && pathname[baselen - 1] != '/') ||
492 strncmp(pathname, base, baselen))
493 return 0;
494 return fnmatch(pattern, pathname + baselen, FNM_PATHNAME) == 0;
495}
496
515106fa
JH
497static int fill_one(const char *what, struct match_attr *a, int rem)
498{
499 struct git_attr_check *check = check_all_attr;
500 int i;
501
502 for (i = 0; 0 < rem && i < a->num_attr; i++) {
503 struct git_attr *attr = a->state[i].attr;
a5e92abd
JH
504 const char **n = &(check[attr->attr_nr].value);
505 const char *v = a->state[i].setto;
515106fa
JH
506
507 if (*n == ATTR__UNKNOWN) {
508 debug_set(what, a->u.pattern, attr, v);
509 *n = v;
510 rem--;
511 }
512 }
513 return rem;
514}
515
f48fd688 516static int fill(const char *path, int pathlen, struct attr_stack *stk, int rem)
d0bfd026 517{
515106fa 518 int i;
d0bfd026
JH
519 const char *base = stk->origin ? stk->origin : "";
520
521 for (i = stk->num_matches - 1; 0 < rem && 0 <= i; i--) {
522 struct match_attr *a = stk->attrs[i];
f48fd688
JH
523 if (a->is_macro)
524 continue;
d0bfd026 525 if (path_matches(path, pathlen,
515106fa
JH
526 a->u.pattern, base, strlen(base)))
527 rem = fill_one("fill", a, rem);
d0bfd026
JH
528 }
529 return rem;
530}
531
f48fd688
JH
532static int macroexpand(struct attr_stack *stk, int rem)
533{
515106fa 534 int i;
f48fd688
JH
535 struct git_attr_check *check = check_all_attr;
536
537 for (i = stk->num_matches - 1; 0 < rem && 0 <= i; i--) {
538 struct match_attr *a = stk->attrs[i];
539 if (!a->is_macro)
540 continue;
515106fa 541 if (check[a->u.attr->attr_nr].value != ATTR__TRUE)
f48fd688 542 continue;
515106fa 543 rem = fill_one("expand", a, rem);
f48fd688
JH
544 }
545 return rem;
546}
547
d0bfd026
JH
548int git_checkattr(const char *path, int num, struct git_attr_check *check)
549{
550 struct attr_stack *stk;
551 const char *cp;
552 int dirlen, pathlen, i, rem;
553
f48fd688
JH
554 bootstrap_attr_stack();
555 for (i = 0; i < attr_nr; i++)
515106fa 556 check_all_attr[i].value = ATTR__UNKNOWN;
d0bfd026
JH
557
558 pathlen = strlen(path);
559 cp = strrchr(path, '/');
560 if (!cp)
561 dirlen = 0;
562 else
563 dirlen = cp - path;
564 prepare_attr_stack(path, dirlen);
f48fd688
JH
565 rem = attr_nr;
566 for (stk = attr_stack; 0 < rem && stk; stk = stk->prev)
567 rem = fill(path, pathlen, stk, rem);
568
d0bfd026 569 for (stk = attr_stack; 0 < rem && stk; stk = stk->prev)
f48fd688
JH
570 rem = macroexpand(stk, rem);
571
515106fa 572 for (i = 0; i < num; i++) {
a5e92abd 573 const char *value = check_all_attr[check[i].attr->attr_nr].value;
515106fa
JH
574 if (value == ATTR__UNKNOWN)
575 value = ATTR__UNSET;
576 check[i].value = value;
577 }
f48fd688 578
d0bfd026
JH
579 return 0;
580}