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

packfile: get object size correctly for delta objects #1060

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion plumbing/format/packfile/packfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func (p *Packfile) GetSizeByOffset(o int64) (size int64, err error) {
if err != nil {
return 0, err
}
return h.Length, nil
return p.getObjectSize(h)
}

func (p *Packfile) objectHeaderAtOffset(offset int64) (*ObjectHeader, error) {
Expand Down
26 changes: 26 additions & 0 deletions plumbing/format/packfile/packfile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,3 +277,29 @@ func getIndexFromIdxFile(r io.Reader) idxfile.Index {

return idxf
}

func (s *PackfileSuite) TestSize(c *C) {
f := fixtures.Basic().ByTag("ref-delta").One()

index := getIndexFromIdxFile(f.Idx())
fs := osfs.New("")
pf, err := fs.Open(f.Packfile().Name())
c.Assert(err, IsNil)

packfile := packfile.NewPackfile(index, fs, pf)
defer packfile.Close()

// Get the size of binary.jpg, which is not delta-encoded.
offset, err := packfile.FindOffset(plumbing.NewHash("d5c0f4ab811897cadf03aec358ae60d21f91c50d"))
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Please let me know if there's a non-hardcoded way to get this hash.

Copy link
Contributor

Choose a reason for hiding this comment

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

Not really

c.Assert(err, IsNil)
size, err := packfile.GetSizeByOffset(offset)
c.Assert(err, IsNil)
c.Assert(size, Equals, int64(76110))

// Get the size of the root commit, which is delta-encoded.
offset, err = packfile.FindOffset(f.Head)
c.Assert(err, IsNil)
size, err = packfile.GetSizeByOffset(offset)
c.Assert(err, IsNil)
c.Assert(size, Equals, int64(245))
}