github.com/2lambda123/git-lfs@v2.5.2+incompatible/commands/command_checkout.go (about)

     1  package commands
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"strings"
     7  
     8  	"github.com/git-lfs/git-lfs/filepathfilter"
     9  	"github.com/git-lfs/git-lfs/git"
    10  	"github.com/git-lfs/git-lfs/lfs"
    11  	"github.com/git-lfs/git-lfs/tasklog"
    12  	"github.com/git-lfs/git-lfs/tq"
    13  	"github.com/spf13/cobra"
    14  )
    15  
    16  func checkoutCommand(cmd *cobra.Command, args []string) {
    17  	requireInRepo()
    18  
    19  	msg := []string{
    20  		"WARNING: 'git lfs checkout' is deprecated and will be removed in v3.0.0.",
    21  
    22  		"'git checkout' has been updated in upstream Git to have comparable speeds",
    23  		"to 'git lfs checkout'.",
    24  	}
    25  	fmt.Fprintln(os.Stderr, strings.Join(msg, "\n"))
    26  
    27  	ref, err := git.CurrentRef()
    28  	if err != nil {
    29  		Panic(err, "Could not checkout")
    30  	}
    31  
    32  	singleCheckout := newSingleCheckout(cfg.Git, "")
    33  	if singleCheckout.Skip() {
    34  		fmt.Println("Cannot checkout LFS objects, Git LFS is not installed.")
    35  		return
    36  	}
    37  
    38  	var totalBytes int64
    39  	var pointers []*lfs.WrappedPointer
    40  	logger := tasklog.NewLogger(os.Stdout)
    41  	meter := tq.NewMeter()
    42  	meter.Direction = tq.Checkout
    43  	meter.Logger = meter.LoggerFromEnv(cfg.Os)
    44  	logger.Enqueue(meter)
    45  	chgitscanner := lfs.NewGitScanner(func(p *lfs.WrappedPointer, err error) {
    46  		if err != nil {
    47  			LoggedError(err, "Scanner error: %s", err)
    48  			return
    49  		}
    50  
    51  		totalBytes += p.Size
    52  		meter.Add(p.Size)
    53  		meter.StartTransfer(p.Name)
    54  		pointers = append(pointers, p)
    55  	})
    56  
    57  	chgitscanner.Filter = filepathfilter.New(rootedPaths(args), nil)
    58  
    59  	if err := chgitscanner.ScanTree(ref.Sha); err != nil {
    60  		ExitWithError(err)
    61  	}
    62  	chgitscanner.Close()
    63  
    64  	meter.Start()
    65  	for _, p := range pointers {
    66  		singleCheckout.Run(p)
    67  
    68  		// not strictly correct (parallel) but we don't have a callback & it's just local
    69  		// plus only 1 slot in channel so it'll block & be close
    70  		meter.TransferBytes("checkout", p.Name, p.Size, totalBytes, int(p.Size))
    71  		meter.FinishTransfer(p.Name)
    72  	}
    73  
    74  	meter.Finish()
    75  	singleCheckout.Close()
    76  }
    77  
    78  // Parameters are filters
    79  // firstly convert any pathspecs to the root of the repo, in case this is being
    80  // executed in a sub-folder
    81  func rootedPaths(args []string) []string {
    82  	pathConverter, err := lfs.NewCurrentToRepoPathConverter(cfg)
    83  	if err != nil {
    84  		Panic(err, "Could not checkout")
    85  	}
    86  
    87  	rootedpaths := make([]string, 0, len(args))
    88  	for _, arg := range args {
    89  		rootedpaths = append(rootedpaths, pathConverter.Convert(arg))
    90  	}
    91  	return rootedpaths
    92  }
    93  
    94  func init() {
    95  	RegisterCommand("checkout", checkoutCommand, nil)
    96  }