]>
Commit | Line | Data |
---|---|---|
86ab7f0c MH |
1 | /* |
2 | * Handle git attributes. See gitattributes(5) for a description of | |
3 | * the file syntax, and Documentation/technical/api-gitattributes.txt | |
4 | * for a description of the API. | |
5 | * | |
6 | * One basic design decision here is that we are not going to support | |
7 | * an insanely large number of attributes. | |
8 | */ | |
9 | ||
06f33c17 | 10 | #define NO_THE_INDEX_COMPATIBILITY_MACROS |
d0bfd026 | 11 | #include "cache.h" |
6df42ab9 | 12 | #include "exec_cmd.h" |
d0bfd026 | 13 | #include "attr.h" |
6eba6210 | 14 | #include "dir.h" |
d0bfd026 | 15 | |
a5e92abd JH |
16 | const char git_attr__true[] = "(builtin)true"; |
17 | const char git_attr__false[] = "\0(builtin)false"; | |
18 | static const char git_attr__unknown[] = "(builtin)unknown"; | |
19 | #define ATTR__TRUE git_attr__true | |
20 | #define ATTR__FALSE git_attr__false | |
21 | #define ATTR__UNSET NULL | |
22 | #define ATTR__UNKNOWN git_attr__unknown | |
515106fa | 23 | |
86ab7f0c | 24 | /* This is a randomly chosen prime. */ |
d0bfd026 JH |
25 | #define HASHSIZE 257 |
26 | ||
27 | #ifndef DEBUG_ATTR | |
28 | #define DEBUG_ATTR 0 | |
29 | #endif | |
30 | ||
31 | struct git_attr { | |
32 | struct git_attr *next; | |
33 | unsigned h; | |
f48fd688 | 34 | int attr_nr; |
fad32bcd | 35 | int maybe_macro; |
06a604e6 | 36 | int maybe_real; |
d0bfd026 JH |
37 | char name[FLEX_ARRAY]; |
38 | }; | |
f48fd688 | 39 | static int attr_nr; |
06a604e6 | 40 | static int cannot_trust_maybe_real; |
d0bfd026 | 41 | |
f48fd688 | 42 | static struct git_attr_check *check_all_attr; |
d0bfd026 JH |
43 | static struct git_attr *(git_attr_hash[HASHSIZE]); |
44 | ||
352404ac MH |
45 | char *git_attr_name(struct git_attr *attr) |
46 | { | |
47 | return attr->name; | |
48 | } | |
49 | ||
d0bfd026 JH |
50 | static unsigned hash_name(const char *name, int namelen) |
51 | { | |
48fb7deb | 52 | unsigned val = 0, c; |
d0bfd026 JH |
53 | |
54 | while (namelen--) { | |
55 | c = *name++; | |
56 | val = ((val << 7) | (val >> 22)) ^ c; | |
57 | } | |
58 | return val; | |
59 | } | |
60 | ||
e4aee10a JH |
61 | static int invalid_attr_name(const char *name, int namelen) |
62 | { | |
63 | /* | |
d42453ab MH |
64 | * Attribute name cannot begin with '-' and must consist of |
65 | * characters from [-A-Za-z0-9_.]. | |
e4aee10a | 66 | */ |
c0b13b21 | 67 | if (namelen <= 0 || *name == '-') |
e4aee10a JH |
68 | return -1; |
69 | while (namelen--) { | |
70 | char ch = *name++; | |
71 | if (! (ch == '-' || ch == '.' || ch == '_' || | |
72 | ('0' <= ch && ch <= '9') || | |
73 | ('a' <= ch && ch <= 'z') || | |
74 | ('A' <= ch && ch <= 'Z')) ) | |
75 | return -1; | |
76 | } | |
77 | return 0; | |
78 | } | |
79 | ||
7fb0eaa2 | 80 | static struct git_attr *git_attr_internal(const char *name, int len) |
d0bfd026 JH |
81 | { |
82 | unsigned hval = hash_name(name, len); | |
83 | unsigned pos = hval % HASHSIZE; | |
84 | struct git_attr *a; | |
85 | ||
86 | for (a = git_attr_hash[pos]; a; a = a->next) { | |
87 | if (a->h == hval && | |
88 | !memcmp(a->name, name, len) && !a->name[len]) | |
89 | return a; | |
90 | } | |
91 | ||
e4aee10a JH |
92 | if (invalid_attr_name(name, len)) |
93 | return NULL; | |
94 | ||
d0bfd026 JH |
95 | a = xmalloc(sizeof(*a) + len + 1); |
96 | memcpy(a->name, name, len); | |
97 | a->name[len] = 0; | |
98 | a->h = hval; | |
99 | a->next = git_attr_hash[pos]; | |
f48fd688 | 100 | a->attr_nr = attr_nr++; |
fad32bcd | 101 | a->maybe_macro = 0; |
06a604e6 | 102 | a->maybe_real = 0; |
d0bfd026 | 103 | git_attr_hash[pos] = a; |
f48fd688 | 104 | |
2756ca43 | 105 | REALLOC_ARRAY(check_all_attr, attr_nr); |
f48fd688 | 106 | check_all_attr[a->attr_nr].attr = a; |
515106fa | 107 | check_all_attr[a->attr_nr].value = ATTR__UNKNOWN; |
d0bfd026 JH |
108 | return a; |
109 | } | |
110 | ||
7fb0eaa2 JH |
111 | struct git_attr *git_attr(const char *name) |
112 | { | |
113 | return git_attr_internal(name, strlen(name)); | |
114 | } | |
115 | ||
515106fa | 116 | /* What does a matched pattern decide? */ |
d0bfd026 | 117 | struct attr_state { |
d0bfd026 | 118 | struct git_attr *attr; |
a5e92abd | 119 | const char *setto; |
d0bfd026 JH |
120 | }; |
121 | ||
82dce998 NTND |
122 | struct pattern { |
123 | const char *pattern; | |
124 | int patternlen; | |
125 | int nowildcardlen; | |
126 | int flags; /* EXC_FLAG_* */ | |
127 | }; | |
128 | ||
ba845b75 MH |
129 | /* |
130 | * One rule, as from a .gitattributes file. | |
131 | * | |
132 | * If is_macro is true, then u.attr is a pointer to the git_attr being | |
133 | * defined. | |
134 | * | |
135 | * If is_macro is false, then u.pattern points at the filename pattern | |
136 | * to which the rule applies. (The memory pointed to is part of the | |
137 | * memory block allocated for the match_attr instance.) | |
138 | * | |
139 | * In either case, num_attr is the number of attributes affected by | |
140 | * this rule, and state is an array listing them. The attributes are | |
141 | * listed as they appear in the file (macros unexpanded). | |
142 | */ | |
d0bfd026 | 143 | struct match_attr { |
f48fd688 | 144 | union { |
82dce998 | 145 | struct pattern pat; |
f48fd688 JH |
146 | struct git_attr *attr; |
147 | } u; | |
148 | char is_macro; | |
d0bfd026 JH |
149 | unsigned num_attr; |
150 | struct attr_state state[FLEX_ARRAY]; | |
151 | }; | |
152 | ||
153 | static const char blank[] = " \t\r\n"; | |
154 | ||
d1751298 MH |
155 | /* |
156 | * Parse a whitespace-delimited attribute state (i.e., "attr", | |
157 | * "-attr", "!attr", or "attr=value") from the string starting at src. | |
158 | * If e is not NULL, write the results to *e. Return a pointer to the | |
159 | * remainder of the string (with leading whitespace removed), or NULL | |
160 | * if there was an error. | |
161 | */ | |
515106fa | 162 | static const char *parse_attr(const char *src, int lineno, const char *cp, |
d1751298 | 163 | struct attr_state *e) |
515106fa JH |
164 | { |
165 | const char *ep, *equals; | |
166 | int len; | |
167 | ||
168 | ep = cp + strcspn(cp, blank); | |
169 | equals = strchr(cp, '='); | |
170 | if (equals && ep < equals) | |
171 | equals = NULL; | |
172 | if (equals) | |
173 | len = equals - cp; | |
174 | else | |
175 | len = ep - cp; | |
d1751298 | 176 | if (!e) { |
515106fa JH |
177 | if (*cp == '-' || *cp == '!') { |
178 | cp++; | |
179 | len--; | |
180 | } | |
181 | if (invalid_attr_name(cp, len)) { | |
182 | fprintf(stderr, | |
183 | "%.*s is not a valid attribute name: %s:%d\n", | |
184 | len, cp, src, lineno); | |
185 | return NULL; | |
186 | } | |
187 | } else { | |
515106fa JH |
188 | if (*cp == '-' || *cp == '!') { |
189 | e->setto = (*cp == '-') ? ATTR__FALSE : ATTR__UNSET; | |
190 | cp++; | |
191 | len--; | |
192 | } | |
193 | else if (!equals) | |
194 | e->setto = ATTR__TRUE; | |
195 | else { | |
182af834 | 196 | e->setto = xmemdupz(equals + 1, ep - equals - 1); |
515106fa | 197 | } |
7fb0eaa2 | 198 | e->attr = git_attr_internal(cp, len); |
515106fa | 199 | } |
515106fa JH |
200 | return ep + strspn(ep, blank); |
201 | } | |
202 | ||
f48fd688 JH |
203 | static struct match_attr *parse_attr_line(const char *line, const char *src, |
204 | int lineno, int macro_ok) | |
d0bfd026 JH |
205 | { |
206 | int namelen; | |
d68e1c18 | 207 | int num_attr, i; |
85c4a0d0 | 208 | const char *cp, *name, *states; |
515106fa | 209 | struct match_attr *res = NULL; |
f48fd688 | 210 | int is_macro; |
d0bfd026 JH |
211 | |
212 | cp = line + strspn(line, blank); | |
213 | if (!*cp || *cp == '#') | |
214 | return NULL; | |
215 | name = cp; | |
216 | namelen = strcspn(name, blank); | |
f48fd688 | 217 | if (strlen(ATTRIBUTE_MACRO_PREFIX) < namelen && |
59556548 | 218 | starts_with(name, ATTRIBUTE_MACRO_PREFIX)) { |
f48fd688 JH |
219 | if (!macro_ok) { |
220 | fprintf(stderr, "%s not allowed: %s:%d\n", | |
221 | name, src, lineno); | |
222 | return NULL; | |
223 | } | |
224 | is_macro = 1; | |
225 | name += strlen(ATTRIBUTE_MACRO_PREFIX); | |
226 | name += strspn(name, blank); | |
227 | namelen = strcspn(name, blank); | |
e4aee10a JH |
228 | if (invalid_attr_name(name, namelen)) { |
229 | fprintf(stderr, | |
230 | "%.*s is not a valid attribute name: %s:%d\n", | |
231 | namelen, name, src, lineno); | |
232 | return NULL; | |
233 | } | |
f48fd688 JH |
234 | } |
235 | else | |
236 | is_macro = 0; | |
d0bfd026 | 237 | |
85c4a0d0 MH |
238 | states = name + namelen; |
239 | states += strspn(states, blank); | |
240 | ||
d68e1c18 MH |
241 | /* First pass to count the attr_states */ |
242 | for (cp = states, num_attr = 0; *cp; num_attr++) { | |
243 | cp = parse_attr(src, lineno, cp, NULL); | |
244 | if (!cp) | |
245 | return NULL; | |
d0bfd026 | 246 | } |
d68e1c18 MH |
247 | |
248 | res = xcalloc(1, | |
249 | sizeof(*res) + | |
250 | sizeof(struct attr_state) * num_attr + | |
251 | (is_macro ? 0 : namelen + 1)); | |
fad32bcd | 252 | if (is_macro) { |
d68e1c18 | 253 | res->u.attr = git_attr_internal(name, namelen); |
fad32bcd NTND |
254 | res->u.attr->maybe_macro = 1; |
255 | } else { | |
82dce998 NTND |
256 | char *p = (char *)&(res->state[num_attr]); |
257 | memcpy(p, name, namelen); | |
258 | res->u.pat.pattern = p; | |
259 | parse_exclude_pattern(&res->u.pat.pattern, | |
260 | &res->u.pat.patternlen, | |
261 | &res->u.pat.flags, | |
262 | &res->u.pat.nowildcardlen); | |
8b1bd024 TR |
263 | if (res->u.pat.flags & EXC_FLAG_NEGATIVE) { |
264 | warning(_("Negative patterns are ignored in git attributes\n" | |
265 | "Use '\\!' for literal leading exclamation.")); | |
266 | return NULL; | |
267 | } | |
d0bfd026 | 268 | } |
d68e1c18 MH |
269 | res->is_macro = is_macro; |
270 | res->num_attr = num_attr; | |
271 | ||
272 | /* Second pass to fill the attr_states */ | |
273 | for (cp = states, i = 0; *cp; i++) { | |
274 | cp = parse_attr(src, lineno, cp, &(res->state[i])); | |
06a604e6 NTND |
275 | if (!is_macro) |
276 | res->state[i].attr->maybe_real = 1; | |
277 | if (res->state[i].attr->maybe_macro) | |
278 | cannot_trust_maybe_real = 1; | |
d68e1c18 MH |
279 | } |
280 | ||
d0bfd026 JH |
281 | return res; |
282 | } | |
283 | ||
284 | /* | |
285 | * Like info/exclude and .gitignore, the attribute information can | |
286 | * come from many places. | |
287 | * | |
288 | * (1) .gitattribute file of the same directory; | |
515106fa JH |
289 | * (2) .gitattribute file of the parent directory if (1) does not have |
290 | * any match; this goes recursively upwards, just like .gitignore. | |
291 | * (3) $GIT_DIR/info/attributes, which overrides both of the above. | |
d0bfd026 JH |
292 | * |
293 | * In the same file, later entries override the earlier match, so in the | |
294 | * global list, we would have entries from info/attributes the earliest | |
295 | * (reading the file from top to bottom), .gitattribute of the root | |
296 | * directory (again, reading the file from top to bottom) down to the | |
297 | * current directory, and then scan the list backwards to find the first match. | |
6d24e7a8 | 298 | * This is exactly the same as what is_excluded() does in dir.c to deal with |
d0bfd026 JH |
299 | * .gitignore |
300 | */ | |
301 | ||
302 | static struct attr_stack { | |
303 | struct attr_stack *prev; | |
304 | char *origin; | |
cd6a0b26 | 305 | size_t originlen; |
d0bfd026 | 306 | unsigned num_matches; |
a4413118 | 307 | unsigned alloc; |
d0bfd026 JH |
308 | struct match_attr **attrs; |
309 | } *attr_stack; | |
310 | ||
311 | static void free_attr_elem(struct attr_stack *e) | |
312 | { | |
313 | int i; | |
314 | free(e->origin); | |
515106fa JH |
315 | for (i = 0; i < e->num_matches; i++) { |
316 | struct match_attr *a = e->attrs[i]; | |
317 | int j; | |
318 | for (j = 0; j < a->num_attr; j++) { | |
a5e92abd | 319 | const char *setto = a->state[j].setto; |
515106fa JH |
320 | if (setto == ATTR__TRUE || |
321 | setto == ATTR__FALSE || | |
322 | setto == ATTR__UNSET || | |
323 | setto == ATTR__UNKNOWN) | |
324 | ; | |
325 | else | |
4b25d091 | 326 | free((char *) setto); |
515106fa JH |
327 | } |
328 | free(a); | |
329 | } | |
37475f97 | 330 | free(e->attrs); |
d0bfd026 JH |
331 | free(e); |
332 | } | |
333 | ||
334 | static const char *builtin_attr[] = { | |
155a4b71 | 335 | "[attr]binary -diff -merge -text", |
d0bfd026 JH |
336 | NULL, |
337 | }; | |
338 | ||
a4413118 JH |
339 | static void handle_attr_line(struct attr_stack *res, |
340 | const char *line, | |
341 | const char *src, | |
342 | int lineno, | |
343 | int macro_ok) | |
344 | { | |
345 | struct match_attr *a; | |
346 | ||
347 | a = parse_attr_line(line, src, lineno, macro_ok); | |
348 | if (!a) | |
349 | return; | |
3a7fa03d | 350 | ALLOC_GROW(res->attrs, res->num_matches + 1, res->alloc); |
a4413118 JH |
351 | res->attrs[res->num_matches++] = a; |
352 | } | |
353 | ||
d0bfd026 JH |
354 | static struct attr_stack *read_attr_from_array(const char **list) |
355 | { | |
356 | struct attr_stack *res; | |
357 | const char *line; | |
f48fd688 | 358 | int lineno = 0; |
d0bfd026 JH |
359 | |
360 | res = xcalloc(1, sizeof(*res)); | |
a4413118 JH |
361 | while ((line = *(list++)) != NULL) |
362 | handle_attr_line(res, line, "[builtin]", ++lineno, 1); | |
d0bfd026 JH |
363 | return res; |
364 | } | |
365 | ||
06f33c17 JH |
366 | static enum git_attr_direction direction; |
367 | static struct index_state *use_index; | |
368 | ||
f48fd688 | 369 | static struct attr_stack *read_attr_from_file(const char *path, int macro_ok) |
d0bfd026 | 370 | { |
a4413118 | 371 | FILE *fp = fopen(path, "r"); |
d0bfd026 JH |
372 | struct attr_stack *res; |
373 | char buf[2048]; | |
f48fd688 | 374 | int lineno = 0; |
d0bfd026 | 375 | |
11e50b27 | 376 | if (!fp) { |
8e950dab | 377 | if (errno != ENOENT && errno != ENOTDIR) |
55b38a48 | 378 | warn_on_inaccessible(path); |
a4413118 | 379 | return NULL; |
11e50b27 | 380 | } |
a4413118 JH |
381 | res = xcalloc(1, sizeof(*res)); |
382 | while (fgets(buf, sizeof(buf), fp)) | |
383 | handle_attr_line(res, buf, path, ++lineno, macro_ok); | |
384 | fclose(fp); | |
385 | return res; | |
386 | } | |
d0bfd026 | 387 | |
06f33c17 | 388 | static struct attr_stack *read_attr_from_index(const char *path, int macro_ok) |
a4413118 JH |
389 | { |
390 | struct attr_stack *res; | |
1a9d7e9b JH |
391 | char *buf, *sp; |
392 | int lineno = 0; | |
f48fd688 | 393 | |
ff366825 | 394 | buf = read_blob_data_from_index(use_index ? use_index : &the_index, path, NULL); |
1a9d7e9b | 395 | if (!buf) |
06f33c17 | 396 | return NULL; |
1a9d7e9b | 397 | |
06f33c17 | 398 | res = xcalloc(1, sizeof(*res)); |
1a9d7e9b JH |
399 | for (sp = buf; *sp; ) { |
400 | char *ep; | |
401 | int more; | |
402 | for (ep = sp; *ep && *ep != '\n'; ep++) | |
403 | ; | |
404 | more = (*ep == '\n'); | |
405 | *ep = '\0'; | |
406 | handle_attr_line(res, sp, path, ++lineno, macro_ok); | |
407 | sp = ep + more; | |
408 | } | |
409 | free(buf); | |
d0bfd026 JH |
410 | return res; |
411 | } | |
412 | ||
06f33c17 JH |
413 | static struct attr_stack *read_attr(const char *path, int macro_ok) |
414 | { | |
415 | struct attr_stack *res; | |
416 | ||
417 | if (direction == GIT_ATTR_CHECKOUT) { | |
418 | res = read_attr_from_index(path, macro_ok); | |
419 | if (!res) | |
420 | res = read_attr_from_file(path, macro_ok); | |
421 | } | |
4191e80a | 422 | else if (direction == GIT_ATTR_CHECKIN) { |
06f33c17 JH |
423 | res = read_attr_from_file(path, macro_ok); |
424 | if (!res) | |
425 | /* | |
426 | * There is no checked out .gitattributes file there, but | |
427 | * we might have it in the index. We allow operation in a | |
428 | * sparsely checked out work tree, so read from it. | |
429 | */ | |
430 | res = read_attr_from_index(path, macro_ok); | |
431 | } | |
4191e80a NTND |
432 | else |
433 | res = read_attr_from_index(path, macro_ok); | |
06f33c17 JH |
434 | if (!res) |
435 | res = xcalloc(1, sizeof(*res)); | |
436 | return res; | |
437 | } | |
438 | ||
d0bfd026 JH |
439 | #if DEBUG_ATTR |
440 | static void debug_info(const char *what, struct attr_stack *elem) | |
441 | { | |
442 | fprintf(stderr, "%s: %s\n", what, elem->origin ? elem->origin : "()"); | |
443 | } | |
cf94ccda | 444 | static void debug_set(const char *what, const char *match, struct git_attr *attr, const void *v) |
f48fd688 | 445 | { |
515106fa JH |
446 | const char *value = v; |
447 | ||
448 | if (ATTR_TRUE(value)) | |
449 | value = "set"; | |
450 | else if (ATTR_FALSE(value)) | |
451 | value = "unset"; | |
452 | else if (ATTR_UNSET(value)) | |
453 | value = "unspecified"; | |
454 | ||
455 | fprintf(stderr, "%s: %s => %s (%s)\n", | |
456 | what, attr->name, (char *) value, match); | |
f48fd688 | 457 | } |
d0bfd026 JH |
458 | #define debug_push(a) debug_info("push", (a)) |
459 | #define debug_pop(a) debug_info("pop", (a)) | |
460 | #else | |
461 | #define debug_push(a) do { ; } while (0) | |
462 | #define debug_pop(a) do { ; } while (0) | |
f48fd688 | 463 | #define debug_set(a,b,c,d) do { ; } while (0) |
d0bfd026 JH |
464 | #endif |
465 | ||
06f33c17 JH |
466 | static void drop_attr_stack(void) |
467 | { | |
468 | while (attr_stack) { | |
469 | struct attr_stack *elem = attr_stack; | |
470 | attr_stack = elem->prev; | |
471 | free_attr_elem(elem); | |
472 | } | |
473 | } | |
474 | ||
c5147722 | 475 | static const char *git_etc_gitattributes(void) |
6df42ab9 PO |
476 | { |
477 | static const char *system_wide; | |
478 | if (!system_wide) | |
479 | system_wide = system_path(ETC_GITATTRIBUTES); | |
480 | return system_wide; | |
481 | } | |
482 | ||
c5147722 | 483 | static int git_attr_system(void) |
6df42ab9 PO |
484 | { |
485 | return !git_env_bool("GIT_ATTR_NOSYSTEM", 0); | |
486 | } | |
487 | ||
f48fd688 JH |
488 | static void bootstrap_attr_stack(void) |
489 | { | |
909ca7b9 | 490 | struct attr_stack *elem; |
684e40f6 | 491 | char *xdg_attributes_file; |
f48fd688 | 492 | |
909ca7b9 JH |
493 | if (attr_stack) |
494 | return; | |
f48fd688 | 495 | |
909ca7b9 JH |
496 | elem = read_attr_from_array(builtin_attr); |
497 | elem->origin = NULL; | |
498 | elem->prev = attr_stack; | |
499 | attr_stack = elem; | |
6df42ab9 | 500 | |
909ca7b9 JH |
501 | if (git_attr_system()) { |
502 | elem = read_attr_from_file(git_etc_gitattributes(), 1); | |
503 | if (elem) { | |
504 | elem->origin = NULL; | |
505 | elem->prev = attr_stack; | |
506 | attr_stack = elem; | |
6df42ab9 | 507 | } |
909ca7b9 | 508 | } |
6df42ab9 | 509 | |
684e40f6 HKNN |
510 | if (!git_attributes_file) { |
511 | home_config_paths(NULL, &xdg_attributes_file, "attributes"); | |
512 | git_attributes_file = xdg_attributes_file; | |
513 | } | |
f0c1c15c JK |
514 | if (git_attributes_file) { |
515 | elem = read_attr_from_file(git_attributes_file, 1); | |
516 | if (elem) { | |
517 | elem->origin = NULL; | |
518 | elem->prev = attr_stack; | |
519 | attr_stack = elem; | |
520 | } | |
909ca7b9 | 521 | } |
f48fd688 | 522 | |
909ca7b9 JH |
523 | if (!is_bare_repository() || direction == GIT_ATTR_INDEX) { |
524 | elem = read_attr(GITATTRIBUTES_FILE, 1); | |
6c65b5ea | 525 | elem->origin = xstrdup(""); |
cd6a0b26 | 526 | elem->originlen = 0; |
f48fd688 JH |
527 | elem->prev = attr_stack; |
528 | attr_stack = elem; | |
909ca7b9 | 529 | debug_push(elem); |
f48fd688 | 530 | } |
909ca7b9 JH |
531 | |
532 | elem = read_attr_from_file(git_path(INFOATTRIBUTES_FILE), 1); | |
533 | if (!elem) | |
534 | elem = xcalloc(1, sizeof(*elem)); | |
535 | elem->origin = NULL; | |
536 | elem->prev = attr_stack; | |
537 | attr_stack = elem; | |
f48fd688 JH |
538 | } |
539 | ||
9db9eecf | 540 | static void prepare_attr_stack(const char *path, int dirlen) |
d0bfd026 JH |
541 | { |
542 | struct attr_stack *elem, *info; | |
9db9eecf | 543 | int len; |
a8727017 MH |
544 | const char *cp; |
545 | ||
d0bfd026 JH |
546 | /* |
547 | * At the bottom of the attribute stack is the built-in | |
6df42ab9 PO |
548 | * set of attribute definitions, followed by the contents |
549 | * of $(prefix)/etc/gitattributes and a file specified by | |
550 | * core.attributesfile. Then, contents from | |
d0bfd026 JH |
551 | * .gitattribute files from directories closer to the |
552 | * root to the ones in deeper directories are pushed | |
553 | * to the stack. Finally, at the very top of the stack | |
554 | * we always keep the contents of $GIT_DIR/info/attributes. | |
555 | * | |
556 | * When checking, we use entries from near the top of the | |
557 | * stack, preferring $GIT_DIR/info/attributes, then | |
558 | * .gitattributes in deeper directories to shallower ones, | |
559 | * and finally use the built-in set as the default. | |
560 | */ | |
7373eab4 | 561 | bootstrap_attr_stack(); |
d0bfd026 JH |
562 | |
563 | /* | |
564 | * Pop the "info" one that is always at the top of the stack. | |
565 | */ | |
566 | info = attr_stack; | |
567 | attr_stack = info->prev; | |
568 | ||
569 | /* | |
570 | * Pop the ones from directories that are not the prefix of | |
c432ef99 JH |
571 | * the path we are checking. Break out of the loop when we see |
572 | * the root one (whose origin is an empty string "") or the builtin | |
573 | * one (whose origin is NULL) without popping it. | |
d0bfd026 | 574 | */ |
77f7f822 | 575 | while (attr_stack->origin) { |
d0bfd026 JH |
576 | int namelen = strlen(attr_stack->origin); |
577 | ||
578 | elem = attr_stack; | |
579 | if (namelen <= dirlen && | |
1afca444 JK |
580 | !strncmp(elem->origin, path, namelen) && |
581 | (!namelen || path[namelen] == '/')) | |
d0bfd026 JH |
582 | break; |
583 | ||
584 | debug_pop(elem); | |
585 | attr_stack = elem->prev; | |
586 | free_attr_elem(elem); | |
587 | } | |
588 | ||
589 | /* | |
590 | * Read from parent directories and push them down | |
591 | */ | |
4191e80a | 592 | if (!is_bare_repository() || direction == GIT_ATTR_INDEX) { |
c432ef99 JH |
593 | /* |
594 | * bootstrap_attr_stack() should have added, and the | |
595 | * above loop should have stopped before popping, the | |
596 | * root element whose attr_stack->origin is set to an | |
597 | * empty string. | |
598 | */ | |
97410b27 | 599 | struct strbuf pathbuf = STRBUF_INIT; |
2d35d556 | 600 | |
c432ef99 | 601 | assert(attr_stack->origin); |
97410b27 | 602 | while (1) { |
2d35d556 RS |
603 | len = strlen(attr_stack->origin); |
604 | if (dirlen <= len) | |
605 | break; | |
97410b27 BC |
606 | cp = memchr(path + len + 1, '/', dirlen - len - 1); |
607 | if (!cp) | |
608 | cp = path + dirlen; | |
609 | strbuf_add(&pathbuf, path, cp - path); | |
f66cf96d | 610 | strbuf_addch(&pathbuf, '/'); |
97410b27 | 611 | strbuf_addstr(&pathbuf, GITATTRIBUTES_FILE); |
f66cf96d | 612 | elem = read_attr(pathbuf.buf, 0); |
97410b27 | 613 | strbuf_setlen(&pathbuf, cp - path); |
cd6a0b26 | 614 | elem->origin = strbuf_detach(&pathbuf, &elem->originlen); |
2d35d556 RS |
615 | elem->prev = attr_stack; |
616 | attr_stack = elem; | |
617 | debug_push(elem); | |
618 | } | |
d0bfd026 | 619 | |
97410b27 BC |
620 | strbuf_release(&pathbuf); |
621 | } | |
d4c98565 | 622 | |
d0bfd026 JH |
623 | /* |
624 | * Finally push the "info" one at the top of the stack. | |
625 | */ | |
626 | info->prev = attr_stack; | |
627 | attr_stack = info; | |
628 | } | |
629 | ||
630 | static int path_matches(const char *pathname, int pathlen, | |
bd2f371d | 631 | int basename_offset, |
82dce998 | 632 | const struct pattern *pat, |
d0bfd026 JH |
633 | const char *base, int baselen) |
634 | { | |
82dce998 NTND |
635 | const char *pattern = pat->pattern; |
636 | int prefix = pat->nowildcardlen; | |
dc09e9ec | 637 | int isdir = (pathlen && pathname[pathlen - 1] == '/'); |
82dce998 | 638 | |
dc09e9ec | 639 | if ((pat->flags & EXC_FLAG_MUSTBEDIR) && !isdir) |
94bc671a JNA |
640 | return 0; |
641 | ||
82dce998 | 642 | if (pat->flags & EXC_FLAG_NODIR) { |
bd2f371d | 643 | return match_basename(pathname + basename_offset, |
dc09e9ec | 644 | pathlen - basename_offset - isdir, |
82dce998 NTND |
645 | pattern, prefix, |
646 | pat->patternlen, pat->flags); | |
d0bfd026 | 647 | } |
dc09e9ec | 648 | return match_pathname(pathname, pathlen - isdir, |
82dce998 NTND |
649 | base, baselen, |
650 | pattern, prefix, pat->patternlen, pat->flags); | |
d0bfd026 JH |
651 | } |
652 | ||
ec775c41 HG |
653 | static int macroexpand_one(int attr_nr, int rem); |
654 | ||
515106fa JH |
655 | static int fill_one(const char *what, struct match_attr *a, int rem) |
656 | { | |
657 | struct git_attr_check *check = check_all_attr; | |
658 | int i; | |
659 | ||
969f9d73 | 660 | for (i = a->num_attr - 1; 0 < rem && 0 <= i; i--) { |
515106fa | 661 | struct git_attr *attr = a->state[i].attr; |
a5e92abd JH |
662 | const char **n = &(check[attr->attr_nr].value); |
663 | const char *v = a->state[i].setto; | |
515106fa JH |
664 | |
665 | if (*n == ATTR__UNKNOWN) { | |
426c27b7 | 666 | debug_set(what, |
712efb1a | 667 | a->is_macro ? a->u.attr->name : a->u.pat.pattern, |
426c27b7 | 668 | attr, v); |
515106fa JH |
669 | *n = v; |
670 | rem--; | |
ec775c41 | 671 | rem = macroexpand_one(attr->attr_nr, rem); |
515106fa JH |
672 | } |
673 | } | |
674 | return rem; | |
675 | } | |
676 | ||
bd2f371d | 677 | static int fill(const char *path, int pathlen, int basename_offset, |
4742d136 | 678 | struct attr_stack *stk, int rem) |
d0bfd026 | 679 | { |
515106fa | 680 | int i; |
d0bfd026 JH |
681 | const char *base = stk->origin ? stk->origin : ""; |
682 | ||
683 | for (i = stk->num_matches - 1; 0 < rem && 0 <= i; i--) { | |
684 | struct match_attr *a = stk->attrs[i]; | |
f48fd688 JH |
685 | if (a->is_macro) |
686 | continue; | |
bd2f371d | 687 | if (path_matches(path, pathlen, basename_offset, |
82dce998 | 688 | &a->u.pat, base, stk->originlen)) |
515106fa | 689 | rem = fill_one("fill", a, rem); |
d0bfd026 JH |
690 | } |
691 | return rem; | |
692 | } | |
693 | ||
aa7710e0 | 694 | static int macroexpand_one(int nr, int rem) |
f48fd688 | 695 | { |
ec775c41 HG |
696 | struct attr_stack *stk; |
697 | struct match_attr *a = NULL; | |
515106fa | 698 | int i; |
f48fd688 | 699 | |
fad32bcd NTND |
700 | if (check_all_attr[nr].value != ATTR__TRUE || |
701 | !check_all_attr[nr].attr->maybe_macro) | |
ec775c41 HG |
702 | return rem; |
703 | ||
704 | for (stk = attr_stack; !a && stk; stk = stk->prev) | |
705 | for (i = stk->num_matches - 1; !a && 0 <= i; i--) { | |
706 | struct match_attr *ma = stk->attrs[i]; | |
707 | if (!ma->is_macro) | |
708 | continue; | |
aa7710e0 | 709 | if (ma->u.attr->attr_nr == nr) |
ec775c41 HG |
710 | a = ma; |
711 | } | |
712 | ||
713 | if (a) | |
515106fa | 714 | rem = fill_one("expand", a, rem); |
ec775c41 | 715 | |
f48fd688 JH |
716 | return rem; |
717 | } | |
718 | ||
2d721744 | 719 | /* |
06a604e6 NTND |
720 | * Collect attributes for path into the array pointed to by |
721 | * check_all_attr. If num is non-zero, only attributes in check[] are | |
722 | * collected. Otherwise all attributes are collected. | |
2d721744 | 723 | */ |
06a604e6 NTND |
724 | static void collect_some_attrs(const char *path, int num, |
725 | struct git_attr_check *check) | |
726 | ||
d0bfd026 JH |
727 | { |
728 | struct attr_stack *stk; | |
9db9eecf | 729 | int i, pathlen, rem, dirlen; |
bd2f371d JH |
730 | const char *cp, *last_slash = NULL; |
731 | int basename_offset; | |
9db9eecf NTND |
732 | |
733 | for (cp = path; *cp; cp++) { | |
734 | if (*cp == '/' && cp[1]) | |
735 | last_slash = cp; | |
736 | } | |
737 | pathlen = cp - path; | |
738 | if (last_slash) { | |
bd2f371d | 739 | basename_offset = last_slash + 1 - path; |
9db9eecf NTND |
740 | dirlen = last_slash - path; |
741 | } else { | |
bd2f371d | 742 | basename_offset = 0; |
9db9eecf NTND |
743 | dirlen = 0; |
744 | } | |
d0bfd026 | 745 | |
9db9eecf | 746 | prepare_attr_stack(path, dirlen); |
f48fd688 | 747 | for (i = 0; i < attr_nr; i++) |
515106fa | 748 | check_all_attr[i].value = ATTR__UNKNOWN; |
06a604e6 NTND |
749 | if (num && !cannot_trust_maybe_real) { |
750 | rem = 0; | |
751 | for (i = 0; i < num; i++) { | |
752 | if (!check[i].attr->maybe_real) { | |
753 | struct git_attr_check *c; | |
754 | c = check_all_attr + check[i].attr->attr_nr; | |
755 | c->value = ATTR__UNSET; | |
756 | rem++; | |
757 | } | |
758 | } | |
759 | if (rem == num) | |
760 | return; | |
761 | } | |
d0bfd026 | 762 | |
f48fd688 JH |
763 | rem = attr_nr; |
764 | for (stk = attr_stack; 0 < rem && stk; stk = stk->prev) | |
bd2f371d | 765 | rem = fill(path, pathlen, basename_offset, stk, rem); |
2d721744 MH |
766 | } |
767 | ||
d932f4eb | 768 | int git_check_attr(const char *path, int num, struct git_attr_check *check) |
2d721744 MH |
769 | { |
770 | int i; | |
771 | ||
06a604e6 | 772 | collect_some_attrs(path, num, check); |
f48fd688 | 773 | |
515106fa | 774 | for (i = 0; i < num; i++) { |
a5e92abd | 775 | const char *value = check_all_attr[check[i].attr->attr_nr].value; |
515106fa JH |
776 | if (value == ATTR__UNKNOWN) |
777 | value = ATTR__UNSET; | |
778 | check[i].value = value; | |
779 | } | |
f48fd688 | 780 | |
d0bfd026 JH |
781 | return 0; |
782 | } | |
06f33c17 | 783 | |
ee548df3 MH |
784 | int git_all_attrs(const char *path, int *num, struct git_attr_check **check) |
785 | { | |
786 | int i, count, j; | |
787 | ||
06a604e6 | 788 | collect_some_attrs(path, 0, NULL); |
ee548df3 MH |
789 | |
790 | /* Count the number of attributes that are set. */ | |
791 | count = 0; | |
792 | for (i = 0; i < attr_nr; i++) { | |
793 | const char *value = check_all_attr[i].value; | |
794 | if (value != ATTR__UNSET && value != ATTR__UNKNOWN) | |
795 | ++count; | |
796 | } | |
797 | *num = count; | |
798 | *check = xmalloc(sizeof(**check) * count); | |
799 | j = 0; | |
800 | for (i = 0; i < attr_nr; i++) { | |
801 | const char *value = check_all_attr[i].value; | |
802 | if (value != ATTR__UNSET && value != ATTR__UNKNOWN) { | |
803 | (*check)[j].attr = check_all_attr[i].attr; | |
804 | (*check)[j].value = value; | |
805 | ++j; | |
806 | } | |
807 | } | |
808 | ||
809 | return 0; | |
810 | } | |
811 | ||
06f33c17 JH |
812 | void git_attr_set_direction(enum git_attr_direction new, struct index_state *istate) |
813 | { | |
814 | enum git_attr_direction old = direction; | |
4191e80a NTND |
815 | |
816 | if (is_bare_repository() && new != GIT_ATTR_INDEX) | |
817 | die("BUG: non-INDEX attr direction in a bare repo"); | |
818 | ||
06f33c17 JH |
819 | direction = new; |
820 | if (new != old) | |
821 | drop_attr_stack(); | |
822 | use_index = istate; | |
823 | } |