Merge pull request #470 from lhchavez/fix-odbreadstream-read

Return io.EOF on OdbReadStream.Read()

(cherry picked from commit 7e9128bd58)
This commit is contained in:
Carlos Martín Nieto 2019-01-02 23:03:07 +01:00 committed by lhchavez
parent 13d27a4f62
commit c74ce46055
2 changed files with 17 additions and 5 deletions

4
odb.go
View File

@ -8,6 +8,7 @@ extern void _go_git_odb_backend_free(git_odb_backend *backend);
*/
import "C"
import (
"io"
"reflect"
"runtime"
"unsafe"
@ -287,6 +288,9 @@ func (stream *OdbReadStream) Read(data []byte) (int, error) {
if ret < 0 {
return 0, MakeGitError(ret)
}
if ret == 0 {
return 0, io.EOF
}
header.Len = int(ret)

View File

@ -3,6 +3,7 @@ package git
import (
"errors"
"io"
"io/ioutil"
"testing"
)
@ -47,22 +48,29 @@ func TestOdbStream(t *testing.T) {
str := "hello, world!"
stream, error := odb.NewWriteStream(int64(len(str)), ObjectBlob)
writeStream, error := odb.NewWriteStream(int64(len(str)), ObjectBlob)
checkFatal(t, error)
n, error := io.WriteString(stream, str)
n, error := io.WriteString(writeStream, str)
checkFatal(t, error)
if n != len(str) {
t.Fatalf("Bad write length %v != %v", n, len(str))
}
error = stream.Close()
error = writeStream.Close()
checkFatal(t, error)
expectedId, error := NewOid("30f51a3fba5274d53522d0f19748456974647b4f")
checkFatal(t, error)
if stream.Id.Cmp(expectedId) != 0 {
if writeStream.Id.Cmp(expectedId) != 0 {
t.Fatal("Wrong data written")
}
readStream, error := odb.NewReadStream(&writeStream.Id)
checkFatal(t, error)
data, error := ioutil.ReadAll(readStream)
if str != string(data) {
t.Fatalf("Wrong data read %v != %v", str, string(data))
}
}
func TestOdbHash(t *testing.T) {