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