diff options
Diffstat (limited to 'formats')
-rw-r--r-- | formats/config/common.go | 1 | ||||
-rw-r--r-- | formats/config/doc.go | 3 | ||||
-rw-r--r-- | formats/config/section.go | 47 | ||||
-rw-r--r-- | formats/idxfile/decoder_test.go | 2 | ||||
-rw-r--r-- | formats/packfile/decoder.go | 75 | ||||
-rw-r--r-- | formats/packfile/decoder_test.go | 10 |
6 files changed, 79 insertions, 59 deletions
diff --git a/formats/config/common.go b/formats/config/common.go index e7292e9..d2f1e5c 100644 --- a/formats/config/common.go +++ b/formats/config/common.go @@ -32,6 +32,7 @@ func (c *Config) Section(name string) *Section { return s } } + s := &Section{Name: name} c.Sections = append(c.Sections, s) return s diff --git a/formats/config/doc.go b/formats/config/doc.go index 1f7eb78..dd77fbc 100644 --- a/formats/config/doc.go +++ b/formats/config/doc.go @@ -1,5 +1,4 @@ -// Package config implements decoding, encoding and manipulation -// of git config files. +// Package config implements decoding/encoding of git config files. package config /* diff --git a/formats/config/section.go b/formats/config/section.go index 552ce74..1844913 100644 --- a/formats/config/section.go +++ b/formats/config/section.go @@ -21,28 +21,15 @@ func (s *Section) IsName(name string) bool { return strings.ToLower(s.Name) == strings.ToLower(name) } -func (s *Subsection) IsName(name string) bool { - return s.Name == name -} - func (s *Section) Option(key string) string { return s.Options.Get(key) } -func (s *Subsection) Option(key string) string { - return s.Options.Get(key) -} - func (s *Section) AddOption(key string, value string) *Section { s.Options = s.Options.withAddedOption(key, value) return s } -func (s *Subsection) AddOption(key string, value string) *Subsection { - s.Options = s.Options.withAddedOption(key, value) - return s -} - func (s *Section) SetOption(key string, value string) *Section { s.Options = s.Options.withSettedOption(key, value) return s @@ -53,16 +40,6 @@ func (s *Section) RemoveOption(key string) *Section { return s } -func (s *Subsection) SetOption(key string, value string) *Subsection { - s.Options = s.Options.withSettedOption(key, value) - return s -} - -func (s *Subsection) RemoveOption(key string) *Subsection { - s.Options = s.Options.withoutOption(key) - return s -} - func (s *Section) Subsection(name string) *Subsection { for i := len(s.Subsections) - 1; i >= 0; i-- { ss := s.Subsections[i] @@ -70,6 +47,7 @@ func (s *Section) Subsection(name string) *Subsection { return ss } } + ss := &Subsection{Name: name} s.Subsections = append(s.Subsections, ss) return ss @@ -84,3 +62,26 @@ func (s *Section) HasSubsection(name string) bool { return false } + +func (s *Subsection) IsName(name string) bool { + return s.Name == name +} + +func (s *Subsection) Option(key string) string { + return s.Options.Get(key) +} + +func (s *Subsection) AddOption(key string, value string) *Subsection { + s.Options = s.Options.withAddedOption(key, value) + return s +} + +func (s *Subsection) SetOption(key string, value string) *Subsection { + s.Options = s.Options.withSettedOption(key, value) + return s +} + +func (s *Subsection) RemoveOption(key string) *Subsection { + s.Options = s.Options.withoutOption(key) + return s +} diff --git a/formats/idxfile/decoder_test.go b/formats/idxfile/decoder_test.go index 02167a7..18546d2 100644 --- a/formats/idxfile/decoder_test.go +++ b/formats/idxfile/decoder_test.go @@ -42,7 +42,7 @@ func (s *IdxfileSuite) TestDecodeCRCs(c *C) { scanner := packfile.NewScanner(f.Packfile()) storage := memory.NewStorage() - pd, err := packfile.NewDecoder(scanner, storage.ObjectStorage()) + pd, err := packfile.NewDecoder(scanner, storage) c.Assert(err, IsNil) _, err = pd.Decode() c.Assert(err, IsNil) diff --git a/formats/packfile/decoder.go b/formats/packfile/decoder.go index 4606a3f..e96980a 100644 --- a/formats/packfile/decoder.go +++ b/formats/packfile/decoder.go @@ -37,13 +37,15 @@ var ( // reader and without a core.ObjectStorage or ReadObjectAt method is called // without a seekable scanner ErrNonSeekable = NewError("non-seekable scanner") + // ErrRollback error making Rollback over a transaction after an error + ErrRollback = NewError("rollback error, during set error") ) // Decoder reads and decodes packfiles from an input stream. type Decoder struct { s *Scanner - o core.ObjectStorage - tx core.TxObjectStorage + o core.ObjectStorer + tx core.Transaction offsetToHash map[int64]core.Hash hashToOffset map[core.Hash]int64 @@ -51,20 +53,14 @@ type Decoder struct { } // NewDecoder returns a new Decoder that reads from r. -func NewDecoder(s *Scanner, o core.ObjectStorage) (*Decoder, error) { +func NewDecoder(s *Scanner, o core.ObjectStorer) (*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: tx, + s: s, + o: o, offsetToHash: make(map[int64]core.Hash, 0), hashToOffset: make(map[core.Hash]int64, 0), @@ -87,39 +83,64 @@ func (d *Decoder) doDecode() error { return err } - if d.o == nil { - return d.readObjects(count) + _, isTxStorer := d.o.(core.Transactioner) + switch { + case d.o == nil: + return d.readObjects(int(count)) + case isTxStorer: + return d.readObjectsWithObjectStorerTx(int(count)) + default: + return d.readObjectsWithObjectStorer(int(count)) } +} - if err := d.readObjects(count); err != nil { - if err := d.tx.Rollback(); err != nil { - return nil +func (d *Decoder) readObjects(count int) error { + for i := 0; i < count; i++ { + if _, err := d.ReadObject(); err != nil { + return err } - - return err } - return d.tx.Commit() + return nil } -func (d *Decoder) readObjects(count uint32) error { - for i := 0; i < int(count); i++ { +func (d *Decoder) readObjectsWithObjectStorer(count int) error { + for i := 0; i < count; i++ { obj, err := d.ReadObject() if err != nil { return err } - if d.o == nil { - continue + if _, err := d.o.SetObject(obj); err != nil { + return err + } + } + + return nil +} + +func (d *Decoder) readObjectsWithObjectStorerTx(count int) error { + tx := d.o.(core.Transactioner).Begin() + + for i := 0; i < count; i++ { + obj, err := d.ReadObject() + if err != nil { + return err } - if _, err := d.tx.Set(obj); err != nil { + if _, err := tx.SetObject(obj); err != nil { + if rerr := d.tx.Rollback(); rerr != nil { + return ErrRollback.AddDetails( + "error: %s, during tx.Set error: %s", rerr, err, + ) + } + return err } } - return nil + return tx.Commit() } // ReadObject reads a object from the stream and return it @@ -241,7 +262,7 @@ func (d *Decoder) recallByOffset(o int64) (core.Object, error) { } if h, ok := d.offsetToHash[o]; ok { - return d.tx.Get(core.AnyObject, h) + return d.tx.Object(core.AnyObject, h) } return nil, core.ErrObjectNotFound @@ -254,7 +275,7 @@ func (d *Decoder) recallByHash(h core.Hash) (core.Object, error) { } } - obj, err := d.tx.Get(core.AnyObject, h) + obj, err := d.tx.Object(core.AnyObject, h) if err != core.ErrObjectNotFound { return obj, err } diff --git a/formats/packfile/decoder_test.go b/formats/packfile/decoder_test.go index 48790b4..aa178d7 100644 --- a/formats/packfile/decoder_test.go +++ b/formats/packfile/decoder_test.go @@ -33,7 +33,7 @@ func (s *ReaderSuite) TestDecode(c *C) { scanner := NewScanner(f.Packfile()) storage := memory.NewStorage() - d, err := NewDecoder(scanner, storage.ObjectStorage()) + d, err := NewDecoder(scanner, storage) c.Assert(err, IsNil) defer d.Close() @@ -97,7 +97,7 @@ func (s *ReaderSuite) TestDecodeCRCs(c *C) { scanner := NewScanner(f.Packfile()) storage := memory.NewStorage() - d, err := NewDecoder(scanner, storage.ObjectStorage()) + d, err := NewDecoder(scanner, storage) c.Assert(err, IsNil) _, err = d.Decode() c.Assert(err, IsNil) @@ -158,11 +158,9 @@ func (s *ReaderSuite) TestSetOffsets(c *C) { } func assertObjects(c *C, s *memory.Storage, expects []string) { - o := s.ObjectStorage().(*memory.ObjectStorage) - - c.Assert(len(expects), Equals, len(o.Objects)) + c.Assert(len(expects), Equals, len(s.Objects)) for _, exp := range expects { - obt, err := o.Get(core.AnyObject, core.NewHash(exp)) + obt, err := s.Object(core.AnyObject, core.NewHash(exp)) c.Assert(err, IsNil) c.Assert(obt.Hash().String(), Equals, exp) } |