2014-06-03 15:08:58 +00:00
|
|
|
package commands
|
2013-10-04 17:09:03 +00:00
|
|
|
|
|
|
|
import (
|
2015-01-30 19:08:47 +00:00
|
|
|
"github.com/hawser/git-hawser/git"
|
|
|
|
"github.com/hawser/git-hawser/hawser"
|
|
|
|
"github.com/hawser/git-hawser/hawserclient"
|
|
|
|
"github.com/hawser/git-hawser/scanner"
|
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-01-30 19:23:41 +00:00
|
|
|
Short: "Push files to the hawser endpoint",
|
2014-06-26 20:53:37 +00:00
|
|
|
Run: pushCommand,
|
|
|
|
}
|
2014-09-19 16:07:00 +00:00
|
|
|
dryRun = false
|
2014-10-01 14:50:48 +00:00
|
|
|
useStdin = false
|
2014-09-19 16:07:00 +00:00
|
|
|
deleteBranch = "(delete)"
|
2014-06-26 20:53:37 +00:00
|
|
|
)
|
2013-10-04 17:09:03 +00:00
|
|
|
|
2015-01-30 19:23:41 +00:00
|
|
|
// pushCommand is the command that's run via `git hawser push`. It has two modes
|
2014-09-24 17:10:29 +00:00
|
|
|
// of operation. The primary mode is run via the git pre-push hook. The pre-push
|
|
|
|
// hook passes two arguments on the command line:
|
|
|
|
// 1. Name of the remote to which the push is being done
|
|
|
|
// 2. URL to which the push is being done
|
|
|
|
//
|
|
|
|
// The hook receives commit information on stdin in the form:
|
|
|
|
// <local ref> <local sha1> <remote ref> <remote sha1>
|
|
|
|
//
|
|
|
|
// In the typical case, pushCommand will get a list of git objects being pushed
|
|
|
|
// by using the following:
|
|
|
|
// git rev-list --objects <local sha1> ^<remote sha1>
|
|
|
|
//
|
2015-01-30 19:23:41 +00:00
|
|
|
// If any of those git objects are associated with hawser objects, those hawser
|
|
|
|
// objects will be pushed to the hawser endpoint.
|
2014-09-24 17:10:29 +00:00
|
|
|
//
|
|
|
|
// In the case of pushing a new branch, the list of git objects will be all of
|
|
|
|
// the git objects in this branch.
|
|
|
|
//
|
2015-01-30 19:23:41 +00:00
|
|
|
// In the case of deleting a branch, no attempts to push hawser objects will be
|
2014-09-24 17:10:29 +00:00
|
|
|
// made.
|
|
|
|
//
|
2015-01-30 19:23:41 +00:00
|
|
|
// When pushing hawser objects, the client will first perform an OPTIONS command
|
2014-09-24 17:10:29 +00:00
|
|
|
// which will determine not only whether or not the client is authorized, but also
|
2015-01-30 19:23:41 +00:00
|
|
|
// whether or not that hawser endpoint already has the hawser object. If it
|
2014-09-24 17:10:29 +00:00
|
|
|
// does, the object will not be pushed.
|
|
|
|
//
|
|
|
|
// The other mode of operation is the dry run mode. In this mode, the repo
|
|
|
|
// and refspec are passed on the command line. pushCommand will calculate the
|
|
|
|
// git objects that would be pushed in a similar manner as above and will print
|
|
|
|
// out each file name.
|
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-01-30 19:23:41 +00:00
|
|
|
Print("The git hawser pre-push hook is out of date. Please run `git hawser update`")
|
2014-10-01 20:02:29 +00:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2014-10-01 14:50:48 +00:00
|
|
|
if useStdin {
|
|
|
|
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 == deleteBranch {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
2014-09-29 19:35:59 +00:00
|
|
|
var repo, refspec string
|
|
|
|
|
2014-09-29 16:52:24 +00:00
|
|
|
if len(args) < 1 {
|
2015-01-30 19:23:41 +00:00
|
|
|
Print("Usage: git hawser push --dry-run <repo> [refspec]")
|
2014-09-24 15:30:05 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-09-29 19:35:59 +00:00
|
|
|
repo = args[0]
|
|
|
|
if len(args) == 2 {
|
|
|
|
refspec = args[1]
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
|
remoteRef, err := git.LsRemote(repo, refspec)
|
|
|
|
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
|
|
|
|
pointers, err := scanner.Scan(left, right)
|
|
|
|
if err != nil {
|
2015-01-30 19:23:41 +00:00
|
|
|
Panic(err, "Error scanning for hawser files")
|
2014-10-20 18:49:15 +00:00
|
|
|
}
|
2014-06-24 14:30:33 +00:00
|
|
|
|
2014-10-20 18:49:15 +00:00
|
|
|
for i, pointer := range pointers {
|
2014-09-19 00:40:37 +00:00
|
|
|
if dryRun {
|
2014-10-20 18:49:15 +00:00
|
|
|
Print("push %s", pointer.Name)
|
2014-09-19 00:40:37 +00:00
|
|
|
continue
|
|
|
|
}
|
2014-10-20 18:49:15 +00:00
|
|
|
if wErr := pushAsset(pointer.Oid, pointer.Name, i+1, len(pointers)); wErr != nil {
|
2014-09-18 20:31:09 +00:00
|
|
|
Panic(wErr.Err, wErr.Error())
|
|
|
|
}
|
|
|
|
}
|
2013-10-04 17:09:03 +00:00
|
|
|
}
|
|
|
|
|
2015-01-30 19:23:41 +00:00
|
|
|
// pushAsset pushes the asset with the given oid to the hawser endpoint. It will
|
2014-09-24 17:10:29 +00:00
|
|
|
// first make an OPTIONS call. If OPTIONS returns a 200 status, it indicates that the
|
2015-01-30 19:23:41 +00:00
|
|
|
// hawser endpoint already has a hawser object for that oid. The object will
|
2014-09-24 17:10:29 +00:00
|
|
|
// not be pushed again.
|
2015-01-30 18:13:15 +00:00
|
|
|
func pushAsset(oid, filename string, index, totalFiles int) *hawser.WrappedError {
|
2015-01-23 22:11:10 +00:00
|
|
|
tracerx.Printf("checking_asset: %s %s %d/%d", oid, filename, index, totalFiles)
|
2015-01-30 18:13:15 +00:00
|
|
|
path, err := hawser.LocalMediaPath(oid)
|
2014-09-22 15:46:19 +00:00
|
|
|
if err != nil {
|
2015-01-30 18:13:15 +00:00
|
|
|
return hawser.Errorf(err, "Error uploading file %s (%s)", filename, oid)
|
2014-08-07 17:15:28 +00:00
|
|
|
}
|
|
|
|
|
2015-01-30 18:13:15 +00:00
|
|
|
linkMeta, status, err := hawserclient.Post(path, filename)
|
2015-02-01 23:34:59 +00:00
|
|
|
if err != nil && status != 302 {
|
2015-01-30 18:13:15 +00:00
|
|
|
return hawser.Errorf(err, "Error starting file upload %s (%s)", filename, oid)
|
2015-01-27 17:07:11 +00:00
|
|
|
}
|
|
|
|
|
2015-02-01 23:34:59 +00:00
|
|
|
if status == 200 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-01-30 18:13:15 +00:00
|
|
|
cb, file, cbErr := hawser.CopyCallbackFile("push", filename, index, totalFiles)
|
2015-01-27 17:07:11 +00:00
|
|
|
if cbErr != nil {
|
|
|
|
Error(cbErr.Error())
|
|
|
|
}
|
|
|
|
if file != nil {
|
|
|
|
defer file.Close()
|
2014-09-22 15:46:19 +00:00
|
|
|
}
|
2014-08-07 17:37:04 +00:00
|
|
|
|
2015-02-01 23:34:59 +00:00
|
|
|
if status == 405 || status == 302 {
|
2015-01-23 22:11:10 +00:00
|
|
|
// Do the old style OPTIONS + PUT
|
2015-01-30 18:13:15 +00:00
|
|
|
status, err := hawserclient.Options(path)
|
2015-01-23 22:11:10 +00:00
|
|
|
if err != nil {
|
2015-01-30 18:13:15 +00:00
|
|
|
return hawser.Errorf(err, "Error getting options for file %s (%s)", filename, oid)
|
2015-01-23 22:11:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if status == 200 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-01-30 18:13:15 +00:00
|
|
|
err = hawserclient.Put(path, filename, cb)
|
2015-01-27 17:07:11 +00:00
|
|
|
if err != nil {
|
2015-01-30 18:13:15 +00:00
|
|
|
return hawser.Errorf(err, "Error uploading file %s (%s)", filename, oid)
|
2015-01-23 22:11:10 +00:00
|
|
|
}
|
2015-01-27 17:07:11 +00:00
|
|
|
|
2015-01-23 22:11:10 +00:00
|
|
|
return nil
|
|
|
|
} // End old style
|
|
|
|
|
|
|
|
if status != 201 {
|
2015-02-01 23:34:59 +00:00
|
|
|
return hawser.Errorf(err, "Unexpected HTTP response: %d for %s (%s)", status, filename, oid)
|
2014-08-07 17:15:28 +00:00
|
|
|
}
|
|
|
|
|
2015-01-30 18:13:15 +00:00
|
|
|
err = hawserclient.ExternalPut(path, filename, linkMeta, cb)
|
2015-01-27 17:07:11 +00:00
|
|
|
if err != nil {
|
2015-01-30 18:13:15 +00:00
|
|
|
return hawser.Errorf(err, "Error uploading file %s (%s)", filename, oid)
|
2014-08-07 17:15:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-09-24 17:10:29 +00:00
|
|
|
// decodeRefs pulls the sha1s out of the line read from the pre-push
|
|
|
|
// hook's stdin.
|
2014-09-23 16:09:35 +00:00
|
|
|
func decodeRefs(input string) (string, string) {
|
|
|
|
refs := strings.Split(strings.TrimSpace(input), " ")
|
|
|
|
var left, right string
|
|
|
|
|
|
|
|
if len(refs) > 1 {
|
|
|
|
left = refs[1]
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(refs) > 3 {
|
|
|
|
right = "^" + refs[3]
|
|
|
|
}
|
|
|
|
|
|
|
|
return left, right
|
|
|
|
}
|
|
|
|
|
2013-10-04 17:09:03 +00:00
|
|
|
func init() {
|
2014-09-19 00:40:37 +00:00
|
|
|
pushCmd.Flags().BoolVarP(&dryRun, "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
|
|
|
}
|