@@ -11,39 +11,6 @@ impl<'a> TreeRefIter<'a> {
1111 pub fn from_bytes ( data : & ' a [ u8 ] ) -> TreeRefIter < ' a > {
1212 TreeRefIter { data }
1313 }
14- }
15-
16- impl < ' a > TreeRef < ' a > {
17- /// Deserialize a Tree from `data`.
18- pub fn from_bytes ( mut data : & ' a [ u8 ] ) -> Result < TreeRef < ' a > , crate :: decode:: Error > {
19- let input = & mut data;
20- match decode:: tree. parse_next ( input) {
21- Ok ( tag) => Ok ( tag) ,
22- Err ( err) => Err ( crate :: decode:: Error :: with_err ( err, input) ) ,
23- }
24- }
25-
26- /// Find an entry named `name` knowing if the entry is a directory or not, using a binary search.
27- ///
28- /// Note that it's impossible to binary search by name alone as the sort order is special.
29- pub fn bisect_entry ( & self , name : & BStr , is_dir : bool ) -> Option < EntryRef < ' a > > {
30- static NULL_HASH : gix_hash:: ObjectId = gix_hash:: Kind :: shortest ( ) . null ( ) ;
31-
32- let search = EntryRef {
33- mode : if is_dir {
34- tree:: EntryKind :: Tree
35- } else {
36- tree:: EntryKind :: Blob
37- }
38- . into ( ) ,
39- filename : name,
40- oid : & NULL_HASH ,
41- } ;
42- self . entries
43- . binary_search_by ( |e| e. cmp ( & search) )
44- . ok ( )
45- . map ( |idx| self . entries [ idx] )
46- }
4714
4815 /// Follow a sequence of `path` components starting from this instance, and look them up one by one until the last component
4916 /// is looked up and its tree entry is returned.
@@ -64,19 +31,24 @@ impl<'a> TreeRef<'a> {
6431 I : IntoIterator < Item = P > ,
6532 P : PartialEq < BStr > ,
6633 {
67- let mut path = path. into_iter ( ) . peekable ( ) ;
68- let mut entries = self . entries . clone ( ) ;
34+ buffer. clear ( ) ;
6935
36+ let mut path = path. into_iter ( ) . peekable ( ) ;
37+ buffer. extend_from_slice ( & self . data ) ;
7038 while let Some ( component) = path. next ( ) {
71- match entries. iter ( ) . find ( |entry| component. eq ( entry. filename ) ) {
39+ match TreeRefIter :: from_bytes ( & buffer)
40+ . filter_map ( Result :: ok)
41+ . find ( |entry| component. eq ( entry. filename ) )
42+ {
7243 Some ( entry) => {
7344 if path. peek ( ) . is_none ( ) {
74- return Ok ( Some ( ( * entry) . into ( ) ) ) ;
45+ return Ok ( Some ( entry. into ( ) ) ) ;
7546 } else {
7647 let next_id = entry. oid . to_owned ( ) ;
77- let obj = odb. find_tree ( & next_id, buffer) ?;
78-
79- entries = obj. entries ;
48+ let obj = odb. find ( & next_id, buffer) ?;
49+ if !obj. kind . is_tree ( ) {
50+ return Ok ( None ) ;
51+ }
8052 }
8153 }
8254 None => return Ok ( None ) ,
@@ -108,6 +80,39 @@ impl<'a> TreeRef<'a> {
10880 } ) ,
10981 )
11082 }
83+ }
84+
85+ impl < ' a > TreeRef < ' a > {
86+ /// Deserialize a Tree from `data`.
87+ pub fn from_bytes ( mut data : & ' a [ u8 ] ) -> Result < TreeRef < ' a > , crate :: decode:: Error > {
88+ let input = & mut data;
89+ match decode:: tree. parse_next ( input) {
90+ Ok ( tag) => Ok ( tag) ,
91+ Err ( err) => Err ( crate :: decode:: Error :: with_err ( err, input) ) ,
92+ }
93+ }
94+
95+ /// Find an entry named `name` knowing if the entry is a directory or not, using a binary search.
96+ ///
97+ /// Note that it's impossible to binary search by name alone as the sort order is special.
98+ pub fn bisect_entry ( & self , name : & BStr , is_dir : bool ) -> Option < EntryRef < ' a > > {
99+ static NULL_HASH : gix_hash:: ObjectId = gix_hash:: Kind :: shortest ( ) . null ( ) ;
100+
101+ let search = EntryRef {
102+ mode : if is_dir {
103+ tree:: EntryKind :: Tree
104+ } else {
105+ tree:: EntryKind :: Blob
106+ }
107+ . into ( ) ,
108+ filename : name,
109+ oid : & NULL_HASH ,
110+ } ;
111+ self . entries
112+ . binary_search_by ( |e| e. cmp ( & search) )
113+ . ok ( )
114+ . map ( |idx| self . entries [ idx] )
115+ }
111116
112117 /// Create an instance of the empty tree.
113118 ///
0 commit comments