Skip to content

Commit 73e6f78

Browse files
committed
wip: add git stage reader
Signed-off-by: Brian McGee <[email protected]>
1 parent 2789b36 commit 73e6f78

File tree

5 files changed

+72
-32
lines changed

5 files changed

+72
-32
lines changed

nix/packages/treefmt/default.nix

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ in
4646
"-X github.com/numtide/treefmt/build.Version=v${version}"
4747
];
4848

49-
nativeBuildInputs =
49+
nativeBuildInputs = [ pkgs.git ] ++
5050
# we need some formatters available for the tests
5151
import ./formatters.nix pkgs;
5252

walk/git.go

+23-6
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@ import (
44
"bufio"
55
"context"
66
"fmt"
7-
"github.com/charmbracelet/log"
8-
"github.com/numtide/treefmt/stats"
9-
"golang.org/x/sync/errgroup"
107
"io"
118
"os"
129
"os/exec"
1310
"path/filepath"
1411
"strings"
12+
13+
"github.com/charmbracelet/log"
14+
"github.com/numtide/treefmt/stats"
15+
"golang.org/x/sync/errgroup"
1516
)
1617

1718
type GitReader struct {
@@ -99,12 +100,12 @@ func (g *GitReader) Close() error {
99100
return g.eg.Wait()
100101
}
101102

102-
func NewGitWorktreeReader(
103+
func newGitReader(
103104
root string,
104105
path string,
106+
args []string,
105107
statz *stats.Stats,
106108
) (*GitReader, error) {
107-
108109
// check if the root is a git repository
109110
cmd := exec.Command("git", "rev-parse", "--is-inside-work-tree")
110111
cmd.Dir = root
@@ -118,9 +119,25 @@ func NewGitWorktreeReader(
118119
return &GitReader{
119120
root: root,
120121
path: path,
121-
args: []string{"ls-files"},
122+
args: args,
122123
stats: statz,
123124
eg: &errgroup.Group{},
124125
log: log.WithPrefix("walk[git]"),
125126
}, nil
126127
}
128+
129+
func NewGitStageReader(
130+
root string,
131+
path string,
132+
statz *stats.Stats,
133+
) (*GitReader, error) {
134+
return newGitReader(root, path, []string{"diff", "--name-only", "--cached"}, statz)
135+
}
136+
137+
func NewGitWorktreeReader(
138+
root string,
139+
path string,
140+
statz *stats.Stats,
141+
) (*GitReader, error) {
142+
return newGitReader(root, path, []string{"ls-files"}, statz)
143+
}

walk/git_test.go

+14-4
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,24 @@ func TestGitWorktreeReader(t *testing.T) {
3333
)
3434
as.NoError(err, "failed to init git repository")
3535

36-
// get worktree and add everything to it
36+
// read empty worktree
37+
statz := stats.New()
38+
reader, err := walk.NewGitWorktreeReader(tempDir, "", &statz)
39+
as.NoError(err)
40+
41+
files := make([]*walk.File, 8)
42+
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
43+
n, err := reader.Read(ctx, files)
44+
cancel()
45+
as.Equal(0, n)
46+
as.ErrorIs(err, io.EOF)
47+
48+
// add everything to the worktree
3749
wt, err := repo.Worktree()
3850
as.NoError(err, "failed to get git worktree")
3951
as.NoError(wt.AddGlob("."))
4052

41-
statz := stats.New()
42-
43-
reader, err := walk.NewGitWorktreeReader(tempDir, "", &statz)
53+
reader, err = walk.NewGitWorktreeReader(tempDir, "", &statz)
4454
as.NoError(err)
4555

4656
count := 0

walk/type_enum.go

+23-15
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

walk/walk.go

+11-6
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,11 @@ type Type int
2020

2121
const (
2222
Auto Type = iota
23-
Git
24-
Filesystem
2523
Stdin
24+
Filesystem
25+
Git
26+
GitWorktree
27+
GitStage
2628

2729
BatchSize = 1024
2830
)
@@ -159,12 +161,15 @@ func NewReader(
159161
reader, err = NewReader(Filesystem, root, path, db, statz)
160162
}
161163
return reader, err
162-
case Git:
163-
reader, err = NewGitWorktreeReader(root, path, statz)
164-
case Filesystem:
165-
reader = NewFilesystemReader(root, path, statz, BatchSize)
166164
case Stdin:
167165
return nil, fmt.Errorf("stdin walk type is not supported")
166+
case Filesystem:
167+
reader = NewFilesystemReader(root, path, statz, BatchSize)
168+
case Git, GitWorktree:
169+
reader, err = NewGitWorktreeReader(root, path, statz)
170+
case GitStage:
171+
reader, err = NewGitStageReader(root, path, statz)
172+
168173
default:
169174
return nil, fmt.Errorf("unknown walk type: %v", walkType)
170175
}

0 commit comments

Comments
 (0)