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