Skip to content
This repository was archived by the owner on Sep 11, 2020. It is now read-only.

adds Tree method to Tree #224

Merged
merged 4 commits into from
Jan 26, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions plumbing/object/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ const (

// New errors defined by this package.
var (
ErrMaxTreeDepth = errors.New("maximum tree depth exceeded")
ErrFileNotFound = errors.New("file not found")
ErrMaxTreeDepth = errors.New("maximum tree depth exceeded")
ErrFileNotFound = errors.New("file not found")
ErrDirectoryNotFound = errors.New("directory not found")
)

// Tree is basically like a directory - it references a bunch of other trees
Expand Down Expand Up @@ -82,6 +83,17 @@ func (t *Tree) File(path string) (*File, error) {
return NewFile(path, e.Mode, blob), nil
}

// 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)
if err != nil {
return nil, ErrDirectoryNotFound
}

return GetTree(t.s, e.Hash)
}

// TreeEntryFile returns the *File for a given *TreeEntry.
func (t *Tree) TreeEntryFile(e *TreeEntry) (*File, error) {
blob, err := GetBlob(t.s, e.Hash)
Expand Down
16 changes: 16 additions & 0 deletions plumbing/object/tree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,22 @@ func (s *TreeSuite) TestType(c *C) {
c.Assert(s.Tree.Type(), Equals, plumbing.TreeObject)
}

func (s *TreeSuite) TestTree(c *C) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a test for Tree(path) when path points to a Blob instead of a Tree?

Copy link
Contributor Author

@alcortesm alcortesm Jan 26, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch.
Fixed in a new commit.
Also add the equivalent test for File, for consistency.

expectedEntry, ok := s.Tree.m["vendor"]
c.Assert(ok, Equals, true)
expected := expectedEntry.Hash

obtainedTree, err := s.Tree.Tree("vendor")
c.Assert(err, IsNil)
c.Assert(obtainedTree.Hash, Equals, expected)
}

func (s *TreeSuite) TestTreeNotFound(c *C) {
d, err := s.Tree.Tree("not-found")
c.Assert(d, IsNil)
c.Assert(err, Equals, ErrDirectoryNotFound)
}

func (s *TreeSuite) TestFile(c *C) {
f, err := s.Tree.File("LICENSE")
c.Assert(err, IsNil)
Expand Down