git-lfs/commands/command_push.go

210 lines
5.1 KiB
Go
Raw Normal View History

package commands
2013-10-04 17:09:03 +00:00
import (
"fmt"
2015-03-19 19:30:55 +00:00
"github.com/github/git-lfs/git"
"github.com/github/git-lfs/lfs"
"github.com/github/git-lfs/pointer"
"github.com/github/git-lfs/scanner"
2014-10-04 16:10:02 +00:00
"github.com/rubyist/tracerx"
2014-06-26 20:53:37 +00:00
"github.com/spf13/cobra"
"io/ioutil"
"os"
"path/filepath"
2014-03-12 14:55:01 +00:00
"strings"
2013-10-04 17:09:03 +00:00
)
2014-06-26 20:53:37 +00:00
var (
pushCmd = &cobra.Command{
Use: "push",
2015-03-19 19:30:55 +00:00
Short: "Push files to the Git LFS endpoint",
2014-06-26 20:53:37 +00:00
Run: pushCommand,
}
dryRun = false
useStdin = false
deleteBranch = "(delete)"
2014-06-26 20:53:37 +00:00
)
2013-10-04 17:09:03 +00:00
2015-03-19 19:30:55 +00:00
// pushCommand is the command that's run via `git lfs push`. It has two modes
2014-09-24 17:10:29 +00:00
// of operation. The primary mode is run via the git pre-push hook. The pre-push
// hook passes two arguments on the command line:
// 1. Name of the remote to which the push is being done
// 2. URL to which the push is being done
//
// The hook receives commit information on stdin in the form:
// <local ref> <local sha1> <remote ref> <remote sha1>
//
// In the typical case, pushCommand will get a list of git objects being pushed
// by using the following:
// git rev-list --objects <local sha1> ^<remote sha1>
//
2015-03-19 19:30:55 +00:00
// If any of those git objects are associated with Git LFS objects, those
// objects will be pushed to the Git LFS API.
2014-09-24 17:10:29 +00:00
//
// In the case of pushing a new branch, the list of git objects will be all of
// the git objects in this branch.
//
2015-03-19 19:30:55 +00:00
// In the case of deleting a branch, no attempts to push Git LFS objects will be
2014-09-24 17:10:29 +00:00
// made.
//
// The other mode of operation is the dry run mode. In this mode, the repo
// and refspec are passed on the command line. pushCommand will calculate the
// git objects that would be pushed in a similar manner as above and will print
// out each file name.
2014-06-26 20:53:37 +00:00
func pushCommand(cmd *cobra.Command, args []string) {
var left, right string
2014-06-05 18:48:23 +00:00
if len(args) == 0 {
2015-03-19 19:30:55 +00:00
Print("The git lfs pre-push hook is out of date. Please run `git lfs update`")
os.Exit(1)
}
2015-03-19 19:30:55 +00:00
lfs.Config.CurrentRemote = args[0]
if useStdin {
refsData, err := ioutil.ReadAll(os.Stdin)
if err != nil {
Panic(err, "Error reading refs on stdin")
}
if len(refsData) == 0 {
return
}
left, right = decodeRefs(string(refsData))
if left == deleteBranch {
return
}
} else {
var repo, refspec string
2014-09-29 16:52:24 +00:00
if len(args) < 1 {
2015-03-19 19:30:55 +00:00
Print("Usage: git lfs push --dry-run <repo> [refspec]")
return
}
repo = args[0]
if len(args) == 2 {
refspec = args[1]
}
localRef, err := git.CurrentRef()
if err != nil {
Panic(err, "Error getting local ref")
}
left = localRef
remoteRef, err := git.LsRemote(repo, refspec)
if err != nil {
Panic(err, "Error getting remote ref")
}
if remoteRef != "" {
2014-09-30 16:04:27 +00:00
right = "^" + strings.Split(remoteRef, "\t")[0]
2014-09-29 16:52:24 +00:00
}
}
2014-10-20 18:49:15 +00:00
// Just use scanner here
pointers, err := scanner.Scan(left, right)
if err != nil {
2015-03-19 19:30:55 +00:00
Panic(err, "Error scanning for Git LFS files")
2014-10-20 18:49:15 +00:00
}
2014-10-20 18:49:15 +00:00
for i, pointer := range pointers {
if dryRun {
2014-10-20 18:49:15 +00:00
Print("push %s", pointer.Name)
continue
}
2014-10-20 18:49:15 +00:00
if wErr := pushAsset(pointer.Oid, pointer.Name, i+1, len(pointers)); wErr != nil {
2015-02-17 18:46:08 +00:00
if Debugging || wErr.Panic {
Panic(wErr.Err, wErr.Error())
} else {
Exit(wErr.Error())
}
}
}
2013-10-04 17:09:03 +00:00
}
2015-03-19 19:30:55 +00:00
// pushAsset pushes the asset with the given oid to the Git LFS API.
func pushAsset(oid, filename string, index, totalFiles int) *lfs.WrappedError {
2015-01-23 22:11:10 +00:00
tracerx.Printf("checking_asset: %s %s %d/%d", oid, filename, index, totalFiles)
2015-03-19 19:30:55 +00:00
path, err := lfs.LocalMediaPath(oid)
if err != nil {
2015-03-19 19:30:55 +00:00
return lfs.Errorf(err, "Error uploading file %s (%s)", filename, oid)
2014-08-07 17:15:28 +00:00
}
if err := ensureFile(filename, path); err != nil {
2015-03-19 19:30:55 +00:00
return lfs.Errorf(err, "Error uploading file %s (%s)", filename, oid)
}
2015-03-19 19:30:55 +00:00
cb, file, cbErr := lfs.CopyCallbackFile("push", filename, index, totalFiles)
2015-01-27 17:07:11 +00:00
if cbErr != nil {
Error(cbErr.Error())
}
2015-01-27 17:07:11 +00:00
if file != nil {
defer file.Close()
}
2015-04-08 14:55:54 +00:00
fmt.Fprintf(os.Stderr, "Uploading %s\n", filename)
2015-03-19 19:30:55 +00:00
return lfs.Upload(path, filename, cb)
2014-08-07 17:15:28 +00:00
}
// ensureFile makes sure that the cleanPath exists before pushing it. If it
// does not exist, it attempts to clean it by reading the file at smudgePath.
func ensureFile(smudgePath, cleanPath string) error {
if _, err := os.Stat(cleanPath); err == nil {
return nil
}
expectedOid := filepath.Base(cleanPath)
2015-03-19 19:30:55 +00:00
localPath := filepath.Join(lfs.LocalWorkingDir, smudgePath)
file, err := os.Open(localPath)
if err != nil {
return err
}
defer file.Close()
stat, err := file.Stat()
if err != nil {
return err
}
cleaned, err := pointer.Clean(file, stat.Size(), nil)
if err != nil {
return err
}
cleaned.Close()
if expectedOid != cleaned.Oid {
return fmt.Errorf("Expected %s to have an OID of %s, got %s", smudgePath, expectedOid, cleaned.Oid)
}
return nil
}
2014-09-24 17:10:29 +00:00
// decodeRefs pulls the sha1s out of the line read from the pre-push
// hook's stdin.
func decodeRefs(input string) (string, string) {
refs := strings.Split(strings.TrimSpace(input), " ")
var left, right string
if len(refs) > 1 {
left = refs[1]
}
if len(refs) > 3 {
right = "^" + refs[3]
}
return left, right
}
2013-10-04 17:09:03 +00:00
func init() {
pushCmd.Flags().BoolVarP(&dryRun, "dry-run", "d", false, "Do everything except actually send the updates")
pushCmd.Flags().BoolVarP(&useStdin, "stdin", "s", false, "Take refs on stdin (for pre-push hook)")
2014-06-26 20:53:37 +00:00
RootCmd.AddCommand(pushCmd)
2013-10-04 17:09:03 +00:00
}