progress,tools: move CopyCallback (and related) to 'tools'

This commit is contained in:
Taylor Blau 2017-11-21 18:00:50 -08:00
parent 45c580e60a
commit a4ff7b8d41
12 changed files with 28 additions and 33 deletions

@ -6,7 +6,7 @@ import (
"github.com/git-lfs/git-lfs/errors"
"github.com/git-lfs/git-lfs/lfs"
"github.com/git-lfs/git-lfs/progress"
"github.com/git-lfs/git-lfs/tools"
"github.com/spf13/cobra"
)
@ -23,7 +23,7 @@ import (
// If the object read from "from" is _already_ a clean pointer, then it will be
// written out verbatim to "to", without trying to make it a pointer again.
func clean(gf *lfs.GitFilter, to io.Writer, from io.Reader, fileName string, fileSize int64) (*lfs.Pointer, error) {
var cb progress.CopyCallback
var cb tools.CopyCallback
var file *os.File
if len(fileName) > 0 {

@ -9,7 +9,6 @@ import (
"os"
"github.com/git-lfs/git-lfs/errors"
"github.com/git-lfs/git-lfs/progress"
"github.com/git-lfs/git-lfs/tools"
)
@ -18,7 +17,7 @@ type cleanedAsset struct {
*Pointer
}
func (f *GitFilter) Clean(reader io.Reader, fileName string, fileSize int64, cb progress.CopyCallback) (*cleanedAsset, error) {
func (f *GitFilter) Clean(reader io.Reader, fileName string, fileSize int64, cb tools.CopyCallback) (*cleanedAsset, error) {
extensions, err := f.cfg.SortedExtensions()
if err != nil {
return nil, err
@ -61,7 +60,7 @@ func (f *GitFilter) Clean(reader io.Reader, fileName string, fileSize int64, cb
return &cleanedAsset{tmp.Name(), pointer}, err
}
func (f *GitFilter) copyToTemp(reader io.Reader, fileSize int64, cb progress.CopyCallback) (oid string, size int64, tmp *os.File, err error) {
func (f *GitFilter) copyToTemp(reader io.Reader, fileSize int64, cb tools.CopyCallback) (oid string, size int64, tmp *os.File, err error) {
tmp, err = ioutil.TempFile(f.cfg.TempDir(), "")
if err != nil {
return

@ -6,17 +6,15 @@ import (
"os"
"path/filepath"
"github.com/git-lfs/git-lfs/config"
"github.com/git-lfs/git-lfs/errors"
"github.com/git-lfs/git-lfs/tools"
"github.com/git-lfs/git-lfs/tools/humanize"
"github.com/git-lfs/git-lfs/tq"
"github.com/git-lfs/git-lfs/config"
"github.com/git-lfs/git-lfs/errors"
"github.com/git-lfs/git-lfs/progress"
"github.com/rubyist/tracerx"
)
func (f *GitFilter) SmudgeToFile(filename string, ptr *Pointer, download bool, manifest *tq.Manifest, cb progress.CopyCallback) error {
func (f *GitFilter) SmudgeToFile(filename string, ptr *Pointer, download bool, manifest *tq.Manifest, cb tools.CopyCallback) error {
os.MkdirAll(filepath.Dir(filename), 0755)
file, err := os.Create(filename)
if err != nil {
@ -36,7 +34,7 @@ func (f *GitFilter) SmudgeToFile(filename string, ptr *Pointer, download bool, m
return nil
}
func (f *GitFilter) Smudge(writer io.Writer, ptr *Pointer, workingfile string, download bool, manifest *tq.Manifest, cb progress.CopyCallback) (int64, error) {
func (f *GitFilter) Smudge(writer io.Writer, ptr *Pointer, workingfile string, download bool, manifest *tq.Manifest, cb tools.CopyCallback) (int64, error) {
mediafile, err := f.ObjectPath(ptr.Oid)
if err != nil {
return 0, err
@ -73,10 +71,10 @@ func (f *GitFilter) Smudge(writer io.Writer, ptr *Pointer, workingfile string, d
return n, nil
}
func (f *GitFilter) downloadFile(writer io.Writer, ptr *Pointer, workingfile, mediafile string, manifest *tq.Manifest, cb progress.CopyCallback) (int64, error) {
func (f *GitFilter) downloadFile(writer io.Writer, ptr *Pointer, workingfile, mediafile string, manifest *tq.Manifest, cb tools.CopyCallback) (int64, error) {
fmt.Fprintf(os.Stderr, "Downloading %s (%s)\n", workingfile, humanize.FormatBytes(uint64(ptr.Size)))
// NOTE: if given, "cb" is a progress.CopyCallback which writes updates
// NOTE: if given, "cb" is a tools.CopyCallback which writes updates
// to the logpath specified by GIT_LFS_PROGRESS.
//
// Either way, forward it into the *tq.TransferQueue so that updates are
@ -101,7 +99,7 @@ func (f *GitFilter) downloadFile(writer io.Writer, ptr *Pointer, workingfile, me
return f.readLocalFile(writer, ptr, mediafile, workingfile, nil)
}
func (f *GitFilter) readLocalFile(writer io.Writer, ptr *Pointer, mediafile string, workingfile string, cb progress.CopyCallback) (int64, error) {
func (f *GitFilter) readLocalFile(writer io.Writer, ptr *Pointer, mediafile string, workingfile string, cb tools.CopyCallback) (int64, error) {
reader, err := os.Open(mediafile)
if err != nil {
return 0, errors.Wrapf(err, "Error opening media file.")

@ -9,7 +9,6 @@ import (
"runtime"
"github.com/git-lfs/git-lfs/config"
"github.com/git-lfs/git-lfs/progress"
"github.com/git-lfs/git-lfs/tools"
)
@ -25,7 +24,7 @@ const (
var currentPlatform = PlatformUndetermined
func (f *GitFilter) CopyCallbackFile(event, filename string, index, totalFiles int) (progress.CopyCallback, *os.File, error) {
func (f *GitFilter) CopyCallbackFile(event, filename string, index, totalFiles int) (tools.CopyCallback, *os.File, error) {
logPath, _ := f.cfg.Os.Get("GIT_LFS_PROGRESS")
if len(logPath) == 0 || len(filename) == 0 || len(event) == 0 {
return nil, nil, nil
@ -47,7 +46,7 @@ func (f *GitFilter) CopyCallbackFile(event, filename string, index, totalFiles i
var prevWritten int64
cb := progress.CopyCallback(func(total int64, written int64, current int) error {
cb := tools.CopyCallback(func(total int64, written int64, current int) error {
if written != prevWritten {
_, err := file.Write([]byte(fmt.Sprintf("%s %d/%d %d/%d %s\n", event, index, totalFiles, written, total, filename)))
file.Sync()

@ -4,7 +4,7 @@ import (
"bytes"
"testing"
"github.com/git-lfs/git-lfs/progress"
"github.com/git-lfs/git-lfs/tools"
"github.com/stretchr/testify/assert"
)
@ -18,7 +18,7 @@ func TestBodyWithCallback(t *testing.T) {
assert.Equal(t, 5, int(total))
return nil
}
reader := progress.NewByteBodyWithCallback([]byte("BOOYA"), 5, cb)
reader := tools.NewByteBodyWithCallback([]byte("BOOYA"), 5, cb)
readBuf := make([]byte, 3)
n, err := reader.Read(readBuf)
@ -39,7 +39,7 @@ func TestReadWithCallback(t *testing.T) {
called := 0
calledRead := make([]int64, 0, 2)
reader := &progress.CallbackReader{
reader := &tools.CallbackReader{
TotalSize: 5,
Reader: bytes.NewBufferString("BOOYA"),
C: func(total int64, read int64, current int) error {

@ -16,7 +16,6 @@ import (
"github.com/git-lfs/git-lfs/config"
"github.com/git-lfs/git-lfs/lfsapi"
"github.com/git-lfs/git-lfs/progress"
"github.com/git-lfs/git-lfs/tools"
)
@ -192,7 +191,7 @@ func performUpload(apiClient *lfsapi.Client, oid string, size int64, a *action,
sendProgress(oid, readSoFar, readSinceLast, writer, errWriter)
return nil
}
req.Body = progress.NewBodyWithCallback(f, size, cb)
req.Body = tools.NewBodyWithCallback(f, size, cb)
res, err := apiClient.DoWithAuth("origin", req)
if err != nil {

@ -1,4 +1,4 @@
package progress
package tools
import (
"bytes"

@ -1,4 +1,4 @@
package progress
package tools
import (
"io"

@ -10,7 +10,6 @@ import (
"os"
"github.com/git-lfs/git-lfs/errors"
"github.com/git-lfs/git-lfs/progress"
)
const (
@ -21,7 +20,7 @@ const (
)
// 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) {
func CopyWithCallback(writer io.Writer, reader io.Reader, totalSize int64, cb CopyCallback) (int64, error) {
if success, _ := CloneFile(writer, reader); success {
if cb != nil {
cb(totalSize, totalSize, 0)
@ -32,7 +31,7 @@ func CopyWithCallback(writer io.Writer, reader io.Reader, totalSize int64, cb pr
return io.Copy(writer, reader)
}
cbReader := &progress.CallbackReader{
cbReader := &CallbackReader{
C: cb,
TotalSize: totalSize,
Reader: reader,

@ -10,7 +10,7 @@ import (
"github.com/git-lfs/git-lfs/errors"
"github.com/git-lfs/git-lfs/lfsapi"
"github.com/git-lfs/git-lfs/progress"
"github.com/git-lfs/git-lfs/tools"
)
const (
@ -83,7 +83,7 @@ func (a *basicUploadAdapter) DoTransfer(ctx interface{}, t *Transfer, cb Progres
return nil
}
cbr := progress.NewBodyWithCallback(f, t.Size, ccb)
cbr := tools.NewBodyWithCallback(f, t.Size, ccb)
var reader lfsapi.ReadSeekCloser = cbr
// Signal auth was ok on first read; this frees up other workers to start

@ -8,6 +8,7 @@ import (
"github.com/git-lfs/git-lfs/errors"
"github.com/git-lfs/git-lfs/lfsapi"
"github.com/git-lfs/git-lfs/progress"
"github.com/git-lfs/git-lfs/tools"
"github.com/rubyist/tracerx"
)
@ -107,7 +108,7 @@ type TransferQueue struct {
adapterInProgress bool
adapterInitMutex sync.Mutex
dryRun bool
cb progress.CopyCallback
cb tools.CopyCallback
meter progress.Meter
errors []error
transfers map[string]*objects
@ -183,7 +184,7 @@ func WithProgress(m progress.Meter) Option {
}
}
func WithProgressCallback(cb progress.CopyCallback) Option {
func WithProgressCallback(cb tools.CopyCallback) Option {
return func(tq *TransferQueue) {
tq.cb = cb
}

@ -10,7 +10,7 @@ import (
"github.com/git-lfs/git-lfs/errors"
"github.com/git-lfs/git-lfs/lfsapi"
"github.com/git-lfs/git-lfs/progress"
"github.com/git-lfs/git-lfs/tools"
)
const (
@ -119,7 +119,7 @@ func (a *tusUploadAdapter) DoTransfer(ctx interface{}, t *Transfer, cb ProgressC
return nil
}
var reader lfsapi.ReadSeekCloser = progress.NewBodyWithCallback(f, t.Size, ccb)
var reader lfsapi.ReadSeekCloser = tools.NewBodyWithCallback(f, t.Size, ccb)
reader = newStartCallbackReader(reader, func() error {
// seek to the offset since lfsapi.Client rewinds the body
if _, err := f.Seek(offset, os.SEEK_CUR); err != nil {