Debian Codegraph @

commands/command_push.go

raw symbols SCIP
1 package commands
2
3 import (
4 "bufio"
5 "os"
6
7 "github.com/git-lfs/git-lfs/v3/errors"
8 "github.com/git-lfs/git-lfs/v3/git"
9 "github.com/git-lfs/git-lfs/v3/lfs"
10 "github.com/git-lfs/git-lfs/v3/tq"
11 "github.com/git-lfs/git-lfs/v3/tr"
12 "github.com/rubyist/tracerx"
13 "github.com/spf13/cobra"
14 )
15
16 var (
17 pushDryRun = false
18 pushObjectIDs = false
19 pushAll = false
20 useStdin = false
21
22 // shares some global vars and functions with command_pre_push.go
23 )
24
25 // pushCommand pushes local objects to a Git LFS server. It has four forms:
26 //
27 // `<remote> <ref>...`
28 // `<remote> --stdin` (reads refs from stdin)
29 // `<remote> --object-id <oid>...`
30 // `<remote> --object-id --stdin` (reads oids from stdin)
31 //
32 // Remote must be a remote name, not a URL. With --stdin, values are newline
33 // separated.
34 //
35 // pushCommand calculates the git objects to send by comparing the range
36 // of commits between the local and remote git servers.
37 func pushCommand(cmd *cobra.Command, args []string) {
38 if len(args) == 0 {
39 Exit(tr.Tr.Get("Specify a remote and a remote branch name (`git lfs push origin main`)"))
40 }
41
42 requireGitVersion()
43
44 // Remote is first arg
45 if err := cfg.SetValidPushRemote(args[0]); err != nil {
46 Exit(tr.Tr.Get("Invalid remote name %q: %s", args[0], err))
47 }
48
49 ctx := newUploadContext(pushDryRun)
50
51 var argList []string
52 if useStdin {
53 if len(args) > 1 {
54 Exit(tr.Tr.Get("Further command line arguments are ignored with --stdin"))
55 }
56
57 scanner := bufio.NewScanner(os.Stdin) // line-delimited
58 for scanner.Scan() {
59 line := scanner.Text()
60 if line != "" {
61 argList = append(argList, line)
62 }
63 }
64 if err := scanner.Err(); err != nil {
65 ExitWithError(errors.Wrap(err, tr.Tr.Get("Error reading from stdin:")))
66 }
67 } else {
68 argList = args[1:]
69 }
70
71 if pushObjectIDs {
72 // We allow no object IDs with `--stdin` to make scripting
73 // easier and avoid having to special-case this in scripts.
74 if !useStdin && len(argList) < 1 {
75 Print(tr.Tr.Get("At least one object ID must be supplied with --object-id"))
76 ExitWithCode(1)
77 }
78 uploadsWithObjectIDs(ctx, argList)
79 } else {
80 if !useStdin && !pushAll && len(argList) < 1 {
81 Print(tr.Tr.Get("At least one ref must be supplied without --all"))
82 ExitWithCode(1)
83 }
84 uploadsBetweenRefAndRemote(ctx, argList)
85 }
86 }
87
88 func uploadsBetweenRefAndRemote(ctx *uploadContext, refnames []string) {
89 tracerx.Printf("Upload refs %v to remote %v", refnames, ctx.Remote)
90
91 updates, err := lfsPushRefs(refnames, pushAll)
92 if err != nil {
93 Error(err.Error())
94 Exit(tr.Tr.Get("Error getting local refs."))
95 }
96
97 if err := uploadForRefUpdates(ctx, updates, pushAll); err != nil {
98 ExitWithError(err)
99 }
100 }
101
102 func uploadsWithObjectIDs(ctx *uploadContext, oids []string) {
103 pointers := make([]*lfs.WrappedPointer, len(oids))
104 for i, oid := range oids {
105 mp, err := ctx.gitfilter.ObjectPath(oid)
106 if err != nil {
107 ExitWithError(errors.Wrap(err, tr.Tr.Get("Unable to find local media path:")))
108 }
109
110 stat, err := os.Stat(mp)
111 if err != nil {
112 ExitWithError(errors.Wrap(err, tr.Tr.Get("Unable to stat local media path")))
113 }
114
115 pointers[i] = &lfs.WrappedPointer{
116 Name: mp,
117 Pointer: &lfs.Pointer{
118 Oid: oid,
119 Size: stat.Size(),
120 },
121 }
122 }
123
124 q := ctx.NewQueue(tq.RemoteRef(pushRemoteRef()))
125 ctx.UploadPointers(q, pointers...)
126 ctx.CollectErrors(q)
127 ctx.ReportErrors()
128 }
129
130 // lfsPushRefs returns valid ref updates from the given ref and --all arguments.
131 // Either one or more refs can be explicitly specified, or --all indicates all
132 // local refs are pushed.
133 func lfsPushRefs(refnames []string, pushAll bool) ([]*git.RefUpdate, error) {
134 localrefs, err := git.LocalRefs()
135 if err != nil {
136 return nil, err
137 }
138
139 if pushAll && len(refnames) == 0 {
140 refs := make([]*git.RefUpdate, len(localrefs))
141 for i, lr := range localrefs {
142 refs[i] = git.NewRefUpdate(cfg.Git, cfg.PushRemote(), lr, nil)
143 }
144 return refs, nil
145 }
146
147 reflookup := make(map[string]*git.Ref, len(localrefs))
148 for _, ref := range localrefs {
149 reflookup[ref.Name] = ref
150 }
151
152 refs := make([]*git.RefUpdate, len(refnames))
153 for i, name := range refnames {
154 if ref, ok := reflookup[name]; ok {
155 refs[i] = git.NewRefUpdate(cfg.Git, cfg.PushRemote(), ref, nil)
156 } else {
157 ref := &git.Ref{Name: name, Type: git.RefTypeOther, Sha: name}
158 if _, err := git.ResolveRef(name); err != nil {
159 return nil, errors.New(tr.Tr.Get("Invalid ref argument: %v", name))
160 }
161 refs[i] = git.NewRefUpdate(cfg.Git, cfg.PushRemote(), ref, nil)
162 }
163 }
164
165 return refs, nil
166 }
167
168 func init() {
169 RegisterCommand("push", pushCommand, func(cmd *cobra.Command) {
170 cmd.Flags().BoolVarP(&pushDryRun, "dry-run", "d", false, "Do everything except actually send the updates")
171 cmd.Flags().BoolVarP(&pushObjectIDs, "object-id", "o", false, "Push LFS object ID(s)")
172 cmd.Flags().BoolVarP(&useStdin, "stdin", "", false, "Read object IDs or refs from stdin")
173 cmd.Flags().BoolVarP(&pushAll, "all", "a", false, "Push all objects for the current ref to the remote.")
174 })
175 }