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