Use start_command() to run content filters instead of explicit fork/exec.
[git/raj.git] / attr.c
blob92704a3f61c6d43d0377d86feb6598e68797022b
1 #include "cache.h"
2 #include "attr.h"
4 const char git_attr__true[] = "(builtin)true";
5 const char git_attr__false[] = "\0(builtin)false";
6 static 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
13 * The basic design decision here is that we are not going to have
14 * insanely large number of attributes.
16 * This is a randomly chosen prime.
18 #define HASHSIZE 257
20 #ifndef DEBUG_ATTR
21 #define DEBUG_ATTR 0
22 #endif
24 struct git_attr {
25 struct git_attr *next;
26 unsigned h;
27 int attr_nr;
28 char name[FLEX_ARRAY];
30 static int attr_nr;
32 static struct git_attr_check *check_all_attr;
33 static struct git_attr *(git_attr_hash[HASHSIZE]);
35 static unsigned hash_name(const char *name, int namelen)
37 unsigned val = 0;
38 unsigned char c;
40 while (namelen--) {
41 c = *name++;
42 val = ((val << 7) | (val >> 22)) ^ c;
44 return val;
47 static int invalid_attr_name(const char *name, int namelen)
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"
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;
65 return 0;
68 struct git_attr *git_attr(const char *name, int len)
70 unsigned hval = hash_name(name, len);
71 unsigned pos = hval % HASHSIZE;
72 struct git_attr *a;
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;
80 if (invalid_attr_name(name, len))
81 return NULL;
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];
88 a->attr_nr = attr_nr++;
89 git_attr_hash[pos] = a;
91 check_all_attr = xrealloc(check_all_attr,
92 sizeof(*check_all_attr) * attr_nr);
93 check_all_attr[a->attr_nr].attr = a;
94 check_all_attr[a->attr_nr].value = ATTR__UNKNOWN;
95 return a;
99 * .gitattributes file is one line per record, each of which is
101 * (1) glob pattern.
102 * (2) whitespace
103 * (3) whitespace separated list of attribute names, each of which
104 * could be prefixed with '-' to mean "set to false", '!' to mean
105 * "unset".
108 /* What does a matched pattern decide? */
109 struct attr_state {
110 struct git_attr *attr;
111 const char *setto;
114 struct match_attr {
115 union {
116 char *pattern;
117 struct git_attr *attr;
118 } u;
119 char is_macro;
120 unsigned num_attr;
121 struct attr_state state[FLEX_ARRAY];
124 static const char blank[] = " \t\r\n";
126 static const char *parse_attr(const char *src, int lineno, const char *cp,
127 int *num_attr, struct match_attr *res)
129 const char *ep, *equals;
130 int len;
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--;
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;
151 } else {
152 struct attr_state *e;
154 e = &(res->state[*num_attr]);
155 if (*cp == '-' || *cp == '!') {
156 e->setto = (*cp == '-') ? ATTR__FALSE : ATTR__UNSET;
157 cp++;
158 len--;
160 else if (!equals)
161 e->setto = ATTR__TRUE;
162 else {
163 e->setto = xmemdupz(equals + 1, ep - equals - 1);
165 e->attr = git_attr(cp, len);
167 (*num_attr)++;
168 return ep + strspn(ep, blank);
171 static struct match_attr *parse_attr_line(const char *line, const char *src,
172 int lineno, int macro_ok)
174 int namelen;
175 int num_attr;
176 const char *cp, *name;
177 struct match_attr *res = NULL;
178 int pass;
179 int is_macro;
181 cp = line + strspn(line, blank);
182 if (!*cp || *cp == '#')
183 return NULL;
184 name = cp;
185 namelen = strcspn(name, blank);
186 if (strlen(ATTRIBUTE_MACRO_PREFIX) < namelen &&
187 !prefixcmp(name, ATTRIBUTE_MACRO_PREFIX)) {
188 if (!macro_ok) {
189 fprintf(stderr, "%s not allowed: %s:%d\n",
190 name, src, lineno);
191 return NULL;
193 is_macro = 1;
194 name += strlen(ATTRIBUTE_MACRO_PREFIX);
195 name += strspn(name, blank);
196 namelen = strcspn(name, blank);
197 if (invalid_attr_name(name, namelen)) {
198 fprintf(stderr,
199 "%.*s is not a valid attribute name: %s:%d\n",
200 namelen, name, src, lineno);
201 return NULL;
204 else
205 is_macro = 0;
207 for (pass = 0; pass < 2; pass++) {
208 /* pass 0 counts and allocates, pass 1 fills */
209 num_attr = 0;
210 cp = name + namelen;
211 cp = cp + strspn(cp, blank);
212 while (*cp)
213 cp = parse_attr(src, lineno, cp, &num_attr, res);
214 if (pass)
215 break;
216 res = xcalloc(1,
217 sizeof(*res) +
218 sizeof(struct attr_state) * num_attr +
219 (is_macro ? 0 : namelen + 1));
220 if (is_macro)
221 res->u.attr = git_attr(name, namelen);
222 else {
223 res->u.pattern = (char*)&(res->state[num_attr]);
224 memcpy(res->u.pattern, name, namelen);
225 res->u.pattern[namelen] = 0;
227 res->is_macro = is_macro;
228 res->num_attr = num_attr;
230 return res;
234 * Like info/exclude and .gitignore, the attribute information can
235 * come from many places.
237 * (1) .gitattribute file of the same directory;
238 * (2) .gitattribute file of the parent directory if (1) does not have
239 * any match; this goes recursively upwards, just like .gitignore.
240 * (3) $GIT_DIR/info/attributes, which overrides both of the above.
242 * In the same file, later entries override the earlier match, so in the
243 * global list, we would have entries from info/attributes the earliest
244 * (reading the file from top to bottom), .gitattribute of the root
245 * directory (again, reading the file from top to bottom) down to the
246 * current directory, and then scan the list backwards to find the first match.
247 * This is exactly the same as what excluded() does in dir.c to deal with
248 * .gitignore
251 static struct attr_stack {
252 struct attr_stack *prev;
253 char *origin;
254 unsigned num_matches;
255 unsigned alloc;
256 struct match_attr **attrs;
257 } *attr_stack;
259 static void free_attr_elem(struct attr_stack *e)
261 int i;
262 free(e->origin);
263 for (i = 0; i < e->num_matches; i++) {
264 struct match_attr *a = e->attrs[i];
265 int j;
266 for (j = 0; j < a->num_attr; j++) {
267 const char *setto = a->state[j].setto;
268 if (setto == ATTR__TRUE ||
269 setto == ATTR__FALSE ||
270 setto == ATTR__UNSET ||
271 setto == ATTR__UNKNOWN)
273 else
274 free((char*) setto);
276 free(a);
278 free(e);
281 static const char *builtin_attr[] = {
282 "[attr]binary -diff -crlf",
283 NULL,
286 static void handle_attr_line(struct attr_stack *res,
287 const char *line,
288 const char *src,
289 int lineno,
290 int macro_ok)
292 struct match_attr *a;
294 a = parse_attr_line(line, src, lineno, macro_ok);
295 if (!a)
296 return;
297 if (res->alloc <= res->num_matches) {
298 res->alloc = alloc_nr(res->num_matches);
299 res->attrs = xrealloc(res->attrs,
300 sizeof(struct match_attr *) *
301 res->alloc);
303 res->attrs[res->num_matches++] = a;
306 static struct attr_stack *read_attr_from_array(const char **list)
308 struct attr_stack *res;
309 const char *line;
310 int lineno = 0;
312 res = xcalloc(1, sizeof(*res));
313 while ((line = *(list++)) != NULL)
314 handle_attr_line(res, line, "[builtin]", ++lineno, 1);
315 return res;
318 static struct attr_stack *read_attr_from_file(const char *path, int macro_ok)
320 FILE *fp = fopen(path, "r");
321 struct attr_stack *res;
322 char buf[2048];
323 int lineno = 0;
325 if (!fp)
326 return NULL;
327 res = xcalloc(1, sizeof(*res));
328 while (fgets(buf, sizeof(buf), fp))
329 handle_attr_line(res, buf, path, ++lineno, macro_ok);
330 fclose(fp);
331 return res;
334 static void *read_index_data(const char *path)
336 int pos, len;
337 unsigned long sz;
338 enum object_type type;
339 void *data;
341 len = strlen(path);
342 pos = cache_name_pos(path, len);
343 if (pos < 0) {
345 * We might be in the middle of a merge, in which
346 * case we would read stage #2 (ours).
348 int i;
349 for (i = -pos - 1;
350 (pos < 0 && i < active_nr &&
351 !strcmp(active_cache[i]->name, path));
352 i++)
353 if (ce_stage(active_cache[i]) == 2)
354 pos = i;
356 if (pos < 0)
357 return NULL;
358 data = read_sha1_file(active_cache[pos]->sha1, &type, &sz);
359 if (!data || type != OBJ_BLOB) {
360 free(data);
361 return NULL;
363 return data;
366 static struct attr_stack *read_attr(const char *path, int macro_ok)
368 struct attr_stack *res;
369 char *buf, *sp;
370 int lineno = 0;
372 res = read_attr_from_file(path, macro_ok);
373 if (res)
374 return res;
376 res = xcalloc(1, sizeof(*res));
379 * There is no checked out .gitattributes file there, but
380 * we might have it in the index. We allow operation in a
381 * sparsely checked out work tree, so read from it.
383 buf = read_index_data(path);
384 if (!buf)
385 return res;
387 for (sp = buf; *sp; ) {
388 char *ep;
389 int more;
390 for (ep = sp; *ep && *ep != '\n'; ep++)
392 more = (*ep == '\n');
393 *ep = '\0';
394 handle_attr_line(res, sp, path, ++lineno, macro_ok);
395 sp = ep + more;
397 free(buf);
398 return res;
401 #if DEBUG_ATTR
402 static void debug_info(const char *what, struct attr_stack *elem)
404 fprintf(stderr, "%s: %s\n", what, elem->origin ? elem->origin : "()");
406 static void debug_set(const char *what, const char *match, struct git_attr *attr, void *v)
408 const char *value = v;
410 if (ATTR_TRUE(value))
411 value = "set";
412 else if (ATTR_FALSE(value))
413 value = "unset";
414 else if (ATTR_UNSET(value))
415 value = "unspecified";
417 fprintf(stderr, "%s: %s => %s (%s)\n",
418 what, attr->name, (char *) value, match);
420 #define debug_push(a) debug_info("push", (a))
421 #define debug_pop(a) debug_info("pop", (a))
422 #else
423 #define debug_push(a) do { ; } while (0)
424 #define debug_pop(a) do { ; } while (0)
425 #define debug_set(a,b,c,d) do { ; } while (0)
426 #endif
428 static void bootstrap_attr_stack(void)
430 if (!attr_stack) {
431 struct attr_stack *elem;
433 elem = read_attr_from_array(builtin_attr);
434 elem->origin = NULL;
435 elem->prev = attr_stack;
436 attr_stack = elem;
438 elem = read_attr(GITATTRIBUTES_FILE, 1);
439 elem->origin = strdup("");
440 elem->prev = attr_stack;
441 attr_stack = elem;
442 debug_push(elem);
444 elem = read_attr_from_file(git_path(INFOATTRIBUTES_FILE), 1);
445 if (!elem)
446 elem = xcalloc(1, sizeof(*elem));
447 elem->origin = NULL;
448 elem->prev = attr_stack;
449 attr_stack = elem;
453 static void prepare_attr_stack(const char *path, int dirlen)
455 struct attr_stack *elem, *info;
456 int len;
457 char pathbuf[PATH_MAX];
460 * At the bottom of the attribute stack is the built-in
461 * set of attribute definitions. Then, contents from
462 * .gitattribute files from directories closer to the
463 * root to the ones in deeper directories are pushed
464 * to the stack. Finally, at the very top of the stack
465 * we always keep the contents of $GIT_DIR/info/attributes.
467 * When checking, we use entries from near the top of the
468 * stack, preferring $GIT_DIR/info/attributes, then
469 * .gitattributes in deeper directories to shallower ones,
470 * and finally use the built-in set as the default.
472 if (!attr_stack)
473 bootstrap_attr_stack();
476 * Pop the "info" one that is always at the top of the stack.
478 info = attr_stack;
479 attr_stack = info->prev;
482 * Pop the ones from directories that are not the prefix of
483 * the path we are checking.
485 while (attr_stack && attr_stack->origin) {
486 int namelen = strlen(attr_stack->origin);
488 elem = attr_stack;
489 if (namelen <= dirlen &&
490 !strncmp(elem->origin, path, namelen))
491 break;
493 debug_pop(elem);
494 attr_stack = elem->prev;
495 free_attr_elem(elem);
499 * Read from parent directories and push them down
501 while (1) {
502 char *cp;
504 len = strlen(attr_stack->origin);
505 if (dirlen <= len)
506 break;
507 memcpy(pathbuf, path, dirlen);
508 memcpy(pathbuf + dirlen, "/", 2);
509 cp = strchr(pathbuf + len + 1, '/');
510 strcpy(cp + 1, GITATTRIBUTES_FILE);
511 elem = read_attr(pathbuf, 0);
512 *cp = '\0';
513 elem->origin = strdup(pathbuf);
514 elem->prev = attr_stack;
515 attr_stack = elem;
516 debug_push(elem);
520 * Finally push the "info" one at the top of the stack.
522 info->prev = attr_stack;
523 attr_stack = info;
526 static int path_matches(const char *pathname, int pathlen,
527 const char *pattern,
528 const char *base, int baselen)
530 if (!strchr(pattern, '/')) {
531 /* match basename */
532 const char *basename = strrchr(pathname, '/');
533 basename = basename ? basename + 1 : pathname;
534 return (fnmatch(pattern, basename, 0) == 0);
537 * match with FNM_PATHNAME; the pattern has base implicitly
538 * in front of it.
540 if (*pattern == '/')
541 pattern++;
542 if (pathlen < baselen ||
543 (baselen && pathname[baselen - 1] != '/') ||
544 strncmp(pathname, base, baselen))
545 return 0;
546 return fnmatch(pattern, pathname + baselen, FNM_PATHNAME) == 0;
549 static int fill_one(const char *what, struct match_attr *a, int rem)
551 struct git_attr_check *check = check_all_attr;
552 int i;
554 for (i = 0; 0 < rem && i < a->num_attr; i++) {
555 struct git_attr *attr = a->state[i].attr;
556 const char **n = &(check[attr->attr_nr].value);
557 const char *v = a->state[i].setto;
559 if (*n == ATTR__UNKNOWN) {
560 debug_set(what, a->u.pattern, attr, v);
561 *n = v;
562 rem--;
565 return rem;
568 static int fill(const char *path, int pathlen, struct attr_stack *stk, int rem)
570 int i;
571 const char *base = stk->origin ? stk->origin : "";
573 for (i = stk->num_matches - 1; 0 < rem && 0 <= i; i--) {
574 struct match_attr *a = stk->attrs[i];
575 if (a->is_macro)
576 continue;
577 if (path_matches(path, pathlen,
578 a->u.pattern, base, strlen(base)))
579 rem = fill_one("fill", a, rem);
581 return rem;
584 static int macroexpand(struct attr_stack *stk, int rem)
586 int i;
587 struct git_attr_check *check = check_all_attr;
589 for (i = stk->num_matches - 1; 0 < rem && 0 <= i; i--) {
590 struct match_attr *a = stk->attrs[i];
591 if (!a->is_macro)
592 continue;
593 if (check[a->u.attr->attr_nr].value != ATTR__TRUE)
594 continue;
595 rem = fill_one("expand", a, rem);
597 return rem;
600 int git_checkattr(const char *path, int num, struct git_attr_check *check)
602 struct attr_stack *stk;
603 const char *cp;
604 int dirlen, pathlen, i, rem;
606 bootstrap_attr_stack();
607 for (i = 0; i < attr_nr; i++)
608 check_all_attr[i].value = ATTR__UNKNOWN;
610 pathlen = strlen(path);
611 cp = strrchr(path, '/');
612 if (!cp)
613 dirlen = 0;
614 else
615 dirlen = cp - path;
616 prepare_attr_stack(path, dirlen);
617 rem = attr_nr;
618 for (stk = attr_stack; 0 < rem && stk; stk = stk->prev)
619 rem = fill(path, pathlen, stk, rem);
621 for (stk = attr_stack; 0 < rem && stk; stk = stk->prev)
622 rem = macroexpand(stk, rem);
624 for (i = 0; i < num; i++) {
625 const char *value = check_all_attr[check[i].attr->attr_nr].value;
626 if (value == ATTR__UNKNOWN)
627 value = ATTR__UNSET;
628 check[i].value = value;
631 return 0;