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 tree_desc
*desc
, const struct object_id
*oid
)
92 unsigned long size
= 0;
96 buf
= read_object_with_reference(oid
, tree_type
, &size
, NULL
);
98 die("unable to read tree %s", oid_to_hex(oid
));
100 init_tree_desc(desc
, buf
, size
);
104 static void entry_clear(struct name_entry
*a
)
106 memset(a
, 0, sizeof(*a
));
109 static void entry_extract(struct tree_desc
*t
, struct name_entry
*a
)
114 static int update_tree_entry_internal(struct tree_desc
*desc
, struct strbuf
*err
)
116 const void *buf
= desc
->buffer
;
117 const unsigned char *end
= (const unsigned char *)desc
->entry
.path
+ desc
->entry
.pathlen
+ 1 + the_hash_algo
->rawsz
;
118 unsigned long size
= desc
->size
;
119 unsigned long len
= end
- (const unsigned char *)buf
;
122 die(_("too-short tree file"));
128 return decode_tree_entry(desc
, buf
, size
, err
);
132 void update_tree_entry(struct tree_desc
*desc
)
134 struct strbuf err
= STRBUF_INIT
;
135 if (update_tree_entry_internal(desc
, &err
))
137 strbuf_release(&err
);
140 int update_tree_entry_gently(struct tree_desc
*desc
)
142 struct strbuf err
= STRBUF_INIT
;
143 if (update_tree_entry_internal(desc
, &err
)) {
144 error("%s", err
.buf
);
145 strbuf_release(&err
);
146 /* Stop processing this tree after error */
150 strbuf_release(&err
);
154 int tree_entry(struct tree_desc
*desc
, struct name_entry
*entry
)
159 *entry
= desc
->entry
;
160 update_tree_entry(desc
);
164 int tree_entry_gently(struct tree_desc
*desc
, struct name_entry
*entry
)
169 *entry
= desc
->entry
;
170 if (update_tree_entry_gently(desc
))
175 void setup_traverse_info(struct traverse_info
*info
, const char *base
)
177 int pathlen
= strlen(base
);
178 static struct traverse_info dummy
;
180 memset(info
, 0, sizeof(*info
));
181 if (pathlen
&& base
[pathlen
-1] == '/')
183 info
->pathlen
= pathlen
? pathlen
+ 1 : 0;
184 info
->name
.path
= base
;
185 info
->name
.pathlen
= pathlen
;
187 hashcpy(info
->name
.oid
.hash
, (const unsigned char *)base
+ pathlen
+ 1);
192 char *make_traverse_path(char *path
, const struct traverse_info
*info
, const struct name_entry
*n
)
194 int len
= tree_entry_len(n
);
195 int pathlen
= info
->pathlen
;
197 path
[pathlen
+ len
] = 0;
199 memcpy(path
+ pathlen
, n
->path
, len
);
202 path
[--pathlen
] = '/';
204 len
= tree_entry_len(n
);
211 struct tree_desc_skip
{
212 struct tree_desc_skip
*prev
;
218 struct tree_desc_skip
*skip
;
221 static int check_entry_match(const char *a
, int a_len
, const char *b
, int b_len
)
224 * The caller wants to pick *a* from a tree or nothing.
225 * We are looking at *b* in a tree.
227 * (0) If a and b are the same name, we are trivially happy.
229 * There are three possibilities where *a* could be hiding
232 * (1) *a* == "t", *b* == "ab" i.e. *b* sorts earlier than *a* no
234 * (2) *a* == "t", *b* == "t-2" and "t" is a subtree in the tree;
235 * (3) *a* == "t-2", *b* == "t" and "t-2" is a blob in the tree.
237 * Otherwise we know *a* won't appear in the tree without
241 int cmp
= name_compare(a
, a_len
, b
, b_len
);
243 /* Most common case first -- reading sync'd trees */
248 /* a comes after b; it does not matter if it is case (3)
249 if (b_len < a_len && !memcmp(a, b, b_len) && a[b_len] < '/')
252 return 1; /* keep looking */
255 /* b comes after a; are we looking at case (2)? */
256 if (a_len
< b_len
&& !memcmp(a
, b
, a_len
) && b
[a_len
] < '/')
257 return 1; /* keep looking */
259 return -1; /* a cannot appear in the tree */
263 * From the extended tree_desc, extract the first name entry, while
264 * paying attention to the candidate "first" name. Most importantly,
265 * when looking for an entry, if there are entries that sorts earlier
266 * in the tree object representation than that name, skip them and
267 * process the named entry first. We will remember that we haven't
268 * processed the first entry yet, and in the later call skip the
269 * entry we processed early when update_extended_entry() is called.
271 * E.g. if the underlying tree object has these entries:
278 * and the "first" asks for "t", remember that we still need to
279 * process "t-1" and "t-2" but extract "t". After processing the
280 * entry "t" from this call, the caller will let us know by calling
281 * update_extended_entry() that we can remember "t" has been processed
285 static void extended_entry_extract(struct tree_desc_x
*t
,
286 struct name_entry
*a
,
292 struct tree_desc probe
;
293 struct tree_desc_skip
*skip
;
296 * Extract the first entry from the tree_desc, but skip the
297 * ones that we already returned in earlier rounds.
302 break; /* not found */
304 entry_extract(&t
->d
, a
);
305 for (skip
= t
->skip
; skip
; skip
= skip
->prev
)
306 if (a
->path
== skip
->ptr
)
310 /* We have processed this entry already. */
311 update_tree_entry(&t
->d
);
314 if (!first
|| !a
->path
)
318 * The caller wants "first" from this tree, or nothing.
321 len
= tree_entry_len(a
);
322 switch (check_entry_match(first
, first_len
, path
, len
)) {
332 * We need to look-ahead -- we suspect that a subtree whose
333 * name is "first" may be hiding behind the current entry "path".
337 entry_extract(&probe
, a
);
339 len
= tree_entry_len(a
);
340 switch (check_entry_match(first
, first_len
, path
, len
)) {
346 update_tree_entry(&probe
);
354 static void update_extended_entry(struct tree_desc_x
*t
, struct name_entry
*a
)
356 if (t
->d
.entry
.path
== a
->path
) {
357 update_tree_entry(&t
->d
);
359 /* we have returned this entry early */
360 struct tree_desc_skip
*skip
= xmalloc(sizeof(*skip
));
362 skip
->prev
= t
->skip
;
367 static void free_extended_entry(struct tree_desc_x
*t
)
369 struct tree_desc_skip
*p
, *s
;
371 for (s
= t
->skip
; s
; s
= p
) {
377 static inline int prune_traversal(struct index_state
*istate
,
378 struct name_entry
*e
,
379 struct traverse_info
*info
,
381 int still_interesting
)
383 if (!info
->pathspec
|| still_interesting
== 2)
385 if (still_interesting
< 0)
386 return still_interesting
;
387 return tree_entry_interesting(istate
, e
, base
,
391 int traverse_trees(struct index_state
*istate
,
392 int n
, struct tree_desc
*t
,
393 struct traverse_info
*info
)
396 struct name_entry
*entry
= xmalloc(n
*sizeof(*entry
));
398 struct tree_desc_x
*tx
= xcalloc(n
, sizeof(*tx
));
399 struct strbuf base
= STRBUF_INIT
;
403 for (i
= 0; i
< n
; i
++)
407 strbuf_grow(&base
, info
->pathlen
);
408 make_traverse_path(base
.buf
, info
->prev
, &info
->name
);
409 base
.buf
[info
->pathlen
-1] = '/';
410 strbuf_setlen(&base
, info
->pathlen
);
411 traverse_path
= xstrndup(base
.buf
, info
->pathlen
);
413 traverse_path
= xstrndup(info
->name
.path
, info
->pathlen
);
415 info
->traverse_path
= traverse_path
;
418 unsigned long mask
, dirmask
;
419 const char *first
= NULL
;
421 struct name_entry
*e
= NULL
;
424 for (i
= 0; i
< n
; i
++) {
426 extended_entry_extract(tx
+ i
, e
, NULL
, 0);
430 * A tree may have "t-2" at the current location even
431 * though it may have "t" that is a subtree behind it,
432 * and another tree may return "t". We want to grab
433 * all "t" from all trees to match in such a case.
435 for (i
= 0; i
< n
; i
++) {
439 len
= tree_entry_len(e
);
445 if (name_compare(e
->path
, len
, first
, first_len
) < 0) {
452 for (i
= 0; i
< n
; i
++) {
454 extended_entry_extract(tx
+ i
, e
, first
, first_len
);
455 /* Cull the ones that are not the earliest */
458 len
= tree_entry_len(e
);
459 if (name_compare(e
->path
, len
, first
, first_len
))
464 /* Now we have in entry[i] the earliest name from the trees */
467 for (i
= 0; i
< n
; i
++) {
471 if (S_ISDIR(entry
[i
].mode
))
477 interesting
= prune_traversal(istate
, e
, info
, &base
, interesting
);
481 trees_used
= info
->fn(n
, mask
, dirmask
, entry
, info
);
482 if (trees_used
< 0) {
484 if (!info
->show_all_errors
)
489 for (i
= 0; i
< n
; i
++)
490 if (mask
& (1ul << i
))
491 update_extended_entry(tx
+ i
, entry
+ i
);
494 for (i
= 0; i
< n
; i
++)
495 free_extended_entry(tx
+ i
);
498 info
->traverse_path
= NULL
;
499 strbuf_release(&base
);
506 struct object_id oid
;
509 static int find_tree_entry(struct tree_desc
*t
, const char *name
, struct object_id
*result
, unsigned *mode
)
511 int namelen
= strlen(name
);
514 struct object_id oid
;
517 oidcpy(&oid
, tree_entry_extract(t
, &entry
, mode
));
518 entrylen
= tree_entry_len(&t
->entry
);
519 update_tree_entry(t
);
520 if (entrylen
> namelen
)
522 cmp
= memcmp(name
, entry
, entrylen
);
527 if (entrylen
== namelen
) {
528 oidcpy(result
, &oid
);
531 if (name
[entrylen
] != '/')
535 if (++entrylen
== namelen
) {
536 oidcpy(result
, &oid
);
539 return get_tree_entry(&oid
, name
+ entrylen
, result
, mode
);
544 int get_tree_entry(const struct object_id
*tree_oid
, const char *name
, struct object_id
*oid
, unsigned *mode
)
549 struct object_id root
;
551 tree
= read_object_with_reference(tree_oid
, tree_type
, &size
, &root
);
555 if (name
[0] == '\0') {
565 init_tree_desc(&t
, tree
, size
);
566 retval
= find_tree_entry(&t
, name
, oid
, mode
);
573 * This is Linux's built-in max for the number of symlinks to follow.
574 * That limit, of course, does not affect git, but it's a reasonable
577 #define GET_TREE_ENTRY_FOLLOW_SYMLINKS_MAX_LINKS 40
580 * Find a tree entry by following symlinks in tree_sha (which is
581 * assumed to be the root of the repository). In the event that a
582 * symlink points outside the repository (e.g. a link to /foo or a
583 * root-level link to ../foo), the portion of the link which is
584 * outside the repository will be returned in result_path, and *mode
585 * will be set to 0. It is assumed that result_path is uninitialized.
586 * If there are no symlinks, or the end result of the symlink chain
587 * points to an object inside the repository, result will be filled in
588 * with the sha1 of the found object, and *mode will hold the mode of
591 * See the code for enum get_oid_result for a description of
594 enum get_oid_result
get_tree_entry_follow_symlinks(struct object_id
*tree_oid
, const char *name
, struct object_id
*result
, struct strbuf
*result_path
, unsigned *mode
)
596 int retval
= MISSING_OBJECT
;
597 struct dir_state
*parents
= NULL
;
598 size_t parents_alloc
= 0;
599 size_t i
, parents_nr
= 0;
600 struct object_id current_tree_oid
;
601 struct strbuf namebuf
= STRBUF_INIT
;
603 int follows_remaining
= GET_TREE_ENTRY_FOLLOW_SYMLINKS_MAX_LINKS
;
605 init_tree_desc(&t
, NULL
, 0UL);
606 strbuf_addstr(&namebuf
, name
);
607 oidcpy(¤t_tree_oid
, tree_oid
);
612 char *remainder
= NULL
;
616 struct object_id root
;
618 tree
= read_object_with_reference(¤t_tree_oid
,
624 ALLOC_GROW(parents
, parents_nr
+ 1, parents_alloc
);
625 parents
[parents_nr
].tree
= tree
;
626 parents
[parents_nr
].size
= size
;
627 oidcpy(&parents
[parents_nr
].oid
, &root
);
630 if (namebuf
.buf
[0] == '\0') {
631 oidcpy(result
, &root
);
640 init_tree_desc(&t
, tree
, size
);
643 /* Handle symlinks to e.g. a//b by removing leading slashes */
644 while (namebuf
.buf
[0] == '/') {
645 strbuf_remove(&namebuf
, 0, 1);
648 /* Split namebuf into a first component and a remainder */
649 if ((first_slash
= strchr(namebuf
.buf
, '/'))) {
651 remainder
= first_slash
+ 1;
654 if (!strcmp(namebuf
.buf
, "..")) {
655 struct dir_state
*parent
;
657 * We could end up with .. in the namebuf if it
658 * appears in a symlink.
661 if (parents_nr
== 1) {
664 strbuf_add(result_path
, namebuf
.buf
,
670 parent
= &parents
[parents_nr
- 1];
673 parent
= &parents
[parents_nr
- 1];
674 init_tree_desc(&t
, parent
->tree
, parent
->size
);
675 strbuf_remove(&namebuf
, 0, remainder
? 3 : 2);
679 /* We could end up here via a symlink to dir/.. */
680 if (namebuf
.buf
[0] == '\0') {
681 oidcpy(result
, &parents
[parents_nr
- 1].oid
);
686 /* Look up the first (or only) path component in the tree. */
687 find_result
= find_tree_entry(&t
, namebuf
.buf
,
688 ¤t_tree_oid
, mode
);
693 if (S_ISDIR(*mode
)) {
695 oidcpy(result
, ¤t_tree_oid
);
699 /* Descend the tree */
701 strbuf_remove(&namebuf
, 0,
702 1 + first_slash
- namebuf
.buf
);
703 } else if (S_ISREG(*mode
)) {
705 oidcpy(result
, ¤t_tree_oid
);
711 } else if (S_ISLNK(*mode
)) {
712 /* Follow a symlink */
713 unsigned long link_len
;
715 char *contents
, *contents_start
;
716 struct dir_state
*parent
;
717 enum object_type type
;
719 if (follows_remaining
-- == 0) {
720 /* Too many symlinks followed */
721 retval
= SYMLINK_LOOP
;
726 * At this point, we have followed at a least
727 * one symlink, so on error we need to report this.
729 retval
= DANGLING_SYMLINK
;
731 contents
= read_object_file(¤t_tree_oid
, &type
,
737 if (contents
[0] == '/') {
738 strbuf_addstr(result_path
, contents
);
746 len
= first_slash
- namebuf
.buf
;
750 contents_start
= contents
;
752 parent
= &parents
[parents_nr
- 1];
753 init_tree_desc(&t
, parent
->tree
, parent
->size
);
754 strbuf_splice(&namebuf
, 0, len
,
755 contents_start
, link_len
);
757 namebuf
.buf
[link_len
] = '/';
762 for (i
= 0; i
< parents_nr
; i
++)
763 free(parents
[i
].tree
);
766 strbuf_release(&namebuf
);
770 static int match_entry(const struct pathspec_item
*item
,
771 const struct name_entry
*entry
, int pathlen
,
772 const char *match
, int matchlen
,
773 enum interesting
*never_interesting
)
775 int m
= -1; /* signals that we haven't called strncmp() */
777 if (item
->magic
& PATHSPEC_ICASE
)
779 * "Never interesting" trick requires exact
780 * matching. We could do something clever with inexact
781 * matching, but it's trickier (and not to forget that
782 * strcasecmp is locale-dependent, at least in
783 * glibc). Just disable it for now. It can't be worse
784 * than the wildcard's codepath of '[Tt][Hi][Is][Ss]'
787 *never_interesting
= entry_not_interesting
;
788 else if (*never_interesting
!= entry_not_interesting
) {
790 * We have not seen any match that sorts later
791 * than the current path.
795 * Does match sort strictly earlier than path
796 * with their common parts?
798 m
= strncmp(match
, entry
->path
,
799 (matchlen
< pathlen
) ? matchlen
: pathlen
);
804 * If we come here even once, that means there is at
805 * least one pathspec that would sort equal to or
806 * later than the path we are currently looking at.
807 * In other words, if we have never reached this point
808 * after iterating all pathspecs, it means all
809 * pathspecs are either outside of base, or inside the
810 * base but sorts strictly earlier than the current
811 * one. In either case, they will never match the
812 * subsequent entries. In such a case, we initialized
813 * the variable to -1 and that is what will be
814 * returned, allowing the caller to terminate early.
816 *never_interesting
= entry_not_interesting
;
819 if (pathlen
> matchlen
)
822 if (matchlen
> pathlen
) {
823 if (match
[pathlen
] != '/')
825 if (!S_ISDIR(entry
->mode
) && !S_ISGITLINK(entry
->mode
))
831 * we cheated and did not do strncmp(), so we do
834 m
= ps_strncmp(item
, match
, entry
->path
, pathlen
);
837 * If common part matched earlier then it is a hit,
838 * because we rejected the case where path is not a
839 * leading directory and is shorter than match.
843 * match_entry does not check if the prefix part is
844 * matched case-sensitively. If the entry is a
845 * directory and part of prefix, it'll be rematched
846 * eventually by basecmp with special treatment for
854 /* :(icase)-aware string compare */
855 static int basecmp(const struct pathspec_item
*item
,
856 const char *base
, const char *match
, int len
)
858 if (item
->magic
& PATHSPEC_ICASE
) {
859 int ret
, n
= len
> item
->prefix
? item
->prefix
: len
;
860 ret
= strncmp(base
, match
, n
);
867 return ps_strncmp(item
, base
, match
, len
);
870 static int match_dir_prefix(const struct pathspec_item
*item
,
872 const char *match
, int matchlen
)
874 if (basecmp(item
, base
, match
, matchlen
))
878 * If the base is a subdirectory of a path which
879 * was specified, all of them are interesting.
882 base
[matchlen
] == '/' ||
883 match
[matchlen
- 1] == '/')
886 /* Just a random prefix match */
891 * Perform matching on the leading non-wildcard part of
892 * pathspec. item->nowildcard_len must be greater than zero. Return
893 * non-zero if base is matched.
895 static int match_wildcard_base(const struct pathspec_item
*item
,
896 const char *base
, int baselen
,
899 const char *match
= item
->match
;
900 /* the wildcard part is not considered in this function */
901 int matchlen
= item
->nowildcard_len
;
906 * Return early if base is longer than the
907 * non-wildcard part but it does not match.
909 if (baselen
>= matchlen
) {
911 return !basecmp(item
, base
, match
, matchlen
);
915 while (dirlen
&& match
[dirlen
- 1] != '/')
919 * Return early if base is shorter than the
920 * non-wildcard part but it does not match. Note that
921 * base ends with '/' so we are sure it really matches
924 if (basecmp(item
, base
, match
, baselen
))
930 * we could have checked entry against the non-wildcard part
931 * that is not in base and does similar never_interesting
932 * optimization as in match_entry. For now just be happy with
935 return entry_interesting
;
939 * Is a tree entry interesting given the pathspec we have?
941 * Pre-condition: either baselen == base_offset (i.e. empty path)
942 * or base[baselen-1] == '/' (i.e. with trailing slash).
944 static enum interesting
do_match(struct index_state
*istate
,
945 const struct name_entry
*entry
,
946 struct strbuf
*base
, int base_offset
,
947 const struct pathspec
*ps
,
951 int pathlen
, baselen
= base
->len
- base_offset
;
952 enum interesting never_interesting
= ps
->has_wildcard
?
953 entry_not_interesting
: all_entries_not_interesting
;
965 if (!ps
->recursive
||
966 !(ps
->magic
& PATHSPEC_MAXDEPTH
) ||
968 return all_entries_interesting
;
969 return within_depth(base
->buf
+ base_offset
, baselen
,
970 !!S_ISDIR(entry
->mode
),
972 entry_interesting
: entry_not_interesting
;
975 pathlen
= tree_entry_len(entry
);
977 for (i
= ps
->nr
- 1; i
>= 0; i
--) {
978 const struct pathspec_item
*item
= ps
->items
+i
;
979 const char *match
= item
->match
;
980 const char *base_str
= base
->buf
+ base_offset
;
981 int matchlen
= item
->len
, matched
= 0;
983 if ((!exclude
&& item
->magic
& PATHSPEC_EXCLUDE
) ||
984 ( exclude
&& !(item
->magic
& PATHSPEC_EXCLUDE
)))
987 if (baselen
>= matchlen
) {
988 /* If it doesn't match, move along... */
989 if (!match_dir_prefix(item
, base_str
, match
, matchlen
))
990 goto match_wildcards
;
992 if (!ps
->recursive
||
993 !(ps
->magic
& PATHSPEC_MAXDEPTH
) ||
994 ps
->max_depth
== -1) {
995 if (!item
->attr_match_nr
)
996 return all_entries_interesting
;
1001 if (within_depth(base_str
+ matchlen
+ 1,
1002 baselen
- matchlen
- 1,
1003 !!S_ISDIR(entry
->mode
),
1007 return entry_not_interesting
;
1010 /* Either there must be no base, or the base must match. */
1011 if (baselen
== 0 || !basecmp(item
, base_str
, match
, baselen
)) {
1012 if (match_entry(item
, entry
, pathlen
,
1013 match
+ baselen
, matchlen
- baselen
,
1014 &never_interesting
))
1017 if (item
->nowildcard_len
< item
->len
) {
1018 if (!git_fnmatch(item
, match
+ baselen
, entry
->path
,
1019 item
->nowildcard_len
- baselen
))
1023 * Match all directories. We'll try to
1024 * match files later on.
1026 if (ps
->recursive
&& S_ISDIR(entry
->mode
))
1027 return entry_interesting
;
1030 * When matching against submodules with
1031 * wildcard characters, ensure that the entry
1032 * at least matches up to the first wild
1033 * character. More accurate matching can then
1034 * be performed in the submodule itself.
1036 if (ps
->recurse_submodules
&&
1037 S_ISGITLINK(entry
->mode
) &&
1038 !ps_strncmp(item
, match
+ baselen
,
1040 item
->nowildcard_len
- baselen
))
1048 if (item
->nowildcard_len
== item
->len
)
1051 if (item
->nowildcard_len
&&
1052 !match_wildcard_base(item
, base_str
, baselen
, &matched
))
1056 * Concatenate base and entry->path into one and do
1059 * While we could avoid concatenation in certain cases
1060 * [1], which saves a memcpy and potentially a
1061 * realloc, it turns out not worth it. Measurement on
1062 * linux-2.6 does not show any clear improvements,
1063 * partly because of the nowildcard_len optimization
1064 * in git_fnmatch(). Avoid micro-optimizations here.
1066 * [1] if match_wildcard_base() says the base
1067 * directory is already matched, we only need to match
1068 * the rest, which is shorter so _in theory_ faster.
1071 strbuf_add(base
, entry
->path
, pathlen
);
1073 if (!git_fnmatch(item
, match
, base
->buf
+ base_offset
,
1074 item
->nowildcard_len
)) {
1075 strbuf_setlen(base
, base_offset
+ baselen
);
1080 * When matching against submodules with
1081 * wildcard characters, ensure that the entry
1082 * at least matches up to the first wild
1083 * character. More accurate matching can then
1084 * be performed in the submodule itself.
1086 if (ps
->recurse_submodules
&& S_ISGITLINK(entry
->mode
) &&
1087 !ps_strncmp(item
, match
, base
->buf
+ base_offset
,
1088 item
->nowildcard_len
)) {
1089 strbuf_setlen(base
, base_offset
+ baselen
);
1093 strbuf_setlen(base
, base_offset
+ baselen
);
1096 * Match all directories. We'll try to match files
1098 * max_depth is ignored but we may consider support it
1100 * https://public-inbox.org/git/7vmxo5l2g4.fsf@alter.siamese.dyndns.org/
1102 if (ps
->recursive
&& S_ISDIR(entry
->mode
))
1103 return entry_interesting
;
1106 if (item
->attr_match_nr
) {
1110 * Must not return all_entries_not_interesting
1111 * prematurely. We do not know if all entries do not
1112 * match some attributes with current attr API.
1114 never_interesting
= entry_not_interesting
;
1117 * Consider all directories interesting (because some
1118 * of those files inside may match some attributes
1119 * even though the parent dir does not)
1121 * FIXME: attributes _can_ match directories and we
1122 * can probably return all_entries_interesting or
1123 * all_entries_not_interesting here if matched.
1125 if (S_ISDIR(entry
->mode
))
1126 return entry_interesting
;
1128 strbuf_add(base
, entry
->path
, pathlen
);
1129 ret
= match_pathspec_attrs(istate
, base
->buf
+ base_offset
,
1130 base
->len
- base_offset
, item
);
1131 strbuf_setlen(base
, base_offset
+ baselen
);
1135 return entry_interesting
;
1137 return never_interesting
; /* No matches */
1141 * Is a tree entry interesting given the pathspec we have?
1143 * Pre-condition: either baselen == base_offset (i.e. empty path)
1144 * or base[baselen-1] == '/' (i.e. with trailing slash).
1146 enum interesting
tree_entry_interesting(struct index_state
*istate
,
1147 const struct name_entry
*entry
,
1148 struct strbuf
*base
, int base_offset
,
1149 const struct pathspec
*ps
)
1151 enum interesting positive
, negative
;
1152 positive
= do_match(istate
, entry
, base
, base_offset
, ps
, 0);
1155 * case | entry | positive | negative | result
1156 * -----+-------+----------+----------+-------
1157 * 1 | file | -1 | -1..2 | -1
1158 * 2 | file | 0 | -1..2 | 0
1159 * 3 | file | 1 | -1 | 1
1160 * 4 | file | 1 | 0 | 1
1161 * 5 | file | 1 | 1 | 0
1162 * 6 | file | 1 | 2 | 0
1163 * 7 | file | 2 | -1 | 2
1164 * 8 | file | 2 | 0 | 1
1165 * 9 | file | 2 | 1 | 0
1166 * 10 | file | 2 | 2 | -1
1167 * -----+-------+----------+----------+-------
1168 * 11 | dir | -1 | -1..2 | -1
1169 * 12 | dir | 0 | -1..2 | 0
1170 * 13 | dir | 1 | -1 | 1
1171 * 14 | dir | 1 | 0 | 1
1172 * 15 | dir | 1 | 1 | 1 (*)
1173 * 16 | dir | 1 | 2 | 0
1174 * 17 | dir | 2 | -1 | 2
1175 * 18 | dir | 2 | 0 | 1
1176 * 19 | dir | 2 | 1 | 1 (*)
1177 * 20 | dir | 2 | 2 | -1
1179 * (*) An exclude pattern interested in a directory does not
1180 * necessarily mean it will exclude all of the directory. In
1181 * wildcard case, it can't decide until looking at individual
1182 * files inside. So don't write such directories off yet.
1185 if (!(ps
->magic
& PATHSPEC_EXCLUDE
) ||
1186 positive
<= entry_not_interesting
) /* #1, #2, #11, #12 */
1189 negative
= do_match(istate
, entry
, base
, base_offset
, ps
, 1);
1192 if (positive
== all_entries_interesting
&&
1193 negative
== entry_not_interesting
)
1194 return entry_interesting
;
1196 /* #3, #4, #7, #13, #14, #17 */
1197 if (negative
<= entry_not_interesting
)
1201 if (S_ISDIR(entry
->mode
) &&
1202 positive
>= entry_interesting
&&
1203 negative
== entry_interesting
)
1204 return entry_interesting
;
1206 if ((positive
== entry_interesting
&&
1207 negative
>= entry_interesting
) || /* #5, #6, #16 */
1208 (positive
== all_entries_interesting
&&
1209 negative
== entry_interesting
)) /* #9 */
1210 return entry_not_interesting
;
1212 return all_entries_not_interesting
; /* #10, #20 */