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

Commit 5c471c3

Browse files
committed
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>
1 parent 1e1315d commit 5c471c3

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

plumbing/object/tree.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,17 @@ func (t *Tree) File(path string) (*File, error) {
8787
return NewFile(path, e.Mode, blob), nil
8888
}
8989

90+
// Size returns the plaintext size of an object, without reading it
91+
// into memory.
92+
func (t *Tree) Size(path string) (int64, error) {
93+
e, err := t.FindEntry(path)
94+
if err != nil {
95+
return 0, ErrEntryNotFound
96+
}
97+
98+
return t.s.EncodedObjectSize(e.Hash)
99+
}
100+
90101
// Tree returns the tree identified by the `path` argument.
91102
// The path is interpreted as relative to the tree receiver.
92103
func (t *Tree) Tree(path string) (*Tree, error) {

plumbing/object/tree_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,12 @@ func (s *TreeSuite) TestFileFailsWithExistingTrees(c *C) {
9898
c.Assert(err, Equals, ErrFileNotFound)
9999
}
100100

101+
func (s *TreeSuite) TestSize(c *C) {
102+
size, err := s.Tree.Size("LICENSE")
103+
c.Assert(err, IsNil)
104+
c.Assert(size, Equals, int64(1072))
105+
}
106+
101107
func (s *TreeSuite) TestFiles(c *C) {
102108
var count int
103109
err := s.Tree.Files().ForEach(func(f *File) error {

0 commit comments

Comments
 (0)