]>
Commit | Line | Data |
---|---|---|
06f33c17 | 1 | #define NO_THE_INDEX_COMPATIBILITY_MACROS |
d0bfd026 JH |
2 | #include "cache.h" |
3 | #include "attr.h" | |
4 | ||
a5e92abd JH |
5 | const char git_attr__true[] = "(builtin)true"; |
6 | const char git_attr__false[] = "\0(builtin)false"; | |
7 | static const char git_attr__unknown[] = "(builtin)unknown"; | |
8 | #define ATTR__TRUE git_attr__true | |
9 | #define ATTR__FALSE git_attr__false | |
10 | #define ATTR__UNSET NULL | |
11 | #define ATTR__UNKNOWN git_attr__unknown | |
515106fa | 12 | |
d0bfd026 JH |
13 | /* |
14 | * The basic design decision here is that we are not going to have | |
15 | * insanely large number of attributes. | |
16 | * | |
17 | * This is a randomly chosen prime. | |
18 | */ | |
19 | #define HASHSIZE 257 | |
20 | ||
21 | #ifndef DEBUG_ATTR | |
22 | #define DEBUG_ATTR 0 | |
23 | #endif | |
24 | ||
25 | struct git_attr { | |
26 | struct git_attr *next; | |
27 | unsigned h; | |
f48fd688 | 28 | int attr_nr; |
d0bfd026 JH |
29 | char name[FLEX_ARRAY]; |
30 | }; | |
f48fd688 | 31 | static int attr_nr; |
d0bfd026 | 32 | |
f48fd688 | 33 | static struct git_attr_check *check_all_attr; |
d0bfd026 JH |
34 | static struct git_attr *(git_attr_hash[HASHSIZE]); |
35 | ||
36 | static unsigned hash_name(const char *name, int namelen) | |
37 | { | |
48fb7deb | 38 | unsigned val = 0, c; |
d0bfd026 JH |
39 | |
40 | while (namelen--) { | |
41 | c = *name++; | |
42 | val = ((val << 7) | (val >> 22)) ^ c; | |
43 | } | |
44 | return val; | |
45 | } | |
46 | ||
e4aee10a JH |
47 | static int invalid_attr_name(const char *name, int namelen) |
48 | { | |
49 | /* | |
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" | |
54 | */ | |
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; | |
64 | } | |
65 | return 0; | |
66 | } | |
67 | ||
7fb0eaa2 | 68 | static struct git_attr *git_attr_internal(const char *name, int len) |
d0bfd026 JH |
69 | { |
70 | unsigned hval = hash_name(name, len); | |
71 | unsigned pos = hval % HASHSIZE; | |
72 | struct git_attr *a; | |
73 | ||
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; | |
78 | } | |
79 | ||
e4aee10a JH |
80 | if (invalid_attr_name(name, len)) |
81 | return NULL; | |
82 | ||
d0bfd026 JH |
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]; | |
f48fd688 | 88 | a->attr_nr = attr_nr++; |
d0bfd026 | 89 | git_attr_hash[pos] = a; |
f48fd688 JH |
90 | |
91 | check_all_attr = xrealloc(check_all_attr, | |
92 | sizeof(*check_all_attr) * attr_nr); | |
93 | check_all_attr[a->attr_nr].attr = a; | |
515106fa | 94 | check_all_attr[a->attr_nr].value = ATTR__UNKNOWN; |
d0bfd026 JH |
95 | return a; |
96 | } | |
97 | ||
7fb0eaa2 JH |
98 | struct git_attr *git_attr(const char *name) |
99 | { | |
100 | return git_attr_internal(name, strlen(name)); | |
101 | } | |
102 | ||
d0bfd026 JH |
103 | /* |
104 | * .gitattributes file is one line per record, each of which is | |
105 | * | |
106 | * (1) glob pattern. | |
107 | * (2) whitespace | |
108 | * (3) whitespace separated list of attribute names, each of which | |
515106fa JH |
109 | * could be prefixed with '-' to mean "set to false", '!' to mean |
110 | * "unset". | |
d0bfd026 JH |
111 | */ |
112 | ||
515106fa | 113 | /* What does a matched pattern decide? */ |
d0bfd026 | 114 | struct attr_state { |
d0bfd026 | 115 | struct git_attr *attr; |
a5e92abd | 116 | const char *setto; |
d0bfd026 JH |
117 | }; |
118 | ||
119 | struct match_attr { | |
f48fd688 JH |
120 | union { |
121 | char *pattern; | |
122 | struct git_attr *attr; | |
123 | } u; | |
124 | char is_macro; | |
d0bfd026 JH |
125 | unsigned num_attr; |
126 | struct attr_state state[FLEX_ARRAY]; | |
127 | }; | |
128 | ||
129 | static const char blank[] = " \t\r\n"; | |
130 | ||
515106fa JH |
131 | static const char *parse_attr(const char *src, int lineno, const char *cp, |
132 | int *num_attr, struct match_attr *res) | |
133 | { | |
134 | const char *ep, *equals; | |
135 | int len; | |
136 | ||
137 | ep = cp + strcspn(cp, blank); | |
138 | equals = strchr(cp, '='); | |
139 | if (equals && ep < equals) | |
140 | equals = NULL; | |
141 | if (equals) | |
142 | len = equals - cp; | |
143 | else | |
144 | len = ep - cp; | |
145 | if (!res) { | |
146 | if (*cp == '-' || *cp == '!') { | |
147 | cp++; | |
148 | len--; | |
149 | } | |
150 | if (invalid_attr_name(cp, len)) { | |
151 | fprintf(stderr, | |
152 | "%.*s is not a valid attribute name: %s:%d\n", | |
153 | len, cp, src, lineno); | |
154 | return NULL; | |
155 | } | |
156 | } else { | |
157 | struct attr_state *e; | |
158 | ||
159 | e = &(res->state[*num_attr]); | |
160 | if (*cp == '-' || *cp == '!') { | |
161 | e->setto = (*cp == '-') ? ATTR__FALSE : ATTR__UNSET; | |
162 | cp++; | |
163 | len--; | |
164 | } | |
165 | else if (!equals) | |
166 | e->setto = ATTR__TRUE; | |
167 | else { | |
182af834 | 168 | e->setto = xmemdupz(equals + 1, ep - equals - 1); |
515106fa | 169 | } |
7fb0eaa2 | 170 | e->attr = git_attr_internal(cp, len); |
515106fa JH |
171 | } |
172 | (*num_attr)++; | |
173 | return ep + strspn(ep, blank); | |
174 | } | |
175 | ||
f48fd688 JH |
176 | static struct match_attr *parse_attr_line(const char *line, const char *src, |
177 | int lineno, int macro_ok) | |
d0bfd026 JH |
178 | { |
179 | int namelen; | |
180 | int num_attr; | |
181 | const char *cp, *name; | |
515106fa | 182 | struct match_attr *res = NULL; |
d0bfd026 | 183 | int pass; |
f48fd688 | 184 | int is_macro; |
d0bfd026 JH |
185 | |
186 | cp = line + strspn(line, blank); | |
187 | if (!*cp || *cp == '#') | |
188 | return NULL; | |
189 | name = cp; | |
190 | namelen = strcspn(name, blank); | |
f48fd688 JH |
191 | if (strlen(ATTRIBUTE_MACRO_PREFIX) < namelen && |
192 | !prefixcmp(name, ATTRIBUTE_MACRO_PREFIX)) { | |
193 | if (!macro_ok) { | |
194 | fprintf(stderr, "%s not allowed: %s:%d\n", | |
195 | name, src, lineno); | |
196 | return NULL; | |
197 | } | |
198 | is_macro = 1; | |
199 | name += strlen(ATTRIBUTE_MACRO_PREFIX); | |
200 | name += strspn(name, blank); | |
201 | namelen = strcspn(name, blank); | |
e4aee10a JH |
202 | if (invalid_attr_name(name, namelen)) { |
203 | fprintf(stderr, | |
204 | "%.*s is not a valid attribute name: %s:%d\n", | |
205 | namelen, name, src, lineno); | |
206 | return NULL; | |
207 | } | |
f48fd688 JH |
208 | } |
209 | else | |
210 | is_macro = 0; | |
d0bfd026 JH |
211 | |
212 | for (pass = 0; pass < 2; pass++) { | |
213 | /* pass 0 counts and allocates, pass 1 fills */ | |
214 | num_attr = 0; | |
215 | cp = name + namelen; | |
216 | cp = cp + strspn(cp, blank); | |
d7b0a093 | 217 | while (*cp) { |
515106fa | 218 | cp = parse_attr(src, lineno, cp, &num_attr, res); |
d7b0a093 SP |
219 | if (!cp) |
220 | return NULL; | |
221 | } | |
d0bfd026 JH |
222 | if (pass) |
223 | break; | |
224 | res = xcalloc(1, | |
225 | sizeof(*res) + | |
226 | sizeof(struct attr_state) * num_attr + | |
f48fd688 | 227 | (is_macro ? 0 : namelen + 1)); |
515106fa | 228 | if (is_macro) |
7fb0eaa2 | 229 | res->u.attr = git_attr_internal(name, namelen); |
f48fd688 | 230 | else { |
4b25d091 | 231 | res->u.pattern = (char *)&(res->state[num_attr]); |
f48fd688 JH |
232 | memcpy(res->u.pattern, name, namelen); |
233 | res->u.pattern[namelen] = 0; | |
234 | } | |
235 | res->is_macro = is_macro; | |
d0bfd026 JH |
236 | res->num_attr = num_attr; |
237 | } | |
238 | return res; | |
239 | } | |
240 | ||
241 | /* | |
242 | * Like info/exclude and .gitignore, the attribute information can | |
243 | * come from many places. | |
244 | * | |
245 | * (1) .gitattribute file of the same directory; | |
515106fa JH |
246 | * (2) .gitattribute file of the parent directory if (1) does not have |
247 | * any match; this goes recursively upwards, just like .gitignore. | |
248 | * (3) $GIT_DIR/info/attributes, which overrides both of the above. | |
d0bfd026 JH |
249 | * |
250 | * In the same file, later entries override the earlier match, so in the | |
251 | * global list, we would have entries from info/attributes the earliest | |
252 | * (reading the file from top to bottom), .gitattribute of the root | |
253 | * directory (again, reading the file from top to bottom) down to the | |
254 | * current directory, and then scan the list backwards to find the first match. | |
255 | * This is exactly the same as what excluded() does in dir.c to deal with | |
256 | * .gitignore | |
257 | */ | |
258 | ||
259 | static struct attr_stack { | |
260 | struct attr_stack *prev; | |
261 | char *origin; | |
262 | unsigned num_matches; | |
a4413118 | 263 | unsigned alloc; |
d0bfd026 JH |
264 | struct match_attr **attrs; |
265 | } *attr_stack; | |
266 | ||
267 | static void free_attr_elem(struct attr_stack *e) | |
268 | { | |
269 | int i; | |
270 | free(e->origin); | |
515106fa JH |
271 | for (i = 0; i < e->num_matches; i++) { |
272 | struct match_attr *a = e->attrs[i]; | |
273 | int j; | |
274 | for (j = 0; j < a->num_attr; j++) { | |
a5e92abd | 275 | const char *setto = a->state[j].setto; |
515106fa JH |
276 | if (setto == ATTR__TRUE || |
277 | setto == ATTR__FALSE || | |
278 | setto == ATTR__UNSET || | |
279 | setto == ATTR__UNKNOWN) | |
280 | ; | |
281 | else | |
4b25d091 | 282 | free((char *) setto); |
515106fa JH |
283 | } |
284 | free(a); | |
285 | } | |
d0bfd026 JH |
286 | free(e); |
287 | } | |
288 | ||
289 | static const char *builtin_attr[] = { | |
e4aee10a | 290 | "[attr]binary -diff -crlf", |
d0bfd026 JH |
291 | NULL, |
292 | }; | |
293 | ||
a4413118 JH |
294 | static void handle_attr_line(struct attr_stack *res, |
295 | const char *line, | |
296 | const char *src, | |
297 | int lineno, | |
298 | int macro_ok) | |
299 | { | |
300 | struct match_attr *a; | |
301 | ||
302 | a = parse_attr_line(line, src, lineno, macro_ok); | |
303 | if (!a) | |
304 | return; | |
305 | if (res->alloc <= res->num_matches) { | |
306 | res->alloc = alloc_nr(res->num_matches); | |
307 | res->attrs = xrealloc(res->attrs, | |
308 | sizeof(struct match_attr *) * | |
309 | res->alloc); | |
310 | } | |
311 | res->attrs[res->num_matches++] = a; | |
312 | } | |
313 | ||
d0bfd026 JH |
314 | static struct attr_stack *read_attr_from_array(const char **list) |
315 | { | |
316 | struct attr_stack *res; | |
317 | const char *line; | |
f48fd688 | 318 | int lineno = 0; |
d0bfd026 JH |
319 | |
320 | res = xcalloc(1, sizeof(*res)); | |
a4413118 JH |
321 | while ((line = *(list++)) != NULL) |
322 | handle_attr_line(res, line, "[builtin]", ++lineno, 1); | |
d0bfd026 JH |
323 | return res; |
324 | } | |
325 | ||
06f33c17 JH |
326 | static enum git_attr_direction direction; |
327 | static struct index_state *use_index; | |
328 | ||
f48fd688 | 329 | static struct attr_stack *read_attr_from_file(const char *path, int macro_ok) |
d0bfd026 | 330 | { |
a4413118 | 331 | FILE *fp = fopen(path, "r"); |
d0bfd026 JH |
332 | struct attr_stack *res; |
333 | char buf[2048]; | |
f48fd688 | 334 | int lineno = 0; |
d0bfd026 | 335 | |
d0bfd026 | 336 | if (!fp) |
a4413118 JH |
337 | return NULL; |
338 | res = xcalloc(1, sizeof(*res)); | |
339 | while (fgets(buf, sizeof(buf), fp)) | |
340 | handle_attr_line(res, buf, path, ++lineno, macro_ok); | |
341 | fclose(fp); | |
342 | return res; | |
343 | } | |
d0bfd026 | 344 | |
1a9d7e9b JH |
345 | static void *read_index_data(const char *path) |
346 | { | |
347 | int pos, len; | |
348 | unsigned long sz; | |
349 | enum object_type type; | |
350 | void *data; | |
06f33c17 | 351 | struct index_state *istate = use_index ? use_index : &the_index; |
1a9d7e9b JH |
352 | |
353 | len = strlen(path); | |
06f33c17 | 354 | pos = index_name_pos(istate, path, len); |
1a9d7e9b JH |
355 | if (pos < 0) { |
356 | /* | |
357 | * We might be in the middle of a merge, in which | |
358 | * case we would read stage #2 (ours). | |
359 | */ | |
360 | int i; | |
361 | for (i = -pos - 1; | |
06f33c17 JH |
362 | (pos < 0 && i < istate->cache_nr && |
363 | !strcmp(istate->cache[i]->name, path)); | |
1a9d7e9b | 364 | i++) |
06f33c17 | 365 | if (ce_stage(istate->cache[i]) == 2) |
1a9d7e9b JH |
366 | pos = i; |
367 | } | |
368 | if (pos < 0) | |
369 | return NULL; | |
06f33c17 | 370 | data = read_sha1_file(istate->cache[pos]->sha1, &type, &sz); |
1a9d7e9b JH |
371 | if (!data || type != OBJ_BLOB) { |
372 | free(data); | |
373 | return NULL; | |
374 | } | |
375 | return data; | |
376 | } | |
377 | ||
06f33c17 | 378 | static struct attr_stack *read_attr_from_index(const char *path, int macro_ok) |
a4413118 JH |
379 | { |
380 | struct attr_stack *res; | |
1a9d7e9b JH |
381 | char *buf, *sp; |
382 | int lineno = 0; | |
f48fd688 | 383 | |
1a9d7e9b JH |
384 | buf = read_index_data(path); |
385 | if (!buf) | |
06f33c17 | 386 | return NULL; |
1a9d7e9b | 387 | |
06f33c17 | 388 | res = xcalloc(1, sizeof(*res)); |
1a9d7e9b JH |
389 | for (sp = buf; *sp; ) { |
390 | char *ep; | |
391 | int more; | |
392 | for (ep = sp; *ep && *ep != '\n'; ep++) | |
393 | ; | |
394 | more = (*ep == '\n'); | |
395 | *ep = '\0'; | |
396 | handle_attr_line(res, sp, path, ++lineno, macro_ok); | |
397 | sp = ep + more; | |
398 | } | |
399 | free(buf); | |
d0bfd026 JH |
400 | return res; |
401 | } | |
402 | ||
06f33c17 JH |
403 | static struct attr_stack *read_attr(const char *path, int macro_ok) |
404 | { | |
405 | struct attr_stack *res; | |
406 | ||
407 | if (direction == GIT_ATTR_CHECKOUT) { | |
408 | res = read_attr_from_index(path, macro_ok); | |
409 | if (!res) | |
410 | res = read_attr_from_file(path, macro_ok); | |
411 | } | |
4191e80a | 412 | else if (direction == GIT_ATTR_CHECKIN) { |
06f33c17 JH |
413 | res = read_attr_from_file(path, macro_ok); |
414 | if (!res) | |
415 | /* | |
416 | * There is no checked out .gitattributes file there, but | |
417 | * we might have it in the index. We allow operation in a | |
418 | * sparsely checked out work tree, so read from it. | |
419 | */ | |
420 | res = read_attr_from_index(path, macro_ok); | |
421 | } | |
4191e80a NTND |
422 | else |
423 | res = read_attr_from_index(path, macro_ok); | |
06f33c17 JH |
424 | if (!res) |
425 | res = xcalloc(1, sizeof(*res)); | |
426 | return res; | |
427 | } | |
428 | ||
d0bfd026 JH |
429 | #if DEBUG_ATTR |
430 | static void debug_info(const char *what, struct attr_stack *elem) | |
431 | { | |
432 | fprintf(stderr, "%s: %s\n", what, elem->origin ? elem->origin : "()"); | |
433 | } | |
cf94ccda | 434 | static void debug_set(const char *what, const char *match, struct git_attr *attr, const void *v) |
f48fd688 | 435 | { |
515106fa JH |
436 | const char *value = v; |
437 | ||
438 | if (ATTR_TRUE(value)) | |
439 | value = "set"; | |
440 | else if (ATTR_FALSE(value)) | |
441 | value = "unset"; | |
442 | else if (ATTR_UNSET(value)) | |
443 | value = "unspecified"; | |
444 | ||
445 | fprintf(stderr, "%s: %s => %s (%s)\n", | |
446 | what, attr->name, (char *) value, match); | |
f48fd688 | 447 | } |
d0bfd026 JH |
448 | #define debug_push(a) debug_info("push", (a)) |
449 | #define debug_pop(a) debug_info("pop", (a)) | |
450 | #else | |
451 | #define debug_push(a) do { ; } while (0) | |
452 | #define debug_pop(a) do { ; } while (0) | |
f48fd688 | 453 | #define debug_set(a,b,c,d) do { ; } while (0) |
d0bfd026 JH |
454 | #endif |
455 | ||
06f33c17 JH |
456 | static void drop_attr_stack(void) |
457 | { | |
458 | while (attr_stack) { | |
459 | struct attr_stack *elem = attr_stack; | |
460 | attr_stack = elem->prev; | |
461 | free_attr_elem(elem); | |
462 | } | |
463 | } | |
464 | ||
f48fd688 JH |
465 | static void bootstrap_attr_stack(void) |
466 | { | |
467 | if (!attr_stack) { | |
468 | struct attr_stack *elem; | |
469 | ||
470 | elem = read_attr_from_array(builtin_attr); | |
471 | elem->origin = NULL; | |
472 | elem->prev = attr_stack; | |
473 | attr_stack = elem; | |
474 | ||
4191e80a | 475 | if (!is_bare_repository() || direction == GIT_ATTR_INDEX) { |
2d35d556 RS |
476 | elem = read_attr(GITATTRIBUTES_FILE, 1); |
477 | elem->origin = strdup(""); | |
478 | elem->prev = attr_stack; | |
479 | attr_stack = elem; | |
480 | debug_push(elem); | |
481 | } | |
f48fd688 JH |
482 | |
483 | elem = read_attr_from_file(git_path(INFOATTRIBUTES_FILE), 1); | |
a4413118 JH |
484 | if (!elem) |
485 | elem = xcalloc(1, sizeof(*elem)); | |
f48fd688 JH |
486 | elem->origin = NULL; |
487 | elem->prev = attr_stack; | |
488 | attr_stack = elem; | |
489 | } | |
490 | } | |
491 | ||
d0bfd026 JH |
492 | static void prepare_attr_stack(const char *path, int dirlen) |
493 | { | |
494 | struct attr_stack *elem, *info; | |
495 | int len; | |
f66cf96d DP |
496 | struct strbuf pathbuf; |
497 | ||
498 | strbuf_init(&pathbuf, dirlen+2+strlen(GITATTRIBUTES_FILE)); | |
d0bfd026 JH |
499 | |
500 | /* | |
501 | * At the bottom of the attribute stack is the built-in | |
502 | * set of attribute definitions. Then, contents from | |
503 | * .gitattribute files from directories closer to the | |
504 | * root to the ones in deeper directories are pushed | |
505 | * to the stack. Finally, at the very top of the stack | |
506 | * we always keep the contents of $GIT_DIR/info/attributes. | |
507 | * | |
508 | * When checking, we use entries from near the top of the | |
509 | * stack, preferring $GIT_DIR/info/attributes, then | |
510 | * .gitattributes in deeper directories to shallower ones, | |
511 | * and finally use the built-in set as the default. | |
512 | */ | |
f48fd688 JH |
513 | if (!attr_stack) |
514 | bootstrap_attr_stack(); | |
d0bfd026 JH |
515 | |
516 | /* | |
517 | * Pop the "info" one that is always at the top of the stack. | |
518 | */ | |
519 | info = attr_stack; | |
520 | attr_stack = info->prev; | |
521 | ||
522 | /* | |
523 | * Pop the ones from directories that are not the prefix of | |
524 | * the path we are checking. | |
525 | */ | |
526 | while (attr_stack && attr_stack->origin) { | |
527 | int namelen = strlen(attr_stack->origin); | |
528 | ||
529 | elem = attr_stack; | |
530 | if (namelen <= dirlen && | |
531 | !strncmp(elem->origin, path, namelen)) | |
532 | break; | |
533 | ||
534 | debug_pop(elem); | |
535 | attr_stack = elem->prev; | |
536 | free_attr_elem(elem); | |
537 | } | |
538 | ||
539 | /* | |
540 | * Read from parent directories and push them down | |
541 | */ | |
4191e80a | 542 | if (!is_bare_repository() || direction == GIT_ATTR_INDEX) { |
2d35d556 RS |
543 | while (1) { |
544 | char *cp; | |
545 | ||
546 | len = strlen(attr_stack->origin); | |
547 | if (dirlen <= len) | |
548 | break; | |
f66cf96d DP |
549 | strbuf_reset(&pathbuf); |
550 | strbuf_add(&pathbuf, path, dirlen); | |
551 | strbuf_addch(&pathbuf, '/'); | |
552 | cp = strchr(pathbuf.buf + len + 1, '/'); | |
2d35d556 | 553 | strcpy(cp + 1, GITATTRIBUTES_FILE); |
f66cf96d | 554 | elem = read_attr(pathbuf.buf, 0); |
2d35d556 | 555 | *cp = '\0'; |
f66cf96d | 556 | elem->origin = strdup(pathbuf.buf); |
2d35d556 RS |
557 | elem->prev = attr_stack; |
558 | attr_stack = elem; | |
559 | debug_push(elem); | |
560 | } | |
d0bfd026 JH |
561 | } |
562 | ||
d4c98565 RS |
563 | strbuf_release(&pathbuf); |
564 | ||
d0bfd026 JH |
565 | /* |
566 | * Finally push the "info" one at the top of the stack. | |
567 | */ | |
568 | info->prev = attr_stack; | |
569 | attr_stack = info; | |
570 | } | |
571 | ||
572 | static int path_matches(const char *pathname, int pathlen, | |
573 | const char *pattern, | |
574 | const char *base, int baselen) | |
575 | { | |
576 | if (!strchr(pattern, '/')) { | |
577 | /* match basename */ | |
578 | const char *basename = strrchr(pathname, '/'); | |
579 | basename = basename ? basename + 1 : pathname; | |
580 | return (fnmatch(pattern, basename, 0) == 0); | |
581 | } | |
582 | /* | |
583 | * match with FNM_PATHNAME; the pattern has base implicitly | |
584 | * in front of it. | |
585 | */ | |
586 | if (*pattern == '/') | |
587 | pattern++; | |
588 | if (pathlen < baselen || | |
cf94ccda | 589 | (baselen && pathname[baselen] != '/') || |
d0bfd026 JH |
590 | strncmp(pathname, base, baselen)) |
591 | return 0; | |
82881b38 MO |
592 | if (baselen != 0) |
593 | baselen++; | |
594 | return fnmatch(pattern, pathname + baselen, FNM_PATHNAME) == 0; | |
d0bfd026 JH |
595 | } |
596 | ||
515106fa JH |
597 | static int fill_one(const char *what, struct match_attr *a, int rem) |
598 | { | |
599 | struct git_attr_check *check = check_all_attr; | |
600 | int i; | |
601 | ||
602 | for (i = 0; 0 < rem && i < a->num_attr; i++) { | |
603 | struct git_attr *attr = a->state[i].attr; | |
a5e92abd JH |
604 | const char **n = &(check[attr->attr_nr].value); |
605 | const char *v = a->state[i].setto; | |
515106fa JH |
606 | |
607 | if (*n == ATTR__UNKNOWN) { | |
608 | debug_set(what, a->u.pattern, attr, v); | |
609 | *n = v; | |
610 | rem--; | |
611 | } | |
612 | } | |
613 | return rem; | |
614 | } | |
615 | ||
f48fd688 | 616 | static int fill(const char *path, int pathlen, struct attr_stack *stk, int rem) |
d0bfd026 | 617 | { |
515106fa | 618 | int i; |
d0bfd026 JH |
619 | const char *base = stk->origin ? stk->origin : ""; |
620 | ||
621 | for (i = stk->num_matches - 1; 0 < rem && 0 <= i; i--) { | |
622 | struct match_attr *a = stk->attrs[i]; | |
f48fd688 JH |
623 | if (a->is_macro) |
624 | continue; | |
d0bfd026 | 625 | if (path_matches(path, pathlen, |
515106fa JH |
626 | a->u.pattern, base, strlen(base))) |
627 | rem = fill_one("fill", a, rem); | |
d0bfd026 JH |
628 | } |
629 | return rem; | |
630 | } | |
631 | ||
f48fd688 JH |
632 | static int macroexpand(struct attr_stack *stk, int rem) |
633 | { | |
515106fa | 634 | int i; |
f48fd688 JH |
635 | struct git_attr_check *check = check_all_attr; |
636 | ||
637 | for (i = stk->num_matches - 1; 0 < rem && 0 <= i; i--) { | |
638 | struct match_attr *a = stk->attrs[i]; | |
639 | if (!a->is_macro) | |
640 | continue; | |
515106fa | 641 | if (check[a->u.attr->attr_nr].value != ATTR__TRUE) |
f48fd688 | 642 | continue; |
515106fa | 643 | rem = fill_one("expand", a, rem); |
f48fd688 JH |
644 | } |
645 | return rem; | |
646 | } | |
647 | ||
d0bfd026 JH |
648 | int git_checkattr(const char *path, int num, struct git_attr_check *check) |
649 | { | |
650 | struct attr_stack *stk; | |
651 | const char *cp; | |
652 | int dirlen, pathlen, i, rem; | |
653 | ||
f48fd688 JH |
654 | bootstrap_attr_stack(); |
655 | for (i = 0; i < attr_nr; i++) | |
515106fa | 656 | check_all_attr[i].value = ATTR__UNKNOWN; |
d0bfd026 JH |
657 | |
658 | pathlen = strlen(path); | |
659 | cp = strrchr(path, '/'); | |
660 | if (!cp) | |
661 | dirlen = 0; | |
662 | else | |
663 | dirlen = cp - path; | |
664 | prepare_attr_stack(path, dirlen); | |
f48fd688 JH |
665 | rem = attr_nr; |
666 | for (stk = attr_stack; 0 < rem && stk; stk = stk->prev) | |
667 | rem = fill(path, pathlen, stk, rem); | |
668 | ||
d0bfd026 | 669 | for (stk = attr_stack; 0 < rem && stk; stk = stk->prev) |
f48fd688 JH |
670 | rem = macroexpand(stk, rem); |
671 | ||
515106fa | 672 | for (i = 0; i < num; i++) { |
a5e92abd | 673 | const char *value = check_all_attr[check[i].attr->attr_nr].value; |
515106fa JH |
674 | if (value == ATTR__UNKNOWN) |
675 | value = ATTR__UNSET; | |
676 | check[i].value = value; | |
677 | } | |
f48fd688 | 678 | |
d0bfd026 JH |
679 | return 0; |
680 | } | |
06f33c17 JH |
681 | |
682 | void git_attr_set_direction(enum git_attr_direction new, struct index_state *istate) | |
683 | { | |
684 | enum git_attr_direction old = direction; | |
4191e80a NTND |
685 | |
686 | if (is_bare_repository() && new != GIT_ATTR_INDEX) | |
687 | die("BUG: non-INDEX attr direction in a bare repo"); | |
688 | ||
06f33c17 JH |
689 | direction = new; |
690 | if (new != old) | |
691 | drop_attr_stack(); | |
692 | use_index = istate; | |
693 | } |