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