help.c: Fix detection of custom merge strategy on cygwin
[git/raj.git] / attr.c
blobf6b3f7e850f9fcf5672031049ef1d6f43e619f63
1 #define NO_THE_INDEX_COMPATIBILITY_MACROS
2 #include "cache.h"
3 #include "exec_cmd.h"
4 #include "attr.h"
6 const char git_attr__true[] = "(builtin)true";
7 const char git_attr__false[] = "\0(builtin)false";
8 static const char git_attr__unknown[] = "(builtin)unknown";
9 #define ATTR__TRUE git_attr__true
10 #define ATTR__FALSE git_attr__false
11 #define ATTR__UNSET NULL
12 #define ATTR__UNKNOWN git_attr__unknown
14 static const char *attributes_file;
17 * The basic design decision here is that we are not going to have
18 * insanely large number of attributes.
20 * This is a randomly chosen prime.
22 #define HASHSIZE 257
24 #ifndef DEBUG_ATTR
25 #define DEBUG_ATTR 0
26 #endif
28 struct git_attr {
29 struct git_attr *next;
30 unsigned h;
31 int attr_nr;
32 char name[FLEX_ARRAY];
34 static int attr_nr;
36 static struct git_attr_check *check_all_attr;
37 static struct git_attr *(git_attr_hash[HASHSIZE]);
39 static unsigned hash_name(const char *name, int namelen)
41 unsigned val = 0, c;
43 while (namelen--) {
44 c = *name++;
45 val = ((val << 7) | (val >> 22)) ^ c;
47 return val;
50 static int invalid_attr_name(const char *name, int namelen)
53 * Attribute name cannot begin with '-' and from
54 * [-A-Za-z0-9_.]. We'd specifically exclude '=' for now,
55 * as we might later want to allow non-binary value for
56 * attributes, e.g. "*.svg merge=special-merge-program-for-svg"
58 if (*name == '-')
59 return -1;
60 while (namelen--) {
61 char ch = *name++;
62 if (! (ch == '-' || ch == '.' || ch == '_' ||
63 ('0' <= ch && ch <= '9') ||
64 ('a' <= ch && ch <= 'z') ||
65 ('A' <= ch && ch <= 'Z')) )
66 return -1;
68 return 0;
71 static struct git_attr *git_attr_internal(const char *name, int len)
73 unsigned hval = hash_name(name, len);
74 unsigned pos = hval % HASHSIZE;
75 struct git_attr *a;
77 for (a = git_attr_hash[pos]; a; a = a->next) {
78 if (a->h == hval &&
79 !memcmp(a->name, name, len) && !a->name[len])
80 return a;
83 if (invalid_attr_name(name, len))
84 return NULL;
86 a = xmalloc(sizeof(*a) + len + 1);
87 memcpy(a->name, name, len);
88 a->name[len] = 0;
89 a->h = hval;
90 a->next = git_attr_hash[pos];
91 a->attr_nr = attr_nr++;
92 git_attr_hash[pos] = a;
94 check_all_attr = xrealloc(check_all_attr,
95 sizeof(*check_all_attr) * attr_nr);
96 check_all_attr[a->attr_nr].attr = a;
97 check_all_attr[a->attr_nr].value = ATTR__UNKNOWN;
98 return a;
101 struct git_attr *git_attr(const char *name)
103 return git_attr_internal(name, strlen(name));
107 * .gitattributes file is one line per record, each of which is
109 * (1) glob pattern.
110 * (2) whitespace
111 * (3) whitespace separated list of attribute names, each of which
112 * could be prefixed with '-' to mean "set to false", '!' to mean
113 * "unset".
116 /* What does a matched pattern decide? */
117 struct attr_state {
118 struct git_attr *attr;
119 const char *setto;
122 struct match_attr {
123 union {
124 char *pattern;
125 struct git_attr *attr;
126 } u;
127 char is_macro;
128 unsigned num_attr;
129 struct attr_state state[FLEX_ARRAY];
132 static const char blank[] = " \t\r\n";
134 static const char *parse_attr(const char *src, int lineno, const char *cp,
135 int *num_attr, struct match_attr *res)
137 const char *ep, *equals;
138 int len;
140 ep = cp + strcspn(cp, blank);
141 equals = strchr(cp, '=');
142 if (equals && ep < equals)
143 equals = NULL;
144 if (equals)
145 len = equals - cp;
146 else
147 len = ep - cp;
148 if (!res) {
149 if (*cp == '-' || *cp == '!') {
150 cp++;
151 len--;
153 if (invalid_attr_name(cp, len)) {
154 fprintf(stderr,
155 "%.*s is not a valid attribute name: %s:%d\n",
156 len, cp, src, lineno);
157 return NULL;
159 } else {
160 struct attr_state *e;
162 e = &(res->state[*num_attr]);
163 if (*cp == '-' || *cp == '!') {
164 e->setto = (*cp == '-') ? ATTR__FALSE : ATTR__UNSET;
165 cp++;
166 len--;
168 else if (!equals)
169 e->setto = ATTR__TRUE;
170 else {
171 e->setto = xmemdupz(equals + 1, ep - equals - 1);
173 e->attr = git_attr_internal(cp, len);
175 (*num_attr)++;
176 return ep + strspn(ep, blank);
179 static struct match_attr *parse_attr_line(const char *line, const char *src,
180 int lineno, int macro_ok)
182 int namelen;
183 int num_attr;
184 const char *cp, *name;
185 struct match_attr *res = NULL;
186 int pass;
187 int is_macro;
189 cp = line + strspn(line, blank);
190 if (!*cp || *cp == '#')
191 return NULL;
192 name = cp;
193 namelen = strcspn(name, blank);
194 if (strlen(ATTRIBUTE_MACRO_PREFIX) < namelen &&
195 !prefixcmp(name, ATTRIBUTE_MACRO_PREFIX)) {
196 if (!macro_ok) {
197 fprintf(stderr, "%s not allowed: %s:%d\n",
198 name, src, lineno);
199 return NULL;
201 is_macro = 1;
202 name += strlen(ATTRIBUTE_MACRO_PREFIX);
203 name += strspn(name, blank);
204 namelen = strcspn(name, blank);
205 if (invalid_attr_name(name, namelen)) {
206 fprintf(stderr,
207 "%.*s is not a valid attribute name: %s:%d\n",
208 namelen, name, src, lineno);
209 return NULL;
212 else
213 is_macro = 0;
215 for (pass = 0; pass < 2; pass++) {
216 /* pass 0 counts and allocates, pass 1 fills */
217 num_attr = 0;
218 cp = name + namelen;
219 cp = cp + strspn(cp, blank);
220 while (*cp) {
221 cp = parse_attr(src, lineno, cp, &num_attr, res);
222 if (!cp)
223 return NULL;
225 if (pass)
226 break;
227 res = xcalloc(1,
228 sizeof(*res) +
229 sizeof(struct attr_state) * num_attr +
230 (is_macro ? 0 : namelen + 1));
231 if (is_macro)
232 res->u.attr = git_attr_internal(name, namelen);
233 else {
234 res->u.pattern = (char *)&(res->state[num_attr]);
235 memcpy(res->u.pattern, name, namelen);
236 res->u.pattern[namelen] = 0;
238 res->is_macro = is_macro;
239 res->num_attr = num_attr;
241 return res;
245 * Like info/exclude and .gitignore, the attribute information can
246 * come from many places.
248 * (1) .gitattribute file of the same directory;
249 * (2) .gitattribute file of the parent directory if (1) does not have
250 * any match; this goes recursively upwards, just like .gitignore.
251 * (3) $GIT_DIR/info/attributes, which overrides both of the above.
253 * In the same file, later entries override the earlier match, so in the
254 * global list, we would have entries from info/attributes the earliest
255 * (reading the file from top to bottom), .gitattribute of the root
256 * directory (again, reading the file from top to bottom) down to the
257 * current directory, and then scan the list backwards to find the first match.
258 * This is exactly the same as what excluded() does in dir.c to deal with
259 * .gitignore
262 static struct attr_stack {
263 struct attr_stack *prev;
264 char *origin;
265 unsigned num_matches;
266 unsigned alloc;
267 struct match_attr **attrs;
268 } *attr_stack;
270 static void free_attr_elem(struct attr_stack *e)
272 int i;
273 free(e->origin);
274 for (i = 0; i < e->num_matches; i++) {
275 struct match_attr *a = e->attrs[i];
276 int j;
277 for (j = 0; j < a->num_attr; j++) {
278 const char *setto = a->state[j].setto;
279 if (setto == ATTR__TRUE ||
280 setto == ATTR__FALSE ||
281 setto == ATTR__UNSET ||
282 setto == ATTR__UNKNOWN)
284 else
285 free((char *) setto);
287 free(a);
289 free(e);
292 static const char *builtin_attr[] = {
293 "[attr]binary -diff -text",
294 NULL,
297 static void handle_attr_line(struct attr_stack *res,
298 const char *line,
299 const char *src,
300 int lineno,
301 int macro_ok)
303 struct match_attr *a;
305 a = parse_attr_line(line, src, lineno, macro_ok);
306 if (!a)
307 return;
308 if (res->alloc <= res->num_matches) {
309 res->alloc = alloc_nr(res->num_matches);
310 res->attrs = xrealloc(res->attrs,
311 sizeof(struct match_attr *) *
312 res->alloc);
314 res->attrs[res->num_matches++] = a;
317 static struct attr_stack *read_attr_from_array(const char **list)
319 struct attr_stack *res;
320 const char *line;
321 int lineno = 0;
323 res = xcalloc(1, sizeof(*res));
324 while ((line = *(list++)) != NULL)
325 handle_attr_line(res, line, "[builtin]", ++lineno, 1);
326 return res;
329 static enum git_attr_direction direction;
330 static struct index_state *use_index;
332 static struct attr_stack *read_attr_from_file(const char *path, int macro_ok)
334 FILE *fp = fopen(path, "r");
335 struct attr_stack *res;
336 char buf[2048];
337 int lineno = 0;
339 if (!fp)
340 return NULL;
341 res = xcalloc(1, sizeof(*res));
342 while (fgets(buf, sizeof(buf), fp))
343 handle_attr_line(res, buf, path, ++lineno, macro_ok);
344 fclose(fp);
345 return res;
348 static void *read_index_data(const char *path)
350 int pos, len;
351 unsigned long sz;
352 enum object_type type;
353 void *data;
354 struct index_state *istate = use_index ? use_index : &the_index;
356 len = strlen(path);
357 pos = index_name_pos(istate, path, len);
358 if (pos < 0) {
360 * We might be in the middle of a merge, in which
361 * case we would read stage #2 (ours).
363 int i;
364 for (i = -pos - 1;
365 (pos < 0 && i < istate->cache_nr &&
366 !strcmp(istate->cache[i]->name, path));
367 i++)
368 if (ce_stage(istate->cache[i]) == 2)
369 pos = i;
371 if (pos < 0)
372 return NULL;
373 data = read_sha1_file(istate->cache[pos]->sha1, &type, &sz);
374 if (!data || type != OBJ_BLOB) {
375 free(data);
376 return NULL;
378 return data;
381 static struct attr_stack *read_attr_from_index(const char *path, int macro_ok)
383 struct attr_stack *res;
384 char *buf, *sp;
385 int lineno = 0;
387 buf = read_index_data(path);
388 if (!buf)
389 return NULL;
391 res = xcalloc(1, sizeof(*res));
392 for (sp = buf; *sp; ) {
393 char *ep;
394 int more;
395 for (ep = sp; *ep && *ep != '\n'; ep++)
397 more = (*ep == '\n');
398 *ep = '\0';
399 handle_attr_line(res, sp, path, ++lineno, macro_ok);
400 sp = ep + more;
402 free(buf);
403 return res;
406 static struct attr_stack *read_attr(const char *path, int macro_ok)
408 struct attr_stack *res;
410 if (direction == GIT_ATTR_CHECKOUT) {
411 res = read_attr_from_index(path, macro_ok);
412 if (!res)
413 res = read_attr_from_file(path, macro_ok);
415 else if (direction == GIT_ATTR_CHECKIN) {
416 res = read_attr_from_file(path, macro_ok);
417 if (!res)
419 * There is no checked out .gitattributes file there, but
420 * we might have it in the index. We allow operation in a
421 * sparsely checked out work tree, so read from it.
423 res = read_attr_from_index(path, macro_ok);
425 else
426 res = read_attr_from_index(path, macro_ok);
427 if (!res)
428 res = xcalloc(1, sizeof(*res));
429 return res;
432 #if DEBUG_ATTR
433 static void debug_info(const char *what, struct attr_stack *elem)
435 fprintf(stderr, "%s: %s\n", what, elem->origin ? elem->origin : "()");
437 static void debug_set(const char *what, const char *match, struct git_attr *attr, const void *v)
439 const char *value = v;
441 if (ATTR_TRUE(value))
442 value = "set";
443 else if (ATTR_FALSE(value))
444 value = "unset";
445 else if (ATTR_UNSET(value))
446 value = "unspecified";
448 fprintf(stderr, "%s: %s => %s (%s)\n",
449 what, attr->name, (char *) value, match);
451 #define debug_push(a) debug_info("push", (a))
452 #define debug_pop(a) debug_info("pop", (a))
453 #else
454 #define debug_push(a) do { ; } while (0)
455 #define debug_pop(a) do { ; } while (0)
456 #define debug_set(a,b,c,d) do { ; } while (0)
457 #endif
459 static void drop_attr_stack(void)
461 while (attr_stack) {
462 struct attr_stack *elem = attr_stack;
463 attr_stack = elem->prev;
464 free_attr_elem(elem);
468 static const char *git_etc_gitattributes(void)
470 static const char *system_wide;
471 if (!system_wide)
472 system_wide = system_path(ETC_GITATTRIBUTES);
473 return system_wide;
476 static int git_attr_system(void)
478 return !git_env_bool("GIT_ATTR_NOSYSTEM", 0);
481 static int git_attr_config(const char *var, const char *value, void *dummy)
483 if (!strcmp(var, "core.attributesfile"))
484 return git_config_pathname(&attributes_file, var, value);
486 return 0;
489 static void bootstrap_attr_stack(void)
491 if (!attr_stack) {
492 struct attr_stack *elem;
494 elem = read_attr_from_array(builtin_attr);
495 elem->origin = NULL;
496 elem->prev = attr_stack;
497 attr_stack = elem;
499 if (git_attr_system()) {
500 elem = read_attr_from_file(git_etc_gitattributes(), 1);
501 if (elem) {
502 elem->origin = NULL;
503 elem->prev = attr_stack;
504 attr_stack = elem;
508 git_config(git_attr_config, NULL);
509 if (attributes_file) {
510 elem = read_attr_from_file(attributes_file, 1);
511 if (elem) {
512 elem->origin = NULL;
513 elem->prev = attr_stack;
514 attr_stack = elem;
518 if (!is_bare_repository() || direction == GIT_ATTR_INDEX) {
519 elem = read_attr(GITATTRIBUTES_FILE, 1);
520 elem->origin = strdup("");
521 elem->prev = attr_stack;
522 attr_stack = elem;
523 debug_push(elem);
526 elem = read_attr_from_file(git_path(INFOATTRIBUTES_FILE), 1);
527 if (!elem)
528 elem = xcalloc(1, sizeof(*elem));
529 elem->origin = NULL;
530 elem->prev = attr_stack;
531 attr_stack = elem;
535 static void prepare_attr_stack(const char *path, int dirlen)
537 struct attr_stack *elem, *info;
538 int len;
539 struct strbuf pathbuf;
541 strbuf_init(&pathbuf, dirlen+2+strlen(GITATTRIBUTES_FILE));
544 * At the bottom of the attribute stack is the built-in
545 * set of attribute definitions, followed by the contents
546 * of $(prefix)/etc/gitattributes and a file specified by
547 * core.attributesfile. Then, contents from
548 * .gitattribute files from directories closer to the
549 * root to the ones in deeper directories are pushed
550 * to the stack. Finally, at the very top of the stack
551 * we always keep the contents of $GIT_DIR/info/attributes.
553 * When checking, we use entries from near the top of the
554 * stack, preferring $GIT_DIR/info/attributes, then
555 * .gitattributes in deeper directories to shallower ones,
556 * and finally use the built-in set as the default.
558 if (!attr_stack)
559 bootstrap_attr_stack();
562 * Pop the "info" one that is always at the top of the stack.
564 info = attr_stack;
565 attr_stack = info->prev;
568 * Pop the ones from directories that are not the prefix of
569 * the path we are checking.
571 while (attr_stack && attr_stack->origin) {
572 int namelen = strlen(attr_stack->origin);
574 elem = attr_stack;
575 if (namelen <= dirlen &&
576 !strncmp(elem->origin, path, namelen))
577 break;
579 debug_pop(elem);
580 attr_stack = elem->prev;
581 free_attr_elem(elem);
585 * Read from parent directories and push them down
587 if (!is_bare_repository() || direction == GIT_ATTR_INDEX) {
588 while (1) {
589 char *cp;
591 len = strlen(attr_stack->origin);
592 if (dirlen <= len)
593 break;
594 strbuf_reset(&pathbuf);
595 strbuf_add(&pathbuf, path, dirlen);
596 strbuf_addch(&pathbuf, '/');
597 cp = strchr(pathbuf.buf + len + 1, '/');
598 strcpy(cp + 1, GITATTRIBUTES_FILE);
599 elem = read_attr(pathbuf.buf, 0);
600 *cp = '\0';
601 elem->origin = strdup(pathbuf.buf);
602 elem->prev = attr_stack;
603 attr_stack = elem;
604 debug_push(elem);
608 strbuf_release(&pathbuf);
611 * Finally push the "info" one at the top of the stack.
613 info->prev = attr_stack;
614 attr_stack = info;
617 static int path_matches(const char *pathname, int pathlen,
618 const char *pattern,
619 const char *base, int baselen)
621 if (!strchr(pattern, '/')) {
622 /* match basename */
623 const char *basename = strrchr(pathname, '/');
624 basename = basename ? basename + 1 : pathname;
625 return (fnmatch(pattern, basename, 0) == 0);
628 * match with FNM_PATHNAME; the pattern has base implicitly
629 * in front of it.
631 if (*pattern == '/')
632 pattern++;
633 if (pathlen < baselen ||
634 (baselen && pathname[baselen] != '/') ||
635 strncmp(pathname, base, baselen))
636 return 0;
637 if (baselen != 0)
638 baselen++;
639 return fnmatch(pattern, pathname + baselen, FNM_PATHNAME) == 0;
642 static int macroexpand_one(int attr_nr, int rem);
644 static int fill_one(const char *what, struct match_attr *a, int rem)
646 struct git_attr_check *check = check_all_attr;
647 int i;
649 for (i = a->num_attr - 1; 0 < rem && 0 <= i; i--) {
650 struct git_attr *attr = a->state[i].attr;
651 const char **n = &(check[attr->attr_nr].value);
652 const char *v = a->state[i].setto;
654 if (*n == ATTR__UNKNOWN) {
655 debug_set(what,
656 a->is_macro ? a->u.attr->name : a->u.pattern,
657 attr, v);
658 *n = v;
659 rem--;
660 rem = macroexpand_one(attr->attr_nr, rem);
663 return rem;
666 static int fill(const char *path, int pathlen, struct attr_stack *stk, int rem)
668 int i;
669 const char *base = stk->origin ? stk->origin : "";
671 for (i = stk->num_matches - 1; 0 < rem && 0 <= i; i--) {
672 struct match_attr *a = stk->attrs[i];
673 if (a->is_macro)
674 continue;
675 if (path_matches(path, pathlen,
676 a->u.pattern, base, strlen(base)))
677 rem = fill_one("fill", a, rem);
679 return rem;
682 static int macroexpand_one(int attr_nr, int rem)
684 struct attr_stack *stk;
685 struct match_attr *a = NULL;
686 int i;
688 if (check_all_attr[attr_nr].value != ATTR__TRUE)
689 return rem;
691 for (stk = attr_stack; !a && stk; stk = stk->prev)
692 for (i = stk->num_matches - 1; !a && 0 <= i; i--) {
693 struct match_attr *ma = stk->attrs[i];
694 if (!ma->is_macro)
695 continue;
696 if (ma->u.attr->attr_nr == attr_nr)
697 a = ma;
700 if (a)
701 rem = fill_one("expand", a, rem);
703 return rem;
706 int git_checkattr(const char *path, int num, struct git_attr_check *check)
708 struct attr_stack *stk;
709 const char *cp;
710 int dirlen, pathlen, i, rem;
712 bootstrap_attr_stack();
713 for (i = 0; i < attr_nr; i++)
714 check_all_attr[i].value = ATTR__UNKNOWN;
716 pathlen = strlen(path);
717 cp = strrchr(path, '/');
718 if (!cp)
719 dirlen = 0;
720 else
721 dirlen = cp - path;
722 prepare_attr_stack(path, dirlen);
723 rem = attr_nr;
724 for (stk = attr_stack; 0 < rem && stk; stk = stk->prev)
725 rem = fill(path, pathlen, stk, rem);
727 for (i = 0; i < num; i++) {
728 const char *value = check_all_attr[check[i].attr->attr_nr].value;
729 if (value == ATTR__UNKNOWN)
730 value = ATTR__UNSET;
731 check[i].value = value;
734 return 0;
737 void git_attr_set_direction(enum git_attr_direction new, struct index_state *istate)
739 enum git_attr_direction old = direction;
741 if (is_bare_repository() && new != GIT_ATTR_INDEX)
742 die("BUG: non-INDEX attr direction in a bare repo");
744 direction = new;
745 if (new != old)
746 drop_attr_stack();
747 use_index = istate;