Debian Codegraph @

git/ls_files.go

raw symbols SCIP
1 package git
2
3 import (
4 "bufio"
5 "io"
6 "path"
7 "strings"
8
9 "github.com/git-lfs/git-lfs/v3/errors"
10 "github.com/git-lfs/git-lfs/v3/tools"
11 "github.com/git-lfs/git-lfs/v3/tr"
12 "github.com/rubyist/tracerx"
13 )
14
15 type lsFileInfo struct {
16 BaseName string
17 FullPath string
18 }
19 type LsFiles struct {
20 Files map[string]*lsFileInfo
21 FilesByName map[string][]*lsFileInfo
22 }
23
24 func NewLsFiles(workingDir string, standardExclude bool, untracked bool) (*LsFiles, error) {
25
26 args := []string{
27 "ls-files",
28 "-z", // Use a NUL separator. This also disables the escaping of special characters.
29 "--cached",
30 }
31
32 if IsGitVersionAtLeast("2.35.0") {
33 args = append(args, "--sparse")
34 }
35
36 if standardExclude {
37 args = append(args, "--exclude-standard")
38 }
39 if untracked {
40 args = append(args, "--others")
41 }
42 cmd, err := gitNoLFS(args...)
43 if err != nil {
44 return nil, err
45 }
46 cmd.Dir = workingDir
47
48 tracerx.Printf("NewLsFiles: running in %s git %s",
49 workingDir, strings.Join(args, " "))
50
51 // Capture stdout and stderr
52 stdout, err := cmd.StdoutPipe()
53 if err != nil {
54 return nil, err
55 }
56 stderr, err := cmd.StderrPipe()
57 if err != nil {
58 return nil, err
59 }
60
61 scanner := bufio.NewScanner(stdout)
62 scanner.Split(tools.SplitOnNul)
63
64 if err := cmd.Start(); err != nil {
65 return nil, err
66 }
67
68 rv := &LsFiles{
69 Files: make(map[string]*lsFileInfo),
70 FilesByName: make(map[string][]*lsFileInfo),
71 }
72
73 // Setup a goroutine to drain stderr as large amounts of error output may cause
74 // the subprocess to block.
75 errorMessages := make(chan []byte)
76 go func() {
77 msg, _ := io.ReadAll(stderr)
78 errorMessages <- msg
79 }()
80
81 // Read all files
82 for scanner.Scan() {
83 base := path.Base(scanner.Text())
84 finfo := &lsFileInfo{
85 BaseName: base,
86 FullPath: scanner.Text(),
87 }
88 rv.Files[scanner.Text()] = finfo
89 rv.FilesByName[base] = append(rv.FilesByName[base], finfo)
90 }
91
92 // Check the output of the subprocess, output stderr if the command failed.
93 msg := <-errorMessages
94 if err := cmd.Wait(); err != nil {
95 return nil, errors.New(tr.Tr.Get("Error in `git %s`: %v %s",
96 strings.Join(args, " "), err, msg))
97 }
98
99 return rv, nil
100 }