support explicit paths and processing paths from stdin #21

Closed
brianmcgee wants to merge 5 commits from feat/explicit-paths into main
Showing only changes of commit a0ec1c5e9e - Show all commits

View File

@ -1,83 +0,0 @@
package cache
import (
"bufio"
"context"
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"github.com/charmbracelet/log"
"golang.org/x/sync/errgroup"
)
const (
GitWalker = "git"
FilesystemWalker = "filesystem"
)
func walk(ctx context.Context, root string, walker string, fn filepath.WalkFunc) error {
l := log.WithPrefix("walk")
if walker == GitWalker {
// check if we're dealing with a git repository
cmd := exec.Command("git", "-C", root, "rev-parse", "--is-inside-work-tree")
_, err := cmd.CombinedOutput()
if err != nil {
l.Info("git repo check failed, falling back to filesystem", "err", err)
walker = "filesystem"
}
}
l.Infof("walking %s with %s", root, walker)
switch walker {
case GitWalker:
return walkGit(ctx, root, fn)
case FilesystemWalker:
return filepath.Walk(root, fn)
default:
return fmt.Errorf("unknown walker: %s", walker)
}
}
func walkGit(ctx context.Context, root string, fn filepath.WalkFunc) error {
r, w := io.Pipe()
cmd := exec.Command("git", "-C", root, "ls-files")
cmd.Stdout = w
cmd.Stderr = w
eg := errgroup.Group{}
eg.Go(func() error {
scanner := bufio.NewScanner(r)
for scanner.Scan() {
select {
case <-ctx.Done():
return ctx.Err()
default:
line := scanner.Text()
path := filepath.Join(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()
}