Debian Codegraph @

lfs/gitscanner_refs.go

raw symbols SCIP
1 package lfs
2
3 import (
4 "encoding/hex"
5 "sync"
6
7 "github.com/git-lfs/git-lfs/v3/config"
8 "github.com/git-lfs/git-lfs/v3/git"
9 )
10
11 // The nameMap structure provides a goroutine-safe mapping of Git object IDs
12 // (as SHA hex strings) to their pathspecs, either fully-qualified directory
13 // paths for trees or file paths for blobs, as returned by "git rev-list".
14 type nameMap struct {
15 names map[string]string
16 mutex *sync.Mutex
17 }
18
19 func (m *nameMap) getName(sha string) (string, bool) {
20 m.mutex.Lock()
21 name, ok := m.names[sha]
22 m.mutex.Unlock()
23 return name, ok
24 }
25
26 func (m *nameMap) setName(sha, name string) {
27 m.mutex.Lock()
28 m.names[sha] = name
29 m.mutex.Unlock()
30 }
31
32 func newNameMap() *nameMap {
33 return &nameMap{
34 names: make(map[string]string, 0),
35 mutex: &sync.Mutex{},
36 }
37 }
38
39 type lockableNameSet struct {
40 nameMap *nameMap
41 set GitScannerSet
42 }
43
44 // Determines if the given blob sha matches a locked file.
45 func (s *lockableNameSet) Check(blobSha string) (string, bool) {
46 if s == nil || s.nameMap == nil || s.set == nil {
47 return "", false
48 }
49
50 name, ok := s.nameMap.getName(blobSha)
51 if !ok {
52 return name, ok
53 }
54
55 if s.set.Contains(name) {
56 return name, true
57 }
58 return name, false
59 }
60
61 func noopFoundLockable(name string) {}
62
63 // scanRefsToChan scans through all unique objects reachable from the
64 // "include" refs and not reachable from any "exclude" refs and invokes the
65 // provided callback for each pointer file, valid or invalid, that it finds.
66 // Reports unique OIDs once only, not multiple times if more than one file
67 // has the same content.
68 func scanRefsToChan(scanner *GitScanner, pointerCb GitScannerFoundPointer, include, exclude []string, gitEnv, osEnv config.Environment) error {
69 revs, nameMap, err := revListShas(scanner, include, exclude)
70 if err != nil {
71 return err
72 }
73
74 lockableSet := &lockableNameSet{nameMap: nameMap, set: scanner.potentialLockables}
75 smallShas, batchLockableCh, err := catFileBatchCheck(revs, lockableSet)
76 if err != nil {
77 return err
78 }
79
80 lockableCb := scanner.foundLockable
81 if lockableCb == nil {
82 lockableCb = noopFoundLockable
83 }
84
85 go func(cb GitScannerFoundLockable, ch chan string) {
86 for name := range ch {
87 cb(name)
88 }
89 }(lockableCb, batchLockableCh)
90
91 pointers, checkLockableCh, err := catFileBatch(smallShas, lockableSet, gitEnv, osEnv)
92 if err != nil {
93 return err
94 }
95
96 for p := range pointers.Results {
97 if name, ok := nameMap.getName(p.Sha1); ok {
98 p.Name = name
99 }
100
101 if scanner.Filter.Allows(p.Name) {
102 pointerCb(p, nil)
103 }
104 }
105
106 for lockableName := range checkLockableCh {
107 if scanner.Filter.Allows(lockableName) {
108 lockableCb(lockableName)
109 }
110 }
111
112 if err := pointers.Wait(); err != nil {
113 pointerCb(nil, err)
114 }
115
116 return nil
117 }
118
119 // scanRefsToChanSingleIncludeExclude scans through all unique objects
120 // reachable from the "include" ref and not reachable from the "exclude" ref
121 // and invokes the provided callback for each pointer file, valid or invalid,
122 // that it finds.
123 // Reports unique OIDs once only, not multiple times if more than one file
124 // has the same content.
125 func scanRefsToChanSingleIncludeExclude(scanner *GitScanner, pointerCb GitScannerFoundPointer, include, exclude string, gitEnv, osEnv config.Environment) error {
126 return scanRefsToChan(scanner, pointerCb, []string{include}, []string{exclude}, gitEnv, osEnv)
127 }
128
129 // scanRefsToChanSingleIncludeMultiExclude scans through all unique objects
130 // reachable from the "include" ref and not reachable from any "exclude" refs
131 // and invokes the provided callback for each pointer file, valid or invalid,
132 // that it finds.
133 // Reports unique OIDs once only, not multiple times if more than one file
134 // has the same content.
135 func scanRefsToChanSingleIncludeMultiExclude(scanner *GitScanner, pointerCb GitScannerFoundPointer, include string, exclude []string, gitEnv, osEnv config.Environment) error {
136 return scanRefsToChan(scanner, pointerCb, []string{include}, exclude, gitEnv, osEnv)
137 }
138
139 // scanRefsByTree scans through all objects reachable from the "include" refs
140 // and not reachable from any "exclude" refs and invokes the provided callback
141 // for each pointer file, valid or invalid, that it finds.
142 // Objects which appear in multiple trees will be visited once per tree.
143 func scanRefsByTree(scanner *GitScanner, pointerCb GitScannerFoundPointer, include, exclude []string, gitEnv, osEnv config.Environment) error {
144 revs, _, err := revListShas(scanner, include, exclude)
145 if err != nil {
146 return err
147 }
148
149 errchan := make(chan error, 20) // multiple errors possible
150 wg := &sync.WaitGroup{}
151
152 for r := range revs.Results {
153 wg.Add(1)
154 go func(rev string) {
155 defer wg.Done()
156 err := runScanTreeForPointers(pointerCb, rev, gitEnv, osEnv)
157 if err != nil {
158 errchan <- err
159 }
160 }(r)
161 }
162
163 wg.Wait()
164 close(errchan)
165 for err := range errchan {
166 if err != nil {
167 return err
168 }
169 }
170
171 return revs.Wait()
172 }
173
174 // revListShas uses git rev-list to return the list of object sha1s
175 // for the given ref. If all is true, ref is ignored. It returns a
176 // channel from which sha1 strings can be read, and a map of the sha1
177 // object IDs to their pathspecs, which will be populated as the sha1
178 // strings are written to the channel.
179 func revListShas(scanner *GitScanner, include, exclude []string) (*StringChannelWrapper, *nameMap, error) {
180 nameMap := newNameMap()
181 revListScanner, err := git.NewRevListScanner(include, exclude, &git.ScanRefsOptions{
182 Mode: git.ScanningMode(scanner.mode),
183 SkipDeletedBlobs: scanner.skipDeletedBlobs,
184 CommitsOnly: scanner.commitsOnly,
185 Remote: scanner.remote,
186 SkippedRefs: scanner.skippedRefs,
187 Names: nameMap.names,
188 Mutex: nameMap.mutex,
189 })
190
191 if err != nil {
192 return nil, nil, err
193 }
194
195 revs := make(chan string, chanBufSize)
196 errs := make(chan error, 5) // may be multiple errors
197
198 go func() {
199 for revListScanner.Scan() {
200 sha := hex.EncodeToString(revListScanner.OID())
201 if name := revListScanner.Name(); len(name) > 0 {
202 nameMap.setName(sha, name)
203 }
204 revs <- sha
205 }
206
207 if err = revListScanner.Err(); err != nil {
208 errs <- err
209 }
210
211 if err = revListScanner.Close(); err != nil {
212 errs <- err
213 }
214
215 close(revs)
216 close(errs)
217 }()
218
219 return NewStringChannelWrapper(revs, errs), nameMap, nil
220 }