/*
goredo -- djb's redo implementation on pure Go
-Copyright (C) 2020-2021 Sergey Matveev <stargrave@stargrave.org>
+Copyright (C) 2020-2023 Sergey Matveev <stargrave@stargrave.org>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
import (
"os"
"path"
- "path/filepath"
- "strings"
)
-func sourcesWalker() ([]string, error) {
- tgts, err := targetsWalker(Cwd)
- if err != nil {
- return nil, err
- }
- srcs := 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 {
- cwd, f := path.Split(tgt)
- fdDep, err := os.Open(path.Join(cwd, RedoDir, f+DepSuffix))
+ tgtAbsPath := mustAbs(path.Join(Cwd, tgt))
+ cwd, f := path.Split(path.Join(Cwd, tgt))
+ depPath := path.Join(cwd, RedoDir, f+DepSuffix)
+ if _, ok := seenDeps[depPath]; ok {
+ continue
+ }
+ seenDeps[depPath] = struct{}{}
+ fdDep, err := os.Open(depPath)
if err != nil {
- return nil, err
+ if errors.Is(err, fs.ErrNotExist) {
+ continue
+ }
+ return ErrLine(err)
}
depInfo, err := depRead(fdDep)
fdDep.Close()
+ if err != nil {
+ return ErrLine(err)
+ }
for _, m := range depInfo.ifchanges {
- tgt = m["Target"]
- if !strings.HasSuffix(tgt, ".do") && isSrc(cwd, tgt) {
- pth, err := filepath.Abs(path.Join(cwd, tgt))
- if err != nil {
- panic(err)
+ depTgt := m["Target"]
+ depTgtAbsPath := mustAbs(path.Join(cwd, depTgt))
+ if _, ok := seen[depTgtAbsPath]; ok {
+ continue
+ }
+ seen[depTgtAbsPath] = struct{}{}
+ if isSrc(cwd, depTgt) {
+ srcs[cwdMustRel(depTgtAbsPath)] = struct{}{}
+ } else if depTgtAbsPath != tgtAbsPath {
+ if err := sourcesWalker(
+ []string{cwdMustRel(depTgtAbsPath)},
+ seen, seenDeps, srcs,
+ ); err != nil {
+ return err
}
- srcs[cwdMustRel(pth)] = struct{}{}
}
}
}
- tgts = make([]string, 0, len(srcs))
- for tgt := range srcs {
- tgts = append(tgts, tgt)
- }
- return tgts, nil
+ return nil
}