mirror of
https://github.com/numtide/treefmt.git
synced 2024-08-17 04:40:27 +03:00
feat: use go-git index instead of git ls-files
(#23)
Figured out how to use `go-git` properly. ```console # git ❯ nix run .# -- --config-file ./test/echo.toml --tree-root /home/brian/Development/com/github/nixos/nixpkgs -c 38539 files changed in 272.843495ms # filesystem ❯ nix run .# -- --config-file ./test/echo.toml --tree-root /home/brian/Development/com/github/nixos/nixpkgs -c --walk filesystem 38567 files changed in 348.84277ms ``` Signed-off-by: Brian McGee <brian@bmcgee.ie> Reviewed-on: https://git.numtide.com/numtide/treefmt/pulls/23 Reviewed-by: Jonas Chevalier <zimbatm@noreply.git.numtide.com> Co-authored-by: Brian McGee <brian@bmcgee.ie> Co-committed-by: Brian McGee <brian@bmcgee.ie>
This commit is contained in:
parent
5711caebb9
commit
80e99b6d75
@ -5,18 +5,18 @@ import (
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
type filesystem struct {
|
||||
type filesystemWalker struct {
|
||||
root string
|
||||
}
|
||||
|
||||
func (f filesystem) Root() string {
|
||||
func (f filesystemWalker) Root() string {
|
||||
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)
|
||||
}
|
||||
|
||||
func NewFilesystem(root string) (Walker, error) {
|
||||
return filesystem{root}, nil
|
||||
return filesystemWalker{root}, nil
|
||||
}
|
||||
|
@ -1,69 +1,53 @@
|
||||
package walk
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"github.com/go-git/go-git/v5"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
|
||||
"golang.org/x/sync/errgroup"
|
||||
)
|
||||
|
||||
type git struct {
|
||||
type gitWalker struct {
|
||||
root string
|
||||
repo *git.Repository
|
||||
}
|
||||
|
||||
func (g *git) Root() string {
|
||||
func (g *gitWalker) Root() string {
|
||||
return g.root
|
||||
}
|
||||
|
||||
func (g *git) Walk(ctx context.Context, fn filepath.WalkFunc) error {
|
||||
r, w := io.Pipe()
|
||||
func (g *gitWalker) Walk(ctx context.Context, fn filepath.WalkFunc) error {
|
||||
|
||||
cmd := exec.Command("git", "-C", g.root, "ls-files")
|
||||
cmd.Stdout = w
|
||||
cmd.Stderr = w
|
||||
idx, err := g.repo.Storer.Index()
|
||||
if err != nil {
|
||||
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)
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return ctx.Err()
|
||||
default:
|
||||
path := filepath.Join(g.root, entry.Name)
|
||||
|
||||
for scanner.Scan() {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return ctx.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
|
||||
}
|
||||
// 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) {
|
||||
// check if we're dealing with a git repository
|
||||
cmd := exec.Command("git", "-C", root, "rev-parse", "--is-inside-work-tree")
|
||||
_, err := cmd.CombinedOutput()
|
||||
repo, err := git.PlainOpen(root)
|
||||
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}'"
|
||||
];
|
||||
|
||||
# needed for git ls-files
|
||||
buildInputs = [pkgs.git];
|
||||
|
||||
nativeBuildInputs =
|
||||
# needed for git ls-files
|
||||
[pkgs.git]
|
||||
# we need some formatters available for the tests
|
||||
++ (import ./formatters.nix pkgs);
|
||||
(import ./formatters.nix pkgs);
|
||||
|
||||
preCheck = ''
|
||||
XDG_CACHE_HOME=$(mktemp -d)
|
||||
|
Loading…
Reference in New Issue
Block a user