| 1 | package lfs |
| 2 | |
| 3 | import ( |
| 4 | "io" |
| 5 | "path" |
| 6 | "path/filepath" |
| 7 | |
| 8 | "github.com/git-lfs/git-lfs/v3/config" |
| 9 | "github.com/git-lfs/git-lfs/v3/errors" |
| 10 | "github.com/git-lfs/git-lfs/v3/filepathfilter" |
| 11 | "github.com/git-lfs/git-lfs/v3/git" |
| 12 | "github.com/git-lfs/git-lfs/v3/git/gitattr" |
| 13 | "github.com/git-lfs/git-lfs/v3/subprocess" |
| 14 | "github.com/git-lfs/git-lfs/v3/tr" |
| 15 | ) |
| 16 | |
| 17 | func runScanTree(cb GitScannerFoundPointer, ref string, filter *filepathfilter.Filter, gitEnv, osEnv config.Environment) error { |
| 18 | // We don't use the nameMap approach here since that's imprecise when >1 file |
| 19 | // can be using the same content |
| 20 | treeShas, err := lsTreeBlobs(ref, func(t *git.TreeBlob) bool { |
| 21 | return t != nil && t.Size < BlobSizeCutoff && filter.Allows(t.Filename) |
| 22 | }) |
| 23 | if err != nil { |
| 24 | return err |
| 25 | } |
| 26 | |
| 27 | pcw, err := catFileBatchTree(treeShas, gitEnv, osEnv) |
| 28 | if err != nil { |
| 29 | return err |
| 30 | } |
| 31 | |
| 32 | for p := range pcw.Results { |
| 33 | cb(p, nil) |
| 34 | } |
| 35 | |
| 36 | if err := pcw.Wait(); err != nil { |
| 37 | cb(nil, err) |
| 38 | } |
| 39 | return nil |
| 40 | } |
| 41 | |
| 42 | func runScanLFSFiles(cb GitScannerFoundPointer, ref string, filter *filepathfilter.Filter, gitEnv, osEnv config.Environment) error { |
| 43 | var treeShas *TreeBlobChannelWrapper |
| 44 | var err error |
| 45 | if git.IsGitVersionAtLeast("2.42.0") { |
| 46 | treeShas, err = lsFilesBlobs(func(t *git.TreeBlob) bool { |
| 47 | return t != nil && t.Size < BlobSizeCutoff && filter.Allows(t.Filename) |
| 48 | }) |
| 49 | } else { |
| 50 | treeShas, err = lsTreeBlobs(ref, func(t *git.TreeBlob) bool { |
| 51 | return t != nil && t.Size < BlobSizeCutoff && filter.Allows(t.Filename) |
| 52 | }) |
| 53 | } |
| 54 | // We don't use the nameMap approach here since that's imprecise when >1 file |
| 55 | // can be using the same content |
| 56 | if err != nil { |
| 57 | return err |
| 58 | } |
| 59 | |
| 60 | pcw, err := catFileBatchTree(treeShas, gitEnv, osEnv) |
| 61 | if err != nil { |
| 62 | return err |
| 63 | } |
| 64 | |
| 65 | for p := range pcw.Results { |
| 66 | cb(p, nil) |
| 67 | } |
| 68 | |
| 69 | if err := pcw.Wait(); err != nil { |
| 70 | cb(nil, err) |
| 71 | } |
| 72 | return nil |
| 73 | } |
| 74 | |
| 75 | // catFileBatchTree() uses an ObjectDatabase from the |
| 76 | // github.com/git-lfs/gitobj/v2 package to get the contents of Git |
| 77 | // blob objects, given their SHA1s from git.TreeBlob structs, similar |
| 78 | // to the behaviour of 'git cat-file --batch'. |
| 79 | // Input git.TreeBlob structs should be sent over the treeblobs channel. |
| 80 | // The blob contents will be decoded as Git LFS pointers and any valid |
| 81 | // pointers will be returned as pointer.Pointer structs in a new channel. |
| 82 | func catFileBatchTree(treeblobs *TreeBlobChannelWrapper, gitEnv, osEnv config.Environment) (*PointerChannelWrapper, error) { |
| 83 | scanner, err := NewPointerScanner(gitEnv, osEnv) |
| 84 | if err != nil { |
| 85 | return nil, err |
| 86 | } |
| 87 | |
| 88 | pointers := make(chan *WrappedPointer, chanBufSize) |
| 89 | errchan := make(chan error, 10) // Multiple errors possible |
| 90 | |
| 91 | go func() { |
| 92 | hasNext := true |
| 93 | for t := range treeblobs.Results { |
| 94 | hasNext = scanner.Scan(t.Oid) |
| 95 | |
| 96 | if p := scanner.Pointer(); p != nil { |
| 97 | p.Name = t.Filename |
| 98 | pointers <- p |
| 99 | } |
| 100 | |
| 101 | if err := scanner.Err(); err != nil { |
| 102 | errchan <- err |
| 103 | } |
| 104 | |
| 105 | if !hasNext { |
| 106 | break |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | // If the scanner quit early, we may still have treeblobs to |
| 111 | // read, so waiting for it to close will cause a deadlock. |
| 112 | if hasNext { |
| 113 | // Deal with nested error from incoming treeblobs |
| 114 | err := treeblobs.Wait() |
| 115 | if err != nil { |
| 116 | errchan <- err |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | if err = scanner.Close(); err != nil { |
| 121 | errchan <- err |
| 122 | } |
| 123 | |
| 124 | close(pointers) |
| 125 | close(errchan) |
| 126 | }() |
| 127 | |
| 128 | return NewPointerChannelWrapper(pointers, errchan), nil |
| 129 | } |
| 130 | |
| 131 | // Use ls-tree at ref to find a list of candidate tree blobs which might be lfs files |
| 132 | // The returned channel will be sent these blobs which should be sent to catFileBatchTree |
| 133 | // for final check & conversion to Pointer |
| 134 | func lsTreeBlobs(ref string, predicate func(*git.TreeBlob) bool) (*TreeBlobChannelWrapper, error) { |
| 135 | return lsBlobs(func() (*subprocess.BufferedCmd, error) { |
| 136 | return git.LsTree(ref) |
| 137 | }, predicate) |
| 138 | } |
| 139 | |
| 140 | func lsBlobs(backend func() (*subprocess.BufferedCmd, error), predicate func(*git.TreeBlob) bool) (*TreeBlobChannelWrapper, error) { |
| 141 | cmd, err := backend() |
| 142 | if err != nil { |
| 143 | return nil, err |
| 144 | } |
| 145 | |
| 146 | cmd.Stdin.Close() |
| 147 | |
| 148 | blobs := make(chan git.TreeBlob, chanBufSize) |
| 149 | errchan := make(chan error, 1) |
| 150 | |
| 151 | go func() { |
| 152 | scanner := git.NewLsTreeScanner(cmd.Stdout) |
| 153 | for scanner.Scan() { |
| 154 | if t := scanner.TreeBlob(); predicate(t) { |
| 155 | blobs <- *t |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | stderr, _ := io.ReadAll(cmd.Stderr) |
| 160 | err := cmd.Wait() |
| 161 | if err != nil { |
| 162 | errchan <- errors.New(tr.Tr.Get("error in `git ls-tree`: %v %v", err, string(stderr))) |
| 163 | } |
| 164 | close(blobs) |
| 165 | close(errchan) |
| 166 | }() |
| 167 | |
| 168 | return NewTreeBlobChannelWrapper(blobs, errchan), nil |
| 169 | } |
| 170 | |
| 171 | // Use ls-files at ref to find a list of candidate tree blobs which might be lfs files |
| 172 | // The returned channel will be sent these blobs which should be sent to catFileBatchTree |
| 173 | // for final check & conversion to Pointer |
| 174 | func lsFilesBlobs(predicate func(*git.TreeBlob) bool) (*TreeBlobChannelWrapper, error) { |
| 175 | return lsBlobs(func() (*subprocess.BufferedCmd, error) { |
| 176 | return git.LsFilesLFS() |
| 177 | }, predicate) |
| 178 | } |
| 179 | |
| 180 | func catFileBatchTreeForPointers(treeblobs *TreeBlobChannelWrapper, gitEnv, osEnv config.Environment) (map[string]*WrappedPointer, *filepathfilter.Filter, error) { |
| 181 | pscanner, err := NewPointerScanner(gitEnv, osEnv) |
| 182 | if err != nil { |
| 183 | return nil, nil, err |
| 184 | } |
| 185 | oscanner, err := git.NewObjectScanner(gitEnv, osEnv) |
| 186 | if err != nil { |
| 187 | return nil, nil, err |
| 188 | } |
| 189 | |
| 190 | pointers := make(map[string]*WrappedPointer) |
| 191 | |
| 192 | paths := make([]gitattr.AttributePath, 0) |
| 193 | processor := gitattr.NewMacroProcessor() |
| 194 | |
| 195 | hasNext := true |
| 196 | for t := range treeblobs.Results { |
| 197 | if path.Base(t.Filename) == ".gitattributes" { |
| 198 | hasNext = oscanner.Scan(t.Oid) |
| 199 | |
| 200 | if rdr := oscanner.Contents(); rdr != nil { |
| 201 | paths = append(paths, gitattr.AttrPathsFromReader( |
| 202 | processor, |
| 203 | t.Filename, |
| 204 | "", |
| 205 | rdr, |
| 206 | t.Filename == ".gitattributes", // Read macros from the top-level attributes |
| 207 | )...) |
| 208 | } |
| 209 | |
| 210 | if err := oscanner.Err(); err != nil { |
| 211 | return nil, nil, err |
| 212 | } |
| 213 | } else if t.Size < BlobSizeCutoff { |
| 214 | hasNext = pscanner.Scan(t.Oid) |
| 215 | |
| 216 | // It's intentional that we insert nil for |
| 217 | // non-pointers; we want to keep track of them |
| 218 | // as well as pointers. |
| 219 | p := pscanner.Pointer() |
| 220 | if p != nil { |
| 221 | p.Name = t.Filename |
| 222 | } |
| 223 | pointers[t.Filename] = p |
| 224 | |
| 225 | if err := pscanner.Err(); err != nil { |
| 226 | return nil, nil, err |
| 227 | } |
| 228 | } else { |
| 229 | pointers[t.Filename] = nil |
| 230 | } |
| 231 | |
| 232 | if !hasNext { |
| 233 | break |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | // If the scanner quit early, we may still have treeblobs to |
| 238 | // read, so waiting for it to close will cause a deadlock. |
| 239 | if hasNext { |
| 240 | // Deal with nested error from incoming treeblobs |
| 241 | err := treeblobs.Wait() |
| 242 | if err != nil { |
| 243 | return nil, nil, err |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | if err = pscanner.Close(); err != nil { |
| 248 | return nil, nil, err |
| 249 | } |
| 250 | if err = oscanner.Close(); err != nil { |
| 251 | return nil, nil, err |
| 252 | } |
| 253 | |
| 254 | includes := make([]filepathfilter.Pattern, 0, len(paths)) |
| 255 | excludes := make([]filepathfilter.Pattern, 0, len(paths)) |
| 256 | for _, path := range paths { |
| 257 | // Convert all separators to `/` before creating a pattern to |
| 258 | // avoid characters being escaped in situations like `subtree\*.md` |
| 259 | pattern := filepathfilter.NewPattern(filepath.ToSlash(path.Path), filepathfilter.GitAttributes, gitEnv) |
| 260 | if path.Tracked { |
| 261 | includes = append(includes, pattern) |
| 262 | } else { |
| 263 | excludes = append(excludes, pattern) |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | return pointers, filepathfilter.NewFromPatterns(includes, excludes, filepathfilter.DefaultValue(false)), nil |
| 268 | } |
| 269 | |
| 270 | func runScanTreeForPointers(cb GitScannerFoundPointer, tree string, gitEnv, osEnv config.Environment) error { |
| 271 | treeShas, err := lsTreeBlobs(tree, func(t *git.TreeBlob) bool { |
| 272 | return t != nil && (t.Mode == 0100644 || t.Mode == 0100755) |
| 273 | }) |
| 274 | if err != nil { |
| 275 | return err |
| 276 | } |
| 277 | |
| 278 | pointers, filter, err := catFileBatchTreeForPointers(treeShas, gitEnv, osEnv) |
| 279 | if err != nil { |
| 280 | return err |
| 281 | } |
| 282 | |
| 283 | for name, p := range pointers { |
| 284 | // This file matches the patterns in .gitattributes, so it |
| 285 | // should be a pointer. If it is not, then it is a plain Git |
| 286 | // blob, which we report as an error. |
| 287 | if filter.Allows(name) { |
| 288 | if p == nil { |
| 289 | cb(nil, errors.NewPointerScanError(errors.NewNotAPointerError(nil), tree, name)) |
| 290 | } else { |
| 291 | cb(p, nil) |
| 292 | } |
| 293 | } |
| 294 | } |
| 295 | return nil |
| 296 | } |