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;
19 char name[FLEX_ARRAY];
22 static struct git_attr *(git_attr_hash[HASHSIZE]);
24 static unsigned hash_name(const char *name, int namelen)
31 val = ((val << 7) | (val >> 22)) ^ c;
36 struct git_attr *git_attr(const char *name, int len)
38 unsigned hval = hash_name(name, len);
39 unsigned pos = hval % HASHSIZE;
42 for (a = git_attr_hash[pos]; a; a = a->next) {
44 !memcmp(a->name, name, len) && !a->name[len])
48 a = xmalloc(sizeof(*a) + len + 1);
49 memcpy(a->name, name, len);
52 a->next = git_attr_hash[pos];
53 git_attr_hash[pos] = a;
58 * .gitattributes file is one line per record, each of which is
62 * (3) whitespace separated list of attribute names, each of which
63 * could be prefixed with '!' to mean "not set".
68 struct git_attr *attr;
74 struct attr_state state[FLEX_ARRAY];
77 static const char blank[] = " \t\r\n";
79 static struct match_attr *parse_attr_line(const char *line)
83 const char *cp, *name;
84 struct match_attr *res = res;
87 cp = line + strspn(line, blank);
88 if (!*cp || *cp == '#')
91 namelen = strcspn(name, blank);
93 for (pass = 0; pass < 2; pass++) {
94 /* pass 0 counts and allocates, pass 1 fills */
97 cp = cp + strspn(cp, blank);
100 ep = cp + strcspn(cp, blank);
102 struct attr_state *e;
104 e = &(res->state[num_attr]);
109 e->attr = git_attr(cp, ep - cp);
112 cp = ep + strspn(ep, blank);
118 sizeof(struct attr_state) * num_attr +
120 res->pattern = (char*)&(res->state[num_attr]);
121 memcpy(res->pattern, name, namelen);
122 res->pattern[namelen] = 0;
123 res->num_attr = num_attr;
129 * Like info/exclude and .gitignore, the attribute information can
130 * come from many places.
132 * (1) .gitattribute file of the same directory;
133 * (2) .gitattribute file of the parent directory if (1) does not have any match;
134 * this goes recursively upwards, just like .gitignore
135 * (3) perhaps $GIT_DIR/info/attributes, as the final fallback.
137 * In the same file, later entries override the earlier match, so in the
138 * global list, we would have entries from info/attributes the earliest
139 * (reading the file from top to bottom), .gitattribute of the root
140 * directory (again, reading the file from top to bottom) down to the
141 * current directory, and then scan the list backwards to find the first match.
142 * This is exactly the same as what excluded() does in dir.c to deal with
146 static struct attr_stack {
147 struct attr_stack *prev;
149 unsigned num_matches;
150 struct match_attr **attrs;
153 static void free_attr_elem(struct attr_stack *e)
157 for (i = 0; i < e->num_matches; i++)
162 static const char *builtin_attr[] = {
166 static struct attr_stack *read_attr_from_array(const char **list)
168 struct attr_stack *res;
171 res = xcalloc(1, sizeof(*res));
172 while ((line = *(list++)) != NULL) {
173 struct match_attr *a = parse_attr_line(line);
176 res->attrs = xrealloc(res->attrs, res->num_matches + 1);
177 res->attrs[res->num_matches++] = a;
182 static struct attr_stack *read_attr_from_file(const char *path)
185 struct attr_stack *res;
188 res = xcalloc(1, sizeof(*res));
189 fp = fopen(path, "r");
193 while (fgets(buf, sizeof(buf), fp)) {
194 struct match_attr *a = parse_attr_line(buf);
197 res->attrs = xrealloc(res->attrs, res->num_matches + 1);
198 res->attrs[res->num_matches++] = a;
205 static void debug_info(const char *what, struct attr_stack *elem)
207 fprintf(stderr, "%s: %s\n", what, elem->origin ? elem->origin : "()");
209 #define debug_push(a) debug_info("push", (a))
210 #define debug_pop(a) debug_info("pop", (a))
212 #define debug_push(a) do { ; } while (0)
213 #define debug_pop(a) do { ; } while (0)
216 static void prepare_attr_stack(const char *path, int dirlen)
218 struct attr_stack *elem, *info;
220 char pathbuf[PATH_MAX];
223 * At the bottom of the attribute stack is the built-in
224 * set of attribute definitions. Then, contents from
225 * .gitattribute files from directories closer to the
226 * root to the ones in deeper directories are pushed
227 * to the stack. Finally, at the very top of the stack
228 * we always keep the contents of $GIT_DIR/info/attributes.
230 * When checking, we use entries from near the top of the
231 * stack, preferring $GIT_DIR/info/attributes, then
232 * .gitattributes in deeper directories to shallower ones,
233 * and finally use the built-in set as the default.
236 elem = read_attr_from_array(builtin_attr);
238 elem->prev = attr_stack;
241 elem = read_attr_from_file(GITATTRIBUTES_FILE);
242 elem->origin = strdup("");
243 elem->prev = attr_stack;
247 elem = read_attr_from_file(git_path(INFOATTRIBUTES_FILE));
249 elem->prev = attr_stack;
254 * Pop the "info" one that is always at the top of the stack.
257 attr_stack = info->prev;
260 * Pop the ones from directories that are not the prefix of
261 * the path we are checking.
263 while (attr_stack && attr_stack->origin) {
264 int namelen = strlen(attr_stack->origin);
267 if (namelen <= dirlen &&
268 !strncmp(elem->origin, path, namelen))
272 attr_stack = elem->prev;
273 free_attr_elem(elem);
277 * Read from parent directories and push them down
282 len = strlen(attr_stack->origin);
285 memcpy(pathbuf, path, dirlen);
286 memcpy(pathbuf + dirlen, "/", 2);
287 cp = strchr(pathbuf + len + 1, '/');
288 strcpy(cp + 1, GITATTRIBUTES_FILE);
289 elem = read_attr_from_file(pathbuf);
291 elem->origin = strdup(pathbuf);
292 elem->prev = attr_stack;
298 * Finally push the "info" one at the top of the stack.
300 info->prev = attr_stack;
304 static int path_matches(const char *pathname, int pathlen,
306 const char *base, int baselen)
308 if (!strchr(pattern, '/')) {
310 const char *basename = strrchr(pathname, '/');
311 basename = basename ? basename + 1 : pathname;
312 return (fnmatch(pattern, basename, 0) == 0);
315 * match with FNM_PATHNAME; the pattern has base implicitly
320 if (pathlen < baselen ||
321 (baselen && pathname[baselen - 1] != '/') ||
322 strncmp(pathname, base, baselen))
324 return fnmatch(pattern, pathname + baselen, FNM_PATHNAME) == 0;
328 * I do not like this at all. Only because we allow individual
329 * attribute to be set or unset incrementally by individual
330 * lines in .gitattribute files, we need to do this triple
331 * loop which looks quite wasteful.
333 static int fill(const char *path, int pathlen,
334 struct attr_stack *stk, struct git_attr_check *check,
338 const char *base = stk->origin ? stk->origin : "";
340 for (i = stk->num_matches - 1; 0 < rem && 0 <= i; i--) {
341 struct match_attr *a = stk->attrs[i];
342 if (path_matches(path, pathlen,
343 a->pattern, base, strlen(base))) {
344 for (j = 0; j < a->num_attr; j++) {
345 struct git_attr *attr = a->state[j].attr;
346 int set = !a->state[j].unset;
347 for (k = 0; k < num; k++) {
348 if (0 <= check[k].isset ||
349 check[k].attr != attr)
351 check[k].isset = set;
360 int git_checkattr(const char *path, int num, struct git_attr_check *check)
362 struct attr_stack *stk;
364 int dirlen, pathlen, i, rem;
366 for (i = 0; i < num; i++)
369 pathlen = strlen(path);
370 cp = strrchr(path, '/');
375 prepare_attr_stack(path, dirlen);
377 for (stk = attr_stack; 0 < rem && stk; stk = stk->prev)
378 rem = fill(path, pathlen, stk, check, num, rem);