3 #include "unpack-trees.h"
7 static const char *get_mode(const char *str
, unsigned int *modep
)
10 unsigned int mode
= 0;
15 while ((c
= *str
++) != ' ') {
16 if (c
< '0' || c
> '7')
18 mode
= (mode
<< 3) + (c
- '0');
24 static void decode_tree_entry(struct tree_desc
*desc
, const char *buf
, unsigned long size
)
27 unsigned int mode
, len
;
29 if (size
< 24 || buf
[size
- 21])
30 die("corrupt tree file");
32 path
= get_mode(buf
, &mode
);
34 die("corrupt tree file");
35 len
= strlen(path
) + 1;
37 /* Initialize the descriptor entry */
38 desc
->entry
.path
= path
;
39 desc
->entry
.mode
= mode
;
40 desc
->entry
.sha1
= (const unsigned char *)(path
+ len
);
43 void init_tree_desc(struct tree_desc
*desc
, const void *buffer
, unsigned long size
)
45 desc
->buffer
= buffer
;
48 decode_tree_entry(desc
, buffer
, size
);
51 void *fill_tree_descriptor(struct tree_desc
*desc
, const unsigned char *sha1
)
53 unsigned long size
= 0;
57 buf
= read_object_with_reference(sha1
, tree_type
, &size
, NULL
);
59 die("unable to read tree %s", sha1_to_hex(sha1
));
61 init_tree_desc(desc
, buf
, size
);
65 static void entry_clear(struct name_entry
*a
)
67 memset(a
, 0, sizeof(*a
));
70 static void entry_extract(struct tree_desc
*t
, struct name_entry
*a
)
75 void update_tree_entry(struct tree_desc
*desc
)
77 const void *buf
= desc
->buffer
;
78 const unsigned char *end
= desc
->entry
.sha1
+ 20;
79 unsigned long size
= desc
->size
;
80 unsigned long len
= end
- (const unsigned char *)buf
;
83 die("corrupt tree file");
89 decode_tree_entry(desc
, buf
, size
);
92 int tree_entry(struct tree_desc
*desc
, struct name_entry
*entry
)
98 update_tree_entry(desc
);
102 void setup_traverse_info(struct traverse_info
*info
, const char *base
)
104 int pathlen
= strlen(base
);
105 static struct traverse_info dummy
;
107 memset(info
, 0, sizeof(*info
));
108 if (pathlen
&& base
[pathlen
-1] == '/')
110 info
->pathlen
= pathlen
? pathlen
+ 1 : 0;
111 info
->name
.path
= base
;
112 info
->name
.sha1
= (void *)(base
+ pathlen
+ 1);
117 char *make_traverse_path(char *path
, const struct traverse_info
*info
, const struct name_entry
*n
)
119 int len
= tree_entry_len(n
);
120 int pathlen
= info
->pathlen
;
122 path
[pathlen
+ len
] = 0;
124 memcpy(path
+ pathlen
, n
->path
, len
);
127 path
[--pathlen
] = '/';
129 len
= tree_entry_len(n
);
136 struct tree_desc_skip
{
137 struct tree_desc_skip
*prev
;
143 struct tree_desc_skip
*skip
;
146 static int name_compare(const char *a
, int a_len
,
147 const char *b
, int b_len
)
149 int len
= (a_len
< b_len
) ? a_len
: b_len
;
150 int cmp
= memcmp(a
, b
, len
);
153 return (a_len
- b_len
);
156 static int check_entry_match(const char *a
, int a_len
, const char *b
, int b_len
)
159 * The caller wants to pick *a* from a tree or nothing.
160 * We are looking at *b* in a tree.
162 * (0) If a and b are the same name, we are trivially happy.
164 * There are three possibilities where *a* could be hiding
167 * (1) *a* == "t", *b* == "ab" i.e. *b* sorts earlier than *a* no
169 * (2) *a* == "t", *b* == "t-2" and "t" is a subtree in the tree;
170 * (3) *a* == "t-2", *b* == "t" and "t-2" is a blob in the tree.
172 * Otherwise we know *a* won't appear in the tree without
176 int cmp
= name_compare(a
, a_len
, b
, b_len
);
178 /* Most common case first -- reading sync'd trees */
183 /* a comes after b; it does not matter if it is case (3)
184 if (b_len < a_len && !memcmp(a, b, b_len) && a[b_len] < '/')
187 return 1; /* keep looking */
190 /* b comes after a; are we looking at case (2)? */
191 if (a_len
< b_len
&& !memcmp(a
, b
, a_len
) && b
[a_len
] < '/')
192 return 1; /* keep looking */
194 return -1; /* a cannot appear in the tree */
198 * From the extended tree_desc, extract the first name entry, while
199 * paying attention to the candidate "first" name. Most importantly,
200 * when looking for an entry, if there are entries that sorts earlier
201 * in the tree object representation than that name, skip them and
202 * process the named entry first. We will remember that we haven't
203 * processed the first entry yet, and in the later call skip the
204 * entry we processed early when update_extended_entry() is called.
206 * E.g. if the underlying tree object has these entries:
213 * and the "first" asks for "t", remember that we still need to
214 * process "t-1" and "t-2" but extract "t". After processing the
215 * entry "t" from this call, the caller will let us know by calling
216 * update_extended_entry() that we can remember "t" has been processed
220 static void extended_entry_extract(struct tree_desc_x
*t
,
221 struct name_entry
*a
,
227 struct tree_desc probe
;
228 struct tree_desc_skip
*skip
;
231 * Extract the first entry from the tree_desc, but skip the
232 * ones that we already returned in earlier rounds.
237 break; /* not found */
239 entry_extract(&t
->d
, a
);
240 for (skip
= t
->skip
; skip
; skip
= skip
->prev
)
241 if (a
->path
== skip
->ptr
)
245 /* We have processed this entry already. */
246 update_tree_entry(&t
->d
);
249 if (!first
|| !a
->path
)
253 * The caller wants "first" from this tree, or nothing.
256 len
= tree_entry_len(a
);
257 switch (check_entry_match(first
, first_len
, path
, len
)) {
267 * We need to look-ahead -- we suspect that a subtree whose
268 * name is "first" may be hiding behind the current entry "path".
272 entry_extract(&probe
, a
);
274 len
= tree_entry_len(a
);
275 switch (check_entry_match(first
, first_len
, path
, len
)) {
281 update_tree_entry(&probe
);
289 static void update_extended_entry(struct tree_desc_x
*t
, struct name_entry
*a
)
291 if (t
->d
.entry
.path
== a
->path
) {
292 update_tree_entry(&t
->d
);
294 /* we have returned this entry early */
295 struct tree_desc_skip
*skip
= xmalloc(sizeof(*skip
));
297 skip
->prev
= t
->skip
;
302 static void free_extended_entry(struct tree_desc_x
*t
)
304 struct tree_desc_skip
*p
, *s
;
306 for (s
= t
->skip
; s
; s
= p
) {
312 static inline int prune_traversal(struct name_entry
*e
,
313 struct traverse_info
*info
,
315 int still_interesting
)
317 if (!info
->pathspec
|| still_interesting
== 2)
319 if (still_interesting
< 0)
320 return still_interesting
;
321 return tree_entry_interesting(e
, base
, 0, info
->pathspec
);
324 int traverse_trees(int n
, struct tree_desc
*t
, struct traverse_info
*info
)
327 struct name_entry
*entry
= xmalloc(n
*sizeof(*entry
));
329 struct tree_desc_x
*tx
= xcalloc(n
, sizeof(*tx
));
330 struct strbuf base
= STRBUF_INIT
;
333 for (i
= 0; i
< n
; i
++)
337 strbuf_grow(&base
, info
->pathlen
);
338 make_traverse_path(base
.buf
, info
->prev
, &info
->name
);
339 base
.buf
[info
->pathlen
-1] = '/';
340 strbuf_setlen(&base
, info
->pathlen
);
344 unsigned long mask
, dirmask
;
345 const char *first
= NULL
;
347 struct name_entry
*e
= NULL
;
350 for (i
= 0; i
< n
; i
++) {
352 extended_entry_extract(tx
+ i
, e
, NULL
, 0);
356 * A tree may have "t-2" at the current location even
357 * though it may have "t" that is a subtree behind it,
358 * and another tree may return "t". We want to grab
359 * all "t" from all trees to match in such a case.
361 for (i
= 0; i
< n
; i
++) {
365 len
= tree_entry_len(e
);
371 if (name_compare(e
->path
, len
, first
, first_len
) < 0) {
378 for (i
= 0; i
< n
; i
++) {
380 extended_entry_extract(tx
+ i
, e
, first
, first_len
);
381 /* Cull the ones that are not the earliest */
384 len
= tree_entry_len(e
);
385 if (name_compare(e
->path
, len
, first
, first_len
))
390 /* Now we have in entry[i] the earliest name from the trees */
393 for (i
= 0; i
< n
; i
++) {
397 if (S_ISDIR(entry
[i
].mode
))
403 interesting
= prune_traversal(e
, info
, &base
, interesting
);
407 trees_used
= info
->fn(n
, mask
, dirmask
, entry
, info
);
408 if (trees_used
< 0) {
410 if (!info
->show_all_errors
)
415 for (i
= 0; i
< n
; i
++)
416 if (mask
& (1ul << i
))
417 update_extended_entry(tx
+ i
, entry
+ i
);
420 for (i
= 0; i
< n
; i
++)
421 free_extended_entry(tx
+ i
);
423 strbuf_release(&base
);
427 static int find_tree_entry(struct tree_desc
*t
, const char *name
, unsigned char *result
, unsigned *mode
)
429 int namelen
= strlen(name
);
432 const unsigned char *sha1
;
435 sha1
= tree_entry_extract(t
, &entry
, mode
);
436 entrylen
= tree_entry_len(&t
->entry
);
437 update_tree_entry(t
);
438 if (entrylen
> namelen
)
440 cmp
= memcmp(name
, entry
, entrylen
);
445 if (entrylen
== namelen
) {
446 hashcpy(result
, sha1
);
449 if (name
[entrylen
] != '/')
453 if (++entrylen
== namelen
) {
454 hashcpy(result
, sha1
);
457 return get_tree_entry(sha1
, name
+ entrylen
, result
, mode
);
462 int get_tree_entry(const unsigned char *tree_sha1
, const char *name
, unsigned char *sha1
, unsigned *mode
)
467 unsigned char root
[20];
469 tree
= read_object_with_reference(tree_sha1
, tree_type
, &size
, root
);
473 if (name
[0] == '\0') {
483 init_tree_desc(&t
, tree
, size
);
484 retval
= find_tree_entry(&t
, name
, sha1
, mode
);
490 static int match_entry(const struct name_entry
*entry
, int pathlen
,
491 const char *match
, int matchlen
,
492 enum interesting
*never_interesting
)
494 int m
= -1; /* signals that we haven't called strncmp() */
496 if (*never_interesting
!= entry_not_interesting
) {
498 * We have not seen any match that sorts later
499 * than the current path.
503 * Does match sort strictly earlier than path
504 * with their common parts?
506 m
= strncmp(match
, entry
->path
,
507 (matchlen
< pathlen
) ? matchlen
: pathlen
);
512 * If we come here even once, that means there is at
513 * least one pathspec that would sort equal to or
514 * later than the path we are currently looking at.
515 * In other words, if we have never reached this point
516 * after iterating all pathspecs, it means all
517 * pathspecs are either outside of base, or inside the
518 * base but sorts strictly earlier than the current
519 * one. In either case, they will never match the
520 * subsequent entries. In such a case, we initialized
521 * the variable to -1 and that is what will be
522 * returned, allowing the caller to terminate early.
524 *never_interesting
= entry_not_interesting
;
527 if (pathlen
> matchlen
)
530 if (matchlen
> pathlen
) {
531 if (match
[pathlen
] != '/')
533 if (!S_ISDIR(entry
->mode
))
539 * we cheated and did not do strncmp(), so we do
542 m
= strncmp(match
, entry
->path
, pathlen
);
545 * If common part matched earlier then it is a hit,
546 * because we rejected the case where path is not a
547 * leading directory and is shorter than match.
555 static int match_dir_prefix(const char *base
,
556 const char *match
, int matchlen
)
558 if (strncmp(base
, match
, matchlen
))
562 * If the base is a subdirectory of a path which
563 * was specified, all of them are interesting.
566 base
[matchlen
] == '/' ||
567 match
[matchlen
- 1] == '/')
570 /* Just a random prefix match */
575 * Perform matching on the leading non-wildcard part of
576 * pathspec. item->nowildcard_len must be greater than zero. Return
577 * non-zero if base is matched.
579 static int match_wildcard_base(const struct pathspec_item
*item
,
580 const char *base
, int baselen
,
583 const char *match
= item
->match
;
584 /* the wildcard part is not considered in this function */
585 int matchlen
= item
->nowildcard_len
;
590 * Return early if base is longer than the
591 * non-wildcard part but it does not match.
593 if (baselen
>= matchlen
) {
595 return !strncmp(base
, match
, matchlen
);
599 while (dirlen
&& match
[dirlen
- 1] != '/')
603 * Return early if base is shorter than the
604 * non-wildcard part but it does not match. Note that
605 * base ends with '/' so we are sure it really matches
608 if (strncmp(base
, match
, baselen
))
614 * we could have checked entry against the non-wildcard part
615 * that is not in base and does similar never_interesting
616 * optimization as in match_entry. For now just be happy with
619 return entry_interesting
;
623 * Is a tree entry interesting given the pathspec we have?
625 * Pre-condition: either baselen == base_offset (i.e. empty path)
626 * or base[baselen-1] == '/' (i.e. with trailing slash).
628 enum interesting
tree_entry_interesting(const struct name_entry
*entry
,
629 struct strbuf
*base
, int base_offset
,
630 const struct pathspec
*ps
)
633 int pathlen
, baselen
= base
->len
- base_offset
;
634 enum interesting never_interesting
= ps
->has_wildcard
?
635 entry_not_interesting
: all_entries_not_interesting
;
638 if (!ps
->recursive
|| ps
->max_depth
== -1)
639 return all_entries_interesting
;
640 return within_depth(base
->buf
+ base_offset
, baselen
,
641 !!S_ISDIR(entry
->mode
),
643 entry_interesting
: entry_not_interesting
;
646 pathlen
= tree_entry_len(entry
);
648 for (i
= ps
->nr
- 1; i
>= 0; i
--) {
649 const struct pathspec_item
*item
= ps
->items
+i
;
650 const char *match
= item
->match
;
651 const char *base_str
= base
->buf
+ base_offset
;
652 int matchlen
= item
->len
, matched
= 0;
654 if (baselen
>= matchlen
) {
655 /* If it doesn't match, move along... */
656 if (!match_dir_prefix(base_str
, match
, matchlen
))
657 goto match_wildcards
;
659 if (!ps
->recursive
|| ps
->max_depth
== -1)
660 return all_entries_interesting
;
662 return within_depth(base_str
+ matchlen
+ 1,
663 baselen
- matchlen
- 1,
664 !!S_ISDIR(entry
->mode
),
666 entry_interesting
: entry_not_interesting
;
669 /* Either there must be no base, or the base must match. */
670 if (baselen
== 0 || !strncmp(base_str
, match
, baselen
)) {
671 if (match_entry(entry
, pathlen
,
672 match
+ baselen
, matchlen
- baselen
,
674 return entry_interesting
;
676 if (item
->nowildcard_len
< item
->len
) {
677 if (!git_fnmatch(match
+ baselen
, entry
->path
,
678 item
->flags
& PATHSPEC_ONESTAR
? GFNM_ONESTAR
: 0,
679 item
->nowildcard_len
- baselen
))
680 return entry_interesting
;
683 * Match all directories. We'll try to
684 * match files later on.
686 if (ps
->recursive
&& S_ISDIR(entry
->mode
))
687 return entry_interesting
;
694 if (item
->nowildcard_len
== item
->len
)
697 if (item
->nowildcard_len
&&
698 !match_wildcard_base(item
, base_str
, baselen
, &matched
))
702 * Concatenate base and entry->path into one and do
705 * While we could avoid concatenation in certain cases
706 * [1], which saves a memcpy and potentially a
707 * realloc, it turns out not worth it. Measurement on
708 * linux-2.6 does not show any clear improvements,
709 * partly because of the nowildcard_len optimization
710 * in git_fnmatch(). Avoid micro-optimizations here.
712 * [1] if match_wildcard_base() says the base
713 * directory is already matched, we only need to match
714 * the rest, which is shorter so _in theory_ faster.
717 strbuf_add(base
, entry
->path
, pathlen
);
719 if (!git_fnmatch(match
, base
->buf
+ base_offset
,
720 item
->flags
& PATHSPEC_ONESTAR
? GFNM_ONESTAR
: 0,
721 item
->nowildcard_len
)) {
722 strbuf_setlen(base
, base_offset
+ baselen
);
723 return entry_interesting
;
725 strbuf_setlen(base
, base_offset
+ baselen
);
728 * Match all directories. We'll try to match files
730 * max_depth is ignored but we may consider support it
732 * http://thread.gmane.org/gmane.comp.version-control.git/163757/focus=163840
734 if (ps
->recursive
&& S_ISDIR(entry
->mode
))
735 return entry_interesting
;
737 return never_interesting
; /* No matches */