diff options
author | Jeremy Stribling <strib@alum.mit.edu> | 2018-10-11 16:28:19 -0700 |
---|---|---|
committer | Jeremy Stribling <strib@alum.mit.edu> | 2018-10-11 17:12:29 -0700 |
commit | 5c471c34813577a420c1a5af61dd855f70badce1 (patch) | |
tree | e553ca7d22039ecbd0a7cc512ec7bb66ea422da3 /plumbing | |
parent | 1e1315d5e587d481e73e295dd18e50026af48c79 (diff) | |
download | go-git-5c471c34813577a420c1a5af61dd855f70badce1.tar.gz |
tree: add a Size() method for getting plaintext size
Without reading the entire object into memory.
Signed-off-by: Jeremy Stribling <strib@alum.mit.edu>
Diffstat (limited to 'plumbing')
-rw-r--r-- | plumbing/object/tree.go | 11 | ||||
-rw-r--r-- | plumbing/object/tree_test.go | 6 |
2 files changed, 17 insertions, 0 deletions
diff --git a/plumbing/object/tree.go b/plumbing/object/tree.go index c36a137..78d61a1 100644 --- a/plumbing/object/tree.go +++ b/plumbing/object/tree.go @@ -87,6 +87,17 @@ func (t *Tree) File(path string) (*File, error) { return NewFile(path, e.Mode, blob), nil } +// Size returns the plaintext size of an object, without reading it +// into memory. +func (t *Tree) Size(path string) (int64, error) { + e, err := t.FindEntry(path) + if err != nil { + return 0, ErrEntryNotFound + } + + return t.s.EncodedObjectSize(e.Hash) +} + // 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) { diff --git a/plumbing/object/tree_test.go b/plumbing/object/tree_test.go index 7366421..889c63a 100644 --- a/plumbing/object/tree_test.go +++ b/plumbing/object/tree_test.go @@ -98,6 +98,12 @@ func (s *TreeSuite) TestFileFailsWithExistingTrees(c *C) { c.Assert(err, Equals, ErrFileNotFound) } +func (s *TreeSuite) TestSize(c *C) { + size, err := s.Tree.Size("LICENSE") + c.Assert(err, IsNil) + c.Assert(size, Equals, int64(1072)) +} + func (s *TreeSuite) TestFiles(c *C) { var count int err := s.Tree.Files().ForEach(func(f *File) error { |