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