package main
import (
- "log"
"os"
"path"
- "path/filepath"
)
-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, err := filepath.Abs(path.Join(Cwd, tgt))
- if err != nil {
- panic(err)
- }
+ 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{}{}
+ fdDep, err := os.Open(depPath)
if err != nil {
- if os.IsNotExist(err) {
+ if errors.Is(err, fs.ErrNotExist) {
continue
}
- return nil, err
+ return ErrLine(err)
}
depInfo, err := depRead(fdDep)
fdDep.Close()
if err != nil {
- return nil, err
+ return ErrLine(err)
}
for _, m := range depInfo.ifchanges {
depTgt := m["Target"]
- depTgtAbsPath, err := filepath.Abs(path.Join(cwd, depTgt))
- if err != nil {
- panic(err)
+ depTgtAbsPath := mustAbs(path.Join(cwd, depTgt))
+ if _, ok := seen[depTgtAbsPath]; ok {
+ continue
}
+ seen[depTgtAbsPath] = struct{}{}
if isSrc(cwd, depTgt) {
- seen[cwdMustRel(depTgtAbsPath)] = struct{}{}
+ 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
}