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 (
"bufio"
"bytes"
"fmt"
"github.com/github/git-media/gitmedia"
"github.com/github/git-media/gitmediaclient"
2014-06-26 20:53:37 +00:00
"github.com/spf13/cobra"
"io/ioutil"
"os"
"os/exec"
"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",
2014-06-26 21:22:24 +00:00
Short: "Push files to the media endpoint",
2014-06-26 20:53:37 +00:00
Run: pushCommand,
}
dryRun = false
z40 = "0000000000000000000000000000000000000000"
deleteBranch = "(delete)"
2014-06-26 20:53:37 +00:00
)
2013-10-04 17:09:03 +00:00
2014-06-26 20:53:37 +00:00
func pushCommand(cmd *cobra.Command, args []string) {
// TODO handle (delete) case, not sending anything
refsData, err := ioutil.ReadAll(os.Stdin)
2014-06-05 18:48:23 +00:00
if err != nil {
Panic(err, "Error reading refs on stdin")
2014-06-05 18:48:23 +00:00
}
if len(refsData) == 0 {
return
}
2014-08-07 18:11:07 +00:00
2014-09-18 21:07:06 +00:00
// TODO let's pull this into a nice iteratable thing like the queue provides
refs := strings.Split(strings.TrimSpace(string(refsData)), " ")
2014-03-12 14:55:01 +00:00
if refs[0] == deleteBranch {
return
}
2014-09-18 21:07:06 +00:00
refArgs := []string{"rev-list", "--objects"}
if len(refs) > 1 {
refArgs = append(refArgs, refs[1])
}
if len(refs) > 3 && refs[3] != z40 {
refArgs = append(refArgs, "^"+refs[3])
}
output, err := exec.Command("git", refArgs...).Output()
if err != nil {
2014-09-18 21:07:06 +00:00
Panic(err, "Error running git rev-list --objects %v", refArgs)
}
2014-03-12 14:55:01 +00:00
scanner := bufio.NewScanner(bytes.NewBuffer(output))
blobOids := make([]string, 0)
for scanner.Scan() {
line := strings.Split(scanner.Text(), " ")
sha1 := line[0]
2013-10-21 18:42:29 +00:00
linkPath := filepath.Join(gitmedia.LocalLinkDir, sha1[0:2], sha1[2:len(sha1)])
if _, err := os.Stat(linkPath); err == nil {
oid, err := ioutil.ReadFile(linkPath)
if err != nil {
Panic(err, "Error reading link file")
}
blobOids = append(blobOids, string(oid))
}
}
// TODO - filename
for i, oid := range blobOids {
if dryRun {
fmt.Println("push", oid)
continue
}
if wErr := pushAsset(oid, "", i+1, len(blobOids)); wErr != nil {
Panic(wErr.Err, wErr.Error())
}
fmt.Printf("\n")
}
2013-10-04 17:09:03 +00:00
}
2014-08-08 19:37:41 +00:00
func pushAsset(oid, filename string, index, totalFiles int) *gitmedia.WrappedError {
2014-08-07 17:15:28 +00:00
path, err := gitmedia.LocalMediaPath(oid)
if err == nil {
err = gitmediaclient.Options(path)
}
if err == nil {
2014-08-07 18:11:07 +00:00
cb, file, cbErr := gitmedia.CopyCallbackFile("push", filename, index, totalFiles)
if cbErr != nil {
Error(cbErr.Error())
}
err = gitmediaclient.Put(path, filename, cb)
if file != nil {
file.Close()
}
2014-08-07 17:15:28 +00:00
}
if err != nil {
2014-08-08 19:37:41 +00:00
return gitmedia.Errorf(err, "Error uploading file %s (%s)", filename, oid)
2014-08-07 17:15:28 +00:00
}
return nil
}
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")
2014-06-26 20:53:37 +00:00
RootCmd.AddCommand(pushCmd)
2013-10-04 17:09:03 +00:00
}