2024-01-11 23:52:22 +03:00
|
|
|
package walk
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2024-01-10 18:45:57 +03:00
|
|
|
"io/fs"
|
2024-05-07 20:02:50 +03:00
|
|
|
"os"
|
2024-01-11 23:52:22 +03:00
|
|
|
"path/filepath"
|
2024-01-12 18:15:51 +03:00
|
|
|
|
2024-01-10 18:45:57 +03:00
|
|
|
"github.com/charmbracelet/log"
|
|
|
|
|
2024-01-12 18:15:51 +03:00
|
|
|
"github.com/go-git/go-git/v5"
|
2024-01-11 23:52:22 +03:00
|
|
|
)
|
|
|
|
|
2024-01-12 14:33:14 +03:00
|
|
|
type gitWalker struct {
|
2024-01-10 18:45:57 +03:00
|
|
|
root string
|
2024-05-03 22:55:29 +03:00
|
|
|
paths chan string
|
2024-01-10 18:45:57 +03:00
|
|
|
repo *git.Repository
|
2024-01-11 23:52:22 +03:00
|
|
|
}
|
|
|
|
|
2024-01-12 14:33:14 +03:00
|
|
|
func (g *gitWalker) Root() string {
|
2024-01-11 23:52:22 +03:00
|
|
|
return g.root
|
|
|
|
}
|
|
|
|
|
2024-05-01 21:03:26 +03:00
|
|
|
func (g *gitWalker) Walk(ctx context.Context, fn WalkFunc) error {
|
|
|
|
// for quick relative paths
|
|
|
|
relPathOffset := len(g.root) + 1
|
|
|
|
|
|
|
|
relPathFn := func(path string) (relPath string) {
|
|
|
|
if len(path) >= relPathOffset {
|
|
|
|
relPath = path[relPathOffset:]
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-01-12 14:33:14 +03:00
|
|
|
idx, err := g.repo.Storer.Index()
|
|
|
|
if err != nil {
|
2024-05-02 13:40:49 +03:00
|
|
|
return fmt.Errorf("failed to open git index: %w", err)
|
2024-01-12 14:33:14 +03:00
|
|
|
}
|
2024-01-11 23:52:22 +03:00
|
|
|
|
2024-05-07 20:02:50 +03:00
|
|
|
// cache in-memory whether a path is present in the git index
|
|
|
|
var cache map[string]bool
|
|
|
|
|
2024-05-03 22:55:29 +03:00
|
|
|
for path := range g.paths {
|
|
|
|
|
2024-05-07 20:02:50 +03:00
|
|
|
if path == g.root {
|
|
|
|
// we can just iterate the index entries
|
|
|
|
for _, entry := range idx.Entries {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return ctx.Err()
|
|
|
|
default:
|
|
|
|
path := filepath.Join(g.root, entry.Name)
|
|
|
|
|
|
|
|
// stat the file
|
|
|
|
info, err := os.Lstat(path)
|
|
|
|
|
|
|
|
file := File{
|
|
|
|
Path: path,
|
|
|
|
RelPath: relPathFn(path),
|
|
|
|
Info: info,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = fn(&file, err); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// otherwise we ensure the git index entries are cached and then check if they are in the git index
|
|
|
|
if cache == nil {
|
|
|
|
cache = make(map[string]bool)
|
|
|
|
for _, entry := range idx.Entries {
|
|
|
|
cache[entry.Name] = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
relPath, err := filepath.Rel(g.root, path)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to find relative path for %v: %w", path, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, ok := cache[relPath]
|
|
|
|
if !(path == g.root || ok) {
|
|
|
|
log.Debugf("path %v not found in git index, skipping", path)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
return filepath.Walk(path, func(path string, info fs.FileInfo, err error) error {
|
2024-05-03 22:55:29 +03:00
|
|
|
if info.IsDir() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
relPath, err := filepath.Rel(g.root, path)
|
2024-01-10 18:45:57 +03:00
|
|
|
if err != nil {
|
2024-01-12 14:33:14 +03:00
|
|
|
return err
|
2024-01-11 23:52:22 +03:00
|
|
|
}
|
2024-01-10 18:45:57 +03:00
|
|
|
|
2024-05-07 20:02:50 +03:00
|
|
|
if _, ok := cache[relPath]; !ok {
|
|
|
|
log.Debugf("path %v not found in git index, skipping", path)
|
2024-05-03 22:55:29 +03:00
|
|
|
return nil
|
2024-01-10 18:45:57 +03:00
|
|
|
}
|
2024-05-03 22:55:29 +03:00
|
|
|
|
|
|
|
file := File{
|
|
|
|
Path: path,
|
|
|
|
RelPath: relPathFn(path),
|
|
|
|
Info: info,
|
|
|
|
}
|
|
|
|
|
|
|
|
return fn(&file, err)
|
|
|
|
})
|
2024-01-11 23:52:22 +03:00
|
|
|
}
|
|
|
|
|
2024-01-12 14:33:14 +03:00
|
|
|
return nil
|
2024-01-11 23:52:22 +03:00
|
|
|
}
|
|
|
|
|
2024-05-03 22:55:29 +03:00
|
|
|
func NewGit(root string, paths chan string) (Walker, error) {
|
2024-01-12 14:33:14 +03:00
|
|
|
repo, err := git.PlainOpen(root)
|
2024-01-11 23:52:22 +03:00
|
|
|
if err != nil {
|
2024-05-02 13:40:49 +03:00
|
|
|
return nil, fmt.Errorf("failed to open git repo: %w", err)
|
2024-01-11 23:52:22 +03:00
|
|
|
}
|
2024-01-10 18:45:57 +03:00
|
|
|
return &gitWalker{root, paths, repo}, nil
|
2024-01-11 23:52:22 +03:00
|
|
|
}
|