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 size_t 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;
187 info
->namelen
= pathlen
;
192 char *make_traverse_path(char *path
, size_t pathlen
,
193 const struct traverse_info
*info
,
194 const char *name
, size_t namelen
)
196 /* Always points to the end of the name we're about to add */
197 size_t pos
= st_add(info
->pathlen
, namelen
);
200 BUG("too small buffer passed to make_traverse_path");
205 BUG("traverse_info pathlen does not match strings");
207 memcpy(path
+ pos
, name
, namelen
);
214 BUG("traverse_info ran out of list items");
216 namelen
= info
->namelen
;
222 void strbuf_make_traverse_path(struct strbuf
*out
,
223 const struct traverse_info
*info
,
224 const char *name
, size_t namelen
)
226 size_t len
= traverse_path_len(info
, namelen
);
228 strbuf_grow(out
, len
);
229 make_traverse_path(out
->buf
+ out
->len
, out
->alloc
- out
->len
,
230 info
, name
, namelen
);
231 strbuf_setlen(out
, out
->len
+ len
);
234 struct tree_desc_skip
{
235 struct tree_desc_skip
*prev
;
241 struct tree_desc_skip
*skip
;
244 static int check_entry_match(const char *a
, int a_len
, const char *b
, int b_len
)
247 * The caller wants to pick *a* from a tree or nothing.
248 * We are looking at *b* in a tree.
250 * (0) If a and b are the same name, we are trivially happy.
252 * There are three possibilities where *a* could be hiding
255 * (1) *a* == "t", *b* == "ab" i.e. *b* sorts earlier than *a* no
257 * (2) *a* == "t", *b* == "t-2" and "t" is a subtree in the tree;
258 * (3) *a* == "t-2", *b* == "t" and "t-2" is a blob in the tree.
260 * Otherwise we know *a* won't appear in the tree without
264 int cmp
= name_compare(a
, a_len
, b
, b_len
);
266 /* Most common case first -- reading sync'd trees */
271 /* a comes after b; it does not matter if it is case (3)
272 if (b_len < a_len && !memcmp(a, b, b_len) && a[b_len] < '/')
275 return 1; /* keep looking */
278 /* b comes after a; are we looking at case (2)? */
279 if (a_len
< b_len
&& !memcmp(a
, b
, a_len
) && b
[a_len
] < '/')
280 return 1; /* keep looking */
282 return -1; /* a cannot appear in the tree */
286 * From the extended tree_desc, extract the first name entry, while
287 * paying attention to the candidate "first" name. Most importantly,
288 * when looking for an entry, if there are entries that sorts earlier
289 * in the tree object representation than that name, skip them and
290 * process the named entry first. We will remember that we haven't
291 * processed the first entry yet, and in the later call skip the
292 * entry we processed early when update_extended_entry() is called.
294 * E.g. if the underlying tree object has these entries:
301 * and the "first" asks for "t", remember that we still need to
302 * process "t-1" and "t-2" but extract "t". After processing the
303 * entry "t" from this call, the caller will let us know by calling
304 * update_extended_entry() that we can remember "t" has been processed
308 static void extended_entry_extract(struct tree_desc_x
*t
,
309 struct name_entry
*a
,
315 struct tree_desc probe
;
316 struct tree_desc_skip
*skip
;
319 * Extract the first entry from the tree_desc, but skip the
320 * ones that we already returned in earlier rounds.
325 break; /* not found */
327 entry_extract(&t
->d
, a
);
328 for (skip
= t
->skip
; skip
; skip
= skip
->prev
)
329 if (a
->path
== skip
->ptr
)
333 /* We have processed this entry already. */
334 update_tree_entry(&t
->d
);
337 if (!first
|| !a
->path
)
341 * The caller wants "first" from this tree, or nothing.
344 len
= tree_entry_len(a
);
345 switch (check_entry_match(first
, first_len
, path
, len
)) {
355 * We need to look-ahead -- we suspect that a subtree whose
356 * name is "first" may be hiding behind the current entry "path".
360 entry_extract(&probe
, a
);
362 len
= tree_entry_len(a
);
363 switch (check_entry_match(first
, first_len
, path
, len
)) {
369 update_tree_entry(&probe
);
377 static void update_extended_entry(struct tree_desc_x
*t
, struct name_entry
*a
)
379 if (t
->d
.entry
.path
== a
->path
) {
380 update_tree_entry(&t
->d
);
382 /* we have returned this entry early */
383 struct tree_desc_skip
*skip
= xmalloc(sizeof(*skip
));
385 skip
->prev
= t
->skip
;
390 static void free_extended_entry(struct tree_desc_x
*t
)
392 struct tree_desc_skip
*p
, *s
;
394 for (s
= t
->skip
; s
; s
= p
) {
400 static inline int prune_traversal(struct index_state
*istate
,
401 struct name_entry
*e
,
402 struct traverse_info
*info
,
404 int still_interesting
)
406 if (!info
->pathspec
|| still_interesting
== 2)
408 if (still_interesting
< 0)
409 return still_interesting
;
410 return tree_entry_interesting(istate
, e
, base
,
414 int traverse_trees(struct index_state
*istate
,
415 int n
, struct tree_desc
*t
,
416 struct traverse_info
*info
)
419 struct name_entry
*entry
= xmalloc(n
*sizeof(*entry
));
421 struct tree_desc_x
*tx
= xcalloc(n
, sizeof(*tx
));
422 struct strbuf base
= STRBUF_INIT
;
426 for (i
= 0; i
< n
; i
++)
430 strbuf_make_traverse_path(&base
, info
->prev
,
431 info
->name
, info
->namelen
);
432 strbuf_addch(&base
, '/');
433 traverse_path
= xstrndup(base
.buf
, base
.len
);
435 traverse_path
= xstrndup(info
->name
, info
->pathlen
);
437 info
->traverse_path
= traverse_path
;
440 unsigned long mask
, dirmask
;
441 const char *first
= NULL
;
443 struct name_entry
*e
= NULL
;
446 for (i
= 0; i
< n
; i
++) {
448 extended_entry_extract(tx
+ i
, e
, NULL
, 0);
452 * A tree may have "t-2" at the current location even
453 * though it may have "t" that is a subtree behind it,
454 * and another tree may return "t". We want to grab
455 * all "t" from all trees to match in such a case.
457 for (i
= 0; i
< n
; i
++) {
461 len
= tree_entry_len(e
);
467 if (name_compare(e
->path
, len
, first
, first_len
) < 0) {
474 for (i
= 0; i
< n
; i
++) {
476 extended_entry_extract(tx
+ i
, e
, first
, first_len
);
477 /* Cull the ones that are not the earliest */
480 len
= tree_entry_len(e
);
481 if (name_compare(e
->path
, len
, first
, first_len
))
486 /* Now we have in entry[i] the earliest name from the trees */
489 for (i
= 0; i
< n
; i
++) {
493 if (S_ISDIR(entry
[i
].mode
))
499 interesting
= prune_traversal(istate
, e
, info
, &base
, interesting
);
503 trees_used
= info
->fn(n
, mask
, dirmask
, entry
, info
);
504 if (trees_used
< 0) {
506 if (!info
->show_all_errors
)
511 for (i
= 0; i
< n
; i
++)
512 if (mask
& (1ul << i
))
513 update_extended_entry(tx
+ i
, entry
+ i
);
516 for (i
= 0; i
< n
; i
++)
517 free_extended_entry(tx
+ i
);
520 info
->traverse_path
= NULL
;
521 strbuf_release(&base
);
528 struct object_id oid
;
531 static int find_tree_entry(struct repository
*r
, struct tree_desc
*t
,
532 const char *name
, struct object_id
*result
,
533 unsigned short *mode
)
535 int namelen
= strlen(name
);
538 struct object_id oid
;
541 oidcpy(&oid
, tree_entry_extract(t
, &entry
, mode
));
542 entrylen
= tree_entry_len(&t
->entry
);
543 update_tree_entry(t
);
544 if (entrylen
> namelen
)
546 cmp
= memcmp(name
, entry
, entrylen
);
551 if (entrylen
== namelen
) {
552 oidcpy(result
, &oid
);
555 if (name
[entrylen
] != '/')
559 if (++entrylen
== namelen
) {
560 oidcpy(result
, &oid
);
563 return get_tree_entry(r
, &oid
, name
+ entrylen
, result
, mode
);
568 int get_tree_entry(struct repository
*r
,
569 const struct object_id
*tree_oid
,
571 struct object_id
*oid
,
572 unsigned short *mode
)
577 struct object_id root
;
579 tree
= read_object_with_reference(r
, tree_oid
, tree_type
, &size
, &root
);
583 if (name
[0] == '\0') {
593 init_tree_desc(&t
, tree
, size
);
594 retval
= find_tree_entry(r
, &t
, name
, oid
, mode
);
601 * This is Linux's built-in max for the number of symlinks to follow.
602 * That limit, of course, does not affect git, but it's a reasonable
605 #define GET_TREE_ENTRY_FOLLOW_SYMLINKS_MAX_LINKS 40
608 * Find a tree entry by following symlinks in tree_sha (which is
609 * assumed to be the root of the repository). In the event that a
610 * symlink points outside the repository (e.g. a link to /foo or a
611 * root-level link to ../foo), the portion of the link which is
612 * outside the repository will be returned in result_path, and *mode
613 * will be set to 0. It is assumed that result_path is uninitialized.
614 * If there are no symlinks, or the end result of the symlink chain
615 * points to an object inside the repository, result will be filled in
616 * with the sha1 of the found object, and *mode will hold the mode of
619 * See the code for enum get_oid_result for a description of
622 enum get_oid_result
get_tree_entry_follow_symlinks(struct repository
*r
,
623 struct object_id
*tree_oid
, const char *name
,
624 struct object_id
*result
, struct strbuf
*result_path
,
625 unsigned short *mode
)
627 int retval
= MISSING_OBJECT
;
628 struct dir_state
*parents
= NULL
;
629 size_t parents_alloc
= 0;
630 size_t i
, parents_nr
= 0;
631 struct object_id current_tree_oid
;
632 struct strbuf namebuf
= STRBUF_INIT
;
634 int follows_remaining
= GET_TREE_ENTRY_FOLLOW_SYMLINKS_MAX_LINKS
;
636 init_tree_desc(&t
, NULL
, 0UL);
637 strbuf_addstr(&namebuf
, name
);
638 oidcpy(¤t_tree_oid
, tree_oid
);
643 char *remainder
= NULL
;
647 struct object_id root
;
649 tree
= read_object_with_reference(r
,
656 ALLOC_GROW(parents
, parents_nr
+ 1, parents_alloc
);
657 parents
[parents_nr
].tree
= tree
;
658 parents
[parents_nr
].size
= size
;
659 oidcpy(&parents
[parents_nr
].oid
, &root
);
662 if (namebuf
.buf
[0] == '\0') {
663 oidcpy(result
, &root
);
672 init_tree_desc(&t
, tree
, size
);
675 /* Handle symlinks to e.g. a//b by removing leading slashes */
676 while (namebuf
.buf
[0] == '/') {
677 strbuf_remove(&namebuf
, 0, 1);
680 /* Split namebuf into a first component and a remainder */
681 if ((first_slash
= strchr(namebuf
.buf
, '/'))) {
683 remainder
= first_slash
+ 1;
686 if (!strcmp(namebuf
.buf
, "..")) {
687 struct dir_state
*parent
;
689 * We could end up with .. in the namebuf if it
690 * appears in a symlink.
693 if (parents_nr
== 1) {
696 strbuf_add(result_path
, namebuf
.buf
,
702 parent
= &parents
[parents_nr
- 1];
705 parent
= &parents
[parents_nr
- 1];
706 init_tree_desc(&t
, parent
->tree
, parent
->size
);
707 strbuf_remove(&namebuf
, 0, remainder
? 3 : 2);
711 /* We could end up here via a symlink to dir/.. */
712 if (namebuf
.buf
[0] == '\0') {
713 oidcpy(result
, &parents
[parents_nr
- 1].oid
);
718 /* Look up the first (or only) path component in the tree. */
719 find_result
= find_tree_entry(r
, &t
, namebuf
.buf
,
720 ¤t_tree_oid
, mode
);
725 if (S_ISDIR(*mode
)) {
727 oidcpy(result
, ¤t_tree_oid
);
731 /* Descend the tree */
733 strbuf_remove(&namebuf
, 0,
734 1 + first_slash
- namebuf
.buf
);
735 } else if (S_ISREG(*mode
)) {
737 oidcpy(result
, ¤t_tree_oid
);
743 } else if (S_ISLNK(*mode
)) {
744 /* Follow a symlink */
745 unsigned long link_len
;
747 char *contents
, *contents_start
;
748 struct dir_state
*parent
;
749 enum object_type type
;
751 if (follows_remaining
-- == 0) {
752 /* Too many symlinks followed */
753 retval
= SYMLINK_LOOP
;
758 * At this point, we have followed at a least
759 * one symlink, so on error we need to report this.
761 retval
= DANGLING_SYMLINK
;
763 contents
= repo_read_object_file(r
,
764 ¤t_tree_oid
, &type
,
770 if (contents
[0] == '/') {
771 strbuf_addstr(result_path
, contents
);
779 len
= first_slash
- namebuf
.buf
;
783 contents_start
= contents
;
785 parent
= &parents
[parents_nr
- 1];
786 init_tree_desc(&t
, parent
->tree
, parent
->size
);
787 strbuf_splice(&namebuf
, 0, len
,
788 contents_start
, link_len
);
790 namebuf
.buf
[link_len
] = '/';
795 for (i
= 0; i
< parents_nr
; i
++)
796 free(parents
[i
].tree
);
799 strbuf_release(&namebuf
);
803 static int match_entry(const struct pathspec_item
*item
,
804 const struct name_entry
*entry
, int pathlen
,
805 const char *match
, int matchlen
,
806 enum interesting
*never_interesting
)
808 int m
= -1; /* signals that we haven't called strncmp() */
810 if (item
->magic
& PATHSPEC_ICASE
)
812 * "Never interesting" trick requires exact
813 * matching. We could do something clever with inexact
814 * matching, but it's trickier (and not to forget that
815 * strcasecmp is locale-dependent, at least in
816 * glibc). Just disable it for now. It can't be worse
817 * than the wildcard's codepath of '[Tt][Hi][Is][Ss]'
820 *never_interesting
= entry_not_interesting
;
821 else if (*never_interesting
!= entry_not_interesting
) {
823 * We have not seen any match that sorts later
824 * than the current path.
828 * Does match sort strictly earlier than path
829 * with their common parts?
831 m
= strncmp(match
, entry
->path
,
832 (matchlen
< pathlen
) ? matchlen
: pathlen
);
837 * If we come here even once, that means there is at
838 * least one pathspec that would sort equal to or
839 * later than the path we are currently looking at.
840 * In other words, if we have never reached this point
841 * after iterating all pathspecs, it means all
842 * pathspecs are either outside of base, or inside the
843 * base but sorts strictly earlier than the current
844 * one. In either case, they will never match the
845 * subsequent entries. In such a case, we initialized
846 * the variable to -1 and that is what will be
847 * returned, allowing the caller to terminate early.
849 *never_interesting
= entry_not_interesting
;
852 if (pathlen
> matchlen
)
855 if (matchlen
> pathlen
) {
856 if (match
[pathlen
] != '/')
858 if (!S_ISDIR(entry
->mode
) && !S_ISGITLINK(entry
->mode
))
864 * we cheated and did not do strncmp(), so we do
867 m
= ps_strncmp(item
, match
, entry
->path
, pathlen
);
870 * If common part matched earlier then it is a hit,
871 * because we rejected the case where path is not a
872 * leading directory and is shorter than match.
876 * match_entry does not check if the prefix part is
877 * matched case-sensitively. If the entry is a
878 * directory and part of prefix, it'll be rematched
879 * eventually by basecmp with special treatment for
887 /* :(icase)-aware string compare */
888 static int basecmp(const struct pathspec_item
*item
,
889 const char *base
, const char *match
, int len
)
891 if (item
->magic
& PATHSPEC_ICASE
) {
892 int ret
, n
= len
> item
->prefix
? item
->prefix
: len
;
893 ret
= strncmp(base
, match
, n
);
900 return ps_strncmp(item
, base
, match
, len
);
903 static int match_dir_prefix(const struct pathspec_item
*item
,
905 const char *match
, int matchlen
)
907 if (basecmp(item
, base
, match
, matchlen
))
911 * If the base is a subdirectory of a path which
912 * was specified, all of them are interesting.
915 base
[matchlen
] == '/' ||
916 match
[matchlen
- 1] == '/')
919 /* Just a random prefix match */
924 * Perform matching on the leading non-wildcard part of
925 * pathspec. item->nowildcard_len must be greater than zero. Return
926 * non-zero if base is matched.
928 static int match_wildcard_base(const struct pathspec_item
*item
,
929 const char *base
, int baselen
,
932 const char *match
= item
->match
;
933 /* the wildcard part is not considered in this function */
934 int matchlen
= item
->nowildcard_len
;
939 * Return early if base is longer than the
940 * non-wildcard part but it does not match.
942 if (baselen
>= matchlen
) {
944 return !basecmp(item
, base
, match
, matchlen
);
948 while (dirlen
&& match
[dirlen
- 1] != '/')
952 * Return early if base is shorter than the
953 * non-wildcard part but it does not match. Note that
954 * base ends with '/' so we are sure it really matches
957 if (basecmp(item
, base
, match
, baselen
))
963 * we could have checked entry against the non-wildcard part
964 * that is not in base and does similar never_interesting
965 * optimization as in match_entry. For now just be happy with
968 return entry_interesting
;
972 * Is a tree entry interesting given the pathspec we have?
974 * Pre-condition: either baselen == base_offset (i.e. empty path)
975 * or base[baselen-1] == '/' (i.e. with trailing slash).
977 static enum interesting
do_match(struct index_state
*istate
,
978 const struct name_entry
*entry
,
979 struct strbuf
*base
, int base_offset
,
980 const struct pathspec
*ps
,
984 int pathlen
, baselen
= base
->len
- base_offset
;
985 enum interesting never_interesting
= ps
->has_wildcard
?
986 entry_not_interesting
: all_entries_not_interesting
;
998 if (!ps
->recursive
||
999 !(ps
->magic
& PATHSPEC_MAXDEPTH
) ||
1000 ps
->max_depth
== -1)
1001 return all_entries_interesting
;
1002 return within_depth(base
->buf
+ base_offset
, baselen
,
1003 !!S_ISDIR(entry
->mode
),
1005 entry_interesting
: entry_not_interesting
;
1008 pathlen
= tree_entry_len(entry
);
1010 for (i
= ps
->nr
- 1; i
>= 0; i
--) {
1011 const struct pathspec_item
*item
= ps
->items
+i
;
1012 const char *match
= item
->match
;
1013 const char *base_str
= base
->buf
+ base_offset
;
1014 int matchlen
= item
->len
, matched
= 0;
1016 if ((!exclude
&& item
->magic
& PATHSPEC_EXCLUDE
) ||
1017 ( exclude
&& !(item
->magic
& PATHSPEC_EXCLUDE
)))
1020 if (baselen
>= matchlen
) {
1021 /* If it doesn't match, move along... */
1022 if (!match_dir_prefix(item
, base_str
, match
, matchlen
))
1023 goto match_wildcards
;
1025 if (!ps
->recursive
||
1026 !(ps
->magic
& PATHSPEC_MAXDEPTH
) ||
1027 ps
->max_depth
== -1) {
1028 if (!item
->attr_match_nr
)
1029 return all_entries_interesting
;
1034 if (within_depth(base_str
+ matchlen
+ 1,
1035 baselen
- matchlen
- 1,
1036 !!S_ISDIR(entry
->mode
),
1040 return entry_not_interesting
;
1043 /* Either there must be no base, or the base must match. */
1044 if (baselen
== 0 || !basecmp(item
, base_str
, match
, baselen
)) {
1045 if (match_entry(item
, entry
, pathlen
,
1046 match
+ baselen
, matchlen
- baselen
,
1047 &never_interesting
))
1050 if (item
->nowildcard_len
< item
->len
) {
1051 if (!git_fnmatch(item
, match
+ baselen
, entry
->path
,
1052 item
->nowildcard_len
- baselen
))
1056 * Match all directories. We'll try to
1057 * match files later on.
1059 if (ps
->recursive
&& S_ISDIR(entry
->mode
))
1060 return entry_interesting
;
1063 * When matching against submodules with
1064 * wildcard characters, ensure that the entry
1065 * at least matches up to the first wild
1066 * character. More accurate matching can then
1067 * be performed in the submodule itself.
1069 if (ps
->recurse_submodules
&&
1070 S_ISGITLINK(entry
->mode
) &&
1071 !ps_strncmp(item
, match
+ baselen
,
1073 item
->nowildcard_len
- baselen
))
1081 if (item
->nowildcard_len
== item
->len
)
1084 if (item
->nowildcard_len
&&
1085 !match_wildcard_base(item
, base_str
, baselen
, &matched
))
1089 * Concatenate base and entry->path into one and do
1092 * While we could avoid concatenation in certain cases
1093 * [1], which saves a memcpy and potentially a
1094 * realloc, it turns out not worth it. Measurement on
1095 * linux-2.6 does not show any clear improvements,
1096 * partly because of the nowildcard_len optimization
1097 * in git_fnmatch(). Avoid micro-optimizations here.
1099 * [1] if match_wildcard_base() says the base
1100 * directory is already matched, we only need to match
1101 * the rest, which is shorter so _in theory_ faster.
1104 strbuf_add(base
, entry
->path
, pathlen
);
1106 if (!git_fnmatch(item
, match
, base
->buf
+ base_offset
,
1107 item
->nowildcard_len
)) {
1108 strbuf_setlen(base
, base_offset
+ baselen
);
1113 * When matching against submodules with
1114 * wildcard characters, ensure that the entry
1115 * at least matches up to the first wild
1116 * character. More accurate matching can then
1117 * be performed in the submodule itself.
1119 if (ps
->recurse_submodules
&& S_ISGITLINK(entry
->mode
) &&
1120 !ps_strncmp(item
, match
, base
->buf
+ base_offset
,
1121 item
->nowildcard_len
)) {
1122 strbuf_setlen(base
, base_offset
+ baselen
);
1126 strbuf_setlen(base
, base_offset
+ baselen
);
1129 * Match all directories. We'll try to match files
1131 * max_depth is ignored but we may consider support it
1133 * https://lore.kernel.org/git/7vmxo5l2g4.fsf@alter.siamese.dyndns.org/
1135 if (ps
->recursive
&& S_ISDIR(entry
->mode
))
1136 return entry_interesting
;
1139 if (item
->attr_match_nr
) {
1143 * Must not return all_entries_not_interesting
1144 * prematurely. We do not know if all entries do not
1145 * match some attributes with current attr API.
1147 never_interesting
= entry_not_interesting
;
1150 * Consider all directories interesting (because some
1151 * of those files inside may match some attributes
1152 * even though the parent dir does not)
1154 * FIXME: attributes _can_ match directories and we
1155 * can probably return all_entries_interesting or
1156 * all_entries_not_interesting here if matched.
1158 if (S_ISDIR(entry
->mode
))
1159 return entry_interesting
;
1161 strbuf_add(base
, entry
->path
, pathlen
);
1162 ret
= match_pathspec_attrs(istate
, base
->buf
+ base_offset
,
1163 base
->len
- base_offset
, item
);
1164 strbuf_setlen(base
, base_offset
+ baselen
);
1168 return entry_interesting
;
1170 return never_interesting
; /* No matches */
1174 * Is a tree entry interesting given the pathspec we have?
1176 * Pre-condition: either baselen == base_offset (i.e. empty path)
1177 * or base[baselen-1] == '/' (i.e. with trailing slash).
1179 enum interesting
tree_entry_interesting(struct index_state
*istate
,
1180 const struct name_entry
*entry
,
1181 struct strbuf
*base
, int base_offset
,
1182 const struct pathspec
*ps
)
1184 enum interesting positive
, negative
;
1185 positive
= do_match(istate
, entry
, base
, base_offset
, ps
, 0);
1188 * case | entry | positive | negative | result
1189 * -----+-------+----------+----------+-------
1190 * 1 | file | -1 | -1..2 | -1
1191 * 2 | file | 0 | -1..2 | 0
1192 * 3 | file | 1 | -1 | 1
1193 * 4 | file | 1 | 0 | 1
1194 * 5 | file | 1 | 1 | 0
1195 * 6 | file | 1 | 2 | 0
1196 * 7 | file | 2 | -1 | 2
1197 * 8 | file | 2 | 0 | 1
1198 * 9 | file | 2 | 1 | 0
1199 * 10 | file | 2 | 2 | -1
1200 * -----+-------+----------+----------+-------
1201 * 11 | dir | -1 | -1..2 | -1
1202 * 12 | dir | 0 | -1..2 | 0
1203 * 13 | dir | 1 | -1 | 1
1204 * 14 | dir | 1 | 0 | 1
1205 * 15 | dir | 1 | 1 | 1 (*)
1206 * 16 | dir | 1 | 2 | 0
1207 * 17 | dir | 2 | -1 | 2
1208 * 18 | dir | 2 | 0 | 1
1209 * 19 | dir | 2 | 1 | 1 (*)
1210 * 20 | dir | 2 | 2 | -1
1212 * (*) An exclude pattern interested in a directory does not
1213 * necessarily mean it will exclude all of the directory. In
1214 * wildcard case, it can't decide until looking at individual
1215 * files inside. So don't write such directories off yet.
1218 if (!(ps
->magic
& PATHSPEC_EXCLUDE
) ||
1219 positive
<= entry_not_interesting
) /* #1, #2, #11, #12 */
1222 negative
= do_match(istate
, entry
, base
, base_offset
, ps
, 1);
1225 if (positive
== all_entries_interesting
&&
1226 negative
== entry_not_interesting
)
1227 return entry_interesting
;
1229 /* #3, #4, #7, #13, #14, #17 */
1230 if (negative
<= entry_not_interesting
)
1234 if (S_ISDIR(entry
->mode
) &&
1235 positive
>= entry_interesting
&&
1236 negative
== entry_interesting
)
1237 return entry_interesting
;
1239 if ((positive
== entry_interesting
&&
1240 negative
>= entry_interesting
) || /* #5, #6, #16 */
1241 (positive
== all_entries_interesting
&&
1242 negative
== entry_interesting
)) /* #9 */
1243 return entry_not_interesting
;
1245 return all_entries_not_interesting
; /* #10, #20 */