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