1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
|
package commands
import (
"bufio"
"io"
"os"
"strings"
"github.com/git-lfs/git-lfs/v3/git"
"github.com/git-lfs/git-lfs/v3/tr"
"github.com/rubyist/tracerx"
"github.com/spf13/cobra"
)
var (
prePushDryRun = false
)
// 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(tr.Tr.Get("This should be run through Git's pre-push hook. Run `git lfs update` to install it."))
os.Exit(1)
}
if cfg.Os.Bool("GIT_LFS_SKIP_PUSH", false) {
return
}
requireGitVersion()
// Remote is first arg
remote, _ := git.MapRemoteURL(args[0], true)
if err := cfg.SetValidPushRemote(remote); err != nil {
Exit(tr.Tr.Get("Invalid remote name %q: %s", args[0], err))
}
ctx := newUploadContext(prePushDryRun)
updates := prePushRefs(os.Stdin)
if err := uploadForRefUpdates(ctx, updates, false); err != nil {
ExitWithError(err)
}
}
// 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.
func prePushRefs(r io.Reader) []*git.RefUpdate {
scanner := bufio.NewScanner(r)
refs := make([]*git.RefUpdate, 0, 1)
// We can be passed multiple lines of refs
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
if len(line) == 0 {
continue
}
tracerx.Printf("pre-push: %s", line)
localRef, remoteRef := decodeRefs(line)
if git.IsZeroObjectID(localRef.Sha) {
continue
}
refs = append(refs, git.NewRefUpdate(cfg.Git, cfg.PushRemote(), localRef, remoteRef))
}
return refs
}
// decodeRefs pulls the sha1s out of the line read from the pre-push
// hook's stdin.
func decodeRefs(input string) (*git.Ref, *git.Ref) {
refs := strings.Split(strings.TrimSpace(input), " ")
for len(refs) < 4 {
refs = append(refs, "")
}
localRef := git.ParseRef(refs[0], refs[1])
remoteRef := git.ParseRef(refs[2], refs[3])
return localRef, remoteRef
}
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")
})
}
|