Debian Codegraph @

commands/command_pre_push.go

raw symbols SCIP
1 package commands
2
3 import (
4 "bufio"
5 "io"
6 "os"
7 "strings"
8
9 "github.com/git-lfs/git-lfs/v3/git"
10 "github.com/git-lfs/git-lfs/v3/tr"
11 "github.com/rubyist/tracerx"
12 "github.com/spf13/cobra"
13 )
14
15 var (
16 prePushDryRun = false
17 )
18
19 // prePushCommand is run through Git's pre-push hook. The pre-push hook passes
20 // two arguments on the command line:
21 //
22 // 1. Name of the remote to which the push is being done
23 // 2. URL to which the push is being done
24 //
25 // The hook receives commit information on stdin in the form:
26 //
27 // <local ref> <local sha1> <remote ref> <remote sha1>
28 //
29 // In the typical case, prePushCommand will get a list of git objects being
30 // pushed by using the following:
31 //
32 // git rev-list --objects <local sha1> ^<remote sha1>
33 //
34 // If any of those git objects are associated with Git LFS objects, those
35 // objects will be pushed to the Git LFS API.
36 //
37 // In the case of pushing a new branch, the list of git objects will be all of
38 // the git objects in this branch.
39 //
40 // In the case of deleting a branch, no attempts to push Git LFS objects will be
41 // made.
42 func prePushCommand(cmd *cobra.Command, args []string) {
43 if len(args) == 0 {
44 Print(tr.Tr.Get("This should be run through Git's pre-push hook. Run `git lfs update` to install it."))
45 ExitWithCode(1)
46 }
47
48 if cfg.Os.Bool("GIT_LFS_SKIP_PUSH", false) {
49 return
50 }
51
52 requireGitVersion()
53
54 // Remote is first arg
55 remote, _ := git.MapRemoteURL(args[0], true)
56 if err := cfg.SetValidPushRemote(remote); err != nil {
57 Exit(tr.Tr.Get("Invalid remote name %q: %s", args[0], err))
58 }
59
60 ctx := newUploadContext(prePushDryRun)
61 updates := prePushRefs(os.Stdin)
62 if err := uploadForRefUpdates(ctx, updates, false); err != nil {
63 ExitWithError(err)
64 }
65 }
66
67 // prePushRefs parses commit information that the pre-push git hook receives:
68 //
69 // <local ref> <local sha1> <remote ref> <remote sha1>
70 //
71 // Each line describes a proposed update of the remote ref at the remote sha to
72 // the local sha. Multiple updates can be received on multiple lines (such as
73 // from 'git push --all'). These updates are typically received over STDIN.
74 func prePushRefs(r io.Reader) []*git.RefUpdate {
75 scanner := bufio.NewScanner(r)
76 refs := make([]*git.RefUpdate, 0, 1)
77
78 // We can be passed multiple lines of refs
79 for scanner.Scan() {
80 line := strings.TrimSpace(scanner.Text())
81 if len(line) == 0 {
82 continue
83 }
84
85 tracerx.Printf("pre-push: %s", line)
86
87 localRef, remoteRef := decodeRefs(line)
88 if git.IsZeroObjectID(localRef.Sha) {
89 continue
90 }
91
92 refs = append(refs, git.NewRefUpdate(cfg.Git, cfg.PushRemote(), localRef, remoteRef))
93 }
94
95 return refs
96 }
97
98 // decodeRefs pulls the sha1s out of the line read from the pre-push
99 // hook's stdin.
100 func decodeRefs(input string) (*git.Ref, *git.Ref) {
101 refs := strings.Split(strings.TrimSpace(input), " ")
102 for len(refs) < 4 {
103 refs = append(refs, "")
104 }
105
106 localRef := git.ParseRef(refs[0], refs[1])
107 remoteRef := git.ParseRef(refs[2], refs[3])
108 return localRef, remoteRef
109 }
110
111 func init() {
112 RegisterCommand("pre-push", prePushCommand, func(cmd *cobra.Command) {
113 cmd.Flags().BoolVarP(&prePushDryRun, "dry-run", "d", false, "Do everything except actually send the updates")
114 })
115 }