4 #include "object-store.h"
7 #include "json-writer.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 len
= strlen(path
) + 1;
48 /* Initialize the descriptor entry */
49 desc
->entry
.path
= path
;
50 desc
->entry
.mode
= canon_mode(mode
);
51 desc
->entry
.pathlen
= len
- 1;
52 oidread(&desc
->entry
.oid
, (const unsigned char *)path
+ len
);
57 static int init_tree_desc_internal(struct tree_desc
*desc
, const void *buffer
, unsigned long size
, struct strbuf
*err
)
59 desc
->buffer
= buffer
;
62 return decode_tree_entry(desc
, buffer
, size
, err
);
66 void init_tree_desc(struct tree_desc
*desc
, const void *buffer
, unsigned long size
)
68 struct strbuf err
= STRBUF_INIT
;
69 if (init_tree_desc_internal(desc
, buffer
, size
, &err
))
74 int init_tree_desc_gently(struct tree_desc
*desc
, const void *buffer
, unsigned long size
)
76 struct strbuf err
= STRBUF_INIT
;
77 int result
= init_tree_desc_internal(desc
, buffer
, size
, &err
);
84 void *fill_tree_descriptor(struct repository
*r
,
85 struct tree_desc
*desc
,
86 const struct object_id
*oid
)
88 unsigned long size
= 0;
92 buf
= read_object_with_reference(r
, oid
, tree_type
, &size
, NULL
);
94 die("unable to read tree %s", oid_to_hex(oid
));
96 init_tree_desc(desc
, buf
, size
);
100 static void entry_clear(struct name_entry
*a
)
102 memset(a
, 0, sizeof(*a
));
105 static void entry_extract(struct tree_desc
*t
, struct name_entry
*a
)
110 static int update_tree_entry_internal(struct tree_desc
*desc
, struct strbuf
*err
)
112 const void *buf
= desc
->buffer
;
113 const unsigned char *end
= (const unsigned char *)desc
->entry
.path
+ desc
->entry
.pathlen
+ 1 + the_hash_algo
->rawsz
;
114 unsigned long size
= desc
->size
;
115 unsigned long len
= end
- (const unsigned char *)buf
;
118 die(_("too-short tree file"));
124 return decode_tree_entry(desc
, buf
, size
, err
);
128 void update_tree_entry(struct tree_desc
*desc
)
130 struct strbuf err
= STRBUF_INIT
;
131 if (update_tree_entry_internal(desc
, &err
))
133 strbuf_release(&err
);
136 int update_tree_entry_gently(struct tree_desc
*desc
)
138 struct strbuf err
= STRBUF_INIT
;
139 if (update_tree_entry_internal(desc
, &err
)) {
140 error("%s", err
.buf
);
141 strbuf_release(&err
);
142 /* Stop processing this tree after error */
146 strbuf_release(&err
);
150 int tree_entry(struct tree_desc
*desc
, struct name_entry
*entry
)
155 *entry
= desc
->entry
;
156 update_tree_entry(desc
);
160 int tree_entry_gently(struct tree_desc
*desc
, struct name_entry
*entry
)
165 *entry
= desc
->entry
;
166 if (update_tree_entry_gently(desc
))
171 static int traverse_trees_atexit_registered
;
172 static int traverse_trees_count
;
173 static int traverse_trees_cur_depth
;
174 static int traverse_trees_max_depth
;
176 static void trace2_traverse_trees_statistics_atexit(void)
178 struct json_writer jw
= JSON_WRITER_INIT
;
180 jw_object_begin(&jw
, 0);
181 jw_object_intmax(&jw
, "traverse_trees_count", traverse_trees_count
);
182 jw_object_intmax(&jw
, "traverse_trees_max_depth", traverse_trees_max_depth
);
185 trace2_data_json("traverse_trees", the_repository
, "statistics", &jw
);
190 void setup_traverse_info(struct traverse_info
*info
, const char *base
)
192 size_t pathlen
= strlen(base
);
193 static struct traverse_info dummy
;
195 memset(info
, 0, sizeof(*info
));
196 if (pathlen
&& base
[pathlen
-1] == '/')
198 info
->pathlen
= pathlen
? pathlen
+ 1 : 0;
200 info
->namelen
= pathlen
;
204 if (trace2_is_enabled() && !traverse_trees_atexit_registered
) {
205 atexit(trace2_traverse_trees_statistics_atexit
);
206 traverse_trees_atexit_registered
= 1;
210 char *make_traverse_path(char *path
, size_t pathlen
,
211 const struct traverse_info
*info
,
212 const char *name
, size_t namelen
)
214 /* Always points to the end of the name we're about to add */
215 size_t pos
= st_add(info
->pathlen
, namelen
);
218 BUG("too small buffer passed to make_traverse_path");
223 BUG("traverse_info pathlen does not match strings");
225 memcpy(path
+ pos
, name
, namelen
);
232 BUG("traverse_info ran out of list items");
234 namelen
= info
->namelen
;
240 void strbuf_make_traverse_path(struct strbuf
*out
,
241 const struct traverse_info
*info
,
242 const char *name
, size_t namelen
)
244 size_t len
= traverse_path_len(info
, namelen
);
246 strbuf_grow(out
, len
);
247 make_traverse_path(out
->buf
+ out
->len
, out
->alloc
- out
->len
,
248 info
, name
, namelen
);
249 strbuf_setlen(out
, out
->len
+ len
);
252 struct tree_desc_skip
{
253 struct tree_desc_skip
*prev
;
259 struct tree_desc_skip
*skip
;
262 static int check_entry_match(const char *a
, int a_len
, const char *b
, int b_len
)
265 * The caller wants to pick *a* from a tree or nothing.
266 * We are looking at *b* in a tree.
268 * (0) If a and b are the same name, we are trivially happy.
270 * There are three possibilities where *a* could be hiding
273 * (1) *a* == "t", *b* == "ab" i.e. *b* sorts earlier than *a* no
275 * (2) *a* == "t", *b* == "t-2" and "t" is a subtree in the tree;
276 * (3) *a* == "t-2", *b* == "t" and "t-2" is a blob in the tree.
278 * Otherwise we know *a* won't appear in the tree without
282 int cmp
= name_compare(a
, a_len
, b
, b_len
);
284 /* Most common case first -- reading sync'd trees */
289 /* a comes after b; it does not matter if it is case (3)
290 if (b_len < a_len && !memcmp(a, b, b_len) && a[b_len] < '/')
293 return 1; /* keep looking */
296 /* b comes after a; are we looking at case (2)? */
297 if (a_len
< b_len
&& !memcmp(a
, b
, a_len
) && b
[a_len
] < '/')
298 return 1; /* keep looking */
300 return -1; /* a cannot appear in the tree */
304 * From the extended tree_desc, extract the first name entry, while
305 * paying attention to the candidate "first" name. Most importantly,
306 * when looking for an entry, if there are entries that sorts earlier
307 * in the tree object representation than that name, skip them and
308 * process the named entry first. We will remember that we haven't
309 * processed the first entry yet, and in the later call skip the
310 * entry we processed early when update_extended_entry() is called.
312 * E.g. if the underlying tree object has these entries:
319 * and the "first" asks for "t", remember that we still need to
320 * process "t-1" and "t-2" but extract "t". After processing the
321 * entry "t" from this call, the caller will let us know by calling
322 * update_extended_entry() that we can remember "t" has been processed
326 static void extended_entry_extract(struct tree_desc_x
*t
,
327 struct name_entry
*a
,
333 struct tree_desc probe
;
334 struct tree_desc_skip
*skip
;
337 * Extract the first entry from the tree_desc, but skip the
338 * ones that we already returned in earlier rounds.
343 break; /* not found */
345 entry_extract(&t
->d
, a
);
346 for (skip
= t
->skip
; skip
; skip
= skip
->prev
)
347 if (a
->path
== skip
->ptr
)
351 /* We have processed this entry already. */
352 update_tree_entry(&t
->d
);
355 if (!first
|| !a
->path
)
359 * The caller wants "first" from this tree, or nothing.
362 len
= tree_entry_len(a
);
363 switch (check_entry_match(first
, first_len
, path
, len
)) {
373 * We need to look-ahead -- we suspect that a subtree whose
374 * name is "first" may be hiding behind the current entry "path".
378 entry_extract(&probe
, a
);
380 len
= tree_entry_len(a
);
381 switch (check_entry_match(first
, first_len
, path
, len
)) {
387 update_tree_entry(&probe
);
395 static void update_extended_entry(struct tree_desc_x
*t
, struct name_entry
*a
)
397 if (t
->d
.entry
.path
== a
->path
) {
398 update_tree_entry(&t
->d
);
400 /* we have returned this entry early */
401 struct tree_desc_skip
*skip
= xmalloc(sizeof(*skip
));
403 skip
->prev
= t
->skip
;
408 static void free_extended_entry(struct tree_desc_x
*t
)
410 struct tree_desc_skip
*p
, *s
;
412 for (s
= t
->skip
; s
; s
= p
) {
418 static inline int prune_traversal(struct index_state
*istate
,
419 struct name_entry
*e
,
420 struct traverse_info
*info
,
422 int still_interesting
)
424 if (!info
->pathspec
|| still_interesting
== 2)
426 if (still_interesting
< 0)
427 return still_interesting
;
428 return tree_entry_interesting(istate
, e
, base
,
432 int traverse_trees(struct index_state
*istate
,
433 int n
, struct tree_desc
*t
,
434 struct traverse_info
*info
)
437 struct name_entry entry
[MAX_TRAVERSE_TREES
];
439 struct tree_desc_x tx
[ARRAY_SIZE(entry
)];
440 struct strbuf base
= STRBUF_INIT
;
444 traverse_trees_count
++;
445 traverse_trees_cur_depth
++;
447 if (traverse_trees_cur_depth
> traverse_trees_max_depth
)
448 traverse_trees_max_depth
= traverse_trees_cur_depth
;
450 if (n
>= ARRAY_SIZE(entry
))
451 BUG("traverse_trees() called with too many trees (%d)", n
);
453 for (i
= 0; i
< n
; i
++) {
459 strbuf_make_traverse_path(&base
, info
->prev
,
460 info
->name
, info
->namelen
);
461 strbuf_addch(&base
, '/');
462 traverse_path
= xstrndup(base
.buf
, base
.len
);
464 traverse_path
= xstrndup(info
->name
, info
->pathlen
);
466 info
->traverse_path
= traverse_path
;
469 unsigned long mask
, dirmask
;
470 const char *first
= NULL
;
472 struct name_entry
*e
= NULL
;
475 for (i
= 0; i
< n
; i
++) {
477 extended_entry_extract(tx
+ i
, e
, NULL
, 0);
481 * A tree may have "t-2" at the current location even
482 * though it may have "t" that is a subtree behind it,
483 * and another tree may return "t". We want to grab
484 * all "t" from all trees to match in such a case.
486 for (i
= 0; i
< n
; i
++) {
490 len
= tree_entry_len(e
);
496 if (name_compare(e
->path
, len
, first
, first_len
) < 0) {
503 for (i
= 0; i
< n
; i
++) {
505 extended_entry_extract(tx
+ i
, e
, first
, first_len
);
506 /* Cull the ones that are not the earliest */
509 len
= tree_entry_len(e
);
510 if (name_compare(e
->path
, len
, first
, first_len
))
515 /* Now we have in entry[i] the earliest name from the trees */
518 for (i
= 0; i
< n
; i
++) {
522 if (S_ISDIR(entry
[i
].mode
))
528 interesting
= prune_traversal(istate
, e
, info
, &base
, interesting
);
532 trees_used
= info
->fn(n
, mask
, dirmask
, entry
, info
);
533 if (trees_used
< 0) {
535 if (!info
->show_all_errors
)
540 for (i
= 0; i
< n
; i
++)
541 if (mask
& (1ul << i
))
542 update_extended_entry(tx
+ i
, entry
+ i
);
544 for (i
= 0; i
< n
; i
++)
545 free_extended_entry(tx
+ i
);
547 info
->traverse_path
= NULL
;
548 strbuf_release(&base
);
550 traverse_trees_cur_depth
--;
557 struct object_id oid
;
560 static int find_tree_entry(struct repository
*r
, struct tree_desc
*t
,
561 const char *name
, struct object_id
*result
,
562 unsigned short *mode
)
564 int namelen
= strlen(name
);
567 struct object_id oid
;
570 oidcpy(&oid
, tree_entry_extract(t
, &entry
, mode
));
571 entrylen
= tree_entry_len(&t
->entry
);
572 update_tree_entry(t
);
573 if (entrylen
> namelen
)
575 cmp
= memcmp(name
, entry
, entrylen
);
580 if (entrylen
== namelen
) {
581 oidcpy(result
, &oid
);
584 if (name
[entrylen
] != '/')
588 if (++entrylen
== namelen
) {
589 oidcpy(result
, &oid
);
592 return get_tree_entry(r
, &oid
, name
+ entrylen
, result
, mode
);
597 int get_tree_entry(struct repository
*r
,
598 const struct object_id
*tree_oid
,
600 struct object_id
*oid
,
601 unsigned short *mode
)
606 struct object_id root
;
608 tree
= read_object_with_reference(r
, tree_oid
, tree_type
, &size
, &root
);
612 if (name
[0] == '\0') {
622 init_tree_desc(&t
, tree
, size
);
623 retval
= find_tree_entry(r
, &t
, name
, oid
, mode
);
630 * This is Linux's built-in max for the number of symlinks to follow.
631 * That limit, of course, does not affect git, but it's a reasonable
634 #define GET_TREE_ENTRY_FOLLOW_SYMLINKS_MAX_LINKS 40
637 * Find a tree entry by following symlinks in tree_sha (which is
638 * assumed to be the root of the repository). In the event that a
639 * symlink points outside the repository (e.g. a link to /foo or a
640 * root-level link to ../foo), the portion of the link which is
641 * outside the repository will be returned in result_path, and *mode
642 * will be set to 0. It is assumed that result_path is uninitialized.
643 * If there are no symlinks, or the end result of the symlink chain
644 * points to an object inside the repository, result will be filled in
645 * with the sha1 of the found object, and *mode will hold the mode of
648 * See the code for enum get_oid_result for a description of
651 enum get_oid_result
get_tree_entry_follow_symlinks(struct repository
*r
,
652 struct object_id
*tree_oid
, const char *name
,
653 struct object_id
*result
, struct strbuf
*result_path
,
654 unsigned short *mode
)
656 int retval
= MISSING_OBJECT
;
657 struct dir_state
*parents
= NULL
;
658 size_t parents_alloc
= 0;
659 size_t i
, parents_nr
= 0;
660 struct object_id current_tree_oid
;
661 struct strbuf namebuf
= STRBUF_INIT
;
663 int follows_remaining
= GET_TREE_ENTRY_FOLLOW_SYMLINKS_MAX_LINKS
;
665 init_tree_desc(&t
, NULL
, 0UL);
666 strbuf_addstr(&namebuf
, name
);
667 oidcpy(¤t_tree_oid
, tree_oid
);
672 char *remainder
= NULL
;
676 struct object_id root
;
678 tree
= read_object_with_reference(r
,
685 ALLOC_GROW(parents
, parents_nr
+ 1, parents_alloc
);
686 parents
[parents_nr
].tree
= tree
;
687 parents
[parents_nr
].size
= size
;
688 oidcpy(&parents
[parents_nr
].oid
, &root
);
691 if (namebuf
.buf
[0] == '\0') {
692 oidcpy(result
, &root
);
701 init_tree_desc(&t
, tree
, size
);
704 /* Handle symlinks to e.g. a//b by removing leading slashes */
705 while (namebuf
.buf
[0] == '/') {
706 strbuf_remove(&namebuf
, 0, 1);
709 /* Split namebuf into a first component and a remainder */
710 if ((first_slash
= strchr(namebuf
.buf
, '/'))) {
712 remainder
= first_slash
+ 1;
715 if (!strcmp(namebuf
.buf
, "..")) {
716 struct dir_state
*parent
;
718 * We could end up with .. in the namebuf if it
719 * appears in a symlink.
722 if (parents_nr
== 1) {
725 strbuf_add(result_path
, namebuf
.buf
,
731 parent
= &parents
[parents_nr
- 1];
734 parent
= &parents
[parents_nr
- 1];
735 init_tree_desc(&t
, parent
->tree
, parent
->size
);
736 strbuf_remove(&namebuf
, 0, remainder
? 3 : 2);
740 /* We could end up here via a symlink to dir/.. */
741 if (namebuf
.buf
[0] == '\0') {
742 oidcpy(result
, &parents
[parents_nr
- 1].oid
);
747 /* Look up the first (or only) path component in the tree. */
748 find_result
= find_tree_entry(r
, &t
, namebuf
.buf
,
749 ¤t_tree_oid
, mode
);
754 if (S_ISDIR(*mode
)) {
756 oidcpy(result
, ¤t_tree_oid
);
760 /* Descend the tree */
762 strbuf_remove(&namebuf
, 0,
763 1 + first_slash
- namebuf
.buf
);
764 } else if (S_ISREG(*mode
)) {
766 oidcpy(result
, ¤t_tree_oid
);
772 } else if (S_ISLNK(*mode
)) {
773 /* Follow a symlink */
774 unsigned long link_len
;
776 char *contents
, *contents_start
;
777 struct dir_state
*parent
;
778 enum object_type type
;
780 if (follows_remaining
-- == 0) {
781 /* Too many symlinks followed */
782 retval
= SYMLINK_LOOP
;
787 * At this point, we have followed at a least
788 * one symlink, so on error we need to report this.
790 retval
= DANGLING_SYMLINK
;
792 contents
= repo_read_object_file(r
,
793 ¤t_tree_oid
, &type
,
799 if (contents
[0] == '/') {
800 strbuf_addstr(result_path
, contents
);
808 len
= first_slash
- namebuf
.buf
;
812 contents_start
= contents
;
814 parent
= &parents
[parents_nr
- 1];
815 init_tree_desc(&t
, parent
->tree
, parent
->size
);
816 strbuf_splice(&namebuf
, 0, len
,
817 contents_start
, link_len
);
819 namebuf
.buf
[link_len
] = '/';
824 for (i
= 0; i
< parents_nr
; i
++)
825 free(parents
[i
].tree
);
828 strbuf_release(&namebuf
);
832 static int match_entry(const struct pathspec_item
*item
,
833 const struct name_entry
*entry
, int pathlen
,
834 const char *match
, int matchlen
,
835 enum interesting
*never_interesting
)
837 int m
= -1; /* signals that we haven't called strncmp() */
839 if (item
->magic
& PATHSPEC_ICASE
)
841 * "Never interesting" trick requires exact
842 * matching. We could do something clever with inexact
843 * matching, but it's trickier (and not to forget that
844 * strcasecmp is locale-dependent, at least in
845 * glibc). Just disable it for now. It can't be worse
846 * than the wildcard's codepath of '[Tt][Hi][Is][Ss]'
849 *never_interesting
= entry_not_interesting
;
850 else if (*never_interesting
!= entry_not_interesting
) {
852 * We have not seen any match that sorts later
853 * than the current path.
857 * Does match sort strictly earlier than path
858 * with their common parts?
860 m
= strncmp(match
, entry
->path
,
861 (matchlen
< pathlen
) ? matchlen
: pathlen
);
866 * If we come here even once, that means there is at
867 * least one pathspec that would sort equal to or
868 * later than the path we are currently looking at.
869 * In other words, if we have never reached this point
870 * after iterating all pathspecs, it means all
871 * pathspecs are either outside of base, or inside the
872 * base but sorts strictly earlier than the current
873 * one. In either case, they will never match the
874 * subsequent entries. In such a case, we initialized
875 * the variable to -1 and that is what will be
876 * returned, allowing the caller to terminate early.
878 *never_interesting
= entry_not_interesting
;
881 if (pathlen
> matchlen
)
884 if (matchlen
> pathlen
) {
885 if (match
[pathlen
] != '/')
888 * Reject non-directories as partial pathnames, except
889 * when match is a submodule with a trailing slash and
890 * nothing else (to handle 'submod/' and 'submod'
893 if (!S_ISDIR(entry
->mode
) &&
894 (!S_ISGITLINK(entry
->mode
) || matchlen
> pathlen
+ 1))
900 * we cheated and did not do strncmp(), so we do
903 m
= ps_strncmp(item
, match
, entry
->path
, pathlen
);
906 * If common part matched earlier then it is a hit,
907 * because we rejected the case where path is not a
908 * leading directory and is shorter than match.
912 * match_entry does not check if the prefix part is
913 * matched case-sensitively. If the entry is a
914 * directory and part of prefix, it'll be rematched
915 * eventually by basecmp with special treatment for
923 /* :(icase)-aware string compare */
924 static int basecmp(const struct pathspec_item
*item
,
925 const char *base
, const char *match
, int len
)
927 if (item
->magic
& PATHSPEC_ICASE
) {
928 int ret
, n
= len
> item
->prefix
? item
->prefix
: len
;
929 ret
= strncmp(base
, match
, n
);
936 return ps_strncmp(item
, base
, match
, len
);
939 static int match_dir_prefix(const struct pathspec_item
*item
,
941 const char *match
, int matchlen
)
943 if (basecmp(item
, base
, match
, matchlen
))
947 * If the base is a subdirectory of a path which
948 * was specified, all of them are interesting.
951 base
[matchlen
] == '/' ||
952 match
[matchlen
- 1] == '/')
955 /* Just a random prefix match */
960 * Perform matching on the leading non-wildcard part of
961 * pathspec. item->nowildcard_len must be greater than zero. Return
962 * non-zero if base is matched.
964 static int match_wildcard_base(const struct pathspec_item
*item
,
965 const char *base
, int baselen
,
968 const char *match
= item
->match
;
969 /* the wildcard part is not considered in this function */
970 int matchlen
= item
->nowildcard_len
;
975 * Return early if base is longer than the
976 * non-wildcard part but it does not match.
978 if (baselen
>= matchlen
) {
980 return !basecmp(item
, base
, match
, matchlen
);
984 while (dirlen
&& match
[dirlen
- 1] != '/')
988 * Return early if base is shorter than the
989 * non-wildcard part but it does not match. Note that
990 * base ends with '/' so we are sure it really matches
993 if (basecmp(item
, base
, match
, baselen
))
999 * we could have checked entry against the non-wildcard part
1000 * that is not in base and does similar never_interesting
1001 * optimization as in match_entry. For now just be happy with
1004 return entry_interesting
;
1008 * Is a tree entry interesting given the pathspec we have?
1010 * Pre-condition: either baselen == base_offset (i.e. empty path)
1011 * or base[baselen-1] == '/' (i.e. with trailing slash).
1013 static enum interesting
do_match(struct index_state
*istate
,
1014 const struct name_entry
*entry
,
1015 struct strbuf
*base
, int base_offset
,
1016 const struct pathspec
*ps
,
1020 int pathlen
, baselen
= base
->len
- base_offset
;
1021 enum interesting never_interesting
= ps
->has_wildcard
?
1022 entry_not_interesting
: all_entries_not_interesting
;
1034 if (!ps
->recursive
||
1035 !(ps
->magic
& PATHSPEC_MAXDEPTH
) ||
1036 ps
->max_depth
== -1)
1037 return all_entries_interesting
;
1038 return within_depth(base
->buf
+ base_offset
, baselen
,
1039 !!S_ISDIR(entry
->mode
),
1041 entry_interesting
: entry_not_interesting
;
1044 pathlen
= tree_entry_len(entry
);
1046 for (i
= ps
->nr
- 1; i
>= 0; i
--) {
1047 const struct pathspec_item
*item
= ps
->items
+i
;
1048 const char *match
= item
->match
;
1049 const char *base_str
= base
->buf
+ base_offset
;
1050 int matchlen
= item
->len
, matched
= 0;
1052 if ((!exclude
&& item
->magic
& PATHSPEC_EXCLUDE
) ||
1053 ( exclude
&& !(item
->magic
& PATHSPEC_EXCLUDE
)))
1056 if (baselen
>= matchlen
) {
1057 /* If it doesn't match, move along... */
1058 if (!match_dir_prefix(item
, base_str
, match
, matchlen
))
1059 goto match_wildcards
;
1061 if (!ps
->recursive
||
1062 !(ps
->magic
& PATHSPEC_MAXDEPTH
) ||
1063 ps
->max_depth
== -1) {
1064 if (!item
->attr_match_nr
)
1065 return all_entries_interesting
;
1070 if (within_depth(base_str
+ matchlen
+ 1,
1071 baselen
- matchlen
- 1,
1072 !!S_ISDIR(entry
->mode
),
1076 return entry_not_interesting
;
1079 /* Either there must be no base, or the base must match. */
1080 if (baselen
== 0 || !basecmp(item
, base_str
, match
, baselen
)) {
1081 if (match_entry(item
, entry
, pathlen
,
1082 match
+ baselen
, matchlen
- baselen
,
1083 &never_interesting
))
1086 if (item
->nowildcard_len
< item
->len
) {
1087 if (!git_fnmatch(item
, match
+ baselen
, entry
->path
,
1088 item
->nowildcard_len
- baselen
))
1092 * Match all directories. We'll try to
1093 * match files later on.
1095 if (ps
->recursive
&& S_ISDIR(entry
->mode
))
1096 return entry_interesting
;
1099 * When matching against submodules with
1100 * wildcard characters, ensure that the entry
1101 * at least matches up to the first wild
1102 * character. More accurate matching can then
1103 * be performed in the submodule itself.
1105 if (ps
->recurse_submodules
&&
1106 S_ISGITLINK(entry
->mode
) &&
1107 !ps_strncmp(item
, match
+ baselen
,
1109 item
->nowildcard_len
- baselen
))
1117 if (item
->nowildcard_len
== item
->len
)
1120 if (item
->nowildcard_len
&&
1121 !match_wildcard_base(item
, base_str
, baselen
, &matched
))
1125 * Concatenate base and entry->path into one and do
1128 * While we could avoid concatenation in certain cases
1129 * [1], which saves a memcpy and potentially a
1130 * realloc, it turns out not worth it. Measurement on
1131 * linux-2.6 does not show any clear improvements,
1132 * partly because of the nowildcard_len optimization
1133 * in git_fnmatch(). Avoid micro-optimizations here.
1135 * [1] if match_wildcard_base() says the base
1136 * directory is already matched, we only need to match
1137 * the rest, which is shorter so _in theory_ faster.
1140 strbuf_add(base
, entry
->path
, pathlen
);
1142 if (!git_fnmatch(item
, match
, base
->buf
+ base_offset
,
1143 item
->nowildcard_len
)) {
1144 strbuf_setlen(base
, base_offset
+ baselen
);
1149 * When matching against submodules with
1150 * wildcard characters, ensure that the entry
1151 * at least matches up to the first wild
1152 * character. More accurate matching can then
1153 * be performed in the submodule itself.
1155 if (ps
->recurse_submodules
&& S_ISGITLINK(entry
->mode
) &&
1156 !ps_strncmp(item
, match
, base
->buf
+ base_offset
,
1157 item
->nowildcard_len
)) {
1158 strbuf_setlen(base
, base_offset
+ baselen
);
1162 strbuf_setlen(base
, base_offset
+ baselen
);
1165 * Match all directories. We'll try to match files
1167 * max_depth is ignored but we may consider support it
1169 * https://lore.kernel.org/git/7vmxo5l2g4.fsf@alter.siamese.dyndns.org/
1171 if (ps
->recursive
&& S_ISDIR(entry
->mode
))
1172 return entry_interesting
;
1175 if (item
->attr_match_nr
) {
1179 * Must not return all_entries_not_interesting
1180 * prematurely. We do not know if all entries do not
1181 * match some attributes with current attr API.
1183 never_interesting
= entry_not_interesting
;
1186 * Consider all directories interesting (because some
1187 * of those files inside may match some attributes
1188 * even though the parent dir does not)
1190 * FIXME: attributes _can_ match directories and we
1191 * can probably return all_entries_interesting or
1192 * all_entries_not_interesting here if matched.
1194 if (S_ISDIR(entry
->mode
))
1195 return entry_interesting
;
1197 strbuf_add(base
, entry
->path
, pathlen
);
1198 ret
= match_pathspec_attrs(istate
, base
->buf
+ base_offset
,
1199 base
->len
- base_offset
, item
);
1200 strbuf_setlen(base
, base_offset
+ baselen
);
1204 return entry_interesting
;
1206 return never_interesting
; /* No matches */
1210 * Is a tree entry interesting given the pathspec we have?
1212 * Pre-condition: either baselen == base_offset (i.e. empty path)
1213 * or base[baselen-1] == '/' (i.e. with trailing slash).
1215 enum interesting
tree_entry_interesting(struct index_state
*istate
,
1216 const struct name_entry
*entry
,
1217 struct strbuf
*base
, int base_offset
,
1218 const struct pathspec
*ps
)
1220 enum interesting positive
, negative
;
1221 positive
= do_match(istate
, entry
, base
, base_offset
, ps
, 0);
1224 * case | entry | positive | negative | result
1225 * -----+-------+----------+----------+-------
1226 * 1 | file | -1 | -1..2 | -1
1227 * 2 | file | 0 | -1..2 | 0
1228 * 3 | file | 1 | -1 | 1
1229 * 4 | file | 1 | 0 | 1
1230 * 5 | file | 1 | 1 | 0
1231 * 6 | file | 1 | 2 | 0
1232 * 7 | file | 2 | -1 | 2
1233 * 8 | file | 2 | 0 | 1
1234 * 9 | file | 2 | 1 | 0
1235 * 10 | file | 2 | 2 | -1
1236 * -----+-------+----------+----------+-------
1237 * 11 | dir | -1 | -1..2 | -1
1238 * 12 | dir | 0 | -1..2 | 0
1239 * 13 | dir | 1 | -1 | 1
1240 * 14 | dir | 1 | 0 | 1
1241 * 15 | dir | 1 | 1 | 1 (*)
1242 * 16 | dir | 1 | 2 | 0
1243 * 17 | dir | 2 | -1 | 2
1244 * 18 | dir | 2 | 0 | 1
1245 * 19 | dir | 2 | 1 | 1 (*)
1246 * 20 | dir | 2 | 2 | -1
1248 * (*) An exclude pattern interested in a directory does not
1249 * necessarily mean it will exclude all of the directory. In
1250 * wildcard case, it can't decide until looking at individual
1251 * files inside. So don't write such directories off yet.
1254 if (!(ps
->magic
& PATHSPEC_EXCLUDE
) ||
1255 positive
<= entry_not_interesting
) /* #1, #2, #11, #12 */
1258 negative
= do_match(istate
, entry
, base
, base_offset
, ps
, 1);
1261 if (positive
== all_entries_interesting
&&
1262 negative
== entry_not_interesting
)
1263 return entry_interesting
;
1265 /* #3, #4, #7, #13, #14, #17 */
1266 if (negative
<= entry_not_interesting
)
1270 if (S_ISDIR(entry
->mode
) &&
1271 positive
>= entry_interesting
&&
1272 negative
== entry_interesting
)
1273 return entry_interesting
;
1275 if ((positive
== entry_interesting
&&
1276 negative
>= entry_interesting
) || /* #5, #6, #16 */
1277 (positive
== all_entries_interesting
&&
1278 negative
== entry_interesting
)) /* #9 */
1279 return entry_not_interesting
;
1281 return all_entries_not_interesting
; /* #10, #20 */