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