3 #include "unpack-trees.h"
5 #include "object-store.h"
9 static const char *get_mode(const char *str
, unsigned int *modep
)
12 unsigned int mode
= 0;
17 while ((c
= *str
++) != ' ') {
18 if (c
< '0' || c
> '7')
20 mode
= (mode
<< 3) + (c
- '0');
26 static int decode_tree_entry(struct tree_desc
*desc
, const char *buf
, unsigned long size
, struct strbuf
*err
)
29 unsigned int mode
, len
;
30 const unsigned hashsz
= the_hash_algo
->rawsz
;
32 if (size
< hashsz
+ 3 || buf
[size
- (hashsz
+ 1)]) {
33 strbuf_addstr(err
, _("too-short tree object"));
37 path
= get_mode(buf
, &mode
);
39 strbuf_addstr(err
, _("malformed mode in tree entry"));
43 strbuf_addstr(err
, _("empty filename in tree entry"));
46 #ifdef GIT_WINDOWS_NATIVE
47 if (protect_ntfs
&& strchr(path
, '\\')) {
48 strbuf_addf(err
, _("filename in tree entry contains backslash: '%s'"), path
);
52 len
= strlen(path
) + 1;
54 /* Initialize the descriptor entry */
55 desc
->entry
.path
= path
;
56 desc
->entry
.mode
= canon_mode(mode
);
57 desc
->entry
.pathlen
= len
- 1;
58 hashcpy(desc
->entry
.oid
.hash
, (const unsigned char *)path
+ len
);
63 static int init_tree_desc_internal(struct tree_desc
*desc
, const void *buffer
, unsigned long size
, struct strbuf
*err
)
65 desc
->buffer
= buffer
;
68 return decode_tree_entry(desc
, buffer
, size
, err
);
72 void init_tree_desc(struct tree_desc
*desc
, const void *buffer
, unsigned long size
)
74 struct strbuf err
= STRBUF_INIT
;
75 if (init_tree_desc_internal(desc
, buffer
, size
, &err
))
80 int init_tree_desc_gently(struct tree_desc
*desc
, const void *buffer
, unsigned long size
)
82 struct strbuf err
= STRBUF_INIT
;
83 int result
= init_tree_desc_internal(desc
, buffer
, size
, &err
);
90 void *fill_tree_descriptor(struct repository
*r
,
91 struct tree_desc
*desc
,
92 const struct object_id
*oid
)
94 unsigned long size
= 0;
98 buf
= read_object_with_reference(r
, oid
, tree_type
, &size
, NULL
);
100 die("unable to read tree %s", oid_to_hex(oid
));
102 init_tree_desc(desc
, buf
, size
);
106 static void entry_clear(struct name_entry
*a
)
108 memset(a
, 0, sizeof(*a
));
111 static void entry_extract(struct tree_desc
*t
, struct name_entry
*a
)
116 static int update_tree_entry_internal(struct tree_desc
*desc
, struct strbuf
*err
)
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
;
124 die(_("too-short tree file"));
130 return decode_tree_entry(desc
, buf
, size
, err
);
134 void update_tree_entry(struct tree_desc
*desc
)
136 struct strbuf err
= STRBUF_INIT
;
137 if (update_tree_entry_internal(desc
, &err
))
139 strbuf_release(&err
);
142 int update_tree_entry_gently(struct tree_desc
*desc
)
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 */
152 strbuf_release(&err
);
156 int tree_entry(struct tree_desc
*desc
, struct name_entry
*entry
)
161 *entry
= desc
->entry
;
162 update_tree_entry(desc
);
166 int tree_entry_gently(struct tree_desc
*desc
, struct name_entry
*entry
)
171 *entry
= desc
->entry
;
172 if (update_tree_entry_gently(desc
))
177 void setup_traverse_info(struct traverse_info
*info
, const char *base
)
179 int pathlen
= strlen(base
);
180 static struct traverse_info dummy
;
182 memset(info
, 0, sizeof(*info
));
183 if (pathlen
&& base
[pathlen
-1] == '/')
185 info
->pathlen
= pathlen
? pathlen
+ 1 : 0;
186 info
->name
.path
= base
;
187 info
->name
.pathlen
= pathlen
;
189 hashcpy(info
->name
.oid
.hash
, (const unsigned char *)base
+ pathlen
+ 1);
194 char *make_traverse_path(char *path
, const struct traverse_info
*info
, const struct name_entry
*n
)
196 int len
= tree_entry_len(n
);
197 int pathlen
= info
->pathlen
;
199 path
[pathlen
+ len
] = 0;
201 memcpy(path
+ pathlen
, n
->path
, len
);
204 path
[--pathlen
] = '/';
206 len
= tree_entry_len(n
);
213 struct tree_desc_skip
{
214 struct tree_desc_skip
*prev
;
220 struct tree_desc_skip
*skip
;
223 static int check_entry_match(const char *a
, int a_len
, const char *b
, int b_len
)
226 * The caller wants to pick *a* from a tree or nothing.
227 * We are looking at *b* in a tree.
229 * (0) If a and b are the same name, we are trivially happy.
231 * There are three possibilities where *a* could be hiding
234 * (1) *a* == "t", *b* == "ab" i.e. *b* sorts earlier than *a* no
236 * (2) *a* == "t", *b* == "t-2" and "t" is a subtree in the tree;
237 * (3) *a* == "t-2", *b* == "t" and "t-2" is a blob in the tree.
239 * Otherwise we know *a* won't appear in the tree without
243 int cmp
= name_compare(a
, a_len
, b
, b_len
);
245 /* Most common case first -- reading sync'd trees */
250 /* a comes after b; it does not matter if it is case (3)
251 if (b_len < a_len && !memcmp(a, b, b_len) && a[b_len] < '/')
254 return 1; /* keep looking */
257 /* b comes after a; are we looking at case (2)? */
258 if (a_len
< b_len
&& !memcmp(a
, b
, a_len
) && b
[a_len
] < '/')
259 return 1; /* keep looking */
261 return -1; /* a cannot appear in the tree */
265 * From the extended tree_desc, extract the first name entry, while
266 * paying attention to the candidate "first" name. Most importantly,
267 * when looking for an entry, if there are entries that sorts earlier
268 * in the tree object representation than that name, skip them and
269 * process the named entry first. We will remember that we haven't
270 * processed the first entry yet, and in the later call skip the
271 * entry we processed early when update_extended_entry() is called.
273 * E.g. if the underlying tree object has these entries:
280 * and the "first" asks for "t", remember that we still need to
281 * process "t-1" and "t-2" but extract "t". After processing the
282 * entry "t" from this call, the caller will let us know by calling
283 * update_extended_entry() that we can remember "t" has been processed
287 static void extended_entry_extract(struct tree_desc_x
*t
,
288 struct name_entry
*a
,
294 struct tree_desc probe
;
295 struct tree_desc_skip
*skip
;
298 * Extract the first entry from the tree_desc, but skip the
299 * ones that we already returned in earlier rounds.
304 break; /* not found */
306 entry_extract(&t
->d
, a
);
307 for (skip
= t
->skip
; skip
; skip
= skip
->prev
)
308 if (a
->path
== skip
->ptr
)
312 /* We have processed this entry already. */
313 update_tree_entry(&t
->d
);
316 if (!first
|| !a
->path
)
320 * The caller wants "first" from this tree, or nothing.
323 len
= tree_entry_len(a
);
324 switch (check_entry_match(first
, first_len
, path
, len
)) {
334 * We need to look-ahead -- we suspect that a subtree whose
335 * name is "first" may be hiding behind the current entry "path".
339 entry_extract(&probe
, a
);
341 len
= tree_entry_len(a
);
342 switch (check_entry_match(first
, first_len
, path
, len
)) {
348 update_tree_entry(&probe
);
356 static void update_extended_entry(struct tree_desc_x
*t
, struct name_entry
*a
)
358 if (t
->d
.entry
.path
== a
->path
) {
359 update_tree_entry(&t
->d
);
361 /* we have returned this entry early */
362 struct tree_desc_skip
*skip
= xmalloc(sizeof(*skip
));
364 skip
->prev
= t
->skip
;
369 static void free_extended_entry(struct tree_desc_x
*t
)
371 struct tree_desc_skip
*p
, *s
;
373 for (s
= t
->skip
; s
; s
= p
) {
379 static inline int prune_traversal(struct index_state
*istate
,
380 struct name_entry
*e
,
381 struct traverse_info
*info
,
383 int still_interesting
)
385 if (!info
->pathspec
|| still_interesting
== 2)
387 if (still_interesting
< 0)
388 return still_interesting
;
389 return tree_entry_interesting(istate
, e
, base
,
393 int traverse_trees(struct index_state
*istate
,
394 int n
, struct tree_desc
*t
,
395 struct traverse_info
*info
)
398 struct name_entry
*entry
= xmalloc(n
*sizeof(*entry
));
400 struct tree_desc_x
*tx
= xcalloc(n
, sizeof(*tx
));
401 struct strbuf base
= STRBUF_INIT
;
405 for (i
= 0; i
< n
; i
++)
409 strbuf_grow(&base
, info
->pathlen
);
410 make_traverse_path(base
.buf
, info
->prev
, &info
->name
);
411 base
.buf
[info
->pathlen
-1] = '/';
412 strbuf_setlen(&base
, info
->pathlen
);
413 traverse_path
= xstrndup(base
.buf
, info
->pathlen
);
415 traverse_path
= xstrndup(info
->name
.path
, info
->pathlen
);
417 info
->traverse_path
= traverse_path
;
420 unsigned long mask
, dirmask
;
421 const char *first
= NULL
;
423 struct name_entry
*e
= NULL
;
426 for (i
= 0; i
< n
; i
++) {
428 extended_entry_extract(tx
+ i
, e
, NULL
, 0);
432 * A tree may have "t-2" at the current location even
433 * though it may have "t" that is a subtree behind it,
434 * and another tree may return "t". We want to grab
435 * all "t" from all trees to match in such a case.
437 for (i
= 0; i
< n
; i
++) {
441 len
= tree_entry_len(e
);
447 if (name_compare(e
->path
, len
, first
, first_len
) < 0) {
454 for (i
= 0; i
< n
; i
++) {
456 extended_entry_extract(tx
+ i
, e
, first
, first_len
);
457 /* Cull the ones that are not the earliest */
460 len
= tree_entry_len(e
);
461 if (name_compare(e
->path
, len
, first
, first_len
))
466 /* Now we have in entry[i] the earliest name from the trees */
469 for (i
= 0; i
< n
; i
++) {
473 if (S_ISDIR(entry
[i
].mode
))
479 interesting
= prune_traversal(istate
, e
, info
, &base
, interesting
);
483 trees_used
= info
->fn(n
, mask
, dirmask
, entry
, info
);
484 if (trees_used
< 0) {
486 if (!info
->show_all_errors
)
491 for (i
= 0; i
< n
; i
++)
492 if (mask
& (1ul << i
))
493 update_extended_entry(tx
+ i
, entry
+ i
);
496 for (i
= 0; i
< n
; i
++)
497 free_extended_entry(tx
+ i
);
500 info
->traverse_path
= NULL
;
501 strbuf_release(&base
);
508 struct object_id oid
;
511 static int find_tree_entry(struct repository
*r
, struct tree_desc
*t
,
512 const char *name
, struct object_id
*result
,
513 unsigned short *mode
)
515 int namelen
= strlen(name
);
518 struct object_id oid
;
521 oidcpy(&oid
, tree_entry_extract(t
, &entry
, mode
));
522 entrylen
= tree_entry_len(&t
->entry
);
523 update_tree_entry(t
);
524 if (entrylen
> namelen
)
526 cmp
= memcmp(name
, entry
, entrylen
);
531 if (entrylen
== namelen
) {
532 oidcpy(result
, &oid
);
535 if (name
[entrylen
] != '/')
539 if (++entrylen
== namelen
) {
540 oidcpy(result
, &oid
);
543 return get_tree_entry(r
, &oid
, name
+ entrylen
, result
, mode
);
548 int get_tree_entry(struct repository
*r
,
549 const struct object_id
*tree_oid
,
551 struct object_id
*oid
,
552 unsigned short *mode
)
557 struct object_id root
;
559 tree
= read_object_with_reference(r
, tree_oid
, tree_type
, &size
, &root
);
563 if (name
[0] == '\0') {
573 init_tree_desc(&t
, tree
, size
);
574 retval
= find_tree_entry(r
, &t
, name
, oid
, mode
);
581 * This is Linux's built-in max for the number of symlinks to follow.
582 * That limit, of course, does not affect git, but it's a reasonable
585 #define GET_TREE_ENTRY_FOLLOW_SYMLINKS_MAX_LINKS 40
588 * Find a tree entry by following symlinks in tree_sha (which is
589 * assumed to be the root of the repository). In the event that a
590 * symlink points outside the repository (e.g. a link to /foo or a
591 * root-level link to ../foo), the portion of the link which is
592 * outside the repository will be returned in result_path, and *mode
593 * will be set to 0. It is assumed that result_path is uninitialized.
594 * If there are no symlinks, or the end result of the symlink chain
595 * points to an object inside the repository, result will be filled in
596 * with the sha1 of the found object, and *mode will hold the mode of
599 * See the code for enum get_oid_result for a description of
602 enum get_oid_result
get_tree_entry_follow_symlinks(struct repository
*r
,
603 struct object_id
*tree_oid
, const char *name
,
604 struct object_id
*result
, struct strbuf
*result_path
,
605 unsigned short *mode
)
607 int retval
= MISSING_OBJECT
;
608 struct dir_state
*parents
= NULL
;
609 size_t parents_alloc
= 0;
610 size_t i
, parents_nr
= 0;
611 struct object_id current_tree_oid
;
612 struct strbuf namebuf
= STRBUF_INIT
;
614 int follows_remaining
= GET_TREE_ENTRY_FOLLOW_SYMLINKS_MAX_LINKS
;
616 init_tree_desc(&t
, NULL
, 0UL);
617 strbuf_addstr(&namebuf
, name
);
618 oidcpy(¤t_tree_oid
, tree_oid
);
623 char *remainder
= NULL
;
627 struct object_id root
;
629 tree
= read_object_with_reference(r
,
636 ALLOC_GROW(parents
, parents_nr
+ 1, parents_alloc
);
637 parents
[parents_nr
].tree
= tree
;
638 parents
[parents_nr
].size
= size
;
639 oidcpy(&parents
[parents_nr
].oid
, &root
);
642 if (namebuf
.buf
[0] == '\0') {
643 oidcpy(result
, &root
);
652 init_tree_desc(&t
, tree
, size
);
655 /* Handle symlinks to e.g. a//b by removing leading slashes */
656 while (namebuf
.buf
[0] == '/') {
657 strbuf_remove(&namebuf
, 0, 1);
660 /* Split namebuf into a first component and a remainder */
661 if ((first_slash
= strchr(namebuf
.buf
, '/'))) {
663 remainder
= first_slash
+ 1;
666 if (!strcmp(namebuf
.buf
, "..")) {
667 struct dir_state
*parent
;
669 * We could end up with .. in the namebuf if it
670 * appears in a symlink.
673 if (parents_nr
== 1) {
676 strbuf_add(result_path
, namebuf
.buf
,
682 parent
= &parents
[parents_nr
- 1];
685 parent
= &parents
[parents_nr
- 1];
686 init_tree_desc(&t
, parent
->tree
, parent
->size
);
687 strbuf_remove(&namebuf
, 0, remainder
? 3 : 2);
691 /* We could end up here via a symlink to dir/.. */
692 if (namebuf
.buf
[0] == '\0') {
693 oidcpy(result
, &parents
[parents_nr
- 1].oid
);
698 /* Look up the first (or only) path component in the tree. */
699 find_result
= find_tree_entry(r
, &t
, namebuf
.buf
,
700 ¤t_tree_oid
, mode
);
705 if (S_ISDIR(*mode
)) {
707 oidcpy(result
, ¤t_tree_oid
);
711 /* Descend the tree */
713 strbuf_remove(&namebuf
, 0,
714 1 + first_slash
- namebuf
.buf
);
715 } else if (S_ISREG(*mode
)) {
717 oidcpy(result
, ¤t_tree_oid
);
723 } else if (S_ISLNK(*mode
)) {
724 /* Follow a symlink */
725 unsigned long link_len
;
727 char *contents
, *contents_start
;
728 struct dir_state
*parent
;
729 enum object_type type
;
731 if (follows_remaining
-- == 0) {
732 /* Too many symlinks followed */
733 retval
= SYMLINK_LOOP
;
738 * At this point, we have followed at a least
739 * one symlink, so on error we need to report this.
741 retval
= DANGLING_SYMLINK
;
743 contents
= repo_read_object_file(r
,
744 ¤t_tree_oid
, &type
,
750 if (contents
[0] == '/') {
751 strbuf_addstr(result_path
, contents
);
759 len
= first_slash
- namebuf
.buf
;
763 contents_start
= contents
;
765 parent
= &parents
[parents_nr
- 1];
766 init_tree_desc(&t
, parent
->tree
, parent
->size
);
767 strbuf_splice(&namebuf
, 0, len
,
768 contents_start
, link_len
);
770 namebuf
.buf
[link_len
] = '/';
775 for (i
= 0; i
< parents_nr
; i
++)
776 free(parents
[i
].tree
);
779 strbuf_release(&namebuf
);
783 static int match_entry(const struct pathspec_item
*item
,
784 const struct name_entry
*entry
, int pathlen
,
785 const char *match
, int matchlen
,
786 enum interesting
*never_interesting
)
788 int m
= -1; /* signals that we haven't called strncmp() */
790 if (item
->magic
& PATHSPEC_ICASE
)
792 * "Never interesting" trick requires exact
793 * matching. We could do something clever with inexact
794 * matching, but it's trickier (and not to forget that
795 * strcasecmp is locale-dependent, at least in
796 * glibc). Just disable it for now. It can't be worse
797 * than the wildcard's codepath of '[Tt][Hi][Is][Ss]'
800 *never_interesting
= entry_not_interesting
;
801 else if (*never_interesting
!= entry_not_interesting
) {
803 * We have not seen any match that sorts later
804 * than the current path.
808 * Does match sort strictly earlier than path
809 * with their common parts?
811 m
= strncmp(match
, entry
->path
,
812 (matchlen
< pathlen
) ? matchlen
: pathlen
);
817 * If we come here even once, that means there is at
818 * least one pathspec that would sort equal to or
819 * later than the path we are currently looking at.
820 * In other words, if we have never reached this point
821 * after iterating all pathspecs, it means all
822 * pathspecs are either outside of base, or inside the
823 * base but sorts strictly earlier than the current
824 * one. In either case, they will never match the
825 * subsequent entries. In such a case, we initialized
826 * the variable to -1 and that is what will be
827 * returned, allowing the caller to terminate early.
829 *never_interesting
= entry_not_interesting
;
832 if (pathlen
> matchlen
)
835 if (matchlen
> pathlen
) {
836 if (match
[pathlen
] != '/')
838 if (!S_ISDIR(entry
->mode
) && !S_ISGITLINK(entry
->mode
))
844 * we cheated and did not do strncmp(), so we do
847 m
= ps_strncmp(item
, match
, entry
->path
, pathlen
);
850 * If common part matched earlier then it is a hit,
851 * because we rejected the case where path is not a
852 * leading directory and is shorter than match.
856 * match_entry does not check if the prefix part is
857 * matched case-sensitively. If the entry is a
858 * directory and part of prefix, it'll be rematched
859 * eventually by basecmp with special treatment for
867 /* :(icase)-aware string compare */
868 static int basecmp(const struct pathspec_item
*item
,
869 const char *base
, const char *match
, int len
)
871 if (item
->magic
& PATHSPEC_ICASE
) {
872 int ret
, n
= len
> item
->prefix
? item
->prefix
: len
;
873 ret
= strncmp(base
, match
, n
);
880 return ps_strncmp(item
, base
, match
, len
);
883 static int match_dir_prefix(const struct pathspec_item
*item
,
885 const char *match
, int matchlen
)
887 if (basecmp(item
, base
, match
, matchlen
))
891 * If the base is a subdirectory of a path which
892 * was specified, all of them are interesting.
895 base
[matchlen
] == '/' ||
896 match
[matchlen
- 1] == '/')
899 /* Just a random prefix match */
904 * Perform matching on the leading non-wildcard part of
905 * pathspec. item->nowildcard_len must be greater than zero. Return
906 * non-zero if base is matched.
908 static int match_wildcard_base(const struct pathspec_item
*item
,
909 const char *base
, int baselen
,
912 const char *match
= item
->match
;
913 /* the wildcard part is not considered in this function */
914 int matchlen
= item
->nowildcard_len
;
919 * Return early if base is longer than the
920 * non-wildcard part but it does not match.
922 if (baselen
>= matchlen
) {
924 return !basecmp(item
, base
, match
, matchlen
);
928 while (dirlen
&& match
[dirlen
- 1] != '/')
932 * Return early if base is shorter than the
933 * non-wildcard part but it does not match. Note that
934 * base ends with '/' so we are sure it really matches
937 if (basecmp(item
, base
, match
, baselen
))
943 * we could have checked entry against the non-wildcard part
944 * that is not in base and does similar never_interesting
945 * optimization as in match_entry. For now just be happy with
948 return entry_interesting
;
952 * Is a tree entry interesting given the pathspec we have?
954 * Pre-condition: either baselen == base_offset (i.e. empty path)
955 * or base[baselen-1] == '/' (i.e. with trailing slash).
957 static enum interesting
do_match(struct index_state
*istate
,
958 const struct name_entry
*entry
,
959 struct strbuf
*base
, int base_offset
,
960 const struct pathspec
*ps
,
964 int pathlen
, baselen
= base
->len
- base_offset
;
965 enum interesting never_interesting
= ps
->has_wildcard
?
966 entry_not_interesting
: all_entries_not_interesting
;
978 if (!ps
->recursive
||
979 !(ps
->magic
& PATHSPEC_MAXDEPTH
) ||
981 return all_entries_interesting
;
982 return within_depth(base
->buf
+ base_offset
, baselen
,
983 !!S_ISDIR(entry
->mode
),
985 entry_interesting
: entry_not_interesting
;
988 pathlen
= tree_entry_len(entry
);
990 for (i
= ps
->nr
- 1; i
>= 0; i
--) {
991 const struct pathspec_item
*item
= ps
->items
+i
;
992 const char *match
= item
->match
;
993 const char *base_str
= base
->buf
+ base_offset
;
994 int matchlen
= item
->len
, matched
= 0;
996 if ((!exclude
&& item
->magic
& PATHSPEC_EXCLUDE
) ||
997 ( exclude
&& !(item
->magic
& PATHSPEC_EXCLUDE
)))
1000 if (baselen
>= matchlen
) {
1001 /* If it doesn't match, move along... */
1002 if (!match_dir_prefix(item
, base_str
, match
, matchlen
))
1003 goto match_wildcards
;
1005 if (!ps
->recursive
||
1006 !(ps
->magic
& PATHSPEC_MAXDEPTH
) ||
1007 ps
->max_depth
== -1) {
1008 if (!item
->attr_match_nr
)
1009 return all_entries_interesting
;
1014 if (within_depth(base_str
+ matchlen
+ 1,
1015 baselen
- matchlen
- 1,
1016 !!S_ISDIR(entry
->mode
),
1020 return entry_not_interesting
;
1023 /* Either there must be no base, or the base must match. */
1024 if (baselen
== 0 || !basecmp(item
, base_str
, match
, baselen
)) {
1025 if (match_entry(item
, entry
, pathlen
,
1026 match
+ baselen
, matchlen
- baselen
,
1027 &never_interesting
))
1030 if (item
->nowildcard_len
< item
->len
) {
1031 if (!git_fnmatch(item
, match
+ baselen
, entry
->path
,
1032 item
->nowildcard_len
- baselen
))
1036 * Match all directories. We'll try to
1037 * match files later on.
1039 if (ps
->recursive
&& S_ISDIR(entry
->mode
))
1040 return entry_interesting
;
1043 * When matching against submodules with
1044 * wildcard characters, ensure that the entry
1045 * at least matches up to the first wild
1046 * character. More accurate matching can then
1047 * be performed in the submodule itself.
1049 if (ps
->recurse_submodules
&&
1050 S_ISGITLINK(entry
->mode
) &&
1051 !ps_strncmp(item
, match
+ baselen
,
1053 item
->nowildcard_len
- baselen
))
1061 if (item
->nowildcard_len
== item
->len
)
1064 if (item
->nowildcard_len
&&
1065 !match_wildcard_base(item
, base_str
, baselen
, &matched
))
1069 * Concatenate base and entry->path into one and do
1072 * While we could avoid concatenation in certain cases
1073 * [1], which saves a memcpy and potentially a
1074 * realloc, it turns out not worth it. Measurement on
1075 * linux-2.6 does not show any clear improvements,
1076 * partly because of the nowildcard_len optimization
1077 * in git_fnmatch(). Avoid micro-optimizations here.
1079 * [1] if match_wildcard_base() says the base
1080 * directory is already matched, we only need to match
1081 * the rest, which is shorter so _in theory_ faster.
1084 strbuf_add(base
, entry
->path
, pathlen
);
1086 if (!git_fnmatch(item
, match
, base
->buf
+ base_offset
,
1087 item
->nowildcard_len
)) {
1088 strbuf_setlen(base
, base_offset
+ baselen
);
1093 * When matching against submodules with
1094 * wildcard characters, ensure that the entry
1095 * at least matches up to the first wild
1096 * character. More accurate matching can then
1097 * be performed in the submodule itself.
1099 if (ps
->recurse_submodules
&& S_ISGITLINK(entry
->mode
) &&
1100 !ps_strncmp(item
, match
, base
->buf
+ base_offset
,
1101 item
->nowildcard_len
)) {
1102 strbuf_setlen(base
, base_offset
+ baselen
);
1106 strbuf_setlen(base
, base_offset
+ baselen
);
1109 * Match all directories. We'll try to match files
1111 * max_depth is ignored but we may consider support it
1113 * https://public-inbox.org/git/7vmxo5l2g4.fsf@alter.siamese.dyndns.org/
1115 if (ps
->recursive
&& S_ISDIR(entry
->mode
))
1116 return entry_interesting
;
1119 if (item
->attr_match_nr
) {
1123 * Must not return all_entries_not_interesting
1124 * prematurely. We do not know if all entries do not
1125 * match some attributes with current attr API.
1127 never_interesting
= entry_not_interesting
;
1130 * Consider all directories interesting (because some
1131 * of those files inside may match some attributes
1132 * even though the parent dir does not)
1134 * FIXME: attributes _can_ match directories and we
1135 * can probably return all_entries_interesting or
1136 * all_entries_not_interesting here if matched.
1138 if (S_ISDIR(entry
->mode
))
1139 return entry_interesting
;
1141 strbuf_add(base
, entry
->path
, pathlen
);
1142 ret
= match_pathspec_attrs(istate
, base
->buf
+ base_offset
,
1143 base
->len
- base_offset
, item
);
1144 strbuf_setlen(base
, base_offset
+ baselen
);
1148 return entry_interesting
;
1150 return never_interesting
; /* No matches */
1154 * Is a tree entry interesting given the pathspec we have?
1156 * Pre-condition: either baselen == base_offset (i.e. empty path)
1157 * or base[baselen-1] == '/' (i.e. with trailing slash).
1159 enum interesting
tree_entry_interesting(struct index_state
*istate
,
1160 const struct name_entry
*entry
,
1161 struct strbuf
*base
, int base_offset
,
1162 const struct pathspec
*ps
)
1164 enum interesting positive
, negative
;
1165 positive
= do_match(istate
, entry
, base
, base_offset
, ps
, 0);
1168 * case | entry | positive | negative | result
1169 * -----+-------+----------+----------+-------
1170 * 1 | file | -1 | -1..2 | -1
1171 * 2 | file | 0 | -1..2 | 0
1172 * 3 | file | 1 | -1 | 1
1173 * 4 | file | 1 | 0 | 1
1174 * 5 | file | 1 | 1 | 0
1175 * 6 | file | 1 | 2 | 0
1176 * 7 | file | 2 | -1 | 2
1177 * 8 | file | 2 | 0 | 1
1178 * 9 | file | 2 | 1 | 0
1179 * 10 | file | 2 | 2 | -1
1180 * -----+-------+----------+----------+-------
1181 * 11 | dir | -1 | -1..2 | -1
1182 * 12 | dir | 0 | -1..2 | 0
1183 * 13 | dir | 1 | -1 | 1
1184 * 14 | dir | 1 | 0 | 1
1185 * 15 | dir | 1 | 1 | 1 (*)
1186 * 16 | dir | 1 | 2 | 0
1187 * 17 | dir | 2 | -1 | 2
1188 * 18 | dir | 2 | 0 | 1
1189 * 19 | dir | 2 | 1 | 1 (*)
1190 * 20 | dir | 2 | 2 | -1
1192 * (*) An exclude pattern interested in a directory does not
1193 * necessarily mean it will exclude all of the directory. In
1194 * wildcard case, it can't decide until looking at individual
1195 * files inside. So don't write such directories off yet.
1198 if (!(ps
->magic
& PATHSPEC_EXCLUDE
) ||
1199 positive
<= entry_not_interesting
) /* #1, #2, #11, #12 */
1202 negative
= do_match(istate
, entry
, base
, base_offset
, ps
, 1);
1205 if (positive
== all_entries_interesting
&&
1206 negative
== entry_not_interesting
)
1207 return entry_interesting
;
1209 /* #3, #4, #7, #13, #14, #17 */
1210 if (negative
<= entry_not_interesting
)
1214 if (S_ISDIR(entry
->mode
) &&
1215 positive
>= entry_interesting
&&
1216 negative
== entry_interesting
)
1217 return entry_interesting
;
1219 if ((positive
== entry_interesting
&&
1220 negative
>= entry_interesting
) || /* #5, #6, #16 */
1221 (positive
== all_entries_interesting
&&
1222 negative
== entry_interesting
)) /* #9 */
1223 return entry_not_interesting
;
1225 return all_entries_not_interesting
; /* #10, #20 */