diff options
author | Máximo Cuadros <mcuadros@gmail.com> | 2016-09-15 21:20:58 +0200 |
---|---|---|
committer | Máximo Cuadros <mcuadros@gmail.com> | 2016-09-15 21:20:58 +0200 |
commit | 7e8d7125446bb6e12a056a5a6ca979727790d0df (patch) | |
tree | df07bada01b332d00131c33034d88935357ba0d1 /formats | |
parent | d57f90b78eeb6b09b15e3fe57609ede66df4edac (diff) | |
download | go-git-7e8d7125446bb6e12a056a5a6ca979727790d0df.tar.gz |
dotgit: synced write packfile and index generation
Diffstat (limited to 'formats')
-rw-r--r-- | formats/idxfile/decoder_test.go | 5 | ||||
-rw-r--r-- | formats/packfile/decoder.go | 68 | ||||
-rw-r--r-- | formats/packfile/decoder_test.go | 93 | ||||
-rw-r--r-- | formats/packfile/scanner.go | 7 |
4 files changed, 116 insertions, 57 deletions
diff --git a/formats/idxfile/decoder_test.go b/formats/idxfile/decoder_test.go index 8ce2bc7..02167a7 100644 --- a/formats/idxfile/decoder_test.go +++ b/formats/idxfile/decoder_test.go @@ -42,8 +42,9 @@ func (s *IdxfileSuite) TestDecodeCRCs(c *C) { scanner := packfile.NewScanner(f.Packfile()) storage := memory.NewStorage() - pd := packfile.NewDecoder(scanner, storage.ObjectStorage()) - _, err := pd.Decode() + pd, err := packfile.NewDecoder(scanner, storage.ObjectStorage()) + c.Assert(err, IsNil) + _, err = pd.Decode() c.Assert(err, IsNil) i := &Idxfile{Version: VersionSupported} diff --git a/formats/packfile/decoder.go b/formats/packfile/decoder.go index c4b9182..4606a3f 100644 --- a/formats/packfile/decoder.go +++ b/formats/packfile/decoder.go @@ -30,11 +30,13 @@ var ( // ErrZLib is returned by Decode when there was an error unzipping // the packfile contents. ErrZLib = NewError("zlib reading error") - // ErrNotSeeker not seeker supported - ErrNotSeeker = NewError("no seeker capable decode") // ErrCannotRecall is returned by RecallByOffset or RecallByHash if the object // to recall cannot be returned. ErrCannotRecall = NewError("cannot recall object") + // ErrNonSeekable is returned if a NewDecoder is used with a non-seekable + // reader and without a core.ObjectStorage or ReadObjectAt method is called + // without a seekable scanner + ErrNonSeekable = NewError("non-seekable scanner") ) // Decoder reads and decodes packfiles from an input stream. @@ -49,16 +51,25 @@ type Decoder struct { } // NewDecoder returns a new Decoder that reads from r. -func NewDecoder(s *Scanner, o core.ObjectStorage) *Decoder { +func NewDecoder(s *Scanner, o core.ObjectStorage) (*Decoder, error) { + if !s.IsSeekable && o == nil { + return nil, ErrNonSeekable + } + + var tx core.TxObjectStorage + if o != nil { + tx = o.Begin() + } + return &Decoder{ s: s, o: o, - tx: o.Begin(), + tx: tx, offsetToHash: make(map[int64]core.Hash, 0), hashToOffset: make(map[core.Hash]int64, 0), crcs: make(map[core.Hash]uint32, 0), - } + }, nil } // Decode reads a packfile and stores it in the value pointed to by s. @@ -76,6 +87,10 @@ func (d *Decoder) doDecode() error { return err } + if d.o == nil { + return d.readObjects(count) + } + if err := d.readObjects(count); err != nil { if err := d.tx.Rollback(); err != nil { return nil @@ -89,10 +104,19 @@ func (d *Decoder) doDecode() error { func (d *Decoder) readObjects(count uint32) error { for i := 0; i < int(count); i++ { - _, err := d.ReadObject() + obj, err := d.ReadObject() if err != nil { return err } + + if d.o == nil { + continue + } + + if _, err := d.tx.Set(obj); err != nil { + return err + } + } return nil @@ -105,7 +129,7 @@ func (d *Decoder) ReadObject() (core.Object, error) { return nil, err } - obj := d.o.NewObject() + obj := d.newObject() obj.SetSize(h.Length) obj.SetType(h.Type) var crc uint32 @@ -128,15 +152,23 @@ func (d *Decoder) ReadObject() (core.Object, error) { d.setOffset(hash, h.Offset) d.setCRC(hash, crc) - if _, err := d.tx.Set(obj); err != nil { - return nil, err + return obj, nil +} + +func (d *Decoder) newObject() core.Object { + if d.o == nil { + return &core.MemoryObject{} } - return obj, nil + return d.o.NewObject() } // ReadObjectAt reads an object at the given location func (d *Decoder) ReadObjectAt(offset int64) (core.Object, error) { + if !d.s.IsSeekable { + return nil, ErrNonSeekable + } + beforeJump, err := d.s.Seek(offset) if err != nil { return nil, err @@ -204,23 +236,29 @@ func (d *Decoder) setCRC(h core.Hash, crc uint32) { } func (d *Decoder) recallByOffset(o int64) (core.Object, error) { + if d.s.IsSeekable { + return d.ReadObjectAt(o) + } + if h, ok := d.offsetToHash[o]; ok { return d.tx.Get(core.AnyObject, h) } - return d.ReadObjectAt(o) + return nil, core.ErrObjectNotFound } func (d *Decoder) recallByHash(h core.Hash) (core.Object, error) { + if d.s.IsSeekable { + if o, ok := d.hashToOffset[h]; ok { + return d.ReadObjectAt(o) + } + } + obj, err := d.tx.Get(core.AnyObject, h) if err != core.ErrObjectNotFound { return obj, err } - if o, ok := d.hashToOffset[h]; ok { - return d.ReadObjectAt(o) - } - return nil, core.ErrObjectNotFound } diff --git a/formats/packfile/decoder_test.go b/formats/packfile/decoder_test.go index 9fe6142..cbf727e 100644 --- a/formats/packfile/decoder_test.go +++ b/formats/packfile/decoder_test.go @@ -25,56 +25,72 @@ func (s *ReaderSuite) TestDecode(c *C) { scanner := NewScanner(f.Packfile()) storage := memory.NewStorage() - d := NewDecoder(scanner, storage.ObjectStorage()) + d, err := NewDecoder(scanner, storage.ObjectStorage()) + c.Assert(err, IsNil) ch, err := d.Decode() c.Assert(err, IsNil) c.Assert(ch, Equals, f.PackfileHash) - AssertObjects(c, storage, []string{ - "918c48b83bd081e863dbe1b80f8998f058cd8294", - "af2d6a6954d532f8ffb47615169c8fdf9d383a1a", - "1669dce138d9b841a518c64b10914d88f5e488ea", - "a5b8b09e2f8fcb0bb99d3ccb0958157b40890d69", - "b8e471f58bcbca63b07bda20e428190409c2db47", - "35e85108805c84807bc66a02d91535e1e24b38b9", - "b029517f6300c2da0f4b651b8642506cd6aaf45d", - "32858aad3c383ed1ff0a0f9bdf231d54a00c9e88", - "d3ff53e0564a9f87d8e84b6e28e5060e517008aa", - "c192bd6a24ea1ab01d78686e417c8bdc7c3d197f", - "d5c0f4ab811897cadf03aec358ae60d21f91c50d", - "49c6bb89b17060d7b4deacb7b338fcc6ea2352a9", - "cf4aa3b38974fb7d81f367c0830f7d78d65ab86b", - "9dea2395f5403188298c1dabe8bdafe562c491e3", - "586af567d0bb5e771e49bdd9434f5e0fb76d25fa", - "9a48f23120e880dfbe41f7c9b7b708e9ee62a492", - "5a877e6a906a2743ad6e45d99c1793642aaf8eda", - "c8f1d8c61f9da76f4cb49fd86322b6e685dba956", - "a8d315b2b1c615d43042c3a62402b8a54288cf5c", - "a39771a7651f97faf5c72e08224d857fc35133db", - "880cd14280f4b9b6ed3986d6671f907d7cc2a198", - "fb72698cab7617ac416264415f13224dfd7a165e", - "4d081c50e250fa32ea8b1313cf8bb7c2ad7627fd", - "eba74343e2f15d62adedfd8c883ee0262b5c8021", - "c2d30fa8ef288618f65f6eed6e168e0d514886f4", - "8dcef98b1d52143e1e2dbc458ffe38f925786bf2", - "aa9b383c260e1d05fbbf6b30a02914555e20c725", - "6ecf0ef2c2dffb796033e5a02219af86ec6584e5", - "dbd3641b371024f44d0e469a9c8f5457b0660de1", - "e8d3ffab552895c19b9fcf7aa264d277cde33881", - "7e59600739c96546163833214c36459e324bad0a", - }) + AssertObjects(c, storage, expectedHashes) + }) +} +func (s *ReaderSuite) TestDecodeInMemory(c *C) { + fixtures.Basic().Test(c, func(f *fixtures.Fixture) { + scanner := NewScanner(f.Packfile()) + d, err := NewDecoder(scanner, nil) + c.Assert(err, IsNil) + + ch, err := d.Decode() + c.Assert(err, IsNil) + c.Assert(ch, Equals, f.PackfileHash) }) } + +var expectedHashes = []string{ + "918c48b83bd081e863dbe1b80f8998f058cd8294", + "af2d6a6954d532f8ffb47615169c8fdf9d383a1a", + "1669dce138d9b841a518c64b10914d88f5e488ea", + "a5b8b09e2f8fcb0bb99d3ccb0958157b40890d69", + "b8e471f58bcbca63b07bda20e428190409c2db47", + "35e85108805c84807bc66a02d91535e1e24b38b9", + "b029517f6300c2da0f4b651b8642506cd6aaf45d", + "32858aad3c383ed1ff0a0f9bdf231d54a00c9e88", + "d3ff53e0564a9f87d8e84b6e28e5060e517008aa", + "c192bd6a24ea1ab01d78686e417c8bdc7c3d197f", + "d5c0f4ab811897cadf03aec358ae60d21f91c50d", + "49c6bb89b17060d7b4deacb7b338fcc6ea2352a9", + "cf4aa3b38974fb7d81f367c0830f7d78d65ab86b", + "9dea2395f5403188298c1dabe8bdafe562c491e3", + "586af567d0bb5e771e49bdd9434f5e0fb76d25fa", + "9a48f23120e880dfbe41f7c9b7b708e9ee62a492", + "5a877e6a906a2743ad6e45d99c1793642aaf8eda", + "c8f1d8c61f9da76f4cb49fd86322b6e685dba956", + "a8d315b2b1c615d43042c3a62402b8a54288cf5c", + "a39771a7651f97faf5c72e08224d857fc35133db", + "880cd14280f4b9b6ed3986d6671f907d7cc2a198", + "fb72698cab7617ac416264415f13224dfd7a165e", + "4d081c50e250fa32ea8b1313cf8bb7c2ad7627fd", + "eba74343e2f15d62adedfd8c883ee0262b5c8021", + "c2d30fa8ef288618f65f6eed6e168e0d514886f4", + "8dcef98b1d52143e1e2dbc458ffe38f925786bf2", + "aa9b383c260e1d05fbbf6b30a02914555e20c725", + "6ecf0ef2c2dffb796033e5a02219af86ec6584e5", + "dbd3641b371024f44d0e469a9c8f5457b0660de1", + "e8d3ffab552895c19b9fcf7aa264d277cde33881", + "7e59600739c96546163833214c36459e324bad0a", +} + func (s *ReaderSuite) TestDecodeCRCs(c *C) { f := fixtures.Basic().ByTag("ofs-delta").One() scanner := NewScanner(f.Packfile()) storage := memory.NewStorage() - d := NewDecoder(scanner, storage.ObjectStorage()) - _, err := d.Decode() + d, err := NewDecoder(scanner, storage.ObjectStorage()) + c.Assert(err, IsNil) + _, err = d.Decode() c.Assert(err, IsNil) var sum uint64 @@ -88,9 +104,8 @@ func (s *ReaderSuite) TestDecodeCRCs(c *C) { func (s *ReaderSuite) TestReadObjectAt(c *C) { f := fixtures.Basic().One() scanner := NewScanner(f.Packfile()) - storage := memory.NewStorage() - - d := NewDecoder(scanner, storage.ObjectStorage()) + d, err := NewDecoder(scanner, nil) + c.Assert(err, IsNil) // when the packfile is ref-delta based, the offsets are required if f.Is("ref-delta") { diff --git a/formats/packfile/scanner.go b/formats/packfile/scanner.go index 86092a1..a5e4215 100644 --- a/formats/packfile/scanner.go +++ b/formats/packfile/scanner.go @@ -49,6 +49,10 @@ type Scanner struct { // is waiting to be read pendingObject *ObjectHeader version, objects uint32 + + // lsSeekable says if this scanner can do Seek or not, to have a Scanner + // seekable a r implementing io.Seeker is required + IsSeekable bool } // NewScanner returns a new Scanner based on a reader, if the given reader @@ -65,7 +69,8 @@ func NewScanner(r io.Reader) *Scanner { newByteReadSeeker(seeker), crc, }, - crc: crc, + crc: crc, + IsSeekable: ok, } } |