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