feat: use go-git index instead of git ls-files
#23
@ -5,18 +5,18 @@ import (
|
|||||||
"path/filepath"
|
"path/filepath"
|
||||||
)
|
)
|
||||||
|
|
||||||
type filesystem struct {
|
type filesystemWalker struct {
|
||||||
root string
|
root string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f filesystem) Root() string {
|
func (f filesystemWalker) Root() string {
|
||||||
return f.root
|
return f.root
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f filesystem) Walk(_ context.Context, fn filepath.WalkFunc) error {
|
func (f filesystemWalker) Walk(_ context.Context, fn filepath.WalkFunc) error {
|
||||||
return filepath.Walk(f.root, fn)
|
return filepath.Walk(f.root, fn)
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewFilesystem(root string) (Walker, error) {
|
func NewFilesystem(root string) (Walker, error) {
|
||||||
return filesystem{root}, nil
|
return filesystemWalker{root}, nil
|
||||||
}
|
}
|
||||||
|
@ -1,44 +1,36 @@
|
|||||||
package walk
|
package walk
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"github.com/go-git/go-git/v5"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
||||||
"golang.org/x/sync/errgroup"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type git struct {
|
type gitWalker struct {
|
||||||
root string
|
root string
|
||||||
|
repo *git.Repository
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *git) Root() string {
|
func (g *gitWalker) Root() string {
|
||||||
return g.root
|
return g.root
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *git) Walk(ctx context.Context, fn filepath.WalkFunc) error {
|
func (g *gitWalker) Walk(ctx context.Context, fn filepath.WalkFunc) error {
|
||||||
r, w := io.Pipe()
|
|
||||||
|
|
||||||
cmd := exec.Command("git", "-C", g.root, "ls-files")
|
idx, err := g.repo.Storer.Index()
|
||||||
cmd.Stdout = w
|
if err != nil {
|
||||||
cmd.Stderr = w
|
return fmt.Errorf("%w: failed to open index", err)
|
||||||
|
}
|
||||||
|
|
||||||
eg := errgroup.Group{}
|
for _, entry := range idx.Entries {
|
||||||
|
|
||||||
eg.Go(func() error {
|
|
||||||
scanner := bufio.NewScanner(r)
|
|
||||||
|
|
||||||
for scanner.Scan() {
|
|
||||||
select {
|
select {
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
return ctx.Err()
|
return ctx.Err()
|
||||||
default:
|
default:
|
||||||
line := scanner.Text()
|
path := filepath.Join(g.root, entry.Name)
|
||||||
path := filepath.Join(g.root, line)
|
|
||||||
|
|
||||||
// stat the file
|
// stat the file
|
||||||
info, err := os.Lstat(path)
|
info, err := os.Lstat(path)
|
||||||
@ -46,24 +38,16 @@ func (g *git) Walk(ctx context.Context, fn filepath.WalkFunc) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
})
|
|
||||||
|
|
||||||
if err := w.CloseWithError(cmd.Run()); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
return eg.Wait()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewGit(root string) (Walker, error) {
|
func NewGit(root string) (Walker, error) {
|
||||||
// check if we're dealing with a git repository
|
repo, err := git.PlainOpen(root)
|
||||||
cmd := exec.Command("git", "-C", root, "rev-parse", "--is-inside-work-tree")
|
|
||||||
_, err := cmd.CombinedOutput()
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("%w: git repo check failed", err)
|
return nil, fmt.Errorf("%w: failed to open git repo", err)
|
||||||
}
|
}
|
||||||
return &git{root}, nil
|
return &gitWalker{root, repo}, nil
|
||||||
}
|
}
|
||||||
|
@ -26,14 +26,9 @@
|
|||||||
"-X 'build.Version=${version}'"
|
"-X 'build.Version=${version}'"
|
||||||
];
|
];
|
||||||
|
|
||||||
# needed for git ls-files
|
|
||||||
buildInputs = [pkgs.git];
|
|
||||||
|
|
||||||
nativeBuildInputs =
|
nativeBuildInputs =
|
||||||
# needed for git ls-files
|
|
||||||
[pkgs.git]
|
|
||||||
# we need some formatters available for the tests
|
# we need some formatters available for the tests
|
||||||
++ (import ./formatters.nix pkgs);
|
(import ./formatters.nix pkgs);
|
||||||
|
|
||||||
preCheck = ''
|
preCheck = ''
|
||||||
XDG_CACHE_HOME=$(mktemp -d)
|
XDG_CACHE_HOME=$(mktemp -d)
|
||||||
|
Reference in New Issue
Block a user