git-lfs/commands/command_push.go

118 lines
2.6 KiB
Go
Raw Normal View History

package commands
2013-10-04 17:09:03 +00:00
import (
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/scanner"
2014-06-26 20:53:37 +00:00
"github.com/spf13/cobra"
"io/ioutil"
"os"
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-04-24 16:41:11 +00:00
Short: "Push files to the Git LFS server",
2014-06-26 20:53:37 +00:00
Run: pushCommand,
}
2015-04-24 16:41:11 +00:00
pushDryRun = false
pushDeleteBranch = "(delete)"
useStdin = false
// shares some global vars and functions with commmands_pre_push.go
2014-06-26 20:53:37 +00:00
)
2013-10-04 17:09:03 +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
//
2015-04-24 16:41:11 +00:00
// Both a remote name ("origin") or a remote URL are accepted.
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) {
var left, right string
2014-06-05 18:48:23 +00:00
if len(args) == 0 {
Print("Specify a remote and a remote branch name (`git lfs push origin master`)")
os.Exit(1)
}
2015-03-19 19:30:55 +00:00
lfs.Config.CurrentRemote = args[0]
if useStdin {
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))
2015-04-24 16:41:11 +00:00
if left == pushDeleteBranch {
return
}
} else {
2015-04-24 16:41:11 +00:00
var remoteArg, refArg string
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
}
2015-04-24 16:41:11 +00:00
remoteArg = args[0]
if len(args) == 2 {
2015-04-24 16:41:11 +00:00
refArg = args[1]
}
localRef, err := git.CurrentRef()
if err != nil {
Panic(err, "Error getting local ref")
}
left = localRef
2015-04-24 16:41:11 +00:00
remoteRef, err := git.LsRemote(remoteArg, refArg)
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 {
2015-04-24 16:41:11 +00:00
if pushDryRun {
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
}
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)")
2014-06-26 20:53:37 +00:00
RootCmd.AddCommand(pushCmd)
2013-10-04 17:09:03 +00:00
}