aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMáximo Cuadros <mcuadros@gmail.com>2016-01-21 13:20:40 +0100
committerMáximo Cuadros <mcuadros@gmail.com>2016-01-21 13:20:40 +0100
commit050fb78d77b30014acd0b6eefc88ec8a49c20371 (patch)
treea568723447dd0e15b45462494f55e944805fc5c3
parentabe470a3c1b9fae99161ee5531d18d0e3168fdfa (diff)
parentfec62175e8de8ca8daa40683359706c926d9dc2e (diff)
downloadgo-git-050fb78d77b30014acd0b6eefc88ec8a49c20371.tar.gz
Merge pull request #15 from alcortesm/performance-increase-for-big-repos
Performance increase for big repos
-rw-r--r--objects_test.go6
-rw-r--r--tree.go20
2 files changed, 14 insertions, 12 deletions
diff --git a/objects_test.go b/objects_test.go
index 1ead65d..bc83cb7 100644
--- a/objects_test.go
+++ b/objects_test.go
@@ -53,9 +53,9 @@ func (s *ObjectsSuite) TestParseTree(c *C) {
c.Assert(err, IsNil)
c.Assert(tree.Entries, HasLen, 8)
- c.Assert(tree.Entries[0].Name, Equals, ".gitignore")
- c.Assert(tree.Entries[0].Mode.String(), Equals, "-rw-r--r--")
- c.Assert(tree.Entries[0].Hash.String(), Equals, "32858aad3c383ed1ff0a0f9bdf231d54a00c9e88")
+ c.Assert(tree.Entries[".gitignore"].Name, Equals, ".gitignore")
+ c.Assert(tree.Entries[".gitignore"].Mode.String(), Equals, "-rw-r--r--")
+ c.Assert(tree.Entries[".gitignore"].Hash.String(), Equals, "32858aad3c383ed1ff0a0f9bdf231d54a00c9e88")
count := 0
ch := tree.Files()
diff --git a/tree.go b/tree.go
index 523baac..e88b1e6 100644
--- a/tree.go
+++ b/tree.go
@@ -15,7 +15,7 @@ import (
// Tree is basically like a directory - it references a bunch of other trees
// and/or blobs (i.e. files and sub-directories)
type Tree struct {
- Entries []TreeEntry
+ Entries map[string]TreeEntry
Hash core.Hash
r *Repository
@@ -99,13 +99,12 @@ func (t *Tree) dir(baseName string) (*Tree, error) {
var errEntryNotFound = errors.New("entry not found")
func (t *Tree) entry(baseName string) (*TreeEntry, error) {
- for _, entry := range t.Entries {
- if entry.Name == baseName {
- return &entry, nil
- }
+ entry, ok := t.Entries[baseName]
+ if !ok {
+ return nil, errEntryNotFound
}
- return nil, errEntryNotFound
+ return &entry, nil
}
func (t *Tree) Files() chan *File {
@@ -147,6 +146,8 @@ func (t *Tree) Decode(o core.Object) error {
return nil
}
+ t.Entries = make(map[string]TreeEntry)
+
r := bufio.NewReader(o.Reader())
for {
mode, err := r.ReadString(' ')
@@ -174,11 +175,12 @@ func (t *Tree) Decode(o core.Object) error {
return err
}
- t.Entries = append(t.Entries, TreeEntry{
+ baseName := name[:len(name)-1]
+ t.Entries[baseName] = TreeEntry{
Hash: hash,
Mode: os.FileMode(fm),
- Name: name[:len(name)-1],
- })
+ Name: baseName,
+ }
}
return nil