2014-06-03 15:08:58 +00:00
|
|
|
package commands
|
2013-10-04 17:09:03 +00:00
|
|
|
|
|
|
|
import (
|
2015-05-13 19:43:41 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
|
2016-02-03 20:06:05 +00:00
|
|
|
"github.com/github/git-lfs/git"
|
2015-03-19 19:30:55 +00:00
|
|
|
"github.com/github/git-lfs/lfs"
|
2016-05-23 18:02:27 +00:00
|
|
|
"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 (
|
2016-03-31 15:02:01 +00:00
|
|
|
pushDryRun = false
|
|
|
|
pushObjectIDs = false
|
|
|
|
pushAll = false
|
|
|
|
useStdin = false
|
2015-04-24 16:41:11 +00:00
|
|
|
|
2015-10-01 19:39:30 +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
|
|
|
|
2016-04-06 19:06:34 +00:00
|
|
|
func uploadsBetweenRefs(ctx *uploadContext, left string, right string) {
|
2015-08-26 10:29:57 +00:00
|
|
|
tracerx.Printf("Upload between %v and %v", left, right)
|
|
|
|
|
2016-04-06 19:06:34 +00:00
|
|
|
scanOpt := lfs.NewScanRefsOptions()
|
|
|
|
scanOpt.ScanMode = lfs.ScanRefsMode
|
2016-07-21 22:37:53 +00:00
|
|
|
scanOpt.RemoteName = cfg.CurrentRemote
|
2016-04-06 19:06:34 +00:00
|
|
|
|
|
|
|
pointers, err := lfs.ScanRefs(left, right, scanOpt)
|
2015-06-28 15:13:44 +00:00
|
|
|
if err != nil {
|
|
|
|
Panic(err, "Error scanning for Git LFS files")
|
|
|
|
}
|
2016-04-06 19:06:34 +00:00
|
|
|
|
2016-04-07 16:52:24 +00:00
|
|
|
upload(ctx, pointers)
|
2015-08-26 10:29:57 +00:00
|
|
|
}
|
|
|
|
|
2016-04-06 19:06:34 +00:00
|
|
|
func uploadsBetweenRefAndRemote(ctx *uploadContext, refnames []string) {
|
2016-07-21 22:37:53 +00:00
|
|
|
tracerx.Printf("Upload refs %v to remote %v", refnames, cfg.CurrentRemote)
|
2015-09-08 20:51:38 +00:00
|
|
|
|
2015-10-28 16:06:36 +00:00
|
|
|
scanOpt := lfs.NewScanRefsOptions()
|
|
|
|
scanOpt.ScanMode = lfs.ScanLeftToRemoteMode
|
2016-07-21 22:37:53 +00:00
|
|
|
scanOpt.RemoteName = cfg.CurrentRemote
|
2015-09-09 20:58:58 +00:00
|
|
|
|
2015-09-09 20:18:51 +00:00
|
|
|
if pushAll {
|
2016-04-06 19:06:34 +00:00
|
|
|
scanOpt.ScanMode = lfs.ScanRefsMode
|
2015-09-08 20:51:38 +00:00
|
|
|
}
|
2015-09-09 20:18:51 +00:00
|
|
|
|
2016-04-06 19:06:34 +00:00
|
|
|
refs, err := refsByNames(refnames)
|
|
|
|
if err != nil {
|
|
|
|
Error(err.Error())
|
|
|
|
Exit("Error getting local refs.")
|
|
|
|
}
|
2015-09-09 20:18:51 +00:00
|
|
|
|
|
|
|
for _, ref := range refs {
|
2016-04-06 19:06:34 +00:00
|
|
|
pointers, err := lfs.ScanRefs(ref.Name, "", scanOpt)
|
2015-09-09 20:18:51 +00:00
|
|
|
if err != nil {
|
2016-04-06 19:06:34 +00:00
|
|
|
Panic(err, "Error scanning for Git LFS files in the %q ref", ref.Name)
|
2015-09-09 20:18:51 +00:00
|
|
|
}
|
|
|
|
|
2016-04-07 16:52:24 +00:00
|
|
|
upload(ctx, pointers)
|
2015-09-09 20:18:51 +00:00
|
|
|
}
|
2015-08-26 10:29:57 +00:00
|
|
|
}
|
|
|
|
|
2016-04-06 19:06:34 +00:00
|
|
|
func uploadsWithObjectIDs(ctx *uploadContext, oids []string) {
|
|
|
|
pointers := make([]*lfs.WrappedPointer, len(oids))
|
2015-06-28 15:13:44 +00:00
|
|
|
|
2016-04-06 19:06:34 +00:00
|
|
|
for idx, oid := range oids {
|
|
|
|
pointers[idx] = &lfs.WrappedPointer{Pointer: &lfs.Pointer{Oid: oid}}
|
2015-06-28 15:13:44 +00:00
|
|
|
}
|
|
|
|
|
2016-04-07 16:52:24 +00:00
|
|
|
upload(ctx, pointers)
|
2015-06-28 15:13:44 +00:00
|
|
|
}
|
|
|
|
|
2016-04-06 19:06:34 +00:00
|
|
|
func refsByNames(refnames []string) ([]*git.Ref, error) {
|
|
|
|
localrefs, err := git.LocalRefs()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-07-06 13:49:52 +00:00
|
|
|
|
2016-04-06 19:06:34 +00:00
|
|
|
if pushAll && len(refnames) == 0 {
|
|
|
|
return localrefs, nil
|
2015-07-27 20:41:57 +00:00
|
|
|
}
|
|
|
|
|
2016-04-06 19:06:34 +00:00
|
|
|
reflookup := make(map[string]*git.Ref, len(localrefs))
|
|
|
|
for _, ref := range localrefs {
|
|
|
|
reflookup[ref.Name] = ref
|
|
|
|
}
|
2015-07-27 20:41:57 +00:00
|
|
|
|
2016-04-06 19:06:34 +00:00
|
|
|
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}
|
|
|
|
}
|
2015-06-28 15:13:44 +00:00
|
|
|
}
|
|
|
|
|
2016-04-06 19:06:34 +00:00
|
|
|
return refs, nil
|
2015-06-28 15:13:44 +00:00
|
|
|
}
|
|
|
|
|
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) {
|
2014-10-01 20:02:29 +00:00
|
|
|
if len(args) == 0 {
|
2015-04-24 17:48:55 +00:00
|
|
|
Print("Specify a remote and a remote branch name (`git lfs push origin master`)")
|
2014-10-01 20:02:29 +00:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2016-08-17 22:13:36 +00:00
|
|
|
requireGitVersion()
|
|
|
|
|
2016-02-03 20:06:05 +00:00
|
|
|
// Remote is first arg
|
|
|
|
if err := git.ValidateRemote(args[0]); err != nil {
|
|
|
|
Exit("Invalid remote name %q", args[0])
|
|
|
|
}
|
2015-02-02 22:39:03 +00:00
|
|
|
|
2016-07-21 22:37:53 +00:00
|
|
|
cfg.CurrentRemote = args[0]
|
2016-04-07 16:52:24 +00:00
|
|
|
ctx := newUploadContext(pushDryRun)
|
2016-04-06 19:06:34 +00:00
|
|
|
|
2014-10-01 14:50:48 +00:00
|
|
|
if useStdin {
|
2015-04-24 20:24:32 +00:00
|
|
|
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)
|
|
|
|
|
2014-10-01 14:50:48 +00:00
|
|
|
refsData, err := ioutil.ReadAll(os.Stdin)
|
|
|
|
if err != nil {
|
|
|
|
Panic(err, "Error reading refs on stdin")
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(refsData) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-08-26 10:29:57 +00:00
|
|
|
left, right := decodeRefs(string(refsData))
|
2016-03-31 15:02:01 +00:00
|
|
|
if left == prePushDeleteBranch {
|
2014-10-01 14:50:48 +00:00
|
|
|
return
|
|
|
|
}
|
2015-06-28 15:13:44 +00:00
|
|
|
|
2016-04-06 19:06:34 +00:00
|
|
|
uploadsBetweenRefs(ctx, left, right)
|
2015-06-28 15:13:44 +00:00
|
|
|
} 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] ...")
|
2015-06-28 15:13:44 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-04-06 19:06:34 +00:00
|
|
|
uploadsWithObjectIDs(ctx, args[1:])
|
2014-10-01 14:50:48 +00:00
|
|
|
} 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]")
|
2014-09-24 15:30:05 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-04-06 19:06:34 +00:00
|
|
|
uploadsBetweenRefAndRemote(ctx, args[1:])
|
2014-09-18 20:31:09 +00:00
|
|
|
}
|
2013-10-04 17:09:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2016-08-10 15:33:25 +00:00
|
|
|
RegisterSubcommand(func() *cobra.Command {
|
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "push",
|
|
|
|
Run: pushCommand,
|
|
|
|
}
|
2015-09-08 20:51:38 +00:00
|
|
|
|
2016-08-10 15:33:25 +00:00
|
|
|
cmd.Flags().BoolVarP(&pushDryRun, "dry-run", "d", false, "Do everything except actually send the updates")
|
|
|
|
cmd.Flags().BoolVarP(&useStdin, "stdin", "s", false, "Take refs on stdin (for pre-push hook)")
|
|
|
|
cmd.Flags().BoolVarP(&pushObjectIDs, "object-id", "o", false, "Push LFS object ID(s)")
|
|
|
|
cmd.Flags().BoolVarP(&pushAll, "all", "a", false, "Push all objects for the current ref to the remote.")
|
|
|
|
return cmd
|
|
|
|
})
|
2013-10-04 17:09:03 +00:00
|
|
|
}
|