Files
mesh-delivery/modules/mesh-delivery-indexer/internal/index/source.go
T
jschoubben 8d2d490637 H-A-48: no source stamp on a module; the take-in's commit names it
srcOf and the Src and FpSrc stamps go with the source per merged message: a module's value and
its catalog entry carry where it was taken in by its commit alone.

Builds: design 47 H §764 (the indexer); H-A-48

Claude-Session: https://claude.ai/code/session_01JG73KHUnBze48rdua8u2sy
2026-11-20 04:56:27 +01:00

121 lines
4.0 KiB
Go

package index
import (
"context"
"encoding/json"
"errors"
"fmt"
"maps"
"path"
"slices"
"git.novox.be/novox/mesh-sdk/go/catalogue"
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/trunk"
)
// The gate's source revision of a watched branch (design 47 H §737, §743), heard on its event source-moved: it names the heads
// of the repositories it moved, and its number is the mesh's source revision of that branch. The indexer keeps the latest it
// took in.
// sourceNow is the latest source revision seen.
type sourceNow struct {
rev uint64
heads map[string]string
}
// sourceHeads is the head the latest source seen names for each repository (a copy), nil while none has been seen.
func (ix *Indexer) sourceHeads() map[string]string {
ix.mu.Lock()
defer ix.mu.Unlock()
return maps.Clone(ix.source.heads)
}
// wholeAt reads repository's trees at the commit head through the seat git (design 47 H §743): every module directory's manifest,
// templates' needs, lock files and packages, and the catalogue's predefined list where the repository is the catalogue's (§769).
func (ix *Indexer) wholeAt(ctx context.Context, repository, head string) (whole, error) {
f := whole{Repository: repository, Merge: head, Head: head}
file := func(p string) ([]byte, error) {
if ix.ReadFile == nil {
return nil, errors.New("the indexer reads no file of the forge")
}
return ix.ReadFile(ctx, repository, head, p)
}
first, err := ix.Read(ctx, repository, head, "")
if err != nil {
return f, fmt.Errorf("the seat git could not read %s at %.12s: %w", repository, head, err)
}
f.Root, f.Trees = first.Root, maps.Clone(first.Trees)
for _, dir := range slices.Sorted(maps.Keys(first.Trees)) {
read := first
if dir != "" {
if read, err = ix.Read(ctx, repository, head, dir); err != nil {
return f, fmt.Errorf("the seat git could not read %q of %s at %.12s: %w", dir, repository, head, err)
}
}
if f.Manifests == nil {
f.Manifests = map[string]string{}
}
f.Manifests[dir] = read.Manifests[dir]
// The reader capabilities of a manifest that names templates, its templates' text too, as the forge bridge reads them
// (ADR 0632 §2): a template the forge does not have is no need, any other failure to read one is the head's.
if paths := manifestTemplatePaths(read.Manifests[dir]); len(paths) > 0 {
texts := map[string]string{}
for _, t := range paths {
content, err := file(path.Join(dir, t))
if err != nil {
return f, fmt.Errorf("the seat git could not read the template %q of %q of %s at %.12s: %w", t, dir, repository, head, err)
}
if content != nil {
texts[t] = string(content)
}
}
if f.Needs == nil {
f.Needs = map[string][]string{}
}
f.Needs[dir], _ = catalogue.ModuleNeeds([]byte(read.Manifests[dir]), texts)
}
if l := read.Locks[dir]; len(l) > 0 {
if f.Locks == nil {
f.Locks = map[string]map[string]string{}
}
f.Locks[dir] = l
}
if p := read.Packages[dir]; len(p) > 0 {
if f.Packages == nil {
f.Packages = map[string][]string{}
}
f.Packages[dir] = p
}
}
// The catalogue's predefined list at the commit, as the forge bridge carries it (ADR 0565 §2).
if trunk.SameRepository(repository, catalogueRepository) {
raw, err := file(predefinedFile)
if err != nil {
return f, fmt.Errorf("the seat git could not read %s of %s at %.12s: %w", predefinedFile, repository, head, err)
}
if raw == nil {
f.PredefinedRead = true
} else {
f.Predefined = json.RawMessage(raw)
}
}
return f, nil
}
// The catalogue repository and its predefined list's file, whose merged message carries the list (the forge bridge's
// CatalogRepository and PredefinedFile).
const (
catalogueRepository = "novox/mesh-catalog"
predefinedFile = "predefined.json"
)
// manifestTemplatePaths is the template paths a manifest names, none for one that cannot be read.
func manifestTemplatePaths(raw string) []string {
m, err := catalogue.Decode([]byte(raw))
if err != nil {
return nil
}
return catalogue.TemplatePaths(m)
}