aboutsummaryrefslogtreecommitdiffstats
path: root/formats/idxfile
diff options
context:
space:
mode:
authorMáximo Cuadros <mcuadros@gmail.com>2016-10-31 14:56:38 +0000
committerGitHub <noreply@github.com>2016-10-31 14:56:38 +0000
commit659386309f36c482ddc0bb9e854ebda3da216491 (patch)
treeeb156bfbfad599f3dd88e184a7f690f67c64337d /formats/idxfile
parent5944d8a185ff1f91e0e108dab1d756fd88692c3b (diff)
downloadgo-git-659386309f36c482ddc0bb9e854ebda3da216491.tar.gz
utils: binary, new package that collect all the spare helper functions about binary operations (#102)
Diffstat (limited to 'formats/idxfile')
-rw-r--r--formats/idxfile/decoder.go23
-rw-r--r--formats/idxfile/encoder.go24
2 files changed, 11 insertions, 36 deletions
diff --git a/formats/idxfile/decoder.go b/formats/idxfile/decoder.go
index 57b508d..884d32b 100644
--- a/formats/idxfile/decoder.go
+++ b/formats/idxfile/decoder.go
@@ -2,11 +2,11 @@ package idxfile
import (
"bytes"
- "encoding/binary"
"errors"
"io"
"gopkg.in/src-d/go-git.v4/core"
+ "gopkg.in/src-d/go-git.v4/utils/binary"
)
var (
@@ -70,7 +70,7 @@ func validateHeader(r io.Reader) error {
}
func readVersion(idx *Idxfile, r io.Reader) error {
- v, err := readInt32(r)
+ v, err := binary.ReadUint32(r)
if err != nil {
return err
}
@@ -80,21 +80,19 @@ func readVersion(idx *Idxfile, r io.Reader) error {
}
idx.Version = v
-
return nil
}
func readFanout(idx *Idxfile, r io.Reader) error {
var err error
-
for i := 0; i < 255; i++ {
- idx.Fanout[i], err = readInt32(r)
+ idx.Fanout[i], err = binary.ReadUint32(r)
if err != nil {
return err
}
}
- idx.ObjectCount, err = readInt32(r)
+ idx.ObjectCount, err = binary.ReadUint32(r)
return err
}
@@ -115,7 +113,7 @@ func readObjectNames(idx *Idxfile, r io.Reader) error {
func readCRC32(idx *Idxfile, r io.Reader) error {
c := int(idx.ObjectCount)
for i := 0; i < c; i++ {
- if err := binary.Read(r, binary.BigEndian, &idx.Entries[i].CRC32); err != nil {
+ if err := binary.Read(r, &idx.Entries[i].CRC32); err != nil {
return err
}
}
@@ -126,7 +124,7 @@ func readCRC32(idx *Idxfile, r io.Reader) error {
func readOffsets(idx *Idxfile, r io.Reader) error {
c := int(idx.ObjectCount)
for i := 0; i < c; i++ {
- o, err := readInt32(r)
+ o, err := binary.ReadUint32(r)
if err != nil {
return err
}
@@ -148,12 +146,3 @@ func readChecksums(idx *Idxfile, r io.Reader) error {
return nil
}
-
-func readInt32(r io.Reader) (uint32, error) {
- var v uint32
- if err := binary.Read(r, binary.BigEndian, &v); err != nil {
- return 0, err
- }
-
- return v, nil
-}
diff --git a/formats/idxfile/encoder.go b/formats/idxfile/encoder.go
index 0fe9ae6..164414a 100644
--- a/formats/idxfile/encoder.go
+++ b/formats/idxfile/encoder.go
@@ -2,13 +2,11 @@ package idxfile
import (
"crypto/sha1"
- "encoding/binary"
- "fmt"
"hash"
"io"
"sort"
- "gopkg.in/src-d/go-git.v4/core"
+ "gopkg.in/src-d/go-git.v4/utils/binary"
)
// An Encoder writes idx files to an output stream.
@@ -56,13 +54,13 @@ func (e *Encoder) encodeHeader(idx *Idxfile) (int, error) {
return c, err
}
- return c + 4, e.writeInt32(idx.Version)
+ return c + 4, binary.WriteUint32(e, idx.Version)
}
func (e *Encoder) encodeFanout(idx *Idxfile) (int, error) {
fanout := idx.calculateFanout()
for _, c := range fanout {
- if err := e.writeInt32(c); err != nil {
+ if err := binary.WriteUint32(e, c); err != nil {
return 0, err
}
}
@@ -71,31 +69,23 @@ 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
}
func (e *Encoder) encodeCRC32(idx *Idxfile) (int, error) {
sz := 0
for _, ent := range idx.Entries {
- err := binary.Write(e, binary.BigEndian, ent.CRC32)
+ err := binary.Write(e, ent.CRC32)
sz += 4
if err != nil {
@@ -109,7 +99,7 @@ func (e *Encoder) encodeCRC32(idx *Idxfile) (int, error) {
func (e *Encoder) encodeOffsets(idx *Idxfile) (int, error) {
sz := 0
for _, ent := range idx.Entries {
- if err := e.writeInt32(uint32(ent.Offset)); err != nil {
+ if err := binary.WriteUint32(e, uint32(ent.Offset)); err != nil {
return sz, err
}
@@ -133,10 +123,6 @@ func (e *Encoder) encodeChecksums(idx *Idxfile) (int, error) {
return 40, nil
}
-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) }