2015-07-23 16:53:58 +00:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
2017-09-27 23:11:42 +00:00
|
|
|
"fmt"
|
|
|
|
|
2016-11-21 22:14:33 +00:00
|
|
|
"github.com/git-lfs/git-lfs/filepathfilter"
|
2016-11-15 17:01:18 +00:00
|
|
|
"github.com/git-lfs/git-lfs/git"
|
|
|
|
"github.com/git-lfs/git-lfs/lfs"
|
|
|
|
"github.com/git-lfs/git-lfs/progress"
|
2016-05-23 18:02:27 +00:00
|
|
|
"github.com/spf13/cobra"
|
2015-07-23 16:53:58 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func checkoutCommand(cmd *cobra.Command, args []string) {
|
2015-09-08 16:23:27 +00:00
|
|
|
requireInRepo()
|
2015-07-23 16:53:58 +00:00
|
|
|
ref, err := git.CurrentRef()
|
|
|
|
if err != nil {
|
|
|
|
Panic(err, "Could not checkout")
|
|
|
|
}
|
|
|
|
|
2017-09-27 23:11:42 +00:00
|
|
|
singleCheckout := newSingleCheckout(cfg.Git, "")
|
|
|
|
if singleCheckout.Skip() {
|
|
|
|
fmt.Println("Cannot checkout LFS objects, Git LFS is not installed.")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-12-14 19:15:50 +00:00
|
|
|
var totalBytes int64
|
2017-09-22 17:27:09 +00:00
|
|
|
var pointers []*lfs.WrappedPointer
|
2016-12-14 19:15:50 +00:00
|
|
|
meter := progress.NewMeter(progress.WithOSEnv(cfg.Os))
|
2016-11-29 17:56:03 +00:00
|
|
|
chgitscanner := lfs.NewGitScanner(func(p *lfs.WrappedPointer, err error) {
|
|
|
|
if err != nil {
|
2017-04-26 15:11:21 +00:00
|
|
|
LoggedError(err, "Scanner error: %s", err)
|
2016-11-29 17:56:03 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-12-14 19:15:50 +00:00
|
|
|
totalBytes += p.Size
|
|
|
|
meter.Add(p.Size)
|
|
|
|
meter.StartTransfer(p.Name)
|
2017-09-22 17:27:09 +00:00
|
|
|
pointers = append(pointers, p)
|
2016-11-29 17:56:03 +00:00
|
|
|
})
|
|
|
|
|
2016-12-14 22:35:58 +00:00
|
|
|
chgitscanner.Filter = filepathfilter.New(rootedPaths(args), nil)
|
2016-12-06 23:52:28 +00:00
|
|
|
|
2016-12-14 17:53:34 +00:00
|
|
|
if err := chgitscanner.ScanTree(ref.Sha); err != nil {
|
2016-11-18 00:36:38 +00:00
|
|
|
ExitWithError(err)
|
|
|
|
}
|
2017-09-22 17:27:09 +00:00
|
|
|
chgitscanner.Close()
|
2016-11-18 00:36:38 +00:00
|
|
|
|
2016-12-07 02:33:25 +00:00
|
|
|
meter.Start()
|
2017-09-22 17:27:09 +00:00
|
|
|
for _, p := range pointers {
|
|
|
|
singleCheckout.Run(p)
|
|
|
|
|
|
|
|
// not strictly correct (parallel) but we don't have a callback & it's just local
|
|
|
|
// plus only 1 slot in channel so it'll block & be close
|
|
|
|
meter.TransferBytes("checkout", p.Name, p.Size, totalBytes, int(p.Size))
|
|
|
|
meter.FinishTransfer(p.Name)
|
|
|
|
}
|
|
|
|
|
2016-12-07 02:33:25 +00:00
|
|
|
meter.Finish()
|
2016-12-14 22:17:24 +00:00
|
|
|
singleCheckout.Close()
|
2015-07-23 16:53:58 +00:00
|
|
|
}
|
|
|
|
|
2016-12-14 17:56:44 +00:00
|
|
|
// Parameters are filters
|
|
|
|
// firstly convert any pathspecs to the root of the repo, in case this is being
|
|
|
|
// executed in a sub-folder
|
|
|
|
func rootedPaths(args []string) []string {
|
2016-12-14 18:34:29 +00:00
|
|
|
pathConverter, err := lfs.NewCurrentToRepoPathConverter()
|
2016-12-14 17:56:44 +00:00
|
|
|
if err != nil {
|
|
|
|
Panic(err, "Could not checkout")
|
|
|
|
}
|
|
|
|
|
|
|
|
rootedpaths := make([]string, 0, len(args))
|
|
|
|
for _, arg := range args {
|
2016-12-14 18:34:29 +00:00
|
|
|
rootedpaths = append(rootedpaths, pathConverter.Convert(arg))
|
2016-12-14 17:56:44 +00:00
|
|
|
}
|
|
|
|
return rootedpaths
|
|
|
|
}
|
|
|
|
|
2016-12-14 19:15:50 +00:00
|
|
|
func init() {
|
|
|
|
RegisterCommand("checkout", checkoutCommand, nil)
|
|
|
|
}
|