Gate: a held source key gets the registered repositories it lacks, heads unchanged (A-403)

Builds: design 47 G A-403, A-401, A-34
This commit is contained in:
2026-11-14 22:09:06 +01:00
parent 8ea8ef9768
commit 28f616e379
2 changed files with 40 additions and 17 deletions
@@ -3,7 +3,7 @@ package main
// A watched branch starts from the forge, as a clone does (design 47 G A-401, §430, §15): at its start and on each
// registration-changed of the seat mesh-catalog, the gate writes source.<branch> for every watched branch the registrations name,
// each registered repository at the head of that branch, asked of the seat git's verb branch, the group left empty, and says
// source-moved. A repository whose head cannot be read is left out and said. A head a key holds is never rewritten here: from
// source-moved. A repository whose head cannot be read is left out and said. A head a key holds is never changed here (A-403): from
// then on only a take-in moves it (A-34).
import (
@@ -81,7 +81,7 @@ func watchedRepositories(regs []meshcatalog.Registration) map[string][]string {
return out
}
// StartSources writes the source key of every watched branch the registrations name that no key holds yet, and answers the
// StartSources adds to the source key of every watched branch the registrations name each registered repository it lacks, and answers the
// branches it wrote. A registrations answer that cannot be read is an error (the event is offered again); a repository whose
// head cannot be read is said and left out.
func (h *Holder) StartSources() ([]string, error) {
@@ -121,11 +121,13 @@ func (h *Holder) startSources(only string) (started, leftOut []string, err error
errs = append(errs, err)
continue
}
if held != nil {
continue
}
heads := map[string]string{}
for _, repository := range watched[branch] {
if held != nil {
if _, has := held.Heads[repository]; has {
continue // a head a key holds moves by a take-in alone (A-34)
}
}
a, err := h.Forge.Branch(repository, branch)
if err == nil && a.Error != "" {
err = errors.New(a.Error)
@@ -143,10 +145,24 @@ func (h *Holder) startSources(only string) (started, leftOut []string, err error
if len(heads) == 0 {
continue
}
wrote := false
wrote, group := false, ""
rev, err := h.Source.Set(branch, func(prev *Source) (Source, bool) {
wrote = prev == nil // a key written meanwhile is never rewritten
return Source{Heads: heads}, wrote
next := Source{Heads: map[string]string{}}
if prev != nil {
next.Group = prev.Group
for r, c := range prev.Heads {
next.Heads[r] = c
}
}
wrote = false
for r, c := range heads {
if _, has := next.Heads[r]; !has { // a held head is never changed
next.Heads[r] = c
wrote = true
}
}
group = next.Group
return next, wrote
})
if err != nil {
errs = append(errs, fmt.Errorf("source.%s cannot be started: %w", branch, err))
@@ -161,7 +177,7 @@ func (h *Holder) startSources(only string) (started, leftOut []string, err error
}
at := h.Now().UTC()
if err := h.SaySeat(seat.SubjectSourceMoved(keys.EscapeName(branch)),
seat.SourceMovedEvent{Branch: branch, Revision: int64(rev), At: &at},
seat.SourceMovedEvent{Branch: branch, Group: group, Revision: int64(rev), At: &at},
"source-moved."+keys.EscapeName(branch)+"."+strconv.FormatUint(rev, 10)); err != nil {
errs = append(errs, err)
}
@@ -175,8 +191,7 @@ func (h *Holder) logf(format string, a ...any) {
}
}
// SourceStart starts one watched branch by hand, after the hand-act log took it: the same as the start does, for a branch no
// key holds yet.
// SourceStart starts one watched branch by hand, after the hand-act log took it: the same as the start does.
func (h *Holder) SourceStart(branch, why, by string, acts handActs) (map[string]any, error) {
if strings.TrimSpace(branch) == "" || strings.TrimSpace(why) == "" {
return nil, errors.New("say the branch and why its source is started")
@@ -194,7 +209,7 @@ func (h *Holder) SourceStart(branch, why, by string, acts handActs) (map[string]
started, leftOut, err := h.startSources(branch)
out := map[string]any{"branch": branch, "started": started, "left-out": leftOut, "by": by}
if err == nil && len(started) == 0 && len(leftOut) == 0 {
out["note"] = "the gate holds a source key of the branch already: only a take-in moves it"
out["note"] = "the key names every registered repository already: only a take-in moves a head"
}
return out, err
}
@@ -145,10 +145,18 @@ func TestAWatchedBranchStartsFromTheForge(t *testing.T) {
started: []string{"main", "master", "nox-mesh"}, events: 3,
heads: map[string]map[string]string{"main": {"o/car-hunter": "c1", "o/mesh": "c2", "o/photos": "c6"},
"master": {"o/f1-circuits": "c3"}, "nox-mesh": {"o/link2pay": "c4"}}},
{name: "a held key is never rewritten", catalog: fakeCatalog{regs: regs},
{name: "a held key lacking repositories gets them, its heads unchanged", catalog: fakeCatalog{regs: regs},
held: map[string]Source{"main": {Group: "g", Heads: map[string]string{"o/mesh": "old"}}},
started: []string{"master", "nox-mesh"}, events: 2,
heads: map[string]map[string]string{"main": {"o/mesh": "old"}, "master": {"o/f1-circuits": "c3"}, "nox-mesh": {"o/link2pay": "c4"}}},
started: []string{"main", "master", "nox-mesh"}, events: 3,
heads: map[string]map[string]string{"main": {"o/mesh": "old", "o/car-hunter": "c1", "o/photos": "c6"}, "master": {"o/f1-circuits": "c3"}, "nox-mesh": {"o/link2pay": "c4"}}},
{name: "a held key lacking one repository", catalog: fakeCatalog{regs: regs[:2]},
held: map[string]Source{"main": {Group: "g", Heads: map[string]string{"o/mesh": "old"}}},
started: []string{"main"}, events: 1,
heads: map[string]map[string]string{"main": {"o/mesh": "old", "o/car-hunter": "c1"}}},
{name: "a held key naming all: nothing written or said", catalog: fakeCatalog{regs: regs[:2]},
held: map[string]Source{"main": {Group: "g", Heads: map[string]string{"o/mesh": "old", "o/car-hunter": "older"}}},
events: 0,
heads: map[string]map[string]string{"main": {"o/mesh": "old", "o/car-hunter": "older"}}},
{name: "by hand, one branch", catalog: fakeCatalog{regs: regs}, only: "master",
started: []string{"master"}, events: 1, heads: map[string]map[string]string{"master": {"o/f1-circuits": "c3"}}},
{name: "registrations unreadable", catalog: fakeCatalog{err: errors.New("away")}, wantErr: true},
@@ -165,8 +173,8 @@ func TestAWatchedBranchStartsFromTheForge(t *testing.T) {
Logf: func(f string, a ...any) { logged = append(logged, fmt.Sprintf(f, a...)) },
SaySeat: func(subject string, body any, id string) error {
events++
if ev := body.(seat.SourceMovedEvent); ev.Group != "" {
t.Errorf("the group is empty, got %q", ev.Group)
if ev := body.(seat.SourceMovedEvent); ev.Group != c.held[ev.Branch].Group {
t.Errorf("the group is the key's own (empty for a new key), got %q", ev.Group)
}
return nil
}}