aboutsummaryrefslogtreecommitdiffstats
path: root/formats/idxfile/encoder.go
diff options
context:
space:
mode:
authorMáximo Cuadros <mcuadros@gmail.com>2016-09-10 01:16:22 +0200
committerMáximo Cuadros <mcuadros@gmail.com>2016-09-10 01:16:22 +0200
commite013b297b14949aadaec66deaedc130e86c30afb (patch)
tree481504d00841b97daa1d10de1ca5f780f37e0353 /formats/idxfile/encoder.go
parenta98a111780a74fbc1418d8fc8acc852b9d6187d2 (diff)
downloadgo-git-e013b297b14949aadaec66deaedc130e86c30afb.tar.gz
format: idxfile sorted entries
Diffstat (limited to 'formats/idxfile/encoder.go')
-rw-r--r--formats/idxfile/encoder.go21
1 files changed, 21 insertions, 0 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) }