feat: use go-git index instead of git ls-files
Signed-off-by: Brian McGee <brian@bmcgee.ie>
This commit is contained in:
parent
5711caebb9
commit
a20b8c7645
@ -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,69 +1,53 @@
|
|||||||
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 {
|
select {
|
||||||
scanner := bufio.NewScanner(r)
|
case <-ctx.Done():
|
||||||
|
return ctx.Err()
|
||||||
|
default:
|
||||||
|
path := filepath.Join(g.root, entry.Name)
|
||||||
|
|
||||||
for scanner.Scan() {
|
// stat the file
|
||||||
select {
|
info, err := os.Lstat(path)
|
||||||
case <-ctx.Done():
|
if err = fn(path, info, err); err != nil {
|
||||||
return ctx.Err()
|
return err
|
||||||
default:
|
|
||||||
line := scanner.Text()
|
|
||||||
path := filepath.Join(g.root, line)
|
|
||||||
|
|
||||||
// stat the file
|
|
||||||
info, err := os.Lstat(path)
|
|
||||||
if err = fn(path, info, err); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
|
||||||
})
|
|
||||||
|
|
||||||
if err := w.CloseWithError(cmd.Run()); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return eg.Wait()
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
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