2014-06-03 15:08:58 +00:00
|
|
|
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"
|
2014-10-04 16:10:02 +00:00
|
|
|
"github.com/rubyist/tracerx"
|
2014-06-26 20:53:37 +00:00
|
|
|
"github.com/spf13/cobra"
|
2014-09-18 20:31:09 +00:00
|
|
|
"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) {
|
2014-09-24 15:30:05 +00:00
|
|
|
var left, right string
|
2014-06-05 18:48:23 +00:00
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2015-03-19 19:30:55 +00:00
|
|
|
lfs.Config.CurrentRemote = args[0]
|
2015-02-02 22:39:03 +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
|
|
|
|
}
|
|
|
|
|
|
|
|
left, right = decodeRefs(string(refsData))
|
2015-04-24 16:41:11 +00:00
|
|
|
if left == pushDeleteBranch {
|
2014-10-01 14:50:48 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
2015-04-24 16:41:11 +00:00
|
|
|
var remoteArg, refArg string
|
2014-09-29 19:35:59 +00:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2015-04-24 16:41:11 +00:00
|
|
|
remoteArg = args[0]
|
2014-09-29 19:35:59 +00:00
|
|
|
if len(args) == 2 {
|
2015-04-24 16:41:11 +00:00
|
|
|
refArg = args[1]
|
2014-09-29 19:35:59 +00:00
|
|
|
}
|
|
|
|
|
2014-10-28 19:22:38 +00:00
|
|
|
localRef, err := git.CurrentRef()
|
2014-09-24 15:30:05 +00:00
|
|
|
if err != nil {
|
2014-09-29 19:35:59 +00:00
|
|
|
Panic(err, "Error getting local ref")
|
2014-09-24 15:30:05 +00:00
|
|
|
}
|
2014-09-29 19:35:59 +00:00
|
|
|
left = localRef
|
|
|
|
|
2015-04-24 16:41:11 +00:00
|
|
|
remoteRef, err := git.LsRemote(remoteArg, refArg)
|
2014-09-29 19:35:59 +00:00
|
|
|
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-09-19 16:07:00 +00:00
|
|
|
}
|
|
|
|
|
2014-10-20 18:49:15 +00:00
|
|
|
// Just use scanner here
|
2015-04-23 16:20:36 +00:00
|
|
|
pointers, err := lfs.Scan(left, right)
|
2014-10-20 18:49:15 +00:00
|
|
|
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-06-24 14:30:33 +00:00
|
|
|
|
2015-04-24 15:43:38 +00:00
|
|
|
uploadQueue := lfs.NewUploadQueue(lfs.Config.ConcurrentUploads(), len(pointers))
|
2015-04-23 01:07:52 +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)
|
2014-09-19 00:40:37 +00:00
|
|
|
continue
|
|
|
|
}
|
2015-04-23 16:40:45 +00:00
|
|
|
tracerx.Printf("checking_asset: %s %s %d/%d", pointer.Oid, pointer.Name, i+1, len(pointers))
|
|
|
|
|
|
|
|
u, wErr := lfs.NewUploadable(pointer.Oid, pointer.Name, i+1, len(pointers))
|
2015-04-23 01:07:52 +00:00
|
|
|
if wErr != nil {
|
2015-02-17 18:46:08 +00:00
|
|
|
if Debugging || wErr.Panic {
|
|
|
|
Panic(wErr.Err, wErr.Error())
|
|
|
|
} else {
|
|
|
|
Exit(wErr.Error())
|
|
|
|
}
|
2014-09-18 20:31:09 +00:00
|
|
|
}
|
2015-04-29 17:25:39 +00:00
|
|
|
uploadQueue.Add(u)
|
2015-04-23 01:07:52 +00:00
|
|
|
}
|
|
|
|
|
2015-04-29 15:46:20 +00:00
|
|
|
if !pushDryRun {
|
2015-04-24 15:56:22 +00:00
|
|
|
uploadQueue.Process()
|
2015-04-24 18:51:21 +00:00
|
|
|
for _, err := range uploadQueue.Errors() {
|
|
|
|
if Debugging || err.Panic {
|
|
|
|
LoggedError(err.Err, err.Error())
|
2015-04-24 15:56:22 +00:00
|
|
|
} else {
|
2015-04-24 18:51:21 +00:00
|
|
|
Error(err.Error())
|
2015-04-24 15:56:22 +00:00
|
|
|
}
|
2015-04-23 01:07:52 +00:00
|
|
|
}
|
2015-04-24 18:51:21 +00:00
|
|
|
|
|
|
|
if len(uploadQueue.Errors()) > 0 {
|
|
|
|
os.Exit(2)
|
|
|
|
}
|
2014-09-18 20:31:09 +00:00
|
|
|
}
|
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")
|
2014-10-01 14:50:48 +00:00
|
|
|
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
|
|
|
}
|