-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathgit.go
164 lines (133 loc) · 3.26 KB
/
git.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package walk
import (
"bufio"
"context"
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
"github.com/charmbracelet/log"
"github.com/numtide/treefmt/v2/stats"
"golang.org/x/sync/errgroup"
)
type GitReader struct {
root string
path string
includeSubmodules bool
log *log.Logger
stats *stats.Stats
eg *errgroup.Group
scanner *bufio.Scanner
}
func (g *GitReader) Read(ctx context.Context, files []*File) (n int, err error) {
// ensure we record how many files we traversed
defer func() {
g.stats.Add(stats.Traversed, n)
}()
if g.scanner == nil {
// create a pipe to capture the command output
r, w := io.Pipe()
// create a command which will execute from the specified sub path within root
var cmd *exec.Cmd
if g.includeSubmodules {
cmd = exec.Command("git", "ls-files")
} else {
cmd = exec.Command("git", "ls-files", "--recurse-submodules")
}
cmd.Dir = filepath.Join(g.root, g.path)
cmd.Stdout = w
// execute the command in the background
g.eg.Go(func() error {
return w.CloseWithError(cmd.Run())
})
// create a new scanner for reading the output
g.scanner = bufio.NewScanner(r)
}
nextLine := func() (string, error) {
line := g.scanner.Text()
if len(line) == 0 || line[0] != '"' {
return line, nil
}
unquoted, err := strconv.Unquote(line)
if err != nil {
return "", fmt.Errorf("failed to unquote line %s: %w", line, err)
}
return unquoted, nil
}
LOOP:
for n < len(files) {
select {
// exit early if the context was cancelled
case <-ctx.Done():
err = ctx.Err()
if err == nil {
return n, fmt.Errorf("context cancelled: %w", ctx.Err())
}
return n, nil
default:
// read the next file
if g.scanner.Scan() {
entry, err := nextLine()
if err != nil {
return n, err
}
path := filepath.Join(g.root, g.path, entry)
g.log.Debugf("processing file: %s", path)
info, err := os.Stat(path)
if os.IsNotExist(err) {
// the underlying file might have been removed
g.log.Warnf(
"Path %s is in the worktree but appears to have been removed from the filesystem", path,
)
continue
} else if err != nil {
return n, fmt.Errorf("failed to stat %s: %w", path, err)
}
files[n] = &File{
Path: path,
RelPath: filepath.Join(g.path, entry),
Info: info,
}
n++
} else {
// nothing more to read
err = io.EOF
break LOOP
}
}
}
return n, err
}
func (g *GitReader) Close() error {
err := g.eg.Wait()
if err != nil {
return fmt.Errorf("failed to wait for git command to complete: %w", err)
}
return nil
}
func NewGitReader(
root string,
path string,
includeSubmodules bool,
statz *stats.Stats,
) (*GitReader, error) {
// check if the root is a git repository
cmd := exec.Command("git", "rev-parse", "--is-inside-work-tree")
cmd.Dir = root
if out, err := cmd.Output(); err != nil {
return nil, fmt.Errorf("failed to check if %s is a git repository: %w", root, err)
} else if strings.Trim(string(out), "\n") != "true" {
return nil, fmt.Errorf("%s is not a git repository", root)
}
return &GitReader{
root: root,
path: path,
includeSubmodules: includeSubmodules,
stats: statz,
eg: &errgroup.Group{},
log: log.WithPrefix("walk | git"),
}, nil
}