Document git-check-attr
[git/fastimport.git] / attr.c
blob60fe48f3b84d7dfa2d0e3f27852ceae03d7487d4
1 #include "cache.h"
2 #include "attr.h"
4 /*
5 * The basic design decision here is that we are not going to have
6 * insanely large number of attributes.
8 * This is a randomly chosen prime.
9 */
10 #define HASHSIZE 257
12 #ifndef DEBUG_ATTR
13 #define DEBUG_ATTR 0
14 #endif
16 struct git_attr {
17 struct git_attr *next;
18 unsigned h;
19 int attr_nr;
20 char name[FLEX_ARRAY];
22 static int attr_nr;
24 static struct git_attr_check *check_all_attr;
25 static struct git_attr *(git_attr_hash[HASHSIZE]);
27 static unsigned hash_name(const char *name, int namelen)
29 unsigned val = 0;
30 unsigned char c;
32 while (namelen--) {
33 c = *name++;
34 val = ((val << 7) | (val >> 22)) ^ c;
36 return val;
39 static int invalid_attr_name(const char *name, int namelen)
42 * Attribute name cannot begin with '-' and from
43 * [-A-Za-z0-9_.]. We'd specifically exclude '=' for now,
44 * as we might later want to allow non-binary value for
45 * attributes, e.g. "*.svg merge=special-merge-program-for-svg"
47 if (*name == '-')
48 return -1;
49 while (namelen--) {
50 char ch = *name++;
51 if (! (ch == '-' || ch == '.' || ch == '_' ||
52 ('0' <= ch && ch <= '9') ||
53 ('a' <= ch && ch <= 'z') ||
54 ('A' <= ch && ch <= 'Z')) )
55 return -1;
57 return 0;
60 struct git_attr *git_attr(const char *name, int len)
62 unsigned hval = hash_name(name, len);
63 unsigned pos = hval % HASHSIZE;
64 struct git_attr *a;
66 for (a = git_attr_hash[pos]; a; a = a->next) {
67 if (a->h == hval &&
68 !memcmp(a->name, name, len) && !a->name[len])
69 return a;
72 if (invalid_attr_name(name, len))
73 return NULL;
75 a = xmalloc(sizeof(*a) + len + 1);
76 memcpy(a->name, name, len);
77 a->name[len] = 0;
78 a->h = hval;
79 a->next = git_attr_hash[pos];
80 a->attr_nr = attr_nr++;
81 git_attr_hash[pos] = a;
83 check_all_attr = xrealloc(check_all_attr,
84 sizeof(*check_all_attr) * attr_nr);
85 check_all_attr[a->attr_nr].attr = a;
86 return a;
90 * .gitattributes file is one line per record, each of which is
92 * (1) glob pattern.
93 * (2) whitespace
94 * (3) whitespace separated list of attribute names, each of which
95 * could be prefixed with '-' to mean "not set".
98 struct attr_state {
99 int unset;
100 struct git_attr *attr;
103 struct match_attr {
104 union {
105 char *pattern;
106 struct git_attr *attr;
107 } u;
108 char is_macro;
109 unsigned num_attr;
110 struct attr_state state[FLEX_ARRAY];
113 static const char blank[] = " \t\r\n";
115 static struct match_attr *parse_attr_line(const char *line, const char *src,
116 int lineno, int macro_ok)
118 int namelen;
119 int num_attr;
120 const char *cp, *name;
121 struct match_attr *res = res;
122 int pass;
123 int is_macro;
125 cp = line + strspn(line, blank);
126 if (!*cp || *cp == '#')
127 return NULL;
128 name = cp;
129 namelen = strcspn(name, blank);
130 if (strlen(ATTRIBUTE_MACRO_PREFIX) < namelen &&
131 !prefixcmp(name, ATTRIBUTE_MACRO_PREFIX)) {
132 if (!macro_ok) {
133 fprintf(stderr, "%s not allowed: %s:%d\n",
134 name, src, lineno);
135 return NULL;
137 is_macro = 1;
138 name += strlen(ATTRIBUTE_MACRO_PREFIX);
139 name += strspn(name, blank);
140 namelen = strcspn(name, blank);
141 if (invalid_attr_name(name, namelen)) {
142 fprintf(stderr,
143 "%.*s is not a valid attribute name: %s:%d\n",
144 namelen, name, src, lineno);
145 return NULL;
148 else
149 is_macro = 0;
151 for (pass = 0; pass < 2; pass++) {
152 /* pass 0 counts and allocates, pass 1 fills */
153 num_attr = 0;
154 cp = name + namelen;
155 cp = cp + strspn(cp, blank);
156 while (*cp) {
157 const char *ep;
158 ep = cp + strcspn(cp, blank);
159 if (!pass) {
160 if (*cp == '-')
161 cp++;
162 if (invalid_attr_name(cp, ep - cp)) {
163 fprintf(stderr,
164 "%.*s is not a valid attribute name: %s:%d\n",
165 (int)(ep - cp), cp,
166 src, lineno);
167 return NULL;
169 } else {
170 struct attr_state *e;
172 e = &(res->state[num_attr]);
173 if (*cp == '-') {
174 e->unset = 1;
175 cp++;
177 e->attr = git_attr(cp, ep - cp);
179 num_attr++;
180 cp = ep + strspn(ep, blank);
182 if (pass)
183 break;
185 res = xcalloc(1,
186 sizeof(*res) +
187 sizeof(struct attr_state) * num_attr +
188 (is_macro ? 0 : namelen + 1));
189 if (is_macro) {
190 res->u.attr = git_attr(name, namelen);
192 else {
193 res->u.pattern = (char*)&(res->state[num_attr]);
194 memcpy(res->u.pattern, name, namelen);
195 res->u.pattern[namelen] = 0;
197 res->is_macro = is_macro;
198 res->num_attr = num_attr;
200 return res;
204 * Like info/exclude and .gitignore, the attribute information can
205 * come from many places.
207 * (1) .gitattribute file of the same directory;
208 * (2) .gitattribute file of the parent directory if (1) does not have any match;
209 * this goes recursively upwards, just like .gitignore
210 * (3) perhaps $GIT_DIR/info/attributes, as the final fallback.
212 * In the same file, later entries override the earlier match, so in the
213 * global list, we would have entries from info/attributes the earliest
214 * (reading the file from top to bottom), .gitattribute of the root
215 * directory (again, reading the file from top to bottom) down to the
216 * current directory, and then scan the list backwards to find the first match.
217 * This is exactly the same as what excluded() does in dir.c to deal with
218 * .gitignore
221 static struct attr_stack {
222 struct attr_stack *prev;
223 char *origin;
224 unsigned num_matches;
225 struct match_attr **attrs;
226 } *attr_stack;
228 static void free_attr_elem(struct attr_stack *e)
230 int i;
231 free(e->origin);
232 for (i = 0; i < e->num_matches; i++)
233 free(e->attrs[i]);
234 free(e);
237 static const char *builtin_attr[] = {
238 "[attr]binary -diff -crlf",
239 NULL,
242 static struct attr_stack *read_attr_from_array(const char **list)
244 struct attr_stack *res;
245 const char *line;
246 int lineno = 0;
248 res = xcalloc(1, sizeof(*res));
249 while ((line = *(list++)) != NULL) {
250 struct match_attr *a;
252 a = parse_attr_line(line, "[builtin]", ++lineno, 1);
253 if (!a)
254 continue;
255 res->attrs = xrealloc(res->attrs, res->num_matches + 1);
256 res->attrs[res->num_matches++] = a;
258 return res;
261 static struct attr_stack *read_attr_from_file(const char *path, int macro_ok)
263 FILE *fp;
264 struct attr_stack *res;
265 char buf[2048];
266 int lineno = 0;
268 res = xcalloc(1, sizeof(*res));
269 fp = fopen(path, "r");
270 if (!fp)
271 return res;
273 while (fgets(buf, sizeof(buf), fp)) {
274 struct match_attr *a;
276 a = parse_attr_line(buf, path, ++lineno, macro_ok);
277 if (!a)
278 continue;
279 res->attrs = xrealloc(res->attrs, res->num_matches + 1);
280 res->attrs[res->num_matches++] = a;
282 fclose(fp);
283 return res;
286 #if DEBUG_ATTR
287 static void debug_info(const char *what, struct attr_stack *elem)
289 fprintf(stderr, "%s: %s\n", what, elem->origin ? elem->origin : "()");
291 static void debug_set(const char *what, const char *match, struct git_attr *attr, int set)
293 fprintf(stderr, "%s: %s => %d (%s)\n",
294 what, attr->name, set, match);
296 #define debug_push(a) debug_info("push", (a))
297 #define debug_pop(a) debug_info("pop", (a))
298 #else
299 #define debug_push(a) do { ; } while (0)
300 #define debug_pop(a) do { ; } while (0)
301 #define debug_set(a,b,c,d) do { ; } while (0)
302 #endif
304 static void bootstrap_attr_stack(void)
306 if (!attr_stack) {
307 struct attr_stack *elem;
309 elem = read_attr_from_array(builtin_attr);
310 elem->origin = NULL;
311 elem->prev = attr_stack;
312 attr_stack = elem;
314 elem = read_attr_from_file(GITATTRIBUTES_FILE, 1);
315 elem->origin = strdup("");
316 elem->prev = attr_stack;
317 attr_stack = elem;
318 debug_push(elem);
320 elem = read_attr_from_file(git_path(INFOATTRIBUTES_FILE), 1);
321 elem->origin = NULL;
322 elem->prev = attr_stack;
323 attr_stack = elem;
327 static void prepare_attr_stack(const char *path, int dirlen)
329 struct attr_stack *elem, *info;
330 int len;
331 char pathbuf[PATH_MAX];
334 * At the bottom of the attribute stack is the built-in
335 * set of attribute definitions. Then, contents from
336 * .gitattribute files from directories closer to the
337 * root to the ones in deeper directories are pushed
338 * to the stack. Finally, at the very top of the stack
339 * we always keep the contents of $GIT_DIR/info/attributes.
341 * When checking, we use entries from near the top of the
342 * stack, preferring $GIT_DIR/info/attributes, then
343 * .gitattributes in deeper directories to shallower ones,
344 * and finally use the built-in set as the default.
346 if (!attr_stack)
347 bootstrap_attr_stack();
350 * Pop the "info" one that is always at the top of the stack.
352 info = attr_stack;
353 attr_stack = info->prev;
356 * Pop the ones from directories that are not the prefix of
357 * the path we are checking.
359 while (attr_stack && attr_stack->origin) {
360 int namelen = strlen(attr_stack->origin);
362 elem = attr_stack;
363 if (namelen <= dirlen &&
364 !strncmp(elem->origin, path, namelen))
365 break;
367 debug_pop(elem);
368 attr_stack = elem->prev;
369 free_attr_elem(elem);
373 * Read from parent directories and push them down
375 while (1) {
376 char *cp;
378 len = strlen(attr_stack->origin);
379 if (dirlen <= len)
380 break;
381 memcpy(pathbuf, path, dirlen);
382 memcpy(pathbuf + dirlen, "/", 2);
383 cp = strchr(pathbuf + len + 1, '/');
384 strcpy(cp + 1, GITATTRIBUTES_FILE);
385 elem = read_attr_from_file(pathbuf, 0);
386 *cp = '\0';
387 elem->origin = strdup(pathbuf);
388 elem->prev = attr_stack;
389 attr_stack = elem;
390 debug_push(elem);
394 * Finally push the "info" one at the top of the stack.
396 info->prev = attr_stack;
397 attr_stack = info;
400 static int path_matches(const char *pathname, int pathlen,
401 const char *pattern,
402 const char *base, int baselen)
404 if (!strchr(pattern, '/')) {
405 /* match basename */
406 const char *basename = strrchr(pathname, '/');
407 basename = basename ? basename + 1 : pathname;
408 return (fnmatch(pattern, basename, 0) == 0);
411 * match with FNM_PATHNAME; the pattern has base implicitly
412 * in front of it.
414 if (*pattern == '/')
415 pattern++;
416 if (pathlen < baselen ||
417 (baselen && pathname[baselen - 1] != '/') ||
418 strncmp(pathname, base, baselen))
419 return 0;
420 return fnmatch(pattern, pathname + baselen, FNM_PATHNAME) == 0;
423 static int fill(const char *path, int pathlen, struct attr_stack *stk, int rem)
425 const char *base = stk->origin ? stk->origin : "";
426 int i, j;
427 struct git_attr_check *check = check_all_attr;
429 for (i = stk->num_matches - 1; 0 < rem && 0 <= i; i--) {
430 struct match_attr *a = stk->attrs[i];
431 if (a->is_macro)
432 continue;
433 if (path_matches(path, pathlen,
434 a->u.pattern, base, strlen(base))) {
435 for (j = 0; 0 < rem && j < a->num_attr; j++) {
436 struct git_attr *attr = a->state[j].attr;
437 int set = !a->state[j].unset;
438 int *n = &(check[attr->attr_nr].isset);
440 if (*n < 0) {
441 debug_set("fill", a->u.pattern, attr, set);
442 *n = set;
443 rem--;
448 return rem;
451 static int macroexpand(struct attr_stack *stk, int rem)
453 int i, j;
454 struct git_attr_check *check = check_all_attr;
456 for (i = stk->num_matches - 1; 0 < rem && 0 <= i; i--) {
457 struct match_attr *a = stk->attrs[i];
458 if (!a->is_macro)
459 continue;
460 if (check[a->u.attr->attr_nr].isset < 0)
461 continue;
462 for (j = 0; 0 < rem && j < a->num_attr; j++) {
463 struct git_attr *attr = a->state[j].attr;
464 int set = !a->state[j].unset;
465 int *n = &(check[attr->attr_nr].isset);
467 if (*n < 0) {
468 debug_set("expand", a->u.attr->name, attr, set);
469 *n = set;
470 rem--;
474 return rem;
477 int git_checkattr(const char *path, int num, struct git_attr_check *check)
479 struct attr_stack *stk;
480 const char *cp;
481 int dirlen, pathlen, i, rem;
483 bootstrap_attr_stack();
484 for (i = 0; i < attr_nr; i++)
485 check_all_attr[i].isset = -1;
487 pathlen = strlen(path);
488 cp = strrchr(path, '/');
489 if (!cp)
490 dirlen = 0;
491 else
492 dirlen = cp - path;
493 prepare_attr_stack(path, dirlen);
494 rem = attr_nr;
495 for (stk = attr_stack; 0 < rem && stk; stk = stk->prev)
496 rem = fill(path, pathlen, stk, rem);
498 for (stk = attr_stack; 0 < rem && stk; stk = stk->prev)
499 rem = macroexpand(stk, rem);
501 for (i = 0; i < num; i++)
502 check[i].isset = check_all_attr[check[i].attr->attr_nr].isset;
504 return 0;