Move CopyWithCallback into general tools for re-use

This commit is contained in:
Steve Streeting 2016-05-19 15:12:46 +01:00
parent 54e86de23a
commit 1ef2d43306
8 changed files with 71 additions and 45 deletions

@ -10,6 +10,7 @@ import (
"github.com/github/git-lfs/config"
"github.com/github/git-lfs/errutil"
"github.com/github/git-lfs/progress"
"github.com/github/git-lfs/tools"
)
type cleanedAsset struct {
@ -82,7 +83,7 @@ func copyToTemp(reader io.Reader, fileSize int64, cb progress.CopyCallback) (oid
}
multi := io.MultiReader(bytes.NewReader(by), reader)
size, err = CopyWithCallback(writer, multi, fileSize, cb)
size, err = tools.CopyWithCallback(writer, multi, fileSize, cb)
if err != nil {
return

@ -14,6 +14,7 @@ import (
"github.com/github/git-lfs/config"
"github.com/github/git-lfs/errutil"
"github.com/github/git-lfs/progress"
"github.com/github/git-lfs/tools"
"github.com/github/git-lfs/vendor/_nuts/github.com/cheggaaa/pb"
"github.com/github/git-lfs/vendor/_nuts/github.com/rubyist/tracerx"
)
@ -179,7 +180,7 @@ func bufferDownloadedFile(filename string, reader io.Reader, size int64, cb prog
// close below, as close is idempotent.
defer f.Close()
name := f.Name()
written, err := CopyWithCallback(f, hasher, size, cb)
written, err := tools.CopyWithCallback(f, hasher, size, cb)
if err != nil {
return fmt.Errorf("cannot write data to tempfile %q: %v", name, err)
}
@ -286,7 +287,7 @@ func readLocalFile(writer io.Writer, ptr *Pointer, mediafile string, workingfile
defer reader.Close()
}
_, err = CopyWithCallback(writer, reader, ptr.Size, cb)
_, err = tools.CopyWithCallback(writer, reader, ptr.Size, cb)
if err != nil {
return errutil.Errorf(err, "Error reading from media file: %s", err)
}

@ -26,25 +26,6 @@ const (
var currentPlatform = PlatformUndetermined
func CopyWithCallback(writer io.Writer, reader io.Reader, totalSize int64, cb progress.CopyCallback) (int64, error) {
if success, _ := CloneFile(writer, reader); success {
if cb != nil {
cb(totalSize, totalSize, 0)
}
return totalSize, nil
}
if cb == nil {
return io.Copy(writer, reader)
}
cbReader := &progress.CallbackReader{
C: cb,
TotalSize: totalSize,
Reader: reader,
}
return io.Copy(writer, cbReader)
}
func CopyCallbackFile(event, filename string, index, totalFiles int) (progress.CopyCallback, *os.File, error) {
logPath := config.Config.Getenv("GIT_LFS_PROGRESS")
if len(logPath) == 0 || len(filename) == 0 || len(event) == 0 {

@ -2,7 +2,6 @@ package lfs
import (
"bytes"
"io/ioutil"
"strings"
"testing"
@ -40,26 +39,6 @@ func TestWriterWithCallback(t *testing.T) {
assert.Equal(t, 5, int(calledRead[1]))
}
func TestCopyWithCallback(t *testing.T) {
buf := bytes.NewBufferString("BOOYA")
called := 0
calledWritten := make([]int64, 0, 2)
n, err := CopyWithCallback(ioutil.Discard, buf, 5, func(total int64, written int64, current int) error {
called += 1
calledWritten = append(calledWritten, written)
assert.Equal(t, 5, int(total))
return nil
})
assert.Equal(t, nil, err)
assert.Equal(t, 5, int(n))
assert.Equal(t, 1, called)
assert.Equal(t, 1, len(calledWritten))
assert.Equal(t, 5, int(calledWritten[0]))
}
type TestIncludeExcludeCase struct {
expectedResult bool
includes []string

@ -1,6 +1,10 @@
package tools
import "io"
import (
"io"
"github.com/github/git-lfs/progress"
)
type readSeekCloserWrapper struct {
readSeeker io.ReadSeeker
@ -23,3 +27,23 @@ func (r *readSeekCloserWrapper) Close() error {
func NewReadSeekCloserWrapper(r io.ReadSeeker) io.ReadCloser {
return &readSeekCloserWrapper{r}
}
// CopyWithCallback copies reader to writer while performing a progress callback
func CopyWithCallback(writer io.Writer, reader io.Reader, totalSize int64, cb progress.CopyCallback) (int64, error) {
if success, _ := CloneFile(writer, reader); success {
if cb != nil {
cb(totalSize, totalSize, 0)
}
return totalSize, nil
}
if cb == nil {
return io.Copy(writer, reader)
}
cbReader := &progress.CallbackReader{
C: cb,
TotalSize: totalSize,
Reader: reader,
}
return io.Copy(writer, cbReader)
}

11
tools/util_generic.go Normal file

@ -0,0 +1,11 @@
// +build !linux !cgo
package tools
import (
"io"
)
func CloneFile(writer io.Writer, reader io.Reader) (bool, error) {
return false, nil
}

@ -1,6 +1,6 @@
// +build linux,cgo
package lfs
package tools
/*
#include <sys/ioctl.h>

29
tools/util_test.go Normal file

@ -0,0 +1,29 @@
package tools
import (
"bytes"
"io/ioutil"
"testing"
"github.com/bmizerany/assert"
)
func TestCopyWithCallback(t *testing.T) {
buf := bytes.NewBufferString("BOOYA")
called := 0
calledWritten := make([]int64, 0, 2)
n, err := CopyWithCallback(ioutil.Discard, buf, 5, func(total int64, written int64, current int) error {
called += 1
calledWritten = append(calledWritten, written)
assert.Equal(t, 5, int(total))
return nil
})
assert.Equal(t, nil, err)
assert.Equal(t, 5, int(n))
assert.Equal(t, 1, called)
assert.Equal(t, 1, len(calledWritten))
assert.Equal(t, 5, int(calledWritten[0]))
}