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.
17 struct git_attr *next;
20 char name[FLEX_ARRAY];
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)
34 val = ((val << 7) | (val >> 22)) ^ c;
39 struct git_attr *git_attr(const char *name, int len)
41 unsigned hval = hash_name(name, len);
42 unsigned pos = hval % HASHSIZE;
45 for (a = git_attr_hash[pos]; a; a = a->next) {
47 !memcmp(a->name, name, len) && !a->name[len])
51 a = xmalloc(sizeof(*a) + len + 1);
52 memcpy(a->name, name, len);
55 a->next = git_attr_hash[pos];
56 a->attr_nr = attr_nr++;
57 git_attr_hash[pos] = a;
59 check_all_attr = xrealloc(check_all_attr,
60 sizeof(*check_all_attr) * attr_nr);
61 check_all_attr[a->attr_nr].attr = a;
66 * .gitattributes file is one line per record, each of which is
70 * (3) whitespace separated list of attribute names, each of which
71 * could be prefixed with '!' to mean "not set".
76 struct git_attr *attr;
82 struct git_attr *attr;
86 struct attr_state state[FLEX_ARRAY];
89 static const char blank[] = " \t\r\n";
91 static struct match_attr *parse_attr_line(const char *line, const char *src,
92 int lineno, int macro_ok)
96 const char *cp, *name;
97 struct match_attr *res = res;
101 cp = line + strspn(line, blank);
102 if (!*cp || *cp == '#')
105 namelen = strcspn(name, blank);
106 if (strlen(ATTRIBUTE_MACRO_PREFIX) < namelen &&
107 !prefixcmp(name, ATTRIBUTE_MACRO_PREFIX)) {
109 fprintf(stderr, "%s not allowed: %s:%d\n",
114 name += strlen(ATTRIBUTE_MACRO_PREFIX);
115 name += strspn(name, blank);
116 namelen = strcspn(name, blank);
121 for (pass = 0; pass < 2; pass++) {
122 /* pass 0 counts and allocates, pass 1 fills */
125 cp = cp + strspn(cp, blank);
128 ep = cp + strcspn(cp, blank);
130 struct attr_state *e;
132 e = &(res->state[num_attr]);
137 e->attr = git_attr(cp, ep - cp);
140 cp = ep + strspn(ep, blank);
147 sizeof(struct attr_state) * num_attr +
148 (is_macro ? 0 : namelen + 1));
150 res->u.attr = git_attr(name, namelen);
152 res->u.pattern = (char*)&(res->state[num_attr]);
153 memcpy(res->u.pattern, name, namelen);
154 res->u.pattern[namelen] = 0;
156 res->is_macro = is_macro;
157 res->num_attr = num_attr;
163 * Like info/exclude and .gitignore, the attribute information can
164 * come from many places.
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.
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
180 static struct attr_stack {
181 struct attr_stack *prev;
183 unsigned num_matches;
184 struct match_attr **attrs;
187 static void free_attr_elem(struct attr_stack *e)
191 for (i = 0; i < e->num_matches; i++)
196 static const char *builtin_attr[] = {
200 static struct attr_stack *read_attr_from_array(const char **list)
202 struct attr_stack *res;
206 res = xcalloc(1, sizeof(*res));
207 while ((line = *(list++)) != NULL) {
208 struct match_attr *a;
210 a = parse_attr_line(line, "[builtin]", ++lineno, 1);
213 res->attrs = xrealloc(res->attrs, res->num_matches + 1);
214 res->attrs[res->num_matches++] = a;
219 static struct attr_stack *read_attr_from_file(const char *path, int macro_ok)
222 struct attr_stack *res;
226 res = xcalloc(1, sizeof(*res));
227 fp = fopen(path, "r");
231 while (fgets(buf, sizeof(buf), fp)) {
232 struct match_attr *a;
234 a = parse_attr_line(buf, path, ++lineno, macro_ok);
237 res->attrs = xrealloc(res->attrs, res->num_matches + 1);
238 res->attrs[res->num_matches++] = a;
245 static void debug_info(const char *what, struct attr_stack *elem)
247 fprintf(stderr, "%s: %s\n", what, elem->origin ? elem->origin : "()");
249 static void debug_set(const char *what, const char *match, struct git_attr *attr, int set)
251 fprintf(stderr, "%s: %s => %d (%s)\n",
252 what, attr->name, set, match);
254 #define debug_push(a) debug_info("push", (a))
255 #define debug_pop(a) debug_info("pop", (a))
257 #define debug_push(a) do { ; } while (0)
258 #define debug_pop(a) do { ; } while (0)
259 #define debug_set(a,b,c,d) do { ; } while (0)
262 static void bootstrap_attr_stack(void)
265 struct attr_stack *elem;
267 elem = read_attr_from_array(builtin_attr);
269 elem->prev = attr_stack;
272 elem = read_attr_from_file(GITATTRIBUTES_FILE, 1);
273 elem->origin = strdup("");
274 elem->prev = attr_stack;
278 elem = read_attr_from_file(git_path(INFOATTRIBUTES_FILE), 1);
280 elem->prev = attr_stack;
285 static void prepare_attr_stack(const char *path, int dirlen)
287 struct attr_stack *elem, *info;
289 char pathbuf[PATH_MAX];
292 * At the bottom of the attribute stack is the built-in
293 * set of attribute definitions. Then, contents from
294 * .gitattribute files from directories closer to the
295 * root to the ones in deeper directories are pushed
296 * to the stack. Finally, at the very top of the stack
297 * we always keep the contents of $GIT_DIR/info/attributes.
299 * When checking, we use entries from near the top of the
300 * stack, preferring $GIT_DIR/info/attributes, then
301 * .gitattributes in deeper directories to shallower ones,
302 * and finally use the built-in set as the default.
305 bootstrap_attr_stack();
308 * Pop the "info" one that is always at the top of the stack.
311 attr_stack = info->prev;
314 * Pop the ones from directories that are not the prefix of
315 * the path we are checking.
317 while (attr_stack && attr_stack->origin) {
318 int namelen = strlen(attr_stack->origin);
321 if (namelen <= dirlen &&
322 !strncmp(elem->origin, path, namelen))
326 attr_stack = elem->prev;
327 free_attr_elem(elem);
331 * Read from parent directories and push them down
336 len = strlen(attr_stack->origin);
339 memcpy(pathbuf, path, dirlen);
340 memcpy(pathbuf + dirlen, "/", 2);
341 cp = strchr(pathbuf + len + 1, '/');
342 strcpy(cp + 1, GITATTRIBUTES_FILE);
343 elem = read_attr_from_file(pathbuf, 0);
345 elem->origin = strdup(pathbuf);
346 elem->prev = attr_stack;
352 * Finally push the "info" one at the top of the stack.
354 info->prev = attr_stack;
358 static int path_matches(const char *pathname, int pathlen,
360 const char *base, int baselen)
362 if (!strchr(pattern, '/')) {
364 const char *basename = strrchr(pathname, '/');
365 basename = basename ? basename + 1 : pathname;
366 return (fnmatch(pattern, basename, 0) == 0);
369 * match with FNM_PATHNAME; the pattern has base implicitly
374 if (pathlen < baselen ||
375 (baselen && pathname[baselen - 1] != '/') ||
376 strncmp(pathname, base, baselen))
378 return fnmatch(pattern, pathname + baselen, FNM_PATHNAME) == 0;
381 static int fill(const char *path, int pathlen, struct attr_stack *stk, int rem)
383 const char *base = stk->origin ? stk->origin : "";
385 struct git_attr_check *check = check_all_attr;
387 for (i = stk->num_matches - 1; 0 < rem && 0 <= i; i--) {
388 struct match_attr *a = stk->attrs[i];
391 if (path_matches(path, pathlen,
392 a->u.pattern, base, strlen(base))) {
393 for (j = 0; 0 < rem && j < a->num_attr; j++) {
394 struct git_attr *attr = a->state[j].attr;
395 int set = !a->state[j].unset;
396 int *n = &(check[attr->attr_nr].isset);
399 debug_set("fill", a->u.pattern, attr, set);
409 static int macroexpand(struct attr_stack *stk, int rem)
412 struct git_attr_check *check = check_all_attr;
414 for (i = stk->num_matches - 1; 0 < rem && 0 <= i; i--) {
415 struct match_attr *a = stk->attrs[i];
418 if (check[a->u.attr->attr_nr].isset < 0)
420 for (j = 0; 0 < rem && j < a->num_attr; j++) {
421 struct git_attr *attr = a->state[j].attr;
422 int set = !a->state[j].unset;
423 int *n = &(check[attr->attr_nr].isset);
426 debug_set("expand", a->u.attr->name, attr, set);
435 int git_checkattr(const char *path, int num, struct git_attr_check *check)
437 struct attr_stack *stk;
439 int dirlen, pathlen, i, rem;
441 bootstrap_attr_stack();
442 for (i = 0; i < attr_nr; i++)
443 check_all_attr[i].isset = -1;
445 pathlen = strlen(path);
446 cp = strrchr(path, '/');
451 prepare_attr_stack(path, dirlen);
453 for (stk = attr_stack; 0 < rem && stk; stk = stk->prev)
454 rem = fill(path, pathlen, stk, rem);
456 for (stk = attr_stack; 0 < rem && stk; stk = stk->prev)
457 rem = macroexpand(stk, rem);
459 for (i = 0; i < num; i++)
460 check[i].isset = check_all_attr[check[i].attr->attr_nr].isset;