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

Commit 32fbb94

Browse files
committed
format: idxfile sorted entries
1 parent d998fde commit 32fbb94

File tree

3 files changed

+42
-2
lines changed

3 files changed

+42
-2
lines changed

formats/idxfile/encoder.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,12 @@ package idxfile
33
import (
44
"crypto/sha1"
55
"encoding/binary"
6+
"fmt"
67
"hash"
78
"io"
9+
"sort"
10+
11+
"gopkg.in/src-d/go-git.v4/core"
812
)
913

1014
// An Encoder writes idx files to an output stream.
@@ -22,6 +26,8 @@ func NewEncoder(w io.Writer) *Encoder {
2226

2327
// Encode writes the idx in an idx file format to the stream of the encoder.
2428
func (e *Encoder) Encode(idx *Idxfile) (int, error) {
29+
idx.Entries.Sort()
30+
2531
flow := []func(*Idxfile) (int, error){
2632
e.encodeHeader,
2733
e.encodeFanout,
@@ -65,16 +71,24 @@ func (e *Encoder) encodeFanout(idx *Idxfile) (int, error) {
6571
}
6672

6773
func (e *Encoder) encodeHashes(idx *Idxfile) (int, error) {
74+
repet := make(map[core.Hash]int, 0)
75+
6876
sz := 0
6977
for _, ent := range idx.Entries {
7078
i, err := e.Write(ent.Hash[:])
7179
sz += i
7280

81+
repet[ent.Hash]++
7382
if err != nil {
7483
return sz, err
7584
}
7685
}
7786

87+
for h, c := range repet {
88+
if c > 1 {
89+
fmt.Println(h, c)
90+
}
91+
}
7892
return sz, nil
7993
}
8094

@@ -122,3 +136,10 @@ func (e *Encoder) encodeChecksums(idx *Idxfile) (int, error) {
122136
func (e *Encoder) writeInt32(value uint32) error {
123137
return binary.Write(e, binary.BigEndian, value)
124138
}
139+
140+
type EntryList []Entry
141+
142+
func (p EntryList) Len() int { return len(p) }
143+
func (p EntryList) Less(i, j int) bool { return p[i].Hash.String() < p[j].Hash.String() }
144+
func (p EntryList) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
145+
func (p EntryList) Sort() { sort.Sort(p) }

formats/idxfile/encoder_test.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"io/ioutil"
66

77
. "gopkg.in/check.v1"
8+
"gopkg.in/src-d/go-git.v4/core"
89
"gopkg.in/src-d/go-git.v4/fixtures"
910
)
1011

@@ -13,7 +14,25 @@ func (s *IdxfileSuite) SetUpSuite(c *C) {
1314
}
1415

1516
func (s *IdxfileSuite) TestEncode(c *C) {
16-
fixtures.All().Test(c, func(f *fixtures.Fixture) {
17+
expected := &Idxfile{}
18+
expected.Add(core.NewHash("4bfc730165c370df4a012afbb45ba3f9c332c0d4"), 82, 82)
19+
expected.Add(core.NewHash("8fa2238efdae08d83c12ee176fae65ff7c99af46"), 42, 42)
20+
21+
buf := bytes.NewBuffer(nil)
22+
e := NewEncoder(buf)
23+
_, err := e.Encode(expected)
24+
c.Assert(err, IsNil)
25+
26+
idx := &Idxfile{}
27+
d := NewDecoder(buf)
28+
err = d.Decode(idx)
29+
c.Assert(err, IsNil)
30+
31+
c.Assert(idx.Entries, DeepEquals, expected.Entries)
32+
}
33+
34+
func (s *IdxfileSuite) TestDecodeEncode(c *C) {
35+
fixtures.ByTag("packfile").Test(c, func(f *fixtures.Fixture) {
1736
expected, err := ioutil.ReadAll(f.Idx())
1837
c.Assert(err, IsNil)
1938

formats/idxfile/idxfile.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ type Idxfile struct {
1616
Version uint32
1717
Fanout [255]uint32
1818
ObjectCount uint32
19-
Entries []Entry
19+
Entries EntryList
2020
PackfileChecksum [20]byte
2121
IdxChecksum [20]byte
2222
}

0 commit comments

Comments
 (0)