aboutsummaryrefslogtreecommitdiffstats
path: root/plumbing/object/tree.go
diff options
context:
space:
mode:
authorAlberto Cortés <alcortesm@gmail.com>2017-01-26 14:21:01 +0100
committerGitHub <noreply@github.com>2017-01-26 14:21:01 +0100
commit1c2602a791371e76d52f89b2c8193cb200c66ad6 (patch)
treedcefc9d45f91de1450d41bdc415b1056a637f2c6 /plumbing/object/tree.go
parentec28bd3808d42f523eeb05e23909dbfc54eb9bcd (diff)
downloadgo-git-1c2602a791371e76d52f89b2c8193cb200c66ad6.tar.gz
adds Tree method to Tree (#224)
This patch adds a new method to the Tree object that allows you to get a child tree from a parent tree by its relative name. Before this patch, this was only possible with files, using the File method. The new Tree method has a similar signature to the old File method for consistency.
Diffstat (limited to 'plumbing/object/tree.go')
-rw-r--r--plumbing/object/tree.go28
1 files changed, 23 insertions, 5 deletions
diff --git a/plumbing/object/tree.go b/plumbing/object/tree.go
index 7a8c1a3..f3e03bc 100644
--- a/plumbing/object/tree.go
+++ b/plumbing/object/tree.go
@@ -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
@@ -76,12 +77,31 @@ func (t *Tree) File(path string) (*File, error) {
blob, err := GetBlob(t.s, e.Hash)
if err != nil {
+ if err == plumbing.ErrObjectNotFound {
+ return nil, ErrFileNotFound
+ }
return nil, err
}
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
+ }
+
+ tree, err := GetTree(t.s, e.Hash)
+ if err == plumbing.ErrObjectNotFound {
+ return nil, ErrDirectoryNotFound
+ }
+
+ return tree, err
+}
+
// TreeEntryFile returns the *File for a given *TreeEntry.
func (t *Tree) TreeEntryFile(e *TreeEntry) (*File, error) {
blob, err := GetBlob(t.s, e.Hash)
@@ -106,12 +126,10 @@ func (t *Tree) findEntry(path string) (*TreeEntry, error) {
return tree.entry(pathParts[0])
}
-var errDirNotFound = errors.New("directory not found")
-
func (t *Tree) dir(baseName string) (*Tree, error) {
entry, err := t.entry(baseName)
if err != nil {
- return nil, errDirNotFound
+ return nil, ErrDirectoryNotFound
}
obj, err := t.s.EncodedObject(plumbing.TreeObject, entry.Hash)