diff options
Diffstat (limited to 'formats/idxfile')
-rw-r--r-- | formats/idxfile/encoder.go | 21 | ||||
-rw-r--r-- | formats/idxfile/encoder_test.go | 21 | ||||
-rw-r--r-- | formats/idxfile/idxfile.go | 2 |
3 files changed, 42 insertions, 2 deletions
diff --git a/formats/idxfile/encoder.go b/formats/idxfile/encoder.go index e9b0338..0fe9ae6 100644 --- a/formats/idxfile/encoder.go +++ b/formats/idxfile/encoder.go @@ -3,8 +3,12 @@ package idxfile import ( "crypto/sha1" "encoding/binary" + "fmt" "hash" "io" + "sort" + + "gopkg.in/src-d/go-git.v4/core" ) // An Encoder writes idx files to an output stream. @@ -22,6 +26,8 @@ func NewEncoder(w io.Writer) *Encoder { // Encode writes the idx in an idx file format to the stream of the encoder. func (e *Encoder) Encode(idx *Idxfile) (int, error) { + idx.Entries.Sort() + flow := []func(*Idxfile) (int, error){ e.encodeHeader, e.encodeFanout, @@ -65,16 +71,24 @@ func (e *Encoder) encodeFanout(idx *Idxfile) (int, error) { } func (e *Encoder) encodeHashes(idx *Idxfile) (int, error) { + repet := make(map[core.Hash]int, 0) + sz := 0 for _, ent := range idx.Entries { i, err := e.Write(ent.Hash[:]) sz += i + repet[ent.Hash]++ if err != nil { return sz, err } } + for h, c := range repet { + if c > 1 { + fmt.Println(h, c) + } + } return sz, nil } @@ -122,3 +136,10 @@ func (e *Encoder) encodeChecksums(idx *Idxfile) (int, error) { func (e *Encoder) writeInt32(value uint32) error { return binary.Write(e, binary.BigEndian, value) } + +type EntryList []Entry + +func (p EntryList) Len() int { return len(p) } +func (p EntryList) Less(i, j int) bool { return p[i].Hash.String() < p[j].Hash.String() } +func (p EntryList) Swap(i, j int) { p[i], p[j] = p[j], p[i] } +func (p EntryList) Sort() { sort.Sort(p) } diff --git a/formats/idxfile/encoder_test.go b/formats/idxfile/encoder_test.go index 11ecaee..380b8e8 100644 --- a/formats/idxfile/encoder_test.go +++ b/formats/idxfile/encoder_test.go @@ -5,6 +5,7 @@ import ( "io/ioutil" . "gopkg.in/check.v1" + "gopkg.in/src-d/go-git.v4/core" "gopkg.in/src-d/go-git.v4/fixtures" ) @@ -13,7 +14,25 @@ func (s *IdxfileSuite) SetUpSuite(c *C) { } func (s *IdxfileSuite) TestEncode(c *C) { - fixtures.All().Test(c, func(f *fixtures.Fixture) { + expected := &Idxfile{} + expected.Add(core.NewHash("4bfc730165c370df4a012afbb45ba3f9c332c0d4"), 82, 82) + expected.Add(core.NewHash("8fa2238efdae08d83c12ee176fae65ff7c99af46"), 42, 42) + + buf := bytes.NewBuffer(nil) + e := NewEncoder(buf) + _, err := e.Encode(expected) + c.Assert(err, IsNil) + + idx := &Idxfile{} + d := NewDecoder(buf) + err = d.Decode(idx) + c.Assert(err, IsNil) + + c.Assert(idx.Entries, DeepEquals, expected.Entries) +} + +func (s *IdxfileSuite) TestDecodeEncode(c *C) { + fixtures.ByTag("packfile").Test(c, func(f *fixtures.Fixture) { expected, err := ioutil.ReadAll(f.Idx()) c.Assert(err, IsNil) diff --git a/formats/idxfile/idxfile.go b/formats/idxfile/idxfile.go index 65b58b5..8549d3f 100644 --- a/formats/idxfile/idxfile.go +++ b/formats/idxfile/idxfile.go @@ -16,7 +16,7 @@ type Idxfile struct { Version uint32 Fanout [255]uint32 ObjectCount uint32 - Entries []Entry + Entries EntryList PackfileChecksum [20]byte IdxChecksum [20]byte } |