aboutsummaryrefslogtreecommitdiffstats
path: root/utils
diff options
context:
space:
mode:
authorMáximo Cuadros <mcuadros@gmail.com>2017-05-23 15:38:54 +0200
committerGitHub <noreply@github.com>2017-05-23 15:38:54 +0200
commitf663a9384619965ed8df7a7224e6f15ad18ed4af (patch)
tree337a4615e7a3ef24f46cbe008944a687c6a964ac /utils
parent2ff77a8d93529cefdca922dbed89d4b1cd0ee8e5 (diff)
parent65416cf6c0e8264cc7938fe0611998d52780e089 (diff)
downloadgo-git-f663a9384619965ed8df7a7224e6f15ad18ed4af.tar.gz
Merge pull request #388 from ajnavarro/feature/commit-diff
format/diff: unified diff encoder and public API
Diffstat (limited to 'utils')
-rw-r--r--utils/binary/read.go31
-rw-r--r--utils/binary/read_test.go24
2 files changed, 55 insertions, 0 deletions
diff --git a/utils/binary/read.go b/utils/binary/read.go
index 0bcf11a..c256ffe 100644
--- a/utils/binary/read.go
+++ b/utils/binary/read.go
@@ -3,6 +3,7 @@
package binary
import (
+ "bufio"
"encoding/binary"
"io"
@@ -122,3 +123,33 @@ func ReadHash(r io.Reader) (plumbing.Hash, error) {
return h, nil
}
+
+const sniffLen = 8000
+
+// IsBinary detects if data is a binary value based on:
+// http://git.kernel.org/cgit/git/git.git/tree/xdiff-interface.c?id=HEAD#n198
+func IsBinary(r io.Reader) (bool, error) {
+ reader := bufio.NewReader(r)
+ c := 0
+ for {
+ if c == sniffLen {
+ break
+ }
+
+ b, err := reader.ReadByte()
+ if err == io.EOF {
+ break
+ }
+ if err != nil {
+ return false, err
+ }
+
+ if b == byte(0) {
+ return true, nil
+ }
+
+ c++
+ }
+
+ return false, nil
+}
diff --git a/utils/binary/read_test.go b/utils/binary/read_test.go
index 59dbc30..5674653 100644
--- a/utils/binary/read_test.go
+++ b/utils/binary/read_test.go
@@ -85,3 +85,27 @@ func (s *BinarySuite) TestReadHash(c *C) {
c.Assert(err, IsNil)
c.Assert(hash.String(), Equals, expected.String())
}
+
+func (s *BinarySuite) TestIsBinary(c *C) {
+ buf := bytes.NewBuffer(nil)
+ buf.Write(bytes.Repeat([]byte{'A'}, sniffLen))
+ buf.Write([]byte{0})
+ ok, err := IsBinary(buf)
+ c.Assert(err, IsNil)
+ c.Assert(ok, Equals, false)
+
+ buf.Reset()
+
+ buf.Write(bytes.Repeat([]byte{'A'}, sniffLen-1))
+ buf.Write([]byte{0})
+ ok, err = IsBinary(buf)
+ c.Assert(err, IsNil)
+ c.Assert(ok, Equals, true)
+
+ buf.Reset()
+
+ buf.Write(bytes.Repeat([]byte{'A'}, 10))
+ ok, err = IsBinary(buf)
+ c.Assert(err, IsNil)
+ c.Assert(ok, Equals, false)
+}