]>
Commit | Line | Data |
---|---|---|
1b0c7174 JH |
1 | #include "cache.h" |
2 | #include "tree-walk.h" | |
e6c111b4 | 3 | #include "unpack-trees.h" |
bc96cc87 | 4 | #include "dir.h" |
cbd53a21 | 5 | #include "object-store.h" |
8e440259 | 6 | #include "tree.h" |
64acde94 | 7 | #include "pathspec.h" |
1b0c7174 | 8 | |
4651ece8 LT |
9 | static const char *get_mode(const char *str, unsigned int *modep) |
10 | { | |
11 | unsigned char c; | |
12 | unsigned int mode = 0; | |
13 | ||
64cc1c09 MK |
14 | if (*str == ' ') |
15 | return NULL; | |
16 | ||
4651ece8 LT |
17 | while ((c = *str++) != ' ') { |
18 | if (c < '0' || c > '7') | |
19 | return NULL; | |
20 | mode = (mode << 3) + (c - '0'); | |
21 | } | |
22 | *modep = mode; | |
23 | return str; | |
24 | } | |
25 | ||
8354fa3d | 26 | static int decode_tree_entry(struct tree_desc *desc, const char *buf, unsigned long size, struct strbuf *err) |
4651ece8 LT |
27 | { |
28 | const char *path; | |
29 | unsigned int mode, len; | |
83e4b757 | 30 | const unsigned hashsz = the_hash_algo->rawsz; |
4651ece8 | 31 | |
83e4b757 | 32 | if (size < hashsz + 3 || buf[size - (hashsz + 1)]) { |
8354fa3d DT |
33 | strbuf_addstr(err, _("too-short tree object")); |
34 | return -1; | |
35 | } | |
64cc1c09 | 36 | |
4651ece8 | 37 | path = get_mode(buf, &mode); |
8354fa3d DT |
38 | if (!path) { |
39 | strbuf_addstr(err, _("malformed mode in tree entry")); | |
40 | return -1; | |
41 | } | |
42 | if (!*path) { | |
43 | strbuf_addstr(err, _("empty filename in tree entry")); | |
44 | return -1; | |
45 | } | |
e1d911dd JS |
46 | #ifdef GIT_WINDOWS_NATIVE |
47 | if (protect_ntfs && strchr(path, '\\')) { | |
48 | strbuf_addf(err, _("filename in tree entry contains backslash: '%s'"), path); | |
49 | return -1; | |
50 | } | |
51 | #endif | |
4651ece8 LT |
52 | len = strlen(path) + 1; |
53 | ||
54 | /* Initialize the descriptor entry */ | |
55 | desc->entry.path = path; | |
7146e66f | 56 | desc->entry.mode = canon_mode(mode); |
ea82b2a0 | 57 | desc->entry.pathlen = len - 1; |
58 | hashcpy(desc->entry.oid.hash, (const unsigned char *)path + len); | |
8354fa3d DT |
59 | |
60 | return 0; | |
4651ece8 LT |
61 | } |
62 | ||
8354fa3d | 63 | static int init_tree_desc_internal(struct tree_desc *desc, const void *buffer, unsigned long size, struct strbuf *err) |
6fda5e51 LT |
64 | { |
65 | desc->buffer = buffer; | |
66 | desc->size = size; | |
4651ece8 | 67 | if (size) |
8354fa3d DT |
68 | return decode_tree_entry(desc, buffer, size, err); |
69 | return 0; | |
70 | } | |
71 | ||
72 | void init_tree_desc(struct tree_desc *desc, const void *buffer, unsigned long size) | |
73 | { | |
74 | struct strbuf err = STRBUF_INIT; | |
75 | if (init_tree_desc_internal(desc, buffer, size, &err)) | |
76 | die("%s", err.buf); | |
77 | strbuf_release(&err); | |
78 | } | |
79 | ||
80 | int init_tree_desc_gently(struct tree_desc *desc, const void *buffer, unsigned long size) | |
81 | { | |
82 | struct strbuf err = STRBUF_INIT; | |
83 | int result = init_tree_desc_internal(desc, buffer, size, &err); | |
84 | if (result) | |
85 | error("%s", err.buf); | |
86 | strbuf_release(&err); | |
87 | return result; | |
6fda5e51 LT |
88 | } |
89 | ||
5e575807 NTND |
90 | void *fill_tree_descriptor(struct repository *r, |
91 | struct tree_desc *desc, | |
92 | const struct object_id *oid) | |
1b0c7174 JH |
93 | { |
94 | unsigned long size = 0; | |
95 | void *buf = NULL; | |
96 | ||
5c377d3d | 97 | if (oid) { |
5e575807 | 98 | buf = read_object_with_reference(r, oid, tree_type, &size, NULL); |
1b0c7174 | 99 | if (!buf) |
5c377d3d | 100 | die("unable to read tree %s", oid_to_hex(oid)); |
1b0c7174 | 101 | } |
6fda5e51 | 102 | init_tree_desc(desc, buf, size); |
1b0c7174 JH |
103 | return buf; |
104 | } | |
105 | ||
1b0c7174 JH |
106 | static void entry_clear(struct name_entry *a) |
107 | { | |
108 | memset(a, 0, sizeof(*a)); | |
109 | } | |
110 | ||
111 | static void entry_extract(struct tree_desc *t, struct name_entry *a) | |
112 | { | |
4651ece8 | 113 | *a = t->entry; |
1b0c7174 JH |
114 | } |
115 | ||
8354fa3d | 116 | static int update_tree_entry_internal(struct tree_desc *desc, struct strbuf *err) |
1b0c7174 | 117 | { |
6fda5e51 | 118 | const void *buf = desc->buffer; |
ea82b2a0 | 119 | const unsigned char *end = (const unsigned char *)desc->entry.path + desc->entry.pathlen + 1 + the_hash_algo->rawsz; |
1b0c7174 | 120 | unsigned long size = desc->size; |
4651ece8 | 121 | unsigned long len = end - (const unsigned char *)buf; |
1b0c7174 JH |
122 | |
123 | if (size < len) | |
2edffef2 | 124 | die(_("too-short tree file")); |
4651ece8 LT |
125 | buf = end; |
126 | size -= len; | |
127 | desc->buffer = buf; | |
128 | desc->size = size; | |
129 | if (size) | |
8354fa3d DT |
130 | return decode_tree_entry(desc, buf, size, err); |
131 | return 0; | |
132 | } | |
133 | ||
134 | void update_tree_entry(struct tree_desc *desc) | |
135 | { | |
136 | struct strbuf err = STRBUF_INIT; | |
137 | if (update_tree_entry_internal(desc, &err)) | |
138 | die("%s", err.buf); | |
139 | strbuf_release(&err); | |
140 | } | |
141 | ||
142 | int update_tree_entry_gently(struct tree_desc *desc) | |
143 | { | |
144 | struct strbuf err = STRBUF_INIT; | |
145 | if (update_tree_entry_internal(desc, &err)) { | |
146 | error("%s", err.buf); | |
147 | strbuf_release(&err); | |
148 | /* Stop processing this tree after error */ | |
149 | desc->size = 0; | |
150 | return -1; | |
151 | } | |
152 | strbuf_release(&err); | |
153 | return 0; | |
1b0c7174 JH |
154 | } |
155 | ||
4c068a98 LT |
156 | int tree_entry(struct tree_desc *desc, struct name_entry *entry) |
157 | { | |
4651ece8 | 158 | if (!desc->size) |
4c068a98 LT |
159 | return 0; |
160 | ||
4651ece8 LT |
161 | *entry = desc->entry; |
162 | update_tree_entry(desc); | |
4c068a98 LT |
163 | return 1; |
164 | } | |
165 | ||
8354fa3d DT |
166 | int tree_entry_gently(struct tree_desc *desc, struct name_entry *entry) |
167 | { | |
168 | if (!desc->size) | |
169 | return 0; | |
170 | ||
171 | *entry = desc->entry; | |
172 | if (update_tree_entry_gently(desc)) | |
173 | return 0; | |
174 | return 1; | |
175 | } | |
176 | ||
40d934df LT |
177 | void setup_traverse_info(struct traverse_info *info, const char *base) |
178 | { | |
37806080 | 179 | size_t pathlen = strlen(base); |
bcbe5a51 | 180 | static struct traverse_info dummy; |
40d934df LT |
181 | |
182 | memset(info, 0, sizeof(*info)); | |
183 | if (pathlen && base[pathlen-1] == '/') | |
184 | pathlen--; | |
185 | info->pathlen = pathlen ? pathlen + 1 : 0; | |
90553847 JK |
186 | info->name = base; |
187 | info->namelen = pathlen; | |
947208b7 | 188 | if (pathlen) |
bcbe5a51 | 189 | info->prev = &dummy; |
40d934df LT |
190 | } |
191 | ||
5aa02f98 JK |
192 | char *make_traverse_path(char *path, size_t pathlen, |
193 | const struct traverse_info *info, | |
90553847 | 194 | const char *name, size_t namelen) |
40d934df | 195 | { |
5aa02f98 JK |
196 | /* Always points to the end of the name we're about to add */ |
197 | size_t pos = st_add(info->pathlen, namelen); | |
40d934df | 198 | |
5aa02f98 JK |
199 | if (pos >= pathlen) |
200 | BUG("too small buffer passed to make_traverse_path"); | |
40d934df | 201 | |
5aa02f98 | 202 | path[pos] = 0; |
40d934df | 203 | for (;;) { |
5aa02f98 JK |
204 | if (pos < namelen) |
205 | BUG("traverse_info pathlen does not match strings"); | |
206 | pos -= namelen; | |
207 | memcpy(path + pos, name, namelen); | |
208 | ||
209 | if (!pos) | |
40d934df | 210 | break; |
5aa02f98 JK |
211 | path[--pos] = '/'; |
212 | ||
213 | if (!info) | |
214 | BUG("traverse_info ran out of list items"); | |
90553847 JK |
215 | name = info->name; |
216 | namelen = info->namelen; | |
40d934df | 217 | info = info->prev; |
40d934df LT |
218 | } |
219 | return path; | |
220 | } | |
221 | ||
c43ab062 JK |
222 | void strbuf_make_traverse_path(struct strbuf *out, |
223 | const struct traverse_info *info, | |
224 | const char *name, size_t namelen) | |
225 | { | |
226 | size_t len = traverse_path_len(info, namelen); | |
227 | ||
228 | strbuf_grow(out, len); | |
5aa02f98 JK |
229 | make_traverse_path(out->buf + out->len, out->alloc - out->len, |
230 | info, name, namelen); | |
c43ab062 JK |
231 | strbuf_setlen(out, out->len + len); |
232 | } | |
233 | ||
1ee26571 JH |
234 | struct tree_desc_skip { |
235 | struct tree_desc_skip *prev; | |
236 | const void *ptr; | |
237 | }; | |
238 | ||
239 | struct tree_desc_x { | |
240 | struct tree_desc d; | |
241 | struct tree_desc_skip *skip; | |
242 | }; | |
243 | ||
1ee26571 JH |
244 | static int check_entry_match(const char *a, int a_len, const char *b, int b_len) |
245 | { | |
246 | /* | |
247 | * The caller wants to pick *a* from a tree or nothing. | |
248 | * We are looking at *b* in a tree. | |
249 | * | |
250 | * (0) If a and b are the same name, we are trivially happy. | |
251 | * | |
252 | * There are three possibilities where *a* could be hiding | |
253 | * behind *b*. | |
254 | * | |
255 | * (1) *a* == "t", *b* == "ab" i.e. *b* sorts earlier than *a* no | |
256 | * matter what. | |
257 | * (2) *a* == "t", *b* == "t-2" and "t" is a subtree in the tree; | |
258 | * (3) *a* == "t-2", *b* == "t" and "t-2" is a blob in the tree. | |
259 | * | |
260 | * Otherwise we know *a* won't appear in the tree without | |
261 | * scanning further. | |
262 | */ | |
263 | ||
264 | int cmp = name_compare(a, a_len, b, b_len); | |
265 | ||
266 | /* Most common case first -- reading sync'd trees */ | |
267 | if (!cmp) | |
268 | return cmp; | |
269 | ||
270 | if (0 < cmp) { | |
271 | /* a comes after b; it does not matter if it is case (3) | |
272 | if (b_len < a_len && !memcmp(a, b, b_len) && a[b_len] < '/') | |
273 | return 1; | |
274 | */ | |
275 | return 1; /* keep looking */ | |
276 | } | |
277 | ||
278 | /* b comes after a; are we looking at case (2)? */ | |
279 | if (a_len < b_len && !memcmp(a, b, a_len) && b[a_len] < '/') | |
280 | return 1; /* keep looking */ | |
281 | ||
282 | return -1; /* a cannot appear in the tree */ | |
283 | } | |
284 | ||
285 | /* | |
286 | * From the extended tree_desc, extract the first name entry, while | |
287 | * paying attention to the candidate "first" name. Most importantly, | |
288 | * when looking for an entry, if there are entries that sorts earlier | |
289 | * in the tree object representation than that name, skip them and | |
290 | * process the named entry first. We will remember that we haven't | |
291 | * processed the first entry yet, and in the later call skip the | |
292 | * entry we processed early when update_extended_entry() is called. | |
293 | * | |
294 | * E.g. if the underlying tree object has these entries: | |
295 | * | |
296 | * blob "t-1" | |
297 | * blob "t-2" | |
298 | * tree "t" | |
299 | * blob "t=1" | |
300 | * | |
301 | * and the "first" asks for "t", remember that we still need to | |
302 | * process "t-1" and "t-2" but extract "t". After processing the | |
303 | * entry "t" from this call, the caller will let us know by calling | |
304 | * update_extended_entry() that we can remember "t" has been processed | |
305 | * already. | |
306 | */ | |
307 | ||
308 | static void extended_entry_extract(struct tree_desc_x *t, | |
309 | struct name_entry *a, | |
310 | const char *first, | |
311 | int first_len) | |
312 | { | |
313 | const char *path; | |
314 | int len; | |
315 | struct tree_desc probe; | |
316 | struct tree_desc_skip *skip; | |
317 | ||
318 | /* | |
319 | * Extract the first entry from the tree_desc, but skip the | |
320 | * ones that we already returned in earlier rounds. | |
321 | */ | |
322 | while (1) { | |
323 | if (!t->d.size) { | |
324 | entry_clear(a); | |
325 | break; /* not found */ | |
326 | } | |
327 | entry_extract(&t->d, a); | |
328 | for (skip = t->skip; skip; skip = skip->prev) | |
329 | if (a->path == skip->ptr) | |
330 | break; /* found */ | |
331 | if (!skip) | |
332 | break; | |
333 | /* We have processed this entry already. */ | |
334 | update_tree_entry(&t->d); | |
335 | } | |
336 | ||
337 | if (!first || !a->path) | |
338 | return; | |
339 | ||
340 | /* | |
341 | * The caller wants "first" from this tree, or nothing. | |
342 | */ | |
343 | path = a->path; | |
0de16337 | 344 | len = tree_entry_len(a); |
1ee26571 JH |
345 | switch (check_entry_match(first, first_len, path, len)) { |
346 | case -1: | |
347 | entry_clear(a); | |
348 | case 0: | |
349 | return; | |
350 | default: | |
351 | break; | |
352 | } | |
353 | ||
354 | /* | |
355 | * We need to look-ahead -- we suspect that a subtree whose | |
356 | * name is "first" may be hiding behind the current entry "path". | |
357 | */ | |
358 | probe = t->d; | |
359 | while (probe.size) { | |
360 | entry_extract(&probe, a); | |
361 | path = a->path; | |
0de16337 | 362 | len = tree_entry_len(a); |
1ee26571 JH |
363 | switch (check_entry_match(first, first_len, path, len)) { |
364 | case -1: | |
365 | entry_clear(a); | |
366 | case 0: | |
367 | return; | |
368 | default: | |
369 | update_tree_entry(&probe); | |
370 | break; | |
371 | } | |
372 | /* keep looking */ | |
373 | } | |
374 | entry_clear(a); | |
375 | } | |
376 | ||
377 | static void update_extended_entry(struct tree_desc_x *t, struct name_entry *a) | |
378 | { | |
379 | if (t->d.entry.path == a->path) { | |
380 | update_tree_entry(&t->d); | |
381 | } else { | |
382 | /* we have returned this entry early */ | |
383 | struct tree_desc_skip *skip = xmalloc(sizeof(*skip)); | |
384 | skip->ptr = a->path; | |
385 | skip->prev = t->skip; | |
386 | t->skip = skip; | |
387 | } | |
388 | } | |
389 | ||
390 | static void free_extended_entry(struct tree_desc_x *t) | |
391 | { | |
392 | struct tree_desc_skip *p, *s; | |
393 | ||
394 | for (s = t->skip; s; s = p) { | |
395 | p = s->prev; | |
396 | free(s); | |
397 | } | |
398 | } | |
399 | ||
67022e02 NTND |
400 | static inline int prune_traversal(struct index_state *istate, |
401 | struct name_entry *e, | |
2842c0f9 JH |
402 | struct traverse_info *info, |
403 | struct strbuf *base, | |
404 | int still_interesting) | |
405 | { | |
406 | if (!info->pathspec || still_interesting == 2) | |
407 | return 2; | |
408 | if (still_interesting < 0) | |
409 | return still_interesting; | |
67022e02 NTND |
410 | return tree_entry_interesting(istate, e, base, |
411 | 0, info->pathspec); | |
2842c0f9 JH |
412 | } |
413 | ||
67022e02 NTND |
414 | int traverse_trees(struct index_state *istate, |
415 | int n, struct tree_desc *t, | |
416 | struct traverse_info *info) | |
1b0c7174 | 417 | { |
e6c111b4 | 418 | int error = 0; |
1b0c7174 | 419 | struct name_entry *entry = xmalloc(n*sizeof(*entry)); |
1ee26571 JH |
420 | int i; |
421 | struct tree_desc_x *tx = xcalloc(n, sizeof(*tx)); | |
2842c0f9 JH |
422 | struct strbuf base = STRBUF_INIT; |
423 | int interesting = 1; | |
d9c2bd56 | 424 | char *traverse_path; |
1ee26571 JH |
425 | |
426 | for (i = 0; i < n; i++) | |
427 | tx[i].d = t[i]; | |
1b0c7174 | 428 | |
2842c0f9 | 429 | if (info->prev) { |
c43ab062 JK |
430 | strbuf_make_traverse_path(&base, info->prev, |
431 | info->name, info->namelen); | |
432 | strbuf_addch(&base, '/'); | |
433 | traverse_path = xstrndup(base.buf, base.len); | |
d9c2bd56 | 434 | } else { |
90553847 | 435 | traverse_path = xstrndup(info->name, info->pathlen); |
2842c0f9 | 436 | } |
d9c2bd56 | 437 | info->traverse_path = traverse_path; |
1b0c7174 | 438 | for (;;) { |
a04f8196 | 439 | int trees_used; |
1ee26571 JH |
440 | unsigned long mask, dirmask; |
441 | const char *first = NULL; | |
442 | int first_len = 0; | |
27c0f768 | 443 | struct name_entry *e = NULL; |
1ee26571 JH |
444 | int len; |
445 | ||
446 | for (i = 0; i < n; i++) { | |
447 | e = entry + i; | |
448 | extended_entry_extract(tx + i, e, NULL, 0); | |
449 | } | |
1b0c7174 | 450 | |
1ee26571 JH |
451 | /* |
452 | * A tree may have "t-2" at the current location even | |
453 | * though it may have "t" that is a subtree behind it, | |
454 | * and another tree may return "t". We want to grab | |
455 | * all "t" from all trees to match in such a case. | |
456 | */ | |
1b0c7174 | 457 | for (i = 0; i < n; i++) { |
1ee26571 JH |
458 | e = entry + i; |
459 | if (!e->path) | |
1b0c7174 | 460 | continue; |
0de16337 | 461 | len = tree_entry_len(e); |
1ee26571 JH |
462 | if (!first) { |
463 | first = e->path; | |
464 | first_len = len; | |
465 | continue; | |
466 | } | |
467 | if (name_compare(e->path, len, first, first_len) < 0) { | |
468 | first = e->path; | |
469 | first_len = len; | |
470 | } | |
471 | } | |
472 | ||
473 | if (first) { | |
474 | for (i = 0; i < n; i++) { | |
475 | e = entry + i; | |
476 | extended_entry_extract(tx + i, e, first, first_len); | |
477 | /* Cull the ones that are not the earliest */ | |
478 | if (!e->path) | |
1b0c7174 | 479 | continue; |
0de16337 | 480 | len = tree_entry_len(e); |
1ee26571 JH |
481 | if (name_compare(e->path, len, first, first_len)) |
482 | entry_clear(e); | |
1b0c7174 | 483 | } |
1ee26571 JH |
484 | } |
485 | ||
486 | /* Now we have in entry[i] the earliest name from the trees */ | |
487 | mask = 0; | |
488 | dirmask = 0; | |
489 | for (i = 0; i < n; i++) { | |
490 | if (!entry[i].path) | |
491 | continue; | |
1b0c7174 | 492 | mask |= 1ul << i; |
91e4f036 LT |
493 | if (S_ISDIR(entry[i].mode)) |
494 | dirmask |= 1ul << i; | |
2842c0f9 | 495 | e = &entry[i]; |
1b0c7174 JH |
496 | } |
497 | if (!mask) | |
498 | break; | |
67022e02 | 499 | interesting = prune_traversal(istate, e, info, &base, interesting); |
2842c0f9 JH |
500 | if (interesting < 0) |
501 | break; | |
502 | if (interesting) { | |
a04f8196 SB |
503 | trees_used = info->fn(n, mask, dirmask, entry, info); |
504 | if (trees_used < 0) { | |
505 | error = trees_used; | |
2842c0f9 JH |
506 | if (!info->show_all_errors) |
507 | break; | |
508 | } | |
a04f8196 | 509 | mask &= trees_used; |
e6c111b4 | 510 | } |
1ee26571 | 511 | for (i = 0; i < n; i++) |
5803c6f8 | 512 | if (mask & (1ul << i)) |
1ee26571 | 513 | update_extended_entry(tx + i, entry + i); |
1b0c7174 JH |
514 | } |
515 | free(entry); | |
1ee26571 JH |
516 | for (i = 0; i < n; i++) |
517 | free_extended_entry(tx + i); | |
518 | free(tx); | |
d9c2bd56 DT |
519 | free(traverse_path); |
520 | info->traverse_path = NULL; | |
2842c0f9 | 521 | strbuf_release(&base); |
e6c111b4 | 522 | return error; |
1b0c7174 JH |
523 | } |
524 | ||
275721c2 DT |
525 | struct dir_state { |
526 | void *tree; | |
527 | unsigned long size; | |
3b683bcf | 528 | struct object_id oid; |
275721c2 DT |
529 | }; |
530 | ||
50ddb089 NTND |
531 | static int find_tree_entry(struct repository *r, struct tree_desc *t, |
532 | const char *name, struct object_id *result, | |
533 | unsigned short *mode) | |
4dcff634 JH |
534 | { |
535 | int namelen = strlen(name); | |
536 | while (t->size) { | |
537 | const char *entry; | |
0a3faa45 | 538 | struct object_id oid; |
4dcff634 JH |
539 | int entrylen, cmp; |
540 | ||
0a3faa45 | 541 | oidcpy(&oid, tree_entry_extract(t, &entry, mode)); |
0de16337 | 542 | entrylen = tree_entry_len(&t->entry); |
4dcff634 | 543 | update_tree_entry(t); |
4dcff634 JH |
544 | if (entrylen > namelen) |
545 | continue; | |
546 | cmp = memcmp(name, entry, entrylen); | |
547 | if (cmp > 0) | |
548 | continue; | |
549 | if (cmp < 0) | |
550 | break; | |
551 | if (entrylen == namelen) { | |
0a3faa45 | 552 | oidcpy(result, &oid); |
4dcff634 JH |
553 | return 0; |
554 | } | |
555 | if (name[entrylen] != '/') | |
556 | continue; | |
557 | if (!S_ISDIR(*mode)) | |
558 | break; | |
559 | if (++entrylen == namelen) { | |
0a3faa45 | 560 | oidcpy(result, &oid); |
4dcff634 JH |
561 | return 0; |
562 | } | |
50ddb089 | 563 | return get_tree_entry(r, &oid, name + entrylen, result, mode); |
4dcff634 JH |
564 | } |
565 | return -1; | |
566 | } | |
567 | ||
50ddb089 NTND |
568 | int get_tree_entry(struct repository *r, |
569 | const struct object_id *tree_oid, | |
570 | const char *name, | |
571 | struct object_id *oid, | |
572 | unsigned short *mode) | |
4dcff634 JH |
573 | { |
574 | int retval; | |
575 | void *tree; | |
6fda5e51 | 576 | unsigned long size; |
916bc35b | 577 | struct object_id root; |
4dcff634 | 578 | |
50ddb089 | 579 | tree = read_object_with_reference(r, tree_oid, tree_type, &size, &root); |
4dcff634 JH |
580 | if (!tree) |
581 | return -1; | |
d93b7d1c JK |
582 | |
583 | if (name[0] == '\0') { | |
916bc35b | 584 | oidcpy(oid, &root); |
ef006503 | 585 | free(tree); |
d93b7d1c JK |
586 | return 0; |
587 | } | |
588 | ||
5fb8c05f JH |
589 | if (!size) { |
590 | retval = -1; | |
591 | } else { | |
592 | struct tree_desc t; | |
593 | init_tree_desc(&t, tree, size); | |
50ddb089 | 594 | retval = find_tree_entry(r, &t, name, oid, mode); |
5fb8c05f | 595 | } |
4dcff634 JH |
596 | free(tree); |
597 | return retval; | |
598 | } | |
2c389fc8 | 599 | |
275721c2 DT |
600 | /* |
601 | * This is Linux's built-in max for the number of symlinks to follow. | |
602 | * That limit, of course, does not affect git, but it's a reasonable | |
603 | * choice. | |
604 | */ | |
605 | #define GET_TREE_ENTRY_FOLLOW_SYMLINKS_MAX_LINKS 40 | |
606 | ||
607 | /** | |
608 | * Find a tree entry by following symlinks in tree_sha (which is | |
609 | * assumed to be the root of the repository). In the event that a | |
610 | * symlink points outside the repository (e.g. a link to /foo or a | |
611 | * root-level link to ../foo), the portion of the link which is | |
612 | * outside the repository will be returned in result_path, and *mode | |
613 | * will be set to 0. It is assumed that result_path is uninitialized. | |
614 | * If there are no symlinks, or the end result of the symlink chain | |
615 | * points to an object inside the repository, result will be filled in | |
616 | * with the sha1 of the found object, and *mode will hold the mode of | |
617 | * the object. | |
618 | * | |
d1dd94b3 | 619 | * See the code for enum get_oid_result for a description of |
275721c2 DT |
620 | * the return values. |
621 | */ | |
0dd1f0c3 NTND |
622 | enum get_oid_result get_tree_entry_follow_symlinks(struct repository *r, |
623 | struct object_id *tree_oid, const char *name, | |
624 | struct object_id *result, struct strbuf *result_path, | |
625 | unsigned short *mode) | |
275721c2 DT |
626 | { |
627 | int retval = MISSING_OBJECT; | |
628 | struct dir_state *parents = NULL; | |
629 | size_t parents_alloc = 0; | |
071bcaab | 630 | size_t i, parents_nr = 0; |
0d4a8b5b | 631 | struct object_id current_tree_oid; |
275721c2 DT |
632 | struct strbuf namebuf = STRBUF_INIT; |
633 | struct tree_desc t; | |
634 | int follows_remaining = GET_TREE_ENTRY_FOLLOW_SYMLINKS_MAX_LINKS; | |
275721c2 DT |
635 | |
636 | init_tree_desc(&t, NULL, 0UL); | |
275721c2 | 637 | strbuf_addstr(&namebuf, name); |
3b683bcf | 638 | oidcpy(¤t_tree_oid, tree_oid); |
275721c2 DT |
639 | |
640 | while (1) { | |
641 | int find_result; | |
642 | char *first_slash; | |
643 | char *remainder = NULL; | |
644 | ||
645 | if (!t.buffer) { | |
646 | void *tree; | |
0d4a8b5b | 647 | struct object_id root; |
275721c2 | 648 | unsigned long size; |
0dd1f0c3 | 649 | tree = read_object_with_reference(r, |
d3b4705a | 650 | ¤t_tree_oid, |
275721c2 | 651 | tree_type, &size, |
02f0547e | 652 | &root); |
275721c2 DT |
653 | if (!tree) |
654 | goto done; | |
655 | ||
656 | ALLOC_GROW(parents, parents_nr + 1, parents_alloc); | |
657 | parents[parents_nr].tree = tree; | |
658 | parents[parents_nr].size = size; | |
3b683bcf | 659 | oidcpy(&parents[parents_nr].oid, &root); |
275721c2 DT |
660 | parents_nr++; |
661 | ||
662 | if (namebuf.buf[0] == '\0') { | |
3b683bcf | 663 | oidcpy(result, &root); |
275721c2 DT |
664 | retval = FOUND; |
665 | goto done; | |
666 | } | |
667 | ||
668 | if (!size) | |
669 | goto done; | |
670 | ||
671 | /* descend */ | |
672 | init_tree_desc(&t, tree, size); | |
673 | } | |
674 | ||
675 | /* Handle symlinks to e.g. a//b by removing leading slashes */ | |
676 | while (namebuf.buf[0] == '/') { | |
677 | strbuf_remove(&namebuf, 0, 1); | |
678 | } | |
679 | ||
680 | /* Split namebuf into a first component and a remainder */ | |
681 | if ((first_slash = strchr(namebuf.buf, '/'))) { | |
682 | *first_slash = 0; | |
683 | remainder = first_slash + 1; | |
684 | } | |
685 | ||
686 | if (!strcmp(namebuf.buf, "..")) { | |
687 | struct dir_state *parent; | |
688 | /* | |
689 | * We could end up with .. in the namebuf if it | |
690 | * appears in a symlink. | |
691 | */ | |
692 | ||
693 | if (parents_nr == 1) { | |
694 | if (remainder) | |
695 | *first_slash = '/'; | |
696 | strbuf_add(result_path, namebuf.buf, | |
697 | namebuf.len); | |
698 | *mode = 0; | |
699 | retval = FOUND; | |
700 | goto done; | |
701 | } | |
702 | parent = &parents[parents_nr - 1]; | |
703 | free(parent->tree); | |
704 | parents_nr--; | |
705 | parent = &parents[parents_nr - 1]; | |
706 | init_tree_desc(&t, parent->tree, parent->size); | |
707 | strbuf_remove(&namebuf, 0, remainder ? 3 : 2); | |
708 | continue; | |
709 | } | |
710 | ||
711 | /* We could end up here via a symlink to dir/.. */ | |
712 | if (namebuf.buf[0] == '\0') { | |
3b683bcf | 713 | oidcpy(result, &parents[parents_nr - 1].oid); |
275721c2 DT |
714 | retval = FOUND; |
715 | goto done; | |
716 | } | |
717 | ||
718 | /* Look up the first (or only) path component in the tree. */ | |
0dd1f0c3 | 719 | find_result = find_tree_entry(r, &t, namebuf.buf, |
916bc35b | 720 | ¤t_tree_oid, mode); |
275721c2 DT |
721 | if (find_result) { |
722 | goto done; | |
723 | } | |
724 | ||
725 | if (S_ISDIR(*mode)) { | |
726 | if (!remainder) { | |
3b683bcf | 727 | oidcpy(result, ¤t_tree_oid); |
275721c2 DT |
728 | retval = FOUND; |
729 | goto done; | |
730 | } | |
731 | /* Descend the tree */ | |
732 | t.buffer = NULL; | |
733 | strbuf_remove(&namebuf, 0, | |
734 | 1 + first_slash - namebuf.buf); | |
735 | } else if (S_ISREG(*mode)) { | |
736 | if (!remainder) { | |
3b683bcf | 737 | oidcpy(result, ¤t_tree_oid); |
275721c2 DT |
738 | retval = FOUND; |
739 | } else { | |
740 | retval = NOT_DIR; | |
741 | } | |
742 | goto done; | |
743 | } else if (S_ISLNK(*mode)) { | |
744 | /* Follow a symlink */ | |
745 | unsigned long link_len; | |
746 | size_t len; | |
747 | char *contents, *contents_start; | |
748 | struct dir_state *parent; | |
749 | enum object_type type; | |
750 | ||
751 | if (follows_remaining-- == 0) { | |
752 | /* Too many symlinks followed */ | |
753 | retval = SYMLINK_LOOP; | |
754 | goto done; | |
755 | } | |
756 | ||
757 | /* | |
758 | * At this point, we have followed at a least | |
759 | * one symlink, so on error we need to report this. | |
760 | */ | |
761 | retval = DANGLING_SYMLINK; | |
762 | ||
0dd1f0c3 NTND |
763 | contents = repo_read_object_file(r, |
764 | ¤t_tree_oid, &type, | |
b4f5aca4 | 765 | &link_len); |
275721c2 DT |
766 | |
767 | if (!contents) | |
768 | goto done; | |
769 | ||
770 | if (contents[0] == '/') { | |
771 | strbuf_addstr(result_path, contents); | |
772 | free(contents); | |
773 | *mode = 0; | |
774 | retval = FOUND; | |
775 | goto done; | |
776 | } | |
777 | ||
778 | if (remainder) | |
779 | len = first_slash - namebuf.buf; | |
780 | else | |
781 | len = namebuf.len; | |
782 | ||
783 | contents_start = contents; | |
784 | ||
785 | parent = &parents[parents_nr - 1]; | |
786 | init_tree_desc(&t, parent->tree, parent->size); | |
787 | strbuf_splice(&namebuf, 0, len, | |
788 | contents_start, link_len); | |
789 | if (remainder) | |
790 | namebuf.buf[link_len] = '/'; | |
791 | free(contents); | |
792 | } | |
793 | } | |
794 | done: | |
795 | for (i = 0; i < parents_nr; i++) | |
796 | free(parents[i].tree); | |
797 | free(parents); | |
798 | ||
799 | strbuf_release(&namebuf); | |
800 | return retval; | |
801 | } | |
802 | ||
93d93537 NTND |
803 | static int match_entry(const struct pathspec_item *item, |
804 | const struct name_entry *entry, int pathlen, | |
58c4d666 | 805 | const char *match, int matchlen, |
c3a47ca9 | 806 | enum interesting *never_interesting) |
58c4d666 NTND |
807 | { |
808 | int m = -1; /* signals that we haven't called strncmp() */ | |
809 | ||
93d93537 NTND |
810 | if (item->magic & PATHSPEC_ICASE) |
811 | /* | |
812 | * "Never interesting" trick requires exact | |
813 | * matching. We could do something clever with inexact | |
814 | * matching, but it's trickier (and not to forget that | |
815 | * strcasecmp is locale-dependent, at least in | |
816 | * glibc). Just disable it for now. It can't be worse | |
817 | * than the wildcard's codepath of '[Tt][Hi][Is][Ss]' | |
818 | * pattern. | |
819 | */ | |
820 | *never_interesting = entry_not_interesting; | |
821 | else if (*never_interesting != entry_not_interesting) { | |
58c4d666 NTND |
822 | /* |
823 | * We have not seen any match that sorts later | |
824 | * than the current path. | |
825 | */ | |
826 | ||
827 | /* | |
828 | * Does match sort strictly earlier than path | |
829 | * with their common parts? | |
830 | */ | |
831 | m = strncmp(match, entry->path, | |
832 | (matchlen < pathlen) ? matchlen : pathlen); | |
833 | if (m < 0) | |
834 | return 0; | |
835 | ||
836 | /* | |
837 | * If we come here even once, that means there is at | |
838 | * least one pathspec that would sort equal to or | |
839 | * later than the path we are currently looking at. | |
840 | * In other words, if we have never reached this point | |
841 | * after iterating all pathspecs, it means all | |
842 | * pathspecs are either outside of base, or inside the | |
843 | * base but sorts strictly earlier than the current | |
844 | * one. In either case, they will never match the | |
845 | * subsequent entries. In such a case, we initialized | |
846 | * the variable to -1 and that is what will be | |
847 | * returned, allowing the caller to terminate early. | |
848 | */ | |
c3a47ca9 | 849 | *never_interesting = entry_not_interesting; |
58c4d666 NTND |
850 | } |
851 | ||
852 | if (pathlen > matchlen) | |
853 | return 0; | |
854 | ||
855 | if (matchlen > pathlen) { | |
856 | if (match[pathlen] != '/') | |
857 | return 0; | |
74b4f7f2 | 858 | if (!S_ISDIR(entry->mode) && !S_ISGITLINK(entry->mode)) |
58c4d666 NTND |
859 | return 0; |
860 | } | |
861 | ||
862 | if (m == -1) | |
863 | /* | |
864 | * we cheated and did not do strncmp(), so we do | |
865 | * that here. | |
866 | */ | |
93d93537 | 867 | m = ps_strncmp(item, match, entry->path, pathlen); |
58c4d666 NTND |
868 | |
869 | /* | |
870 | * If common part matched earlier then it is a hit, | |
871 | * because we rejected the case where path is not a | |
872 | * leading directory and is shorter than match. | |
873 | */ | |
874 | if (!m) | |
93d93537 NTND |
875 | /* |
876 | * match_entry does not check if the prefix part is | |
877 | * matched case-sensitively. If the entry is a | |
878 | * directory and part of prefix, it'll be rematched | |
879 | * eventually by basecmp with special treatment for | |
880 | * the prefix. | |
881 | */ | |
58c4d666 NTND |
882 | return 1; |
883 | ||
884 | return 0; | |
885 | } | |
886 | ||
93d93537 NTND |
887 | /* :(icase)-aware string compare */ |
888 | static int basecmp(const struct pathspec_item *item, | |
889 | const char *base, const char *match, int len) | |
890 | { | |
891 | if (item->magic & PATHSPEC_ICASE) { | |
892 | int ret, n = len > item->prefix ? item->prefix : len; | |
893 | ret = strncmp(base, match, n); | |
894 | if (ret) | |
895 | return ret; | |
896 | base += n; | |
897 | match += n; | |
898 | len -= n; | |
899 | } | |
900 | return ps_strncmp(item, base, match, len); | |
901 | } | |
902 | ||
903 | static int match_dir_prefix(const struct pathspec_item *item, | |
904 | const char *base, | |
58c4d666 NTND |
905 | const char *match, int matchlen) |
906 | { | |
93d93537 | 907 | if (basecmp(item, base, match, matchlen)) |
58c4d666 NTND |
908 | return 0; |
909 | ||
910 | /* | |
911 | * If the base is a subdirectory of a path which | |
912 | * was specified, all of them are interesting. | |
913 | */ | |
914 | if (!matchlen || | |
915 | base[matchlen] == '/' || | |
916 | match[matchlen - 1] == '/') | |
917 | return 1; | |
918 | ||
919 | /* Just a random prefix match */ | |
920 | return 0; | |
921 | } | |
922 | ||
c904cd89 NTND |
923 | /* |
924 | * Perform matching on the leading non-wildcard part of | |
925 | * pathspec. item->nowildcard_len must be greater than zero. Return | |
926 | * non-zero if base is matched. | |
927 | */ | |
928 | static int match_wildcard_base(const struct pathspec_item *item, | |
929 | const char *base, int baselen, | |
930 | int *matched) | |
931 | { | |
932 | const char *match = item->match; | |
933 | /* the wildcard part is not considered in this function */ | |
934 | int matchlen = item->nowildcard_len; | |
935 | ||
936 | if (baselen) { | |
937 | int dirlen; | |
938 | /* | |
939 | * Return early if base is longer than the | |
940 | * non-wildcard part but it does not match. | |
941 | */ | |
942 | if (baselen >= matchlen) { | |
943 | *matched = matchlen; | |
93d93537 | 944 | return !basecmp(item, base, match, matchlen); |
c904cd89 NTND |
945 | } |
946 | ||
947 | dirlen = matchlen; | |
948 | while (dirlen && match[dirlen - 1] != '/') | |
949 | dirlen--; | |
950 | ||
951 | /* | |
952 | * Return early if base is shorter than the | |
953 | * non-wildcard part but it does not match. Note that | |
954 | * base ends with '/' so we are sure it really matches | |
955 | * directory | |
956 | */ | |
93d93537 | 957 | if (basecmp(item, base, match, baselen)) |
c904cd89 NTND |
958 | return 0; |
959 | *matched = baselen; | |
960 | } else | |
961 | *matched = 0; | |
962 | /* | |
963 | * we could have checked entry against the non-wildcard part | |
964 | * that is not in base and does similar never_interesting | |
965 | * optimization as in match_entry. For now just be happy with | |
966 | * base comparison. | |
967 | */ | |
968 | return entry_interesting; | |
969 | } | |
970 | ||
2c389fc8 NTND |
971 | /* |
972 | * Is a tree entry interesting given the pathspec we have? | |
973 | * | |
1376e507 NTND |
974 | * Pre-condition: either baselen == base_offset (i.e. empty path) |
975 | * or base[baselen-1] == '/' (i.e. with trailing slash). | |
2c389fc8 | 976 | */ |
67022e02 NTND |
977 | static enum interesting do_match(struct index_state *istate, |
978 | const struct name_entry *entry, | |
ef79b1f8 NTND |
979 | struct strbuf *base, int base_offset, |
980 | const struct pathspec *ps, | |
981 | int exclude) | |
2c389fc8 NTND |
982 | { |
983 | int i; | |
1376e507 | 984 | int pathlen, baselen = base->len - base_offset; |
c3a47ca9 | 985 | enum interesting never_interesting = ps->has_wildcard ? |
d688cf07 | 986 | entry_not_interesting : all_entries_not_interesting; |
2c389fc8 | 987 | |
5c6933d2 NTND |
988 | GUARD_PATHSPEC(ps, |
989 | PATHSPEC_FROMTOP | | |
990 | PATHSPEC_MAXDEPTH | | |
bd30c2e4 | 991 | PATHSPEC_LITERAL | |
93d93537 | 992 | PATHSPEC_GLOB | |
ef79b1f8 | 993 | PATHSPEC_ICASE | |
5a0b97b3 NTND |
994 | PATHSPEC_EXCLUDE | |
995 | PATHSPEC_ATTR); | |
8f4f8f45 | 996 | |
bc96cc87 | 997 | if (!ps->nr) { |
6330a171 NTND |
998 | if (!ps->recursive || |
999 | !(ps->magic & PATHSPEC_MAXDEPTH) || | |
1000 | ps->max_depth == -1) | |
d688cf07 NTND |
1001 | return all_entries_interesting; |
1002 | return within_depth(base->buf + base_offset, baselen, | |
1003 | !!S_ISDIR(entry->mode), | |
1004 | ps->max_depth) ? | |
1005 | entry_interesting : entry_not_interesting; | |
bc96cc87 | 1006 | } |
2c389fc8 | 1007 | |
0de16337 | 1008 | pathlen = tree_entry_len(entry); |
2c389fc8 | 1009 | |
1376e507 | 1010 | for (i = ps->nr - 1; i >= 0; i--) { |
2c389fc8 NTND |
1011 | const struct pathspec_item *item = ps->items+i; |
1012 | const char *match = item->match; | |
1376e507 | 1013 | const char *base_str = base->buf + base_offset; |
c904cd89 | 1014 | int matchlen = item->len, matched = 0; |
2c389fc8 | 1015 | |
ef79b1f8 NTND |
1016 | if ((!exclude && item->magic & PATHSPEC_EXCLUDE) || |
1017 | ( exclude && !(item->magic & PATHSPEC_EXCLUDE))) | |
1018 | continue; | |
1019 | ||
2c389fc8 NTND |
1020 | if (baselen >= matchlen) { |
1021 | /* If it doesn't match, move along... */ | |
93d93537 | 1022 | if (!match_dir_prefix(item, base_str, match, matchlen)) |
d38f2809 | 1023 | goto match_wildcards; |
bc96cc87 | 1024 | |
6330a171 NTND |
1025 | if (!ps->recursive || |
1026 | !(ps->magic & PATHSPEC_MAXDEPTH) || | |
5a0b97b3 NTND |
1027 | ps->max_depth == -1) { |
1028 | if (!item->attr_match_nr) | |
1029 | return all_entries_interesting; | |
1030 | else | |
1031 | goto interesting; | |
1032 | } | |
1033 | ||
1034 | if (within_depth(base_str + matchlen + 1, | |
1035 | baselen - matchlen - 1, | |
1036 | !!S_ISDIR(entry->mode), | |
1037 | ps->max_depth)) | |
1038 | goto interesting; | |
1039 | else | |
1040 | return entry_not_interesting; | |
2c389fc8 NTND |
1041 | } |
1042 | ||
1b740923 | 1043 | /* Either there must be no base, or the base must match. */ |
93d93537 NTND |
1044 | if (baselen == 0 || !basecmp(item, base_str, match, baselen)) { |
1045 | if (match_entry(item, entry, pathlen, | |
58c4d666 NTND |
1046 | match + baselen, matchlen - baselen, |
1047 | &never_interesting)) | |
5a0b97b3 | 1048 | goto interesting; |
f1a2ddbb | 1049 | |
170260ae | 1050 | if (item->nowildcard_len < item->len) { |
bd30c2e4 | 1051 | if (!git_fnmatch(item, match + baselen, entry->path, |
8c6abbcd | 1052 | item->nowildcard_len - baselen)) |
5a0b97b3 | 1053 | goto interesting; |
f1a2ddbb NTND |
1054 | |
1055 | /* | |
1056 | * Match all directories. We'll try to | |
1057 | * match files later on. | |
1058 | */ | |
1059 | if (ps->recursive && S_ISDIR(entry->mode)) | |
d688cf07 | 1060 | return entry_interesting; |
74ed4371 BW |
1061 | |
1062 | /* | |
1063 | * When matching against submodules with | |
1064 | * wildcard characters, ensure that the entry | |
1065 | * at least matches up to the first wild | |
1066 | * character. More accurate matching can then | |
1067 | * be performed in the submodule itself. | |
1068 | */ | |
eef3df5a BW |
1069 | if (ps->recurse_submodules && |
1070 | S_ISGITLINK(entry->mode) && | |
74ed4371 BW |
1071 | !ps_strncmp(item, match + baselen, |
1072 | entry->path, | |
1073 | item->nowildcard_len - baselen)) | |
5a0b97b3 | 1074 | goto interesting; |
f1a2ddbb NTND |
1075 | } |
1076 | ||
1077 | continue; | |
2c389fc8 | 1078 | } |
d38f2809 NTND |
1079 | |
1080 | match_wildcards: | |
170260ae | 1081 | if (item->nowildcard_len == item->len) |
d38f2809 NTND |
1082 | continue; |
1083 | ||
c904cd89 NTND |
1084 | if (item->nowildcard_len && |
1085 | !match_wildcard_base(item, base_str, baselen, &matched)) | |
e4ddb057 | 1086 | continue; |
c904cd89 | 1087 | |
d38f2809 NTND |
1088 | /* |
1089 | * Concatenate base and entry->path into one and do | |
1090 | * fnmatch() on it. | |
c904cd89 NTND |
1091 | * |
1092 | * While we could avoid concatenation in certain cases | |
1093 | * [1], which saves a memcpy and potentially a | |
1094 | * realloc, it turns out not worth it. Measurement on | |
1095 | * linux-2.6 does not show any clear improvements, | |
1096 | * partly because of the nowildcard_len optimization | |
1097 | * in git_fnmatch(). Avoid micro-optimizations here. | |
1098 | * | |
1099 | * [1] if match_wildcard_base() says the base | |
1100 | * directory is already matched, we only need to match | |
1101 | * the rest, which is shorter so _in theory_ faster. | |
d38f2809 NTND |
1102 | */ |
1103 | ||
1104 | strbuf_add(base, entry->path, pathlen); | |
1105 | ||
bd30c2e4 | 1106 | if (!git_fnmatch(item, match, base->buf + base_offset, |
8c6abbcd | 1107 | item->nowildcard_len)) { |
1376e507 | 1108 | strbuf_setlen(base, base_offset + baselen); |
5a0b97b3 | 1109 | goto interesting; |
d38f2809 | 1110 | } |
74ed4371 BW |
1111 | |
1112 | /* | |
1113 | * When matching against submodules with | |
1114 | * wildcard characters, ensure that the entry | |
1115 | * at least matches up to the first wild | |
1116 | * character. More accurate matching can then | |
1117 | * be performed in the submodule itself. | |
1118 | */ | |
eef3df5a | 1119 | if (ps->recurse_submodules && S_ISGITLINK(entry->mode) && |
74ed4371 BW |
1120 | !ps_strncmp(item, match, base->buf + base_offset, |
1121 | item->nowildcard_len)) { | |
1122 | strbuf_setlen(base, base_offset + baselen); | |
5a0b97b3 | 1123 | goto interesting; |
74ed4371 BW |
1124 | } |
1125 | ||
1376e507 | 1126 | strbuf_setlen(base, base_offset + baselen); |
d38f2809 NTND |
1127 | |
1128 | /* | |
1129 | * Match all directories. We'll try to match files | |
1130 | * later on. | |
8c69c1f9 NTND |
1131 | * max_depth is ignored but we may consider support it |
1132 | * in future, see | |
3eae30e4 | 1133 | * https://lore.kernel.org/git/7vmxo5l2g4.fsf@alter.siamese.dyndns.org/ |
d38f2809 NTND |
1134 | */ |
1135 | if (ps->recursive && S_ISDIR(entry->mode)) | |
d688cf07 | 1136 | return entry_interesting; |
5a0b97b3 NTND |
1137 | continue; |
1138 | interesting: | |
1139 | if (item->attr_match_nr) { | |
1140 | int ret; | |
1141 | ||
1142 | /* | |
1143 | * Must not return all_entries_not_interesting | |
1144 | * prematurely. We do not know if all entries do not | |
1145 | * match some attributes with current attr API. | |
1146 | */ | |
1147 | never_interesting = entry_not_interesting; | |
1148 | ||
1149 | /* | |
1150 | * Consider all directories interesting (because some | |
1151 | * of those files inside may match some attributes | |
1152 | * even though the parent dir does not) | |
1153 | * | |
1154 | * FIXME: attributes _can_ match directories and we | |
1155 | * can probably return all_entries_interesting or | |
1156 | * all_entries_not_interesting here if matched. | |
1157 | */ | |
1158 | if (S_ISDIR(entry->mode)) | |
1159 | return entry_interesting; | |
1160 | ||
1161 | strbuf_add(base, entry->path, pathlen); | |
1162 | ret = match_pathspec_attrs(istate, base->buf + base_offset, | |
1163 | base->len - base_offset, item); | |
1164 | strbuf_setlen(base, base_offset + baselen); | |
1165 | if (!ret) | |
1166 | continue; | |
1167 | } | |
1168 | return entry_interesting; | |
2c389fc8 NTND |
1169 | } |
1170 | return never_interesting; /* No matches */ | |
1171 | } | |
ef79b1f8 NTND |
1172 | |
1173 | /* | |
1174 | * Is a tree entry interesting given the pathspec we have? | |
1175 | * | |
1176 | * Pre-condition: either baselen == base_offset (i.e. empty path) | |
1177 | * or base[baselen-1] == '/' (i.e. with trailing slash). | |
1178 | */ | |
67022e02 NTND |
1179 | enum interesting tree_entry_interesting(struct index_state *istate, |
1180 | const struct name_entry *entry, | |
ef79b1f8 NTND |
1181 | struct strbuf *base, int base_offset, |
1182 | const struct pathspec *ps) | |
1183 | { | |
1184 | enum interesting positive, negative; | |
67022e02 | 1185 | positive = do_match(istate, entry, base, base_offset, ps, 0); |
ef79b1f8 NTND |
1186 | |
1187 | /* | |
1188 | * case | entry | positive | negative | result | |
1189 | * -----+-------+----------+----------+------- | |
1190 | * 1 | file | -1 | -1..2 | -1 | |
1191 | * 2 | file | 0 | -1..2 | 0 | |
1192 | * 3 | file | 1 | -1 | 1 | |
1193 | * 4 | file | 1 | 0 | 1 | |
1194 | * 5 | file | 1 | 1 | 0 | |
1195 | * 6 | file | 1 | 2 | 0 | |
1196 | * 7 | file | 2 | -1 | 2 | |
b7845ceb | 1197 | * 8 | file | 2 | 0 | 1 |
ef79b1f8 NTND |
1198 | * 9 | file | 2 | 1 | 0 |
1199 | * 10 | file | 2 | 2 | -1 | |
1200 | * -----+-------+----------+----------+------- | |
1201 | * 11 | dir | -1 | -1..2 | -1 | |
1202 | * 12 | dir | 0 | -1..2 | 0 | |
1203 | * 13 | dir | 1 | -1 | 1 | |
1204 | * 14 | dir | 1 | 0 | 1 | |
1205 | * 15 | dir | 1 | 1 | 1 (*) | |
1206 | * 16 | dir | 1 | 2 | 0 | |
1207 | * 17 | dir | 2 | -1 | 2 | |
b7845ceb | 1208 | * 18 | dir | 2 | 0 | 1 |
ef79b1f8 NTND |
1209 | * 19 | dir | 2 | 1 | 1 (*) |
1210 | * 20 | dir | 2 | 2 | -1 | |
1211 | * | |
1212 | * (*) An exclude pattern interested in a directory does not | |
1213 | * necessarily mean it will exclude all of the directory. In | |
1214 | * wildcard case, it can't decide until looking at individual | |
1215 | * files inside. So don't write such directories off yet. | |
1216 | */ | |
1217 | ||
1218 | if (!(ps->magic & PATHSPEC_EXCLUDE) || | |
1219 | positive <= entry_not_interesting) /* #1, #2, #11, #12 */ | |
1220 | return positive; | |
1221 | ||
67022e02 | 1222 | negative = do_match(istate, entry, base, base_offset, ps, 1); |
ef79b1f8 | 1223 | |
b7845ceb NTND |
1224 | /* #8, #18 */ |
1225 | if (positive == all_entries_interesting && | |
1226 | negative == entry_not_interesting) | |
1227 | return entry_interesting; | |
1228 | ||
1229 | /* #3, #4, #7, #13, #14, #17 */ | |
ef79b1f8 NTND |
1230 | if (negative <= entry_not_interesting) |
1231 | return positive; | |
1232 | ||
1233 | /* #15, #19 */ | |
1234 | if (S_ISDIR(entry->mode) && | |
1235 | positive >= entry_interesting && | |
1236 | negative == entry_interesting) | |
1237 | return entry_interesting; | |
1238 | ||
1239 | if ((positive == entry_interesting && | |
1240 | negative >= entry_interesting) || /* #5, #6, #16 */ | |
1241 | (positive == all_entries_interesting && | |
1242 | negative == entry_interesting)) /* #9 */ | |
1243 | return entry_not_interesting; | |
1244 | ||
1245 | return all_entries_not_interesting; /* #10, #20 */ | |
1246 | } |