a version with known bug fixed but not sure any more obscure ones

This commit is contained in:
2026-05-16 03:55:00 +08:00
parent 615c324d9b
commit 3012507fcc
4 changed files with 160 additions and 58 deletions
+83
View File
@@ -0,0 +1,83 @@
package resolver
import (
"cliprepo/internal/tree"
"os"
"path/filepath"
)
type Resolver struct {
root string
seen map[string]struct{}
}
func New(root string) *Resolver {
return &Resolver{
root: root,
seen: make(map[string]struct{}),
}
}
// WalkAll 遍历所有文件,并自动跳过 .git 和 .gitignore 中的文件
func (r *Resolver) WalkAll() ([]string, error) {
var result []string
ig, _ := tree.Load(r.root)
err := filepath.Walk(r.root, func(path string, info os.FileInfo, err error) error {
if err != nil {
return nil
}
// 显式跳过 .git 目录
if info.IsDir() && info.Name() == ".git" {
return filepath.SkipDir
}
rel, _ := filepath.Rel(r.root, path)
// 遵循 .gitignore 规则
if ig != nil && ig.MatchesPath(rel) {
if info.IsDir() {
return filepath.SkipDir
}
return nil
}
if info.IsDir() {
return nil
}
r.add(rel, &result)
return nil
})
return result, err
}
func (r *Resolver) AddFile(path string, result *[]string) {
r.add(path, result)
}
func (r *Resolver) add(path string, result *[]string) {
if _, ok := r.seen[path]; ok {
return
}
r.seen[path] = struct{}{}
*result = append(*result, path)
}
// Unique 保留此方法供 run.go 调用
func (r *Resolver) Unique(in []string) []string {
seen := make(map[string]struct{})
out := make([]string, 0, len(in))
for _, f := range in {
if _, ok := seen[f]; ok {
continue
}
seen[f] = struct{}{}
out = append(out, f)
}
return out
}
+6 -38
View File
@@ -3,52 +3,20 @@ package utils
import (
"os"
"path/filepath"
"strings"
// "strings"
)
func ExpandGlob(patterns []string) ([]string, error) {
var result []string
seen := make(map[string]struct{})
for _, pattern := range patterns {
if pattern == "*" {
filepath.Walk(".", func(path string, info os.FileInfo, err error) error {
if err != nil {
return nil
}
if info.IsDir() {
return nil
}
add(path, seen, &result)
return nil
})
continue
}
isRecursive := strings.Contains(pattern, "**")
if isRecursive {
root := strings.Split(pattern, "**")[0]
if root == "" {
root = "."
files, err := filepath.Glob("**") // fallback
if err != nil {
return nil, err
}
filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
if err != nil || info.IsDir() {
return nil
}
matched, _ := filepath.Match(
strings.Replace(pattern, "**", "*", 1),
path,
)
if matched {
add(path, seen, &result)
}
return nil
})
result = append(result, files...)
continue
}
@@ -62,7 +30,7 @@ func ExpandGlob(patterns []string) ([]string, error) {
if err != nil || info.IsDir() {
continue
}
add(m, seen, &result)
result = append(result, m)
}
}