Skip to content

Commit a4ab95c

Browse files
committed
fix: Concurrent map read/write in Tree methods
1 parent 63343bf commit a4ab95c

File tree

2 files changed

+3
-24
lines changed

2 files changed

+3
-24
lines changed

plumbing/object/tree.go

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ type Tree struct {
3939

4040
s storer.EncodedObjectStorer
4141
m map[string]*TreeEntry
42-
t map[string]*Tree // tree path cache
4342
}
4443

4544
// GetTree gets a tree from an object storer and decodes it.
@@ -128,28 +127,10 @@ func (t *Tree) TreeEntryFile(e *TreeEntry) (*File, error) {
128127

129128
// FindEntry search a TreeEntry in this tree or any subtree.
130129
func (t *Tree) FindEntry(path string) (*TreeEntry, error) {
131-
if t.t == nil {
132-
t.t = make(map[string]*Tree)
133-
}
134-
135130
pathParts := strings.Split(path, "/")
136131
startingTree := t
137132
pathCurrent := ""
138133

139-
// search for the longest path in the tree path cache
140-
for i := len(pathParts) - 1; i > 1; i-- {
141-
path := filepath.Join(pathParts[:i]...)
142-
143-
tree, ok := t.t[path]
144-
if ok {
145-
startingTree = tree
146-
pathParts = pathParts[i:]
147-
pathCurrent = path
148-
149-
break
150-
}
151-
}
152-
153134
var tree *Tree
154135
var err error
155136
for tree = startingTree; len(pathParts) > 1; pathParts = pathParts[1:] {
@@ -158,7 +139,6 @@ func (t *Tree) FindEntry(path string) (*TreeEntry, error) {
158139
}
159140

160141
pathCurrent = filepath.Join(pathCurrent, pathParts[0])
161-
t.t[pathCurrent] = tree
162142
}
163143

164144
return tree.entry(pathParts[0])
@@ -182,10 +162,6 @@ func (t *Tree) dir(baseName string) (*Tree, error) {
182162
}
183163

184164
func (t *Tree) entry(baseName string) (*TreeEntry, error) {
185-
if t.m == nil {
186-
t.buildMap()
187-
}
188-
189165
entry, ok := t.m[baseName]
190166
if !ok {
191167
return nil, ErrEntryNotFound
@@ -269,6 +245,8 @@ func (t *Tree) Decode(o plumbing.EncodedObject) (err error) {
269245
})
270246
}
271247

248+
t.buildMap()
249+
272250
return nil
273251
}
274252

plumbing/object/tree_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,7 @@ func (s *TreeSuite) TestTreeDecodeEncodeIdempotent(c *C) {
264264
err = newTree.Decode(obj)
265265
c.Assert(err, IsNil)
266266
tree.Hash = obj.Hash()
267+
tree.buildMap()
267268
c.Assert(newTree, DeepEquals, tree)
268269
}
269270
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy