Debian Codegraph @

lfs/gitscanner_catfilebatchcheck.go

raw symbols SCIP
1 package lfs
2
3 import (
4 "bufio"
5 "io"
6 "strconv"
7 "strings"
8
9 "github.com/git-lfs/git-lfs/v3/errors"
10 "github.com/git-lfs/git-lfs/v3/git"
11 "github.com/git-lfs/git-lfs/v3/tr"
12 )
13
14 // runCatFileBatchCheck uses 'git cat-file --batch-check' to get the type and
15 // size of a git object. Any object that isn't of type blob and under the
16 // BlobSizeCutoff will be ignored, unless it's a locked file. revs is a channel
17 // over which strings containing git sha1s will be sent. It returns a channel
18 // from which sha1 strings can be read.
19 func runCatFileBatchCheck(smallRevCh chan string, lockableCh chan string, lockableSet *lockableNameSet, revs *StringChannelWrapper, errCh chan error) error {
20 cmd, err := git.CatFile()
21 if err != nil {
22 return err
23 }
24
25 go func() {
26 scanner := &catFileBatchCheckScanner{s: bufio.NewScanner(cmd.Stdout), limit: BlobSizeCutoff}
27 for r := range revs.Results {
28 cmd.Stdin.Write([]byte(r + "\n"))
29 hasNext := scanner.Scan()
30 if err := scanner.Err(); err != nil {
31 errCh <- err
32 } else if b := scanner.LFSBlobOID(); len(b) > 0 {
33 smallRevCh <- b
34 } else if b := scanner.GitBlobOID(); len(b) > 0 {
35 if name, ok := lockableSet.Check(b); ok {
36 lockableCh <- name
37 }
38 }
39
40 if !hasNext {
41 break
42 }
43 }
44
45 if err := revs.Wait(); err != nil {
46 errCh <- err
47 }
48 cmd.Stdin.Close()
49
50 stderr, _ := io.ReadAll(cmd.Stderr)
51 err := cmd.Wait()
52 if err != nil {
53 errCh <- errors.New(tr.Tr.Get("error in `git cat-file --batch-check`: %v %v", err, string(stderr)))
54 }
55 close(smallRevCh)
56 close(errCh)
57 close(lockableCh)
58 }()
59
60 return nil
61 }
62
63 type catFileBatchCheckScanner struct {
64 s *bufio.Scanner
65 limit int
66 lfsBlobOID string
67 gitBlobOID string
68 }
69
70 func (s *catFileBatchCheckScanner) LFSBlobOID() string {
71 return s.lfsBlobOID
72 }
73
74 func (s *catFileBatchCheckScanner) GitBlobOID() string {
75 return s.gitBlobOID
76 }
77
78 func (s *catFileBatchCheckScanner) Err() error {
79 return s.s.Err()
80 }
81
82 func (s *catFileBatchCheckScanner) Scan() bool {
83 lfsBlobSha, gitBlobSha, hasNext := s.next()
84 s.lfsBlobOID = lfsBlobSha
85 s.gitBlobOID = gitBlobSha
86 return hasNext
87 }
88
89 func (s *catFileBatchCheckScanner) next() (string, string, bool) {
90 hasNext := s.s.Scan()
91 line := s.s.Text()
92 lineLen := len(line)
93
94 oidLen := strings.IndexByte(line, ' ')
95
96 // Format is:
97 // <hash> <type> <size>
98 // type is at a fixed spot, if we see that it's "blob", we can avoid
99 // splitting the line just to get the size.
100 if oidLen == -1 || lineLen < oidLen+6 {
101 return "", "", hasNext
102 }
103
104 if line[oidLen+1:oidLen+5] != "blob" {
105 return "", "", hasNext
106 }
107
108 size, err := strconv.Atoi(line[oidLen+6 : lineLen])
109 if err != nil {
110 return "", "", hasNext
111 }
112
113 blobSha := line[0:oidLen]
114 if size >= s.limit {
115 return "", blobSha, hasNext
116 }
117
118 return blobSha, "", hasNext
119 }