git-lfs/commands/command_pre_push.go

134 lines
3.1 KiB
Go
Raw Normal View History

2015-04-24 16:41:11 +00:00
package commands
import (
"bufio"
"fmt"
2015-04-24 16:41:11 +00:00
"os"
"strings"
2015-05-13 19:43:41 +00:00
2016-11-15 17:01:18 +00:00
"github.com/git-lfs/git-lfs/git"
"github.com/git-lfs/git-lfs/lfs"
"github.com/rubyist/tracerx"
"github.com/spf13/cobra"
2015-04-24 16:41:11 +00:00
)
var (
prePushDryRun = false
prePushDeleteBranch = strings.Repeat("0", 40)
2015-04-24 16:41:11 +00:00
)
// prePushCommand is run through Git's 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, prePushCommand will get a list of git objects being
// pushed by using the following:
//
// git rev-list --objects <local sha1> ^<remote sha1>
//
// If any of those git objects are associated with Git LFS objects, those
// objects will be pushed to the Git LFS API.
//
// In the case of pushing a new branch, the list of git objects will be all of
// the git objects in this branch.
//
// In the case of deleting a branch, no attempts to push Git LFS objects will be
// made.
func prePushCommand(cmd *cobra.Command, args []string) {
if len(args) == 0 {
Print("This should be run through Git's pre-push hook. Run `git lfs update` to install it.")
os.Exit(1)
}
requireGitVersion()
2016-02-03 21:23:27 +00:00
// Remote is first arg
if err := git.ValidateRemote(args[0]); err != nil {
Exit("Invalid remote name %q", args[0])
}
2015-04-24 16:41:11 +00:00
2016-07-21 22:37:53 +00:00
cfg.CurrentRemote = args[0]
ctx := newUploadContext(prePushDryRun)
gitscanner := lfs.NewGitScanner(nil)
if err := gitscanner.RemoteForPush(cfg.CurrentRemote); err != nil {
ExitWithError(err)
}
defer gitscanner.Close()
// We can be passed multiple lines of refs
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
2015-04-24 16:41:11 +00:00
if len(line) == 0 {
continue
}
2015-04-24 16:41:11 +00:00
tracerx.Printf("pre-push: %s", line)
left, _ := decodeRefs(line)
if left == prePushDeleteBranch {
continue
}
pointers, err := scanLeftOrAll(gitscanner, left)
if err != nil {
Print("Error scanning for Git LFS files in %q", left)
ExitWithError(err)
}
uploadPointers(ctx, pointers)
}
}
func scanLeft(g *lfs.GitScanner, ref string) ([]*lfs.WrappedPointer, error) {
var pointers []*lfs.WrappedPointer
var multiErr error
cb := func(p *lfs.WrappedPointer, err error) {
if err != nil {
if multiErr != nil {
multiErr = fmt.Errorf("%v\n%v", multiErr, err)
} else {
multiErr = err
}
return
}
pointers = append(pointers, p)
}
if err := g.ScanLeftToRemote(ref, cb); err != nil {
return pointers, err
}
return pointers, multiErr
}
2015-04-24 16:41:11 +00:00
// decodeRefs pulls the sha1s out of the line read from the pre-push
// hook's stdin.
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
}
func init() {
RegisterCommand("pre-push", prePushCommand, func(cmd *cobra.Command) {
cmd.Flags().BoolVarP(&prePushDryRun, "dry-run", "d", false, "Do everything except actually send the updates")
})
2015-04-24 16:41:11 +00:00
}