git-lfs/commands/command_push.go

176 lines
4.2 KiB
Go
Raw Normal View History

package commands
2013-10-04 17:09:03 +00:00
import (
2015-05-13 19:43:41 +00:00
"io/ioutil"
"os"
"github.com/github/git-lfs/config"
"github.com/github/git-lfs/git"
2015-03-19 19:30:55 +00:00
"github.com/github/git-lfs/lfs"
"github.com/rubyist/tracerx"
"github.com/spf13/cobra"
2013-10-04 17:09:03 +00:00
)
2014-06-26 20:53:37 +00:00
var (
pushCmd = &cobra.Command{
2015-09-17 22:08:28 +00:00
Use: "push",
Run: pushCommand,
2014-06-26 20:53:37 +00:00
}
pushDryRun = false
pushObjectIDs = false
pushAll = false
useStdin = false
2015-04-24 16:41:11 +00:00
// shares some global vars and functions with command_pre_push.go
2014-06-26 20:53:37 +00:00
)
2013-10-04 17:09:03 +00:00
func uploadsBetweenRefs(ctx *uploadContext, left string, right string) {
tracerx.Printf("Upload between %v and %v", left, right)
scanOpt := lfs.NewScanRefsOptions()
scanOpt.ScanMode = lfs.ScanRefsMode
scanOpt.RemoteName = config.Config.CurrentRemote
pointers, err := lfs.ScanRefs(left, right, scanOpt)
if err != nil {
Panic(err, "Error scanning for Git LFS files")
}
upload(ctx, pointers)
}
func uploadsBetweenRefAndRemote(ctx *uploadContext, refnames []string) {
tracerx.Printf("Upload refs %v to remote %v", refnames, config.Config.CurrentRemote)
2015-09-08 20:51:38 +00:00
scanOpt := lfs.NewScanRefsOptions()
scanOpt.ScanMode = lfs.ScanLeftToRemoteMode
scanOpt.RemoteName = config.Config.CurrentRemote
if pushAll {
scanOpt.ScanMode = lfs.ScanRefsMode
2015-09-08 20:51:38 +00:00
}
refs, err := refsByNames(refnames)
if err != nil {
Error(err.Error())
Exit("Error getting local refs.")
}
for _, ref := range refs {
pointers, err := lfs.ScanRefs(ref.Name, "", scanOpt)
if err != nil {
Panic(err, "Error scanning for Git LFS files in the %q ref", ref.Name)
}
upload(ctx, pointers)
}
}
func uploadsWithObjectIDs(ctx *uploadContext, oids []string) {
pointers := make([]*lfs.WrappedPointer, len(oids))
for idx, oid := range oids {
pointers[idx] = &lfs.WrappedPointer{Pointer: &lfs.Pointer{Oid: oid}}
}
upload(ctx, pointers)
}
func refsByNames(refnames []string) ([]*git.Ref, error) {
localrefs, err := git.LocalRefs()
if err != nil {
return nil, err
}
if pushAll && len(refnames) == 0 {
return localrefs, nil
}
reflookup := make(map[string]*git.Ref, len(localrefs))
for _, ref := range localrefs {
reflookup[ref.Name] = ref
}
refs := make([]*git.Ref, len(refnames))
for i, name := range refnames {
if ref, ok := reflookup[name]; ok {
refs[i] = ref
} else {
refs[i] = &git.Ref{name, git.RefTypeOther, name}
}
}
return refs, nil
}
2015-04-24 16:41:11 +00:00
// pushCommand pushes local objects to a Git LFS server. It takes two
// arguments:
2014-09-24 17:10:29 +00:00
//
2015-04-24 16:41:11 +00:00
// `<remote> <remote ref>`
2014-09-24 17:10:29 +00:00
//
2016-02-29 14:22:03 +00:00
// Remote must be a remote name, not a URL
2014-09-24 17:10:29 +00:00
//
2015-04-24 16:41:11 +00:00
// pushCommand calculates the git objects to send by looking comparing the range
// of commits between the local and remote git servers.
2014-06-26 20:53:37 +00:00
func pushCommand(cmd *cobra.Command, args []string) {
if len(args) == 0 {
Print("Specify a remote and a remote branch name (`git lfs push origin master`)")
os.Exit(1)
}
// Remote is first arg
if err := git.ValidateRemote(args[0]); err != nil {
Exit("Invalid remote name %q", args[0])
}
config.Config.CurrentRemote = args[0]
ctx := newUploadContext(pushDryRun)
if useStdin {
requireStdin("Run this command from the Git pre-push hook, or leave the --stdin flag off.")
2015-04-27 21:18:12 +00:00
2015-04-24 17:38:55 +00:00
// called from a pre-push hook! Update the existing pre-push hook if it's
// one that git-lfs set.
lfs.InstallHooks(false)
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 == prePushDeleteBranch {
return
}
uploadsBetweenRefs(ctx, left, right)
} else if pushObjectIDs {
if len(args) < 2 {
2015-07-06 13:48:39 +00:00
Print("Usage: git lfs push --object-id <remote> <lfs-object-id> [lfs-object-id] ...")
return
}
uploadsWithObjectIDs(ctx, args[1:])
} else {
2014-09-29 16:52:24 +00:00
if len(args) < 1 {
2015-04-24 16:41:11 +00:00
Print("Usage: git lfs push --dry-run <remote> [ref]")
return
}
uploadsBetweenRefAndRemote(ctx, args[1:])
}
2013-10-04 17:09:03 +00:00
}
func init() {
2015-04-24 16:41:11 +00:00
pushCmd.Flags().BoolVarP(&pushDryRun, "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)")
pushCmd.Flags().BoolVarP(&pushObjectIDs, "object-id", "o", false, "Push LFS object ID(s)")
pushCmd.Flags().BoolVarP(&pushAll, "all", "a", false, "Push all objects for the current ref to the remote.")
2015-09-08 20:51:38 +00:00
2014-06-26 20:53:37 +00:00
RootCmd.AddCommand(pushCmd)
2013-10-04 17:09:03 +00:00
}