From a4ff7b8d413b17b43c3426e21ed7b57c12a029fa Mon Sep 17 00:00:00 2001 From: Taylor Blau Date: Tue, 21 Nov 2017 18:00:50 -0800 Subject: [PATCH] progress,tools: move CopyCallback (and related) to 'tools' --- commands/command_clean.go | 4 ++-- lfs/gitfilter_clean.go | 5 ++--- lfs/gitfilter_smudge.go | 16 +++++++--------- lfs/util.go | 5 ++--- lfs/util_test.go | 6 +++--- test/cmd/lfstest-customadapter.go | 3 +-- {progress => tools}/copycallback.go | 2 +- {progress => tools}/copycallback_test.go | 2 +- tools/iotools.go | 5 ++--- tq/basic_upload.go | 4 ++-- tq/transfer_queue.go | 5 +++-- tq/tus_upload.go | 4 ++-- 12 files changed, 28 insertions(+), 33 deletions(-) rename {progress => tools}/copycallback.go (99%) rename {progress => tools}/copycallback_test.go (99%) diff --git a/commands/command_clean.go b/commands/command_clean.go index 94394277..1910e069 100644 --- a/commands/command_clean.go +++ b/commands/command_clean.go @@ -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 { diff --git a/lfs/gitfilter_clean.go b/lfs/gitfilter_clean.go index 189edce8..42bef6d8 100644 --- a/lfs/gitfilter_clean.go +++ b/lfs/gitfilter_clean.go @@ -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 diff --git a/lfs/gitfilter_smudge.go b/lfs/gitfilter_smudge.go index e3c82f61..7eaa24e7 100644 --- a/lfs/gitfilter_smudge.go +++ b/lfs/gitfilter_smudge.go @@ -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.") diff --git a/lfs/util.go b/lfs/util.go index e54d9aec..611999c3 100644 --- a/lfs/util.go +++ b/lfs/util.go @@ -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() diff --git a/lfs/util_test.go b/lfs/util_test.go index ae0c61e8..5bb29b97 100644 --- a/lfs/util_test.go +++ b/lfs/util_test.go @@ -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 { diff --git a/test/cmd/lfstest-customadapter.go b/test/cmd/lfstest-customadapter.go index 93c341df..e0e06dff 100644 --- a/test/cmd/lfstest-customadapter.go +++ b/test/cmd/lfstest-customadapter.go @@ -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 { diff --git a/progress/copycallback.go b/tools/copycallback.go similarity index 99% rename from progress/copycallback.go rename to tools/copycallback.go index 6eab8b17..d8d33ebe 100644 --- a/progress/copycallback.go +++ b/tools/copycallback.go @@ -1,4 +1,4 @@ -package progress +package tools import ( "bytes" diff --git a/progress/copycallback_test.go b/tools/copycallback_test.go similarity index 99% rename from progress/copycallback_test.go rename to tools/copycallback_test.go index 82655ee2..8dbd7871 100644 --- a/progress/copycallback_test.go +++ b/tools/copycallback_test.go @@ -1,4 +1,4 @@ -package progress +package tools import ( "io" diff --git a/tools/iotools.go b/tools/iotools.go index 0e9dab1f..58df68f4 100644 --- a/tools/iotools.go +++ b/tools/iotools.go @@ -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, diff --git a/tq/basic_upload.go b/tq/basic_upload.go index a0582bb1..00ced92e 100644 --- a/tq/basic_upload.go +++ b/tq/basic_upload.go @@ -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 diff --git a/tq/transfer_queue.go b/tq/transfer_queue.go index c3078733..7db06152 100644 --- a/tq/transfer_queue.go +++ b/tq/transfer_queue.go @@ -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 } diff --git a/tq/tus_upload.go b/tq/tus_upload.go index 76084aac..939ff02d 100644 --- a/tq/tus_upload.go +++ b/tq/tus_upload.go @@ -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 {