package main
import (
- "log"
- "os"
+ "errors"
+ "io/fs"
"path"
)
-func sourcesWalker(tgts []string) ([]string, error) {
- seen := make(map[string]struct{}, 1<<10)
+func sourcesWalker(
+ tgts []string,
+ seen map[string]struct{},
+ seenDeps map[string]struct{},
+ srcs map[string]struct{},
+) error {
for _, tgt := range tgts {
tgtAbsPath := mustAbs(path.Join(Cwd, tgt))
cwd, f := path.Split(path.Join(Cwd, tgt))
- fdDep, err := os.Open(path.Join(cwd, RedoDir, f+DepSuffix))
+ depPath := path.Join(cwd, RedoDir, f+DepSuffix)
+ if _, ok := seenDeps[depPath]; ok {
+ continue
+ }
+ seenDeps[depPath] = struct{}{}
+ depInfo, err := depRead(depPath)
if err != nil {
- if os.IsNotExist(err) {
+ if errors.Is(err, fs.ErrNotExist) {
continue
}
- return nil, ErrLine(err)
- }
- depInfo, err := depRead(fdDep)
- fdDep.Close()
- if err != nil {
- return nil, ErrLine(err)
+ return ErrLine(err)
}
- for _, m := range depInfo.ifchanges {
- depTgt := m["Target"]
- depTgtAbsPath := mustAbs(path.Join(cwd, depTgt))
- if isSrc(cwd, depTgt) {
- seen[cwdMustRel(depTgtAbsPath)] = struct{}{}
+ for _, dep := range depInfo.ifchanges {
+ depTgtAbsPath := mustAbs(path.Join(cwd, dep.tgt))
+ if _, ok := seen[depTgtAbsPath]; ok {
+ continue
+ }
+ seen[depTgtAbsPath] = struct{}{}
+ if isSrc(cwd, dep.tgt) {
+ srcs[cwdMustRel(depTgtAbsPath)] = struct{}{}
} else if depTgtAbsPath != tgtAbsPath {
- subSrcs, err := sourcesWalker([]string{cwdMustRel(depTgtAbsPath)})
- if err != nil {
- log.Fatal(err)
- }
- for _, p := range subSrcs {
- seen[p] = struct{}{}
+ if err := sourcesWalker(
+ []string{cwdMustRel(depTgtAbsPath)},
+ seen, seenDeps, srcs,
+ ); err != nil {
+ return err
}
}
}
}
- srcs := make([]string, 0, len(seen))
- for p := range seen {
- srcs = append(srcs, p)
- }
- return srcs, nil
+ return nil
}