A source-moved take-in waiting over a minute on the seat git writes one journal line and its waiting-on (part, verb, since) in the state takeins under running.<source revision>, removed at the take-in's end; past five minutes it raises take-in-waiting (scope merge), cleared when the wait ends. indexer_pending answers the running take-ins beside the held ones. The wait is measured by a context's bound, no timer (ADR 0437 §8). Builds: design 47 H2 §803, H-A-409
201 lines
7.3 KiB
Go
201 lines
7.3 KiB
Go
package index
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"path"
|
|
"path/filepath"
|
|
"slices"
|
|
"strings"
|
|
|
|
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/trunk"
|
|
"git.novox.be/novox/mesh-sdk/go/catalogue"
|
|
|
|
"mesh-delivery-indexer/internal/fingerprint"
|
|
)
|
|
|
|
// goModTree reads the go.mod files of one merge's tree, each directory once (Indexer.GoMod), for the walk of the module
|
|
// graph a Go build resolves from the tree (design 47 H-A-202, H-A-157).
|
|
type goModTree struct {
|
|
ix *Indexer
|
|
ctx context.Context
|
|
f whole
|
|
read map[string][]byte // by directory from the repository's root; nil for none
|
|
}
|
|
|
|
func (ix *Indexer) goModTree(ctx context.Context, f whole) *goModTree {
|
|
return &goModTree{ix: ix, ctx: ctx, f: f, read: map[string][]byte{}}
|
|
}
|
|
|
|
// goMod is the go.mod of the directory dir (from the root, "" for the root) at the merge commit; nil when there is none
|
|
// or it cannot be read, said.
|
|
func (t *goModTree) goMod(dir string) []byte {
|
|
if raw, ok := t.read[dir]; ok {
|
|
return raw
|
|
}
|
|
var raw []byte
|
|
if t.ix.GoMod != nil {
|
|
var err error
|
|
if raw, err = waited(t.ctx, "git", "file", func() ([]byte, error) { return t.ix.GoMod(t.ctx, t.f.Repository, t.f.Merge, dir) }); err != nil {
|
|
t.ix.logf("%s at %s: the go.mod of %q cannot be read, so no module of the tree is found there: %v", t.f.Repository, t.f.Merge, dir, err)
|
|
raw = nil
|
|
}
|
|
}
|
|
t.read[dir] = raw
|
|
return raw
|
|
}
|
|
|
|
// sameRepositoryDir is the directory of the merge's tree that holds the Go module required: the required path starts with
|
|
// the repository's module prefix, and the directory it names holds a go.mod declaring that path (H-A-198, H-A-202). false
|
|
// for a module of another repository, or one the tree holds no go.mod of.
|
|
func (t *goModTree) sameRepositoryDir(required string) (string, bool) {
|
|
repo, dir, ok := trunk.PackageRepository(required)
|
|
if !ok || dir == "" || !trunk.SameRepository(repo, t.f.Repository) {
|
|
return "", false
|
|
}
|
|
if goModModule(t.goMod(dir)) != required {
|
|
return "", false
|
|
}
|
|
return dir, true
|
|
}
|
|
|
|
// sameRepositoryRequired are the directories (from the root, sorted, each once) of every same-repository Go module the
|
|
// go.mod of each start directory requires, transitively through those modules' own go.mod files (H-A-202, H-A-157). A
|
|
// same-repository module a go.mod names in a replace directive counts as required, whether or not a require line names it
|
|
// (H-A-224). The start directories themselves are not among them.
|
|
func (t *goModTree) sameRepositoryRequired(start []string) []string {
|
|
seen := map[string]bool{}
|
|
for _, s := range start {
|
|
seen[s] = true
|
|
}
|
|
queue := slices.Clone(start)
|
|
var out []string
|
|
for len(queue) > 0 {
|
|
dir := queue[0]
|
|
queue = queue[1:]
|
|
raw := t.goMod(dir)
|
|
for _, req := range append(fingerprint.MeshRequiresOf(raw), fingerprint.ReplacedSiblings(raw, t.f.Repository)...) {
|
|
d, ok := t.sameRepositoryDir(req)
|
|
if !ok || seen[d] {
|
|
continue
|
|
}
|
|
seen[d] = true
|
|
out = append(out, d)
|
|
queue = append(queue, d)
|
|
}
|
|
}
|
|
slices.Sort(out)
|
|
return out
|
|
}
|
|
|
|
// goModuleDirs are the directories (from the root) a module's Go build starts from: its own directory and the directory
|
|
// of each path of each Go package artifact of its manifest m.
|
|
func goModuleDirs(m catalogue.Manifest, dir string) []string {
|
|
out := []string{dir}
|
|
if m.Build == nil {
|
|
return out
|
|
}
|
|
for _, a := range m.Build.Artifacts {
|
|
if a.Kind != catalogue.ArtifactPackage || a.Language != "go" {
|
|
continue
|
|
}
|
|
for _, p := range a.Paths {
|
|
if d := path.Join(dir, strings.TrimSuffix(filepath.ToSlash(p), "/")); !slices.Contains(out, d) {
|
|
out = append(out, d)
|
|
}
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
// otherRequired are the mesh packages of other repositories (sorted, each once) that the go.mod of each start directory,
|
|
// and of every same-repository module those require transitively (sameRepositoryRequired), requires at any version, the
|
|
// zero version included: every one the module's build resolves, which the entry pins (design 47 H-A-201, H-A-183,
|
|
// H-A-157). A same-repository module is none: it is built from the tree (H-A-198, H-A-202). nil for none.
|
|
func (t *goModTree) otherRequired(start []string) []string {
|
|
var out []string
|
|
for _, d := range append(slices.Clone(start), t.sameRepositoryRequired(start)...) {
|
|
out = append(out, fingerprint.OtherPackages(fingerprint.MeshRequiresOf(t.goMod(d)), t.f.Repository)...)
|
|
}
|
|
if len(out) == 0 {
|
|
return nil
|
|
}
|
|
return slices.Compact(slices.Sorted(slices.Values(out)))
|
|
}
|
|
|
|
// unionSorted is a and b together, sorted and each once; nil for none.
|
|
func unionSorted(a, b []string) []string {
|
|
if len(a)+len(b) == 0 {
|
|
return nil
|
|
}
|
|
return slices.Compact(slices.Sorted(slices.Values(append(slices.Clone(a), b...))))
|
|
}
|
|
|
|
// derivedDirs are the same-repository module directories (from the root, sorted) the module's Go build resolves from the
|
|
// tree beyond its own tree input (H-A-202): every one its go.mod files require, transitively, that its tree input does
|
|
// not hold already, which is the module's directory for the directory's tree, and its named paths for the named tree.
|
|
func (t *goModTree) derivedDirs(m catalogue.Manifest, dir string) []string {
|
|
var held []string
|
|
if paths, every := m.NamedPaths(); every {
|
|
for _, p := range paths {
|
|
held = append(held, path.Join(dir, filepath.ToSlash(p)))
|
|
}
|
|
} else {
|
|
held = []string{dir}
|
|
}
|
|
var out []string
|
|
for _, d := range t.sameRepositoryRequired(goModuleDirs(m, dir)) {
|
|
if !slices.ContainsFunc(held, func(h string) bool { return h == "" || h == "." || d == h || strings.HasPrefix(d, h+"/") }) {
|
|
out = append(out, d)
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
// DerivedLine opens each line of the tree input naming a derived directory and its git object (H-A-202, H-A-172).
|
|
const DerivedLine = "derived "
|
|
|
|
// derivedTree is the module's tree input with a line per derived directory and its object appended, sorted by directory
|
|
// (H-A-202, H-A-172: what a build reads moves its fingerprint), so a change of such a directory moves the fingerprint of
|
|
// every form. The objects are asked of the forge at the merge commit by their paths from the root (Named); an answer that
|
|
// lacks one gives the reason the module has no inputs, naming them. A module with no derived directory keeps its tree.
|
|
func (ix *Indexer) derivedTree(ctx context.Context, f whole, t *goModTree, m catalogue.Manifest, dir, tree string) (string, string) {
|
|
dirs := t.derivedDirs(m, dir)
|
|
if len(dirs) == 0 {
|
|
return tree, ""
|
|
}
|
|
if ix.Named == nil {
|
|
return "", "the forge cannot be asked the git objects of the derived directories " + strings.Join(dirs, ", ")
|
|
}
|
|
got, err := waited(ctx, "git", "trees", func() (map[string]map[string]string, error) {
|
|
return ix.Named(ctx, f.Repository, f.Merge, map[string][]string{"": dirs})
|
|
})
|
|
if err != nil {
|
|
return "", fmt.Sprintf("the git objects of the derived directories %s: %v", strings.Join(dirs, ", "), err)
|
|
}
|
|
lines := []string{tree}
|
|
var missing []string
|
|
for _, d := range dirs {
|
|
o := got[""][d]
|
|
if o == "" {
|
|
missing = append(missing, d)
|
|
continue
|
|
}
|
|
lines = append(lines, DerivedLine+d+" "+o)
|
|
}
|
|
if len(missing) > 0 {
|
|
return "", "the forge names no git object of the derived directories " + strings.Join(missing, ", ")
|
|
}
|
|
return strings.Join(lines, "\n"), ""
|
|
}
|
|
|
|
// goModModule is the module path a go.mod declares; "" for none.
|
|
func goModModule(raw []byte) string {
|
|
for _, line := range strings.Split(string(raw), "\n") {
|
|
if f := strings.Fields(line); len(f) == 2 && f[0] == "module" {
|
|
return strings.Trim(f[1], `"`)
|
|
}
|
|
}
|
|
return ""
|
|
}
|