-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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 <[email protected]> Reviewed-on: https://git.numtide.com/numtide/treefmt/pulls/23 Reviewed-by: Jonas Chevalier <[email protected]> Co-authored-by: Brian McGee <[email protected]> Co-committed-by: Brian McGee <[email protected]>
- Loading branch information
1 parent
5711cae
commit 80e99b6
Showing
3 changed files
with
28 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() | ||
|
||
cmd := exec.Command("git", "-C", g.root, "ls-files") | ||
cmd.Stdout = w | ||
cmd.Stderr = w | ||
func (g *gitWalker) Walk(ctx context.Context, fn filepath.WalkFunc) error { | ||
|
||
eg := errgroup.Group{} | ||
idx, err := g.repo.Storer.Index() | ||
if err != nil { | ||
return fmt.Errorf("%w: failed to open index", err) | ||
} | ||
|
||
eg.Go(func() error { | ||
scanner := bufio.NewScanner(r) | ||
for _, entry := range idx.Entries { | ||
|
||
for scanner.Scan() { | ||
select { | ||
case <-ctx.Done(): | ||
return ctx.Err() | ||
default: | ||
line := scanner.Text() | ||
path := filepath.Join(g.root, line) | ||
select { | ||
case <-ctx.Done(): | ||
return ctx.Err() | ||
default: | ||
path := filepath.Join(g.root, entry.Name) | ||
|
||
// 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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters