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