3 #include "unpack-trees.h"
8 static const char *get_mode(const char *str
, unsigned int *modep
)
11 unsigned int mode
= 0;
16 while ((c
= *str
++) != ' ') {
17 if (c
< '0' || c
> '7')
19 mode
= (mode
<< 3) + (c
- '0');
25 static void decode_tree_entry(struct tree_desc
*desc
, const char *buf
, unsigned long size
)
28 unsigned int mode
, len
;
30 if (size
< 24 || buf
[size
- 21])
31 die("corrupt tree file");
33 path
= get_mode(buf
, &mode
);
35 die("corrupt tree file");
36 len
= strlen(path
) + 1;
38 /* Initialize the descriptor entry */
39 desc
->entry
.path
= path
;
40 desc
->entry
.mode
= mode
;
41 desc
->entry
.sha1
= (const unsigned char *)(path
+ len
);
44 void init_tree_desc(struct tree_desc
*desc
, const void *buffer
, unsigned long size
)
46 desc
->buffer
= buffer
;
49 decode_tree_entry(desc
, buffer
, size
);
52 void *fill_tree_descriptor(struct tree_desc
*desc
, const unsigned char *sha1
)
54 unsigned long size
= 0;
58 buf
= read_object_with_reference(sha1
, tree_type
, &size
, NULL
);
60 die("unable to read tree %s", sha1_to_hex(sha1
));
62 init_tree_desc(desc
, buf
, size
);
66 static void entry_clear(struct name_entry
*a
)
68 memset(a
, 0, sizeof(*a
));
71 static void entry_extract(struct tree_desc
*t
, struct name_entry
*a
)
76 void update_tree_entry(struct tree_desc
*desc
)
78 const void *buf
= desc
->buffer
;
79 const unsigned char *end
= desc
->entry
.sha1
+ 20;
80 unsigned long size
= desc
->size
;
81 unsigned long len
= end
- (const unsigned char *)buf
;
84 die("corrupt tree file");
90 decode_tree_entry(desc
, buf
, size
);
93 int tree_entry(struct tree_desc
*desc
, struct name_entry
*entry
)
99 update_tree_entry(desc
);
103 void setup_traverse_info(struct traverse_info
*info
, const char *base
)
105 int pathlen
= strlen(base
);
106 static struct traverse_info dummy
;
108 memset(info
, 0, sizeof(*info
));
109 if (pathlen
&& base
[pathlen
-1] == '/')
111 info
->pathlen
= pathlen
? pathlen
+ 1 : 0;
112 info
->name
.path
= base
;
113 info
->name
.sha1
= (void *)(base
+ pathlen
+ 1);
118 char *make_traverse_path(char *path
, const struct traverse_info
*info
, const struct name_entry
*n
)
120 int len
= tree_entry_len(n
);
121 int pathlen
= info
->pathlen
;
123 path
[pathlen
+ len
] = 0;
125 memcpy(path
+ pathlen
, n
->path
, len
);
128 path
[--pathlen
] = '/';
130 len
= tree_entry_len(n
);
137 struct tree_desc_skip
{
138 struct tree_desc_skip
*prev
;
144 struct tree_desc_skip
*skip
;
147 static int name_compare(const char *a
, int a_len
,
148 const char *b
, int b_len
)
150 int len
= (a_len
< b_len
) ? a_len
: b_len
;
151 int cmp
= memcmp(a
, b
, len
);
154 return (a_len
- b_len
);
157 static int check_entry_match(const char *a
, int a_len
, const char *b
, int b_len
)
160 * The caller wants to pick *a* from a tree or nothing.
161 * We are looking at *b* in a tree.
163 * (0) If a and b are the same name, we are trivially happy.
165 * There are three possibilities where *a* could be hiding
168 * (1) *a* == "t", *b* == "ab" i.e. *b* sorts earlier than *a* no
170 * (2) *a* == "t", *b* == "t-2" and "t" is a subtree in the tree;
171 * (3) *a* == "t-2", *b* == "t" and "t-2" is a blob in the tree.
173 * Otherwise we know *a* won't appear in the tree without
177 int cmp
= name_compare(a
, a_len
, b
, b_len
);
179 /* Most common case first -- reading sync'd trees */
184 /* a comes after b; it does not matter if it is case (3)
185 if (b_len < a_len && !memcmp(a, b, b_len) && a[b_len] < '/')
188 return 1; /* keep looking */
191 /* b comes after a; are we looking at case (2)? */
192 if (a_len
< b_len
&& !memcmp(a
, b
, a_len
) && b
[a_len
] < '/')
193 return 1; /* keep looking */
195 return -1; /* a cannot appear in the tree */
199 * From the extended tree_desc, extract the first name entry, while
200 * paying attention to the candidate "first" name. Most importantly,
201 * when looking for an entry, if there are entries that sorts earlier
202 * in the tree object representation than that name, skip them and
203 * process the named entry first. We will remember that we haven't
204 * processed the first entry yet, and in the later call skip the
205 * entry we processed early when update_extended_entry() is called.
207 * E.g. if the underlying tree object has these entries:
214 * and the "first" asks for "t", remember that we still need to
215 * process "t-1" and "t-2" but extract "t". After processing the
216 * entry "t" from this call, the caller will let us know by calling
217 * update_extended_entry() that we can remember "t" has been processed
221 static void extended_entry_extract(struct tree_desc_x
*t
,
222 struct name_entry
*a
,
228 struct tree_desc probe
;
229 struct tree_desc_skip
*skip
;
232 * Extract the first entry from the tree_desc, but skip the
233 * ones that we already returned in earlier rounds.
238 break; /* not found */
240 entry_extract(&t
->d
, a
);
241 for (skip
= t
->skip
; skip
; skip
= skip
->prev
)
242 if (a
->path
== skip
->ptr
)
246 /* We have processed this entry already. */
247 update_tree_entry(&t
->d
);
250 if (!first
|| !a
->path
)
254 * The caller wants "first" from this tree, or nothing.
257 len
= tree_entry_len(a
);
258 switch (check_entry_match(first
, first_len
, path
, len
)) {
268 * We need to look-ahead -- we suspect that a subtree whose
269 * name is "first" may be hiding behind the current entry "path".
273 entry_extract(&probe
, a
);
275 len
= tree_entry_len(a
);
276 switch (check_entry_match(first
, first_len
, path
, len
)) {
282 update_tree_entry(&probe
);
290 static void update_extended_entry(struct tree_desc_x
*t
, struct name_entry
*a
)
292 if (t
->d
.entry
.path
== a
->path
) {
293 update_tree_entry(&t
->d
);
295 /* we have returned this entry early */
296 struct tree_desc_skip
*skip
= xmalloc(sizeof(*skip
));
298 skip
->prev
= t
->skip
;
303 static void free_extended_entry(struct tree_desc_x
*t
)
305 struct tree_desc_skip
*p
, *s
;
307 for (s
= t
->skip
; s
; s
= p
) {
313 static inline int prune_traversal(struct name_entry
*e
,
314 struct traverse_info
*info
,
316 int still_interesting
)
318 if (!info
->pathspec
|| still_interesting
== 2)
320 if (still_interesting
< 0)
321 return still_interesting
;
322 return tree_entry_interesting(e
, base
, 0, info
->pathspec
);
325 int traverse_trees(int n
, struct tree_desc
*t
, struct traverse_info
*info
)
329 struct name_entry
*entry
= xmalloc(n
*sizeof(*entry
));
331 struct tree_desc_x
*tx
= xcalloc(n
, sizeof(*tx
));
332 struct strbuf base
= STRBUF_INIT
;
335 for (i
= 0; i
< n
; i
++)
339 strbuf_grow(&base
, info
->pathlen
);
340 make_traverse_path(base
.buf
, info
->prev
, &info
->name
);
341 base
.buf
[info
->pathlen
-1] = '/';
342 strbuf_setlen(&base
, info
->pathlen
);
345 unsigned long mask
, dirmask
;
346 const char *first
= NULL
;
348 struct name_entry
*e
= NULL
;
351 for (i
= 0; i
< n
; i
++) {
353 extended_entry_extract(tx
+ i
, e
, NULL
, 0);
357 * A tree may have "t-2" at the current location even
358 * though it may have "t" that is a subtree behind it,
359 * and another tree may return "t". We want to grab
360 * all "t" from all trees to match in such a case.
362 for (i
= 0; i
< n
; i
++) {
366 len
= tree_entry_len(e
);
372 if (name_compare(e
->path
, len
, first
, first_len
) < 0) {
379 for (i
= 0; i
< n
; i
++) {
381 extended_entry_extract(tx
+ i
, e
, first
, first_len
);
382 /* Cull the ones that are not the earliest */
385 len
= tree_entry_len(e
);
386 if (name_compare(e
->path
, len
, first
, first_len
))
391 /* Now we have in entry[i] the earliest name from the trees */
394 for (i
= 0; i
< n
; i
++) {
398 if (S_ISDIR(entry
[i
].mode
))
404 interesting
= prune_traversal(e
, info
, &base
, interesting
);
408 ret
= info
->fn(n
, mask
, dirmask
, entry
, info
);
411 if (!info
->show_all_errors
)
417 for (i
= 0; i
< n
; i
++)
418 if (mask
& (1ul << i
))
419 update_extended_entry(tx
+ i
, entry
+ i
);
422 for (i
= 0; i
< n
; i
++)
423 free_extended_entry(tx
+ i
);
425 strbuf_release(&base
);
429 static int find_tree_entry(struct tree_desc
*t
, const char *name
, unsigned char *result
, unsigned *mode
)
431 int namelen
= strlen(name
);
434 const unsigned char *sha1
;
437 sha1
= tree_entry_extract(t
, &entry
, mode
);
438 entrylen
= tree_entry_len(&t
->entry
);
439 update_tree_entry(t
);
440 if (entrylen
> namelen
)
442 cmp
= memcmp(name
, entry
, entrylen
);
447 if (entrylen
== namelen
) {
448 hashcpy(result
, sha1
);
451 if (name
[entrylen
] != '/')
455 if (++entrylen
== namelen
) {
456 hashcpy(result
, sha1
);
459 return get_tree_entry(sha1
, name
+ entrylen
, result
, mode
);
464 int get_tree_entry(const unsigned char *tree_sha1
, const char *name
, unsigned char *sha1
, unsigned *mode
)
469 unsigned char root
[20];
471 tree
= read_object_with_reference(tree_sha1
, tree_type
, &size
, root
);
475 if (name
[0] == '\0') {
485 init_tree_desc(&t
, tree
, size
);
486 retval
= find_tree_entry(&t
, name
, sha1
, mode
);
492 static int match_entry(const struct pathspec_item
*item
,
493 const struct name_entry
*entry
, int pathlen
,
494 const char *match
, int matchlen
,
495 enum interesting
*never_interesting
)
497 int m
= -1; /* signals that we haven't called strncmp() */
499 if (item
->magic
& PATHSPEC_ICASE
)
501 * "Never interesting" trick requires exact
502 * matching. We could do something clever with inexact
503 * matching, but it's trickier (and not to forget that
504 * strcasecmp is locale-dependent, at least in
505 * glibc). Just disable it for now. It can't be worse
506 * than the wildcard's codepath of '[Tt][Hi][Is][Ss]'
509 *never_interesting
= entry_not_interesting
;
510 else if (*never_interesting
!= entry_not_interesting
) {
512 * We have not seen any match that sorts later
513 * than the current path.
517 * Does match sort strictly earlier than path
518 * with their common parts?
520 m
= strncmp(match
, entry
->path
,
521 (matchlen
< pathlen
) ? matchlen
: pathlen
);
526 * If we come here even once, that means there is at
527 * least one pathspec that would sort equal to or
528 * later than the path we are currently looking at.
529 * In other words, if we have never reached this point
530 * after iterating all pathspecs, it means all
531 * pathspecs are either outside of base, or inside the
532 * base but sorts strictly earlier than the current
533 * one. In either case, they will never match the
534 * subsequent entries. In such a case, we initialized
535 * the variable to -1 and that is what will be
536 * returned, allowing the caller to terminate early.
538 *never_interesting
= entry_not_interesting
;
541 if (pathlen
> matchlen
)
544 if (matchlen
> pathlen
) {
545 if (match
[pathlen
] != '/')
547 if (!S_ISDIR(entry
->mode
))
553 * we cheated and did not do strncmp(), so we do
556 m
= ps_strncmp(item
, match
, entry
->path
, pathlen
);
559 * If common part matched earlier then it is a hit,
560 * because we rejected the case where path is not a
561 * leading directory and is shorter than match.
565 * match_entry does not check if the prefix part is
566 * matched case-sensitively. If the entry is a
567 * directory and part of prefix, it'll be rematched
568 * eventually by basecmp with special treatment for
576 /* :(icase)-aware string compare */
577 static int basecmp(const struct pathspec_item
*item
,
578 const char *base
, const char *match
, int len
)
580 if (item
->magic
& PATHSPEC_ICASE
) {
581 int ret
, n
= len
> item
->prefix
? item
->prefix
: len
;
582 ret
= strncmp(base
, match
, n
);
589 return ps_strncmp(item
, base
, match
, len
);
592 static int match_dir_prefix(const struct pathspec_item
*item
,
594 const char *match
, int matchlen
)
596 if (basecmp(item
, base
, match
, matchlen
))
600 * If the base is a subdirectory of a path which
601 * was specified, all of them are interesting.
604 base
[matchlen
] == '/' ||
605 match
[matchlen
- 1] == '/')
608 /* Just a random prefix match */
613 * Perform matching on the leading non-wildcard part of
614 * pathspec. item->nowildcard_len must be greater than zero. Return
615 * non-zero if base is matched.
617 static int match_wildcard_base(const struct pathspec_item
*item
,
618 const char *base
, int baselen
,
621 const char *match
= item
->match
;
622 /* the wildcard part is not considered in this function */
623 int matchlen
= item
->nowildcard_len
;
628 * Return early if base is longer than the
629 * non-wildcard part but it does not match.
631 if (baselen
>= matchlen
) {
633 return !basecmp(item
, base
, match
, matchlen
);
637 while (dirlen
&& match
[dirlen
- 1] != '/')
641 * Return early if base is shorter than the
642 * non-wildcard part but it does not match. Note that
643 * base ends with '/' so we are sure it really matches
646 if (basecmp(item
, base
, match
, baselen
))
652 * we could have checked entry against the non-wildcard part
653 * that is not in base and does similar never_interesting
654 * optimization as in match_entry. For now just be happy with
657 return entry_interesting
;
661 * Is a tree entry interesting given the pathspec we have?
663 * Pre-condition: either baselen == base_offset (i.e. empty path)
664 * or base[baselen-1] == '/' (i.e. with trailing slash).
666 enum interesting
tree_entry_interesting(const struct name_entry
*entry
,
667 struct strbuf
*base
, int base_offset
,
668 const struct pathspec
*ps
)
671 int pathlen
, baselen
= base
->len
- base_offset
;
672 enum interesting never_interesting
= ps
->has_wildcard
?
673 entry_not_interesting
: all_entries_not_interesting
;
683 if (!ps
->recursive
||
684 !(ps
->magic
& PATHSPEC_MAXDEPTH
) ||
686 return all_entries_interesting
;
687 return within_depth(base
->buf
+ base_offset
, baselen
,
688 !!S_ISDIR(entry
->mode
),
690 entry_interesting
: entry_not_interesting
;
693 pathlen
= tree_entry_len(entry
);
695 for (i
= ps
->nr
- 1; i
>= 0; i
--) {
696 const struct pathspec_item
*item
= ps
->items
+i
;
697 const char *match
= item
->match
;
698 const char *base_str
= base
->buf
+ base_offset
;
699 int matchlen
= item
->len
, matched
= 0;
701 if (baselen
>= matchlen
) {
702 /* If it doesn't match, move along... */
703 if (!match_dir_prefix(item
, base_str
, match
, matchlen
))
704 goto match_wildcards
;
706 if (!ps
->recursive
||
707 !(ps
->magic
& PATHSPEC_MAXDEPTH
) ||
709 return all_entries_interesting
;
711 return within_depth(base_str
+ matchlen
+ 1,
712 baselen
- matchlen
- 1,
713 !!S_ISDIR(entry
->mode
),
715 entry_interesting
: entry_not_interesting
;
718 /* Either there must be no base, or the base must match. */
719 if (baselen
== 0 || !basecmp(item
, base_str
, match
, baselen
)) {
720 if (match_entry(item
, entry
, pathlen
,
721 match
+ baselen
, matchlen
- baselen
,
723 return entry_interesting
;
725 if (item
->nowildcard_len
< item
->len
) {
726 if (!git_fnmatch(item
, match
+ baselen
, entry
->path
,
727 item
->nowildcard_len
- baselen
))
728 return entry_interesting
;
731 * Match all directories. We'll try to
732 * match files later on.
734 if (ps
->recursive
&& S_ISDIR(entry
->mode
))
735 return entry_interesting
;
742 if (item
->nowildcard_len
== item
->len
)
745 if (item
->nowildcard_len
&&
746 !match_wildcard_base(item
, base_str
, baselen
, &matched
))
747 return entry_not_interesting
;
750 * Concatenate base and entry->path into one and do
753 * While we could avoid concatenation in certain cases
754 * [1], which saves a memcpy and potentially a
755 * realloc, it turns out not worth it. Measurement on
756 * linux-2.6 does not show any clear improvements,
757 * partly because of the nowildcard_len optimization
758 * in git_fnmatch(). Avoid micro-optimizations here.
760 * [1] if match_wildcard_base() says the base
761 * directory is already matched, we only need to match
762 * the rest, which is shorter so _in theory_ faster.
765 strbuf_add(base
, entry
->path
, pathlen
);
767 if (!git_fnmatch(item
, match
, base
->buf
+ base_offset
,
768 item
->nowildcard_len
)) {
769 strbuf_setlen(base
, base_offset
+ baselen
);
770 return entry_interesting
;
772 strbuf_setlen(base
, base_offset
+ baselen
);
775 * Match all directories. We'll try to match files
777 * max_depth is ignored but we may consider support it
779 * http://thread.gmane.org/gmane.comp.version-control.git/163757/focus=163840
781 if (ps
->recursive
&& S_ISDIR(entry
->mode
))
782 return entry_interesting
;
784 return never_interesting
; /* No matches */