File tree 1 file changed +26
-1
lines changed
1 file changed +26
-1
lines changed Original file line number Diff line number Diff line change 6
6
"fmt"
7
7
"io"
8
8
"path"
9
+ "path/filepath"
9
10
"strings"
10
11
11
12
"gopkg.in/src-d/go-git.v4/plumbing"
@@ -34,6 +35,7 @@ type Tree struct {
34
35
35
36
s storer.EncodedObjectStorer
36
37
m map [string ]* TreeEntry
38
+ t map [string ]* Tree // tree path cache
37
39
}
38
40
39
41
// GetTree gets a tree from an object storer and decodes it.
@@ -111,14 +113,37 @@ func (t *Tree) TreeEntryFile(e *TreeEntry) (*File, error) {
111
113
112
114
// FindEntry search a TreeEntry in this tree or any subtree.
113
115
func (t * Tree ) FindEntry (path string ) (* TreeEntry , error ) {
116
+ if t .t == nil {
117
+ t .t = make (map [string ]* Tree )
118
+ }
119
+
114
120
pathParts := strings .Split (path , "/" )
121
+ startingTree := t
122
+ pathCurrent := ""
123
+
124
+ // search for the longest path in the tree path cache
125
+ for i := len (pathParts ); i > 1 ; i -- {
126
+ path := filepath .Join (pathParts [:i ]... )
127
+
128
+ tree , ok := t .t [path ]
129
+ if ok {
130
+ startingTree = tree
131
+ pathParts = pathParts [i :]
132
+ pathCurrent = path
133
+
134
+ break
135
+ }
136
+ }
115
137
116
138
var tree * Tree
117
139
var err error
118
- for tree = t ; len (pathParts ) > 1 ; pathParts = pathParts [1 :] {
140
+ for tree = startingTree ; len (pathParts ) > 1 ; pathParts = pathParts [1 :] {
119
141
if tree , err = tree .dir (pathParts [0 ]); err != nil {
120
142
return nil , err
121
143
}
144
+
145
+ pathCurrent = filepath .Join (pathCurrent , pathParts [0 ])
146
+ t .t [pathCurrent ] = tree
122
147
}
123
148
124
149
return tree .entry (pathParts [0 ])
You can’t perform that action at this time.
0 commit comments