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