| 1 | package lfs |
| 2 | |
| 3 | import ( |
| 4 | "strings" |
| 5 | "sync" |
| 6 | |
| 7 | "github.com/git-lfs/git-lfs/v3/config" |
| 8 | "github.com/git-lfs/git-lfs/v3/filepathfilter" |
| 9 | ) |
| 10 | |
| 11 | // ScanIndex returns a slice of WrappedPointer objects for all Git LFS pointers |
| 12 | // it finds in the index. |
| 13 | // |
| 14 | // Ref is the ref at which to scan, which may be "HEAD" if there is at least one |
| 15 | // commit. |
| 16 | func scanIndex(cb GitScannerFoundPointer, ref string, workingDir string, f *filepathfilter.Filter, gitEnv, osEnv config.Environment) error { |
| 17 | indexMap := &indexFileMap{ |
| 18 | nameMap: make(map[string][]*indexFile), |
| 19 | nameShaPairs: make(map[string]bool), |
| 20 | mutex: &sync.Mutex{}, |
| 21 | } |
| 22 | |
| 23 | revs, err := revListIndex(ref, false, indexMap, workingDir) |
| 24 | if err != nil { |
| 25 | return err |
| 26 | } |
| 27 | |
| 28 | cachedRevs, err := revListIndex(ref, true, indexMap, workingDir) |
| 29 | if err != nil { |
| 30 | return err |
| 31 | } |
| 32 | |
| 33 | allRevsErr := make(chan error, 5) // can be multiple errors below |
| 34 | allRevsChan := make(chan string, 1) |
| 35 | allRevs := NewStringChannelWrapper(allRevsChan, allRevsErr) |
| 36 | go func() { |
| 37 | seenRevs := make(map[string]bool, 0) |
| 38 | |
| 39 | for rev := range cachedRevs.Results { |
| 40 | if !seenRevs[rev] { |
| 41 | allRevsChan <- rev |
| 42 | seenRevs[rev] = true |
| 43 | } |
| 44 | } |
| 45 | err = cachedRevs.Wait() |
| 46 | if err != nil { |
| 47 | allRevsErr <- err |
| 48 | } |
| 49 | |
| 50 | for rev := range revs.Results { |
| 51 | if !seenRevs[rev] { |
| 52 | allRevsChan <- rev |
| 53 | seenRevs[rev] = true |
| 54 | } |
| 55 | } |
| 56 | err := revs.Wait() |
| 57 | if err != nil { |
| 58 | allRevsErr <- err |
| 59 | } |
| 60 | close(allRevsChan) |
| 61 | close(allRevsErr) |
| 62 | }() |
| 63 | |
| 64 | smallShas, _, err := catFileBatchCheck(allRevs, nil) |
| 65 | if err != nil { |
| 66 | return err |
| 67 | } |
| 68 | |
| 69 | ch := make(chan gitscannerResult, chanBufSize) |
| 70 | |
| 71 | barePointerCh, _, err := catFileBatch(smallShas, nil, gitEnv, osEnv) |
| 72 | if err != nil { |
| 73 | return err |
| 74 | } |
| 75 | |
| 76 | go func() { |
| 77 | for p := range barePointerCh.Results { |
| 78 | for _, file := range indexMap.FilesFor(p.Sha1) { |
| 79 | // Append a new *WrappedPointer that combines the data |
| 80 | // from the index file, and the pointer "p". |
| 81 | ch <- gitscannerResult{ |
| 82 | Pointer: &WrappedPointer{ |
| 83 | Sha1: p.Sha1, |
| 84 | Name: file.Name, |
| 85 | SrcName: file.SrcName, |
| 86 | Status: file.Status, |
| 87 | Pointer: p.Pointer, |
| 88 | }, |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | if err := barePointerCh.Wait(); err != nil { |
| 94 | ch <- gitscannerResult{Err: err} |
| 95 | } |
| 96 | |
| 97 | close(ch) |
| 98 | }() |
| 99 | |
| 100 | for result := range ch { |
| 101 | if f.Allows(result.Pointer.Name) { |
| 102 | cb(result.Pointer, result.Err) |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | return nil |
| 107 | } |
| 108 | |
| 109 | // revListIndex uses git diff-index to return the list of object sha1s |
| 110 | // for in the indexf. It returns a channel from which sha1 strings can be read. |
| 111 | // The namMap will be filled indexFile pointers mapping sha1s to indexFiles. |
| 112 | func revListIndex(atRef string, cache bool, indexMap *indexFileMap, workingDir string) (*StringChannelWrapper, error) { |
| 113 | scanner, err := NewDiffIndexScanner(atRef, cache, false, workingDir) |
| 114 | if err != nil { |
| 115 | return nil, err |
| 116 | } |
| 117 | |
| 118 | revs := make(chan string, chanBufSize) |
| 119 | errs := make(chan error, 1) |
| 120 | |
| 121 | go func() { |
| 122 | for scanner.Scan() { |
| 123 | var name string = scanner.Entry().DstName |
| 124 | if len(name) == 0 { |
| 125 | name = scanner.Entry().SrcName |
| 126 | } |
| 127 | |
| 128 | indexMap.Add(scanner.Entry().DstSha, &indexFile{ |
| 129 | Name: name, |
| 130 | SrcName: scanner.Entry().SrcName, |
| 131 | Status: string(scanner.Entry().Status), |
| 132 | }) |
| 133 | |
| 134 | revs <- scanner.Entry().DstSha |
| 135 | } |
| 136 | |
| 137 | if err := scanner.Err(); err != nil { |
| 138 | errs <- err |
| 139 | } |
| 140 | |
| 141 | close(revs) |
| 142 | close(errs) |
| 143 | }() |
| 144 | |
| 145 | return NewStringChannelWrapper(revs, errs), nil |
| 146 | } |
| 147 | |
| 148 | // indexFile is used when scanning the index. It stores the name of |
| 149 | // the file, the status of the file in the index, and, in the case of |
| 150 | // a moved or copied file, the original name of the file. |
| 151 | type indexFile struct { |
| 152 | Name string |
| 153 | SrcName string |
| 154 | Status string |
| 155 | } |
| 156 | |
| 157 | type indexFileMap struct { |
| 158 | // mutex guards nameMap and nameShaPairs |
| 159 | mutex *sync.Mutex |
| 160 | // nameMap maps SHA1s to a slice of `*indexFile`s |
| 161 | nameMap map[string][]*indexFile |
| 162 | // nameShaPairs maps "sha1:name" -> bool |
| 163 | nameShaPairs map[string]bool |
| 164 | } |
| 165 | |
| 166 | // FilesFor returns all `*indexFile`s that match the given `sha`. |
| 167 | func (m *indexFileMap) FilesFor(sha string) []*indexFile { |
| 168 | m.mutex.Lock() |
| 169 | defer m.mutex.Unlock() |
| 170 | |
| 171 | return m.nameMap[sha] |
| 172 | } |
| 173 | |
| 174 | // Add appends unique index files to the given SHA, "sha". A file is considered |
| 175 | // unique if its combination of SHA and current filename have not yet been seen |
| 176 | // by this instance "m" of *indexFileMap. |
| 177 | func (m *indexFileMap) Add(sha string, index *indexFile) { |
| 178 | m.mutex.Lock() |
| 179 | defer m.mutex.Unlock() |
| 180 | |
| 181 | pairKey := strings.Join([]string{sha, index.Name}, ":") |
| 182 | if m.nameShaPairs[pairKey] { |
| 183 | return |
| 184 | } |
| 185 | |
| 186 | m.nameMap[sha] = append(m.nameMap[sha], index) |
| 187 | m.nameShaPairs[pairKey] = true |
| 188 | } |