Debian Codegraph @

commands/pull.go

raw symbols SCIP
1 package commands
2
3 import (
4 "bytes"
5 "io"
6 "os"
7 "strings"
8 "sync"
9
10 "github.com/git-lfs/git-lfs/v3/config"
11 "github.com/git-lfs/git-lfs/v3/errors"
12 "github.com/git-lfs/git-lfs/v3/git"
13 "github.com/git-lfs/git-lfs/v3/lfs"
14 "github.com/git-lfs/git-lfs/v3/subprocess"
15 "github.com/git-lfs/git-lfs/v3/tools"
16 "github.com/git-lfs/git-lfs/v3/tq"
17 "github.com/git-lfs/git-lfs/v3/tr"
18 )
19
20 // Handles the process of checking out a single file, and updating the git
21 // index.
22 // Note that the current working directory will be changed to the root
23 // of the working tree, unless no work tree exists (e.g., the repository
24 // is bare and GIT_WORK_TREE is not defined), in which case Run() is a no-op.
25 func newSingleCheckout(gitEnv config.Environment, remote string) abstractCheckout {
26 clean, ok := gitEnv.Get("filter.lfs.clean")
27 if !ok || len(clean) == 0 {
28 return &noOpCheckout{remote: remote}
29 }
30
31 workingDir := cfg.LocalWorkingDir()
32 hasWorkTree := workingDir != ""
33 if hasWorkTree {
34 if err := os.Chdir(workingDir); err != nil {
35 FullError(errors.Wrap(err, tr.Tr.Get("Checkout error trying to change directory: %s", workingDir)))
36 hasWorkTree = false
37 }
38 }
39
40 return &singleCheckout{
41 gitIndexer: &gitIndexer{},
42 hasWorkTree: hasWorkTree,
43 manifest: nil,
44 remote: remote,
45 }
46 }
47
48 type abstractCheckout interface {
49 Manifest() tq.Manifest
50 Skip() bool
51 Run(*lfs.WrappedPointer)
52 RunToPath(*lfs.WrappedPointer, string) error
53 Close()
54 }
55
56 type singleCheckout struct {
57 gitIndexer *gitIndexer
58 hasWorkTree bool
59 manifest tq.Manifest
60 remote string
61 }
62
63 func (c *singleCheckout) Manifest() tq.Manifest {
64 if c.manifest == nil {
65 c.manifest = getTransferManifestOperationRemote("download", c.remote)
66 }
67 return c.manifest
68 }
69
70 func (c *singleCheckout) Skip() bool {
71 return false
72 }
73
74 func (c *singleCheckout) Run(p *lfs.WrappedPointer) {
75 if !c.hasWorkTree {
76 return
77 }
78
79 dirWalker := tools.NewDirWalkerForFile("", p.Name, cfg)
80 err := dirWalker.Walk()
81
82 var filepointer *lfs.Pointer
83 if err != nil {
84 if !os.IsNotExist(err) {
85 LoggedError(err, tr.Tr.Get("Checkout error trying to check path for %q: %s", p.Name, err))
86 return
87 }
88 } else {
89 // Check the content - either missing or still this pointer (not exist is ok)
90 filepointer, err = lfs.DecodePointerFromFile(p.Name)
91 }
92
93 if err != nil {
94 if os.IsNotExist(err) {
95 output, err := git.DiffIndexWithPaths("HEAD", true, []string{p.Name})
96 if err != nil {
97 LoggedError(err, tr.Tr.Get("Checkout error trying to run diff-index: %s", err))
98 return
99 }
100 if strings.HasPrefix(output, ":100644 000000 ") || strings.HasPrefix(output, ":100755 000000 ") {
101 // This file is deleted in the index. Don't try
102 // to check it out.
103 return
104 }
105 } else {
106 if errors.IsNotAPointerError(err) || errors.IsBadPointerKeyError(err) {
107 // File has non-pointer content, leave it alone
108 return
109 }
110
111 LoggedError(err, tr.Tr.Get("Checkout error for %q: %s", p.Name, err))
112 return
113 }
114 }
115
116 if filepointer != nil && filepointer.Oid != p.Oid {
117 // User has probably manually reset a file to another commit
118 // while leaving it a pointer; don't mess with this
119 return
120 }
121
122 if err != nil && os.IsNotExist(err) {
123 if err := dirWalker.WalkAndCreate(); err != nil {
124 LoggedError(err, tr.Tr.Get("Checkout error trying to create path for %q: %s", p.Name, err))
125 return
126 }
127 }
128
129 if err := c.RunToPath(p, p.Name); err != nil {
130 if errors.IsDownloadDeclinedError(err) {
131 // acceptable error, data not local (fetch not run or include/exclude)
132 Error(tr.Tr.Get("Skipped checkout for %q, content not local. Use fetch to download.", p.Name))
133 } else {
134 FullError(errors.Wrap(err, tr.Tr.Get("could not check out %q", p.Name)))
135 }
136 return
137 }
138
139 // errors are only returned when the gitIndexer is starting a new cmd
140 if err := c.gitIndexer.Add(p.Name); err != nil {
141 Panic(err, tr.Tr.Get("Could not update the index"))
142 }
143 }
144
145 // RunToPath checks out the pointer specified by p to the given path. It does
146 // not perform any sort of sanity checking or add the path to the index.
147 func (c *singleCheckout) RunToPath(p *lfs.WrappedPointer, path string) error {
148 gitfilter := lfs.NewGitFilter(cfg)
149 return gitfilter.SmudgeToFile(path, p, false, c.manifest, nil)
150 }
151
152 func (c *singleCheckout) Close() {
153 if err := c.gitIndexer.Close(); err != nil {
154 LoggedError(err, "%s\n%s", tr.Tr.Get("Error updating the Git index:"), c.gitIndexer.Output())
155 }
156 }
157
158 type noOpCheckout struct {
159 manifest tq.Manifest
160 remote string
161 }
162
163 func (c *noOpCheckout) Manifest() tq.Manifest {
164 if c.manifest == nil {
165 c.manifest = getTransferManifestOperationRemote("download", c.remote)
166 }
167 return c.manifest
168 }
169
170 func (c *noOpCheckout) Skip() bool {
171 return true
172 }
173
174 func (c *noOpCheckout) RunToPath(p *lfs.WrappedPointer, path string) error {
175 return nil
176 }
177
178 func (c *noOpCheckout) Run(p *lfs.WrappedPointer) {}
179 func (c *noOpCheckout) Close() {}
180
181 // Don't fire up the update-index command until we have at least one file to
182 // give it. Otherwise git interprets the lack of arguments to mean param-less update-index
183 // which can trigger entire working copy to be re-examined, which triggers clean filters
184 // and which has unexpected side effects (e.g. downloading filtered-out files)
185 type gitIndexer struct {
186 cmd *subprocess.Cmd
187 input io.WriteCloser
188 output bytes.Buffer
189 mu sync.Mutex
190 }
191
192 func (i *gitIndexer) Add(path string) error {
193 i.mu.Lock()
194 defer i.mu.Unlock()
195
196 if i.cmd == nil {
197 // Fire up the update-index command
198 cmd, err := git.UpdateIndexFromStdin()
199 if err != nil {
200 return err
201 }
202 cmd.Stdout = &i.output
203 cmd.Stderr = &i.output
204 stdin, err := cmd.StdinPipe()
205 if err != nil {
206 return err
207 }
208 err = cmd.Start()
209 if err != nil {
210 return err
211 }
212 i.cmd = cmd
213 i.input = stdin
214 }
215
216 i.input.Write([]byte(path + "\n"))
217 return nil
218 }
219
220 func (i *gitIndexer) Output() string {
221 return i.output.String()
222 }
223
224 func (i *gitIndexer) Close() error {
225 i.mu.Lock()
226 defer i.mu.Unlock()
227
228 if i.input != nil {
229 i.input.Close()
230 }
231
232 if i.cmd != nil {
233 return i.cmd.Wait()
234 }
235
236 return nil
237 }