From 2f293f4a5214ccba5bdf0b82ff8b62ed39144078 Mon Sep 17 00:00:00 2001 From: Antonio Jesus Navarro Perez Date: Wed, 10 May 2017 16:47:15 +0200 Subject: format/diff: unified diff encoder and public API - Added Patch interface - Added a Unified Diff encoder from Patches - Added Change method to generate Patches - Added Changes method to generate Patches - Added Tree method to generate Patches - Added Commit method to generate Patches --- utils/binary/read.go | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) (limited to 'utils/binary/read.go') 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 +} -- cgit