github.com/psexton/git-lfs@v2.1.1-0.20170517224304-289a18b2bc53+incompatible/commands/command_checkout.go (about)

     1  package commands
     2  
     3  import (
     4  	"github.com/git-lfs/git-lfs/filepathfilter"
     5  	"github.com/git-lfs/git-lfs/git"
     6  	"github.com/git-lfs/git-lfs/lfs"
     7  	"github.com/git-lfs/git-lfs/progress"
     8  	"github.com/spf13/cobra"
     9  )
    10  
    11  func checkoutCommand(cmd *cobra.Command, args []string) {
    12  	requireInRepo()
    13  	ref, err := git.CurrentRef()
    14  	if err != nil {
    15  		Panic(err, "Could not checkout")
    16  	}
    17  
    18  	var totalBytes int64
    19  	meter := progress.NewMeter(progress.WithOSEnv(cfg.Os))
    20  	singleCheckout := newSingleCheckout()
    21  	chgitscanner := lfs.NewGitScanner(func(p *lfs.WrappedPointer, err error) {
    22  		if err != nil {
    23  			LoggedError(err, "Scanner error: %s", err)
    24  			return
    25  		}
    26  
    27  		totalBytes += p.Size
    28  		meter.Add(p.Size)
    29  		meter.StartTransfer(p.Name)
    30  
    31  		singleCheckout.Run(p)
    32  
    33  		// not strictly correct (parallel) but we don't have a callback & it's just local
    34  		// plus only 1 slot in channel so it'll block & be close
    35  		meter.TransferBytes("checkout", p.Name, p.Size, totalBytes, int(p.Size))
    36  		meter.FinishTransfer(p.Name)
    37  	})
    38  
    39  	chgitscanner.Filter = filepathfilter.New(rootedPaths(args), nil)
    40  
    41  	if err := chgitscanner.ScanTree(ref.Sha); err != nil {
    42  		ExitWithError(err)
    43  	}
    44  
    45  	meter.Start()
    46  	chgitscanner.Close()
    47  	meter.Finish()
    48  	singleCheckout.Close()
    49  }
    50  
    51  // Parameters are filters
    52  // firstly convert any pathspecs to the root of the repo, in case this is being
    53  // executed in a sub-folder
    54  func rootedPaths(args []string) []string {
    55  	pathConverter, err := lfs.NewCurrentToRepoPathConverter()
    56  	if err != nil {
    57  		Panic(err, "Could not checkout")
    58  	}
    59  
    60  	rootedpaths := make([]string, 0, len(args))
    61  	for _, arg := range args {
    62  		rootedpaths = append(rootedpaths, pathConverter.Convert(arg))
    63  	}
    64  	return rootedpaths
    65  }
    66  
    67  func init() {
    68  	RegisterCommand("checkout", checkoutCommand, nil)
    69  }