| 1 | package lfs |
| 2 | |
| 3 | import ( |
| 4 | "github.com/git-lfs/git-lfs/v3/config" |
| 5 | "github.com/git-lfs/git-lfs/v3/git" |
| 6 | "github.com/git-lfs/git-lfs/v3/tools" |
| 7 | ) |
| 8 | |
| 9 | const ( |
| 10 | // BlobSizeCutoff is used to determine which files to scan for Git LFS |
| 11 | // pointers. Any file with a size below this cutoff will be scanned. |
| 12 | BlobSizeCutoff = 1024 |
| 13 | |
| 14 | // stdoutBufSize is the size of the buffers given to a sub-process stdout |
| 15 | stdoutBufSize = 16384 |
| 16 | |
| 17 | // chanBufSize is the size of the channels used to pass data from one |
| 18 | // sub-process to another. |
| 19 | chanBufSize = 100 |
| 20 | ) |
| 21 | |
| 22 | // WrappedPointer wraps a pointer.Pointer and provides the git sha1 |
| 23 | // and the file name associated with the object, taken from the |
| 24 | // rev-list output. |
| 25 | type WrappedPointer struct { |
| 26 | Sha1 string |
| 27 | Name string |
| 28 | SrcName string |
| 29 | Status string |
| 30 | *Pointer |
| 31 | } |
| 32 | |
| 33 | // catFileBatchCheck uses git cat-file --batch-check to get the type |
| 34 | // and size of a git object. Any object that isn't of type blob and |
| 35 | // under the BlobSizeCutoff will be ignored. revs is a channel over |
| 36 | // which strings containing git sha1s will be sent. It returns a channel |
| 37 | // from which sha1 strings can be read. |
| 38 | func catFileBatchCheck(revs *StringChannelWrapper, lockableSet *lockableNameSet) (*StringChannelWrapper, chan string, error) { |
| 39 | smallRevCh := make(chan string, chanBufSize) |
| 40 | lockableCh := make(chan string, chanBufSize) |
| 41 | errCh := make(chan error, 2) // up to 2 errors, one from each goroutine |
| 42 | if err := runCatFileBatchCheck(smallRevCh, lockableCh, lockableSet, revs, errCh); err != nil { |
| 43 | return nil, nil, err |
| 44 | } |
| 45 | return NewStringChannelWrapper(smallRevCh, errCh), lockableCh, nil |
| 46 | } |
| 47 | |
| 48 | // catFileBatch() uses an ObjectDatabase from the |
| 49 | // github.com/git-lfs/gitobj/v2 package to get the contents of Git |
| 50 | // blob objects, given their SHA1s, similar to the behaviour of |
| 51 | // 'git cat-file --batch'. |
| 52 | // Input Git blob SHA1s should be sent over the revs channel. |
| 53 | // The blob contents will be decoded as Git LFS pointers and any valid |
| 54 | // pointers will be returned as pointer.Pointer structs in a new channel. |
| 55 | func catFileBatch(revs *StringChannelWrapper, lockableSet *lockableNameSet, gitEnv, osEnv config.Environment) (*PointerChannelWrapper, chan string, error) { |
| 56 | pointerCh := make(chan *WrappedPointer, chanBufSize) |
| 57 | lockableCh := make(chan string, chanBufSize) |
| 58 | errCh := make(chan error, 5) // shared by 2 goroutines & may add more detail errors? |
| 59 | if err := runCatFileBatch(pointerCh, lockableCh, lockableSet, revs, errCh, gitEnv, osEnv); err != nil { |
| 60 | return nil, nil, err |
| 61 | } |
| 62 | return NewPointerChannelWrapper(pointerCh, errCh), lockableCh, nil |
| 63 | } |
| 64 | |
| 65 | // ChannelWrapper for pointer Scan* functions to more easily return async error data via Wait() |
| 66 | // See NewPointerChannelWrapper for construction / use |
| 67 | type PointerChannelWrapper struct { |
| 68 | *tools.BaseChannelWrapper |
| 69 | Results <-chan *WrappedPointer |
| 70 | } |
| 71 | |
| 72 | // Construct a new channel wrapper for WrappedPointer |
| 73 | // Caller can use s.Results directly for normal processing then call Wait() to finish & check for errors |
| 74 | // Scan function is required to create error channel large enough not to block (usually 1 is ok) |
| 75 | func NewPointerChannelWrapper(pointerChan <-chan *WrappedPointer, errorChan <-chan error) *PointerChannelWrapper { |
| 76 | return &PointerChannelWrapper{tools.NewBaseChannelWrapper(errorChan), pointerChan} |
| 77 | } |
| 78 | |
| 79 | // ChannelWrapper for string channel functions to more easily return async error data via Wait() |
| 80 | // Caller can use s.Results directly for normal processing then call Wait() to finish & check for errors |
| 81 | // See NewStringChannelWrapper for construction / use |
| 82 | type StringChannelWrapper struct { |
| 83 | *tools.BaseChannelWrapper |
| 84 | Results <-chan string |
| 85 | } |
| 86 | |
| 87 | // Construct a new channel wrapper for string |
| 88 | // Caller can use s.Results directly for normal processing then call Wait() to finish & check for errors |
| 89 | func NewStringChannelWrapper(stringChan <-chan string, errorChan <-chan error) *StringChannelWrapper { |
| 90 | return &StringChannelWrapper{tools.NewBaseChannelWrapper(errorChan), stringChan} |
| 91 | } |
| 92 | |
| 93 | // ChannelWrapper for TreeBlob channel functions to more easily return async error data via Wait() |
| 94 | // See NewTreeBlobChannelWrapper for construction / use |
| 95 | type TreeBlobChannelWrapper struct { |
| 96 | *tools.BaseChannelWrapper |
| 97 | Results <-chan git.TreeBlob |
| 98 | } |
| 99 | |
| 100 | // Construct a new channel wrapper for TreeBlob |
| 101 | // Caller can use s.Results directly for normal processing then call Wait() to finish & check for errors |
| 102 | func NewTreeBlobChannelWrapper(treeBlobChan <-chan git.TreeBlob, errorChan <-chan error) *TreeBlobChannelWrapper { |
| 103 | return &TreeBlobChannelWrapper{tools.NewBaseChannelWrapper(errorChan), treeBlobChan} |
| 104 | } |