aboutsummaryrefslogtreecommitdiffstats
path: root/utils
diff options
context:
space:
mode:
authorAntonio Navarro Perez <antnavper@gmail.com>2016-12-14 10:20:00 +0100
committerMáximo Cuadros <mcuadros@gmail.com>2016-12-14 10:20:00 +0100
commit500b1e1e183c73e3087710fca2f96acfd2e2d5cb (patch)
treeb2777dedd22f7279f2df7da8eb3b433d560c5701 /utils
parent40875ee0df345468f36cb00d54820d622b37cbc5 (diff)
downloadgo-git-500b1e1e183c73e3087710fca2f96acfd2e2d5cb.tar.gz
format/packfile: implement delta encoding (#172)
* format/packfile: implement delta encoding - Added all the logic to the encoder to be able to encode ref-delta and offset-delta objects - Created plumbing.ObjectToPack to handle deltas and standard objects when we are writting them into a packfile - Added specific encoder delta tests, one standard object and one delta, and one standard object and two deltas * Requested changes. * Requested changes
Diffstat (limited to 'utils')
-rw-r--r--utils/binary/write.go14
-rw-r--r--utils/binary/writer_test.go16
2 files changed, 30 insertions, 0 deletions
diff --git a/utils/binary/write.go b/utils/binary/write.go
index 3ea1d91..2ec3581 100644
--- a/utils/binary/write.go
+++ b/utils/binary/write.go
@@ -17,6 +17,20 @@ func Write(w io.Writer, data ...interface{}) error {
return nil
}
+func WriteVariableWidthInt(w io.Writer, n int64) error {
+ buf := []byte{byte(n & 0x7f)}
+ n >>= 7
+ for n != 0 {
+ n--
+ buf = append([]byte{0x80 | (byte(n & 0x7f))}, buf...)
+ n >>= 7
+ }
+
+ _, err := w.Write(buf)
+
+ return err
+}
+
// WriteUint32 writes the binary representation of a uint32 into w, in BigEndian
// order
func WriteUint32(w io.Writer, value uint32) error {
diff --git a/utils/binary/writer_test.go b/utils/binary/writer_test.go
index 88140a1..1380280 100644
--- a/utils/binary/writer_test.go
+++ b/utils/binary/writer_test.go
@@ -41,3 +41,19 @@ func (s *BinarySuite) TestWriteUint16(c *C) {
c.Assert(err, IsNil)
c.Assert(buf, DeepEquals, expected)
}
+
+func (s *BinarySuite) TestWriteVariableWidthInt(c *C) {
+ buf := bytes.NewBuffer(nil)
+
+ err := WriteVariableWidthInt(buf, 366)
+ c.Assert(err, IsNil)
+ c.Assert(buf.Bytes(), DeepEquals, []byte{129, 110})
+}
+
+func (s *BinarySuite) TestWriteVariableWidthIntShort(c *C) {
+ buf := bytes.NewBuffer(nil)
+
+ err := WriteVariableWidthInt(buf, 19)
+ c.Assert(err, IsNil)
+ c.Assert(buf.Bytes(), DeepEquals, []byte{19})
+}