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