diff options
-rw-r--r-- | plumbing/object/difftree.go | 4 | ||||
-rw-r--r-- | plumbing/object/tree.go | 8 | ||||
-rw-r--r-- | plumbing/object/tree_test.go | 6 | ||||
-rw-r--r-- | plumbing/object/treenoder.go | 7 |
4 files changed, 17 insertions, 8 deletions
diff --git a/plumbing/object/difftree.go b/plumbing/object/difftree.go index 87a7153..ac58c4d 100644 --- a/plumbing/object/difftree.go +++ b/plumbing/object/difftree.go @@ -10,8 +10,8 @@ import ( // DiffTree compares the content and mode of the blobs found via two // tree objects. func DiffTree(a, b *Tree) (Changes, error) { - from := newTreeNoder(a) - to := newTreeNoder(b) + from := NewTreeRootNode(a) + to := NewTreeRootNode(b) hashEqual := func(a, b noder.Hasher) bool { return bytes.Equal(a.Hash(), b.Hash()) diff --git a/plumbing/object/tree.go b/plumbing/object/tree.go index e70b5cd..b768b96 100644 --- a/plumbing/object/tree.go +++ b/plumbing/object/tree.go @@ -67,7 +67,7 @@ type TreeEntry struct { // File returns the hash of the file identified by the `path` argument. // The path is interpreted as relative to the tree receiver. func (t *Tree) File(path string) (*File, error) { - e, err := t.findEntry(path) + e, err := t.FindEntry(path) if err != nil { return nil, ErrFileNotFound } @@ -86,7 +86,7 @@ func (t *Tree) File(path string) (*File, error) { // Tree returns the tree identified by the `path` argument. // The path is interpreted as relative to the tree receiver. func (t *Tree) Tree(path string) (*Tree, error) { - e, err := t.findEntry(path) + e, err := t.FindEntry(path) if err != nil { return nil, ErrDirectoryNotFound } @@ -109,7 +109,8 @@ func (t *Tree) TreeEntryFile(e *TreeEntry) (*File, error) { return NewFile(e.Name, e.Mode, blob), nil } -func (t *Tree) findEntry(path string) (*TreeEntry, error) { +// FindEntry search a TreeEntry in this tree or any subtree +func (t *Tree) FindEntry(path string) (*TreeEntry, error) { pathParts := strings.Split(path, "/") var tree *Tree @@ -146,6 +147,7 @@ func (t *Tree) entry(baseName string) (*TreeEntry, error) { if t.m == nil { t.buildMap() } + entry, ok := t.m[baseName] if !ok { return nil, errEntryNotFound diff --git a/plumbing/object/tree_test.go b/plumbing/object/tree_test.go index cf5ad5f..aa86517 100644 --- a/plumbing/object/tree_test.go +++ b/plumbing/object/tree_test.go @@ -107,6 +107,12 @@ func (s *TreeSuite) TestFiles(c *C) { c.Assert(count, Equals, 9) } +func (s *TreeSuite) TestFindEntry(c *C) { + e, err := s.Tree.FindEntry("vendor/foo.go") + c.Assert(err, IsNil) + c.Assert(e.Name, Equals, "foo.go") +} + // This plumbing.EncodedObject implementation has a reader that only returns 6 // bytes at a time, this should simulate the conditions when a read // returns less bytes than asked, for example when reading a hash which diff --git a/plumbing/object/treenoder.go b/plumbing/object/treenoder.go index 4da8298..8b56d1b 100644 --- a/plumbing/object/treenoder.go +++ b/plumbing/object/treenoder.go @@ -21,10 +21,11 @@ type treeNoder struct { name string // empty string for the root node mode filemode.FileMode hash plumbing.Hash - children []noder.Noder // memoized + children []noder.Noder // memorized } -func newTreeNoder(t *Tree) *treeNoder { +// NewTreeRootNode returns the root node of a Tree +func NewTreeRootNode(t *Tree) *treeNoder { if t == nil { return &treeNoder{} } @@ -74,7 +75,7 @@ func (t *treeNoder) Children() ([]noder.Noder, error) { return noder.NoChildren, nil } - // children are memoized for efficiency + // children are memorized for efficiency if t.children != nil { return t.children, nil } |