2015-04-24 16:41:11 +00:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
2015-09-04 13:59:56 +00:00
|
|
|
"bufio"
|
2017-11-02 18:33:57 +00:00
|
|
|
"io"
|
2015-04-24 16:41:11 +00:00
|
|
|
"os"
|
|
|
|
"strings"
|
2015-05-13 19:43:41 +00:00
|
|
|
|
2017-11-01 23:35:43 +00:00
|
|
|
"github.com/git-lfs/git-lfs/git"
|
2016-11-10 16:37:51 +00:00
|
|
|
"github.com/rubyist/tracerx"
|
2016-05-23 18:02:27 +00:00
|
|
|
"github.com/spf13/cobra"
|
2015-04-24 16:41:11 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2020-06-10 21:20:05 +00:00
|
|
|
prePushDryRun = false
|
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)
|
|
|
|
}
|
|
|
|
|
2020-07-31 11:21:27 +00:00
|
|
|
if cfg.Os.Bool("GIT_LFS_SKIP_PUSH", false) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-08-17 22:13:36 +00:00
|
|
|
requireGitVersion()
|
|
|
|
|
2016-02-03 21:23:27 +00:00
|
|
|
// Remote is first arg
|
2020-04-17 14:10:54 +00:00
|
|
|
remote, _ := git.MapRemoteURL(args[0], true)
|
|
|
|
if err := cfg.SetValidPushRemote(remote); err != nil {
|
2017-10-27 22:20:38 +00:00
|
|
|
Exit("Invalid remote name %q: %s", args[0], err)
|
2016-02-03 21:23:27 +00:00
|
|
|
}
|
2015-04-24 16:41:11 +00:00
|
|
|
|
2017-10-27 22:14:26 +00:00
|
|
|
ctx := newUploadContext(prePushDryRun)
|
2017-11-02 18:55:44 +00:00
|
|
|
updates := prePushRefs(os.Stdin)
|
2017-11-03 16:08:08 +00:00
|
|
|
if err := uploadForRefUpdates(ctx, updates, false); err != nil {
|
|
|
|
ExitWithError(err)
|
2017-11-02 18:33:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// prePushRefs parses commit information that the pre-push git hook receives:
|
|
|
|
//
|
|
|
|
// <local ref> <local sha1> <remote ref> <remote sha1>
|
|
|
|
//
|
|
|
|
// Each line describes a proposed update of the remote ref at the remote sha to
|
|
|
|
// the local sha. Multiple updates can be received on multiple lines (such as
|
|
|
|
// from 'git push --all'). These updates are typically received over STDIN.
|
2018-01-05 22:22:25 +00:00
|
|
|
func prePushRefs(r io.Reader) []*git.RefUpdate {
|
2017-11-02 18:33:57 +00:00
|
|
|
scanner := bufio.NewScanner(r)
|
2018-01-05 22:22:25 +00:00
|
|
|
refs := make([]*git.RefUpdate, 0, 1)
|
2017-11-02 18:33:57 +00:00
|
|
|
|
|
|
|
// We can be passed multiple lines of refs
|
2015-09-04 13:59:56 +00:00
|
|
|
for scanner.Scan() {
|
|
|
|
line := strings.TrimSpace(scanner.Text())
|
|
|
|
if len(line) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
2015-04-24 16:41:11 +00:00
|
|
|
|
2016-11-10 16:37:51 +00:00
|
|
|
tracerx.Printf("pre-push: %s", line)
|
|
|
|
|
2017-11-01 23:35:43 +00:00
|
|
|
left, right := decodeRefs(line)
|
2020-06-10 21:20:05 +00:00
|
|
|
if git.IsZeroObjectID(left.Sha) {
|
2015-09-04 14:04:16 +00:00
|
|
|
continue
|
2015-09-04 13:59:56 +00:00
|
|
|
}
|
2015-07-27 16:43:41 +00:00
|
|
|
|
2018-01-05 22:22:25 +00:00
|
|
|
refs = append(refs, git.NewRefUpdate(cfg.Git, cfg.PushRemote(), left, right))
|
2016-11-29 20:04:05 +00:00
|
|
|
}
|
2016-11-30 17:34:22 +00:00
|
|
|
|
2017-11-02 18:33:57 +00:00
|
|
|
return refs
|
2016-11-29 20:04:05 +00:00
|
|
|
}
|
|
|
|
|
2015-04-24 16:41:11 +00:00
|
|
|
// decodeRefs pulls the sha1s out of the line read from the pre-push
|
|
|
|
// hook's stdin.
|
2017-11-01 23:35:43 +00:00
|
|
|
func decodeRefs(input string) (*git.Ref, *git.Ref) {
|
2015-04-24 16:41:11 +00:00
|
|
|
refs := strings.Split(strings.TrimSpace(input), " ")
|
2017-11-01 23:35:43 +00:00
|
|
|
for len(refs) < 4 {
|
|
|
|
refs = append(refs, "")
|
2015-04-24 16:41:11 +00:00
|
|
|
}
|
|
|
|
|
2017-11-01 23:35:43 +00:00
|
|
|
leftRef := git.ParseRef(refs[0], refs[1])
|
|
|
|
rightRef := git.ParseRef(refs[2], refs[3])
|
|
|
|
return leftRef, rightRef
|
2015-04-24 16:41:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2016-09-01 16:09:38 +00:00
|
|
|
RegisterCommand("pre-push", prePushCommand, func(cmd *cobra.Command) {
|
2016-08-10 15:33:25 +00:00
|
|
|
cmd.Flags().BoolVarP(&prePushDryRun, "dry-run", "d", false, "Do everything except actually send the updates")
|
|
|
|
})
|
2015-04-24 16:41:11 +00:00
|
|
|
}
|