2015-07-23 16:53:58 +00:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"sync"
|
2015-07-28 13:50:19 +00:00
|
|
|
|
|
|
|
"github.com/github/git-lfs/git"
|
|
|
|
"github.com/github/git-lfs/lfs"
|
2015-07-29 09:30:07 +00:00
|
|
|
"github.com/github/git-lfs/vendor/_nuts/github.com/rubyist/tracerx"
|
2015-07-28 13:50:19 +00:00
|
|
|
"github.com/github/git-lfs/vendor/_nuts/github.com/spf13/cobra"
|
2015-07-23 16:53:58 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
checkoutCmd = &cobra.Command{
|
|
|
|
Use: "checkout",
|
2015-07-24 14:25:43 +00:00
|
|
|
Short: "Checks out LFS files into the working copy",
|
2015-07-23 16:53:58 +00:00
|
|
|
Run: checkoutCommand,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
func checkoutCommand(cmd *cobra.Command, args []string) {
|
2015-07-27 10:28:46 +00:00
|
|
|
|
2015-07-27 14:09:22 +00:00
|
|
|
// Parameters are filters
|
2015-07-28 17:03:15 +00:00
|
|
|
// firstly convert any pathspecs to the root of the repo, in case this is being executed in a sub-folder
|
|
|
|
var rootedpaths = make([]string, len(args))
|
|
|
|
inchan := make(chan string, 1)
|
|
|
|
outchan, err := lfs.ConvertCwdFilesRelativeToRepo(inchan)
|
|
|
|
if err != nil {
|
|
|
|
Panic(err, "Could not checkout")
|
|
|
|
}
|
|
|
|
for _, arg := range args {
|
|
|
|
inchan <- arg
|
|
|
|
rootedpaths = append(rootedpaths, <-outchan)
|
|
|
|
}
|
|
|
|
close(inchan)
|
|
|
|
checkoutWithIncludeExclude(rootedpaths, nil)
|
2015-07-27 10:28:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
RootCmd.AddCommand(checkoutCmd)
|
|
|
|
}
|
|
|
|
|
2015-07-29 09:26:14 +00:00
|
|
|
// Checkout from items reported from the fetch process (in parallel)
|
|
|
|
func checkoutAllFromFetchChan(c chan *lfs.WrappedPointer) {
|
|
|
|
tracerx.Printf("starting fetch/parallel checkout")
|
|
|
|
checkoutFromFetchChan(nil, nil, c)
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkoutFromFetchChan(include []string, exclude []string, in chan *lfs.WrappedPointer) {
|
|
|
|
ref, err := git.CurrentRef()
|
|
|
|
if err != nil {
|
|
|
|
Panic(err, "Could not checkout")
|
|
|
|
}
|
|
|
|
// Need to ScanTree to identify multiple files with the same content (fetch will only report oids once)
|
|
|
|
pointers, err := lfs.ScanTree(ref)
|
|
|
|
if err != nil {
|
|
|
|
Panic(err, "Could not scan for Git LFS files")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Map oid to multiple pointers
|
|
|
|
mapping := make(map[string][]*lfs.WrappedPointer)
|
|
|
|
for _, pointer := range pointers {
|
|
|
|
if lfs.FilenamePassesIncludeExcludeFilter(pointer.Name, include, exclude) {
|
|
|
|
mapping[pointer.Oid] = append(mapping[pointer.Oid], pointer)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Launch git update-index
|
|
|
|
c := make(chan *lfs.WrappedPointer)
|
|
|
|
var wait sync.WaitGroup
|
|
|
|
wait.Add(1)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
checkoutWithChan(c)
|
|
|
|
wait.Done()
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Feed it from in, which comes from fetch
|
|
|
|
for p := range in {
|
|
|
|
// Add all of the files for this oid
|
|
|
|
for _, fp := range mapping[p.Oid] {
|
|
|
|
c <- fp
|
|
|
|
}
|
|
|
|
}
|
|
|
|
close(c)
|
|
|
|
wait.Wait()
|
|
|
|
}
|
|
|
|
|
2015-07-27 14:09:22 +00:00
|
|
|
func checkoutWithIncludeExclude(include []string, exclude []string) {
|
2015-07-23 16:53:58 +00:00
|
|
|
ref, err := git.CurrentRef()
|
|
|
|
if err != nil {
|
|
|
|
Panic(err, "Could not checkout")
|
|
|
|
}
|
|
|
|
|
2015-07-27 16:26:45 +00:00
|
|
|
pointers, err := lfs.ScanTree(ref)
|
2015-07-23 16:53:58 +00:00
|
|
|
if err != nil {
|
|
|
|
Panic(err, "Could not scan for Git LFS files")
|
|
|
|
}
|
|
|
|
|
|
|
|
var wait sync.WaitGroup
|
|
|
|
wait.Add(1)
|
|
|
|
|
2015-08-03 09:41:20 +00:00
|
|
|
c := make(chan *lfs.WrappedPointer, 1)
|
2015-07-23 16:53:58 +00:00
|
|
|
|
2015-07-28 09:18:42 +00:00
|
|
|
go func() {
|
|
|
|
checkoutWithChan(c)
|
|
|
|
wait.Done()
|
|
|
|
}()
|
2015-07-31 15:25:41 +00:00
|
|
|
|
|
|
|
// Count bytes for progress
|
|
|
|
var totalBytes int64
|
|
|
|
for _, pointer := range pointers {
|
|
|
|
totalBytes += pointer.Size
|
|
|
|
}
|
|
|
|
progress := lfs.NewProgressMeter(len(pointers), totalBytes, false)
|
2015-08-03 09:41:20 +00:00
|
|
|
progress.Start()
|
2015-07-31 15:25:41 +00:00
|
|
|
totalBytes = 0
|
2015-07-23 16:53:58 +00:00
|
|
|
for _, pointer := range pointers {
|
2015-07-31 15:25:41 +00:00
|
|
|
totalBytes += pointer.Size
|
2015-07-27 14:09:22 +00:00
|
|
|
if lfs.FilenamePassesIncludeExcludeFilter(pointer.Name, include, exclude) {
|
2015-07-31 15:25:41 +00:00
|
|
|
progress.Add(pointer.Name)
|
2015-07-27 14:09:22 +00:00
|
|
|
c <- pointer
|
2015-07-31 15:25:41 +00:00
|
|
|
// 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
|
|
|
|
progress.TransferBytes("checkout", pointer.Name, pointer.Size, totalBytes, int(pointer.Size))
|
|
|
|
progress.FinishTransfer(pointer.Name)
|
|
|
|
} else {
|
|
|
|
progress.Skip(pointer.Size)
|
2015-07-27 14:09:22 +00:00
|
|
|
}
|
2015-07-23 16:53:58 +00:00
|
|
|
}
|
|
|
|
close(c)
|
|
|
|
wait.Wait()
|
2015-07-31 15:25:41 +00:00
|
|
|
progress.Finish()
|
2015-07-23 16:53:58 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-07-27 14:09:22 +00:00
|
|
|
func checkoutAll() {
|
|
|
|
checkoutWithIncludeExclude(nil, nil)
|
|
|
|
}
|
|
|
|
|
2015-07-23 16:53:58 +00:00
|
|
|
// Populate the working copy with the real content of objects where the file is
|
|
|
|
// either missing, or contains a matching pointer placeholder, from a list of pointers.
|
|
|
|
// If the file exists but has other content it is left alone
|
2015-07-28 09:18:42 +00:00
|
|
|
func checkoutWithChan(in <-chan *lfs.WrappedPointer) {
|
|
|
|
// Fire up the update-index command
|
|
|
|
cmd := exec.Command("git", "update-index", "-q", "--refresh", "--stdin")
|
|
|
|
updateIdxStdin, err := cmd.StdinPipe()
|
|
|
|
if err != nil {
|
|
|
|
Panic(err, "Could not update the index")
|
|
|
|
}
|
2015-07-23 16:53:58 +00:00
|
|
|
|
2015-07-28 09:18:42 +00:00
|
|
|
if err := cmd.Start(); err != nil {
|
|
|
|
Panic(err, "Could not update the index")
|
|
|
|
}
|
2015-07-23 16:53:58 +00:00
|
|
|
|
2015-07-28 17:03:15 +00:00
|
|
|
// Get a converter from repo-relative to cwd-relative
|
|
|
|
// Since writing data & calling git update-index must be relative to cwd
|
|
|
|
repopathchan := make(chan string, 1)
|
|
|
|
cwdpathchan, err := lfs.ConvertRepoFilesRelativeToCwd(repopathchan)
|
|
|
|
if err != nil {
|
|
|
|
Panic(err, "Could not convert file paths")
|
|
|
|
}
|
|
|
|
|
2015-07-28 09:18:42 +00:00
|
|
|
// As files come in, write them to the wd and update the index
|
|
|
|
for pointer := range in {
|
2015-07-23 16:53:58 +00:00
|
|
|
|
2015-07-28 09:18:42 +00:00
|
|
|
// Check the content - either missing or still this pointer (not exist is ok)
|
|
|
|
filepointer, err := lfs.DecodePointerFromFile(pointer.Name)
|
|
|
|
if err != nil && !os.IsNotExist(err) {
|
|
|
|
if err == lfs.NotAPointerError {
|
|
|
|
// File has non-pointer content, leave it alone
|
|
|
|
continue
|
2015-07-23 16:53:58 +00:00
|
|
|
}
|
2015-07-28 09:18:42 +00:00
|
|
|
Panic(err, "Problem accessing %v", pointer.Name)
|
|
|
|
}
|
|
|
|
if filepointer != nil && filepointer.Oid != pointer.Oid {
|
|
|
|
// User has probably manually reset a file to another commit
|
|
|
|
// while leaving it a pointer; don't mess with this
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// OK now we can (over)write the file content
|
2015-07-28 17:03:15 +00:00
|
|
|
repopathchan <- pointer.Name
|
|
|
|
cwdfilepath := <-cwdpathchan
|
2015-08-06 16:12:11 +00:00
|
|
|
// smudge but set auto-download to false
|
|
|
|
err = lfs.PointerSmudgeToFile(cwdfilepath, pointer.Pointer, false, nil)
|
2015-07-28 09:18:42 +00:00
|
|
|
if err != nil {
|
2015-08-06 16:12:11 +00:00
|
|
|
if err == lfs.DownloadDeclinedError {
|
|
|
|
// acceptable error, data not local (fetch not run or include/exclude)
|
|
|
|
LoggedError(err, "Skipped checkout for %v, content not local. Use fetch to download.", pointer.Name)
|
|
|
|
} else {
|
|
|
|
Panic(err, "Could not checkout file")
|
|
|
|
}
|
2015-07-23 16:53:58 +00:00
|
|
|
}
|
|
|
|
|
2015-07-28 17:03:15 +00:00
|
|
|
updateIdxStdin.Write([]byte(cwdfilepath + "\n"))
|
2015-07-28 09:18:42 +00:00
|
|
|
}
|
2015-07-28 17:03:15 +00:00
|
|
|
close(repopathchan)
|
2015-07-28 09:18:42 +00:00
|
|
|
|
|
|
|
updateIdxStdin.Close()
|
|
|
|
if err := cmd.Wait(); err != nil {
|
|
|
|
Panic(err, "Error updating the git index")
|
|
|
|
}
|
2015-07-23 16:53:58 +00:00
|
|
|
|
|
|
|
}
|