gate: a head moves only forward and only when it changes (A-464c)
Builds: design 47 G-22 A-464c, A-464
This commit is contained in:
@@ -71,6 +71,8 @@ type ForgeReads interface {
|
||||
Pull(repository string, number int64) (gitseat.PullAnswer, error)
|
||||
// Branch is a repository branch's head (the seat git's verb branch).
|
||||
Branch(repository, branch string) (gitseat.BranchAnswer, error)
|
||||
// Contains is whether commit is in (or is) the commit in (the seat git's verb contains).
|
||||
Contains(repository, commit, in string) (bool, error)
|
||||
}
|
||||
|
||||
// kvKept is the store over the runtime.
|
||||
|
||||
@@ -42,7 +42,9 @@ type fakeForge struct {
|
||||
open []gitseat.OpenPull
|
||||
pulls map[string]gitseat.Pull
|
||||
heads map[string]string // "<repository>@<branch>" -> head
|
||||
fail bool
|
||||
// contains says whether commit is in `in`; nil means every commit is in every other (a straight line forward)
|
||||
contains func(commit, in string) bool
|
||||
fail bool
|
||||
}
|
||||
|
||||
func (f *fakeForge) OpenPulls() (gitseat.OpenPullsAnswer, error) {
|
||||
@@ -61,6 +63,15 @@ func (f *fakeForge) Branch(repository, branch string) (gitseat.BranchAnswer, err
|
||||
}
|
||||
return gitseat.BranchAnswer{Branch: branch, Repository: repository, Head: head}, nil
|
||||
}
|
||||
func (f *fakeForge) Contains(repository, commit, in string) (bool, error) {
|
||||
if f.fail {
|
||||
return false, errors.New("the forge is away")
|
||||
}
|
||||
if f.contains == nil {
|
||||
return true, nil
|
||||
}
|
||||
return f.contains(commit, in), nil
|
||||
}
|
||||
func (f *fakeForge) Pull(repository string, number int64) (gitseat.PullAnswer, error) {
|
||||
p, ok := f.pulls[fmt.Sprintf("%s#%d", repository, number)]
|
||||
if !ok {
|
||||
|
||||
@@ -31,19 +31,36 @@ func (h *Holder) moveHead(branch, repository, head, group string) (moved bool, e
|
||||
if h.Source == nil || head == "" {
|
||||
return false, nil
|
||||
}
|
||||
held, _, err := h.Source.Read(branch)
|
||||
if err != nil || held == nil {
|
||||
return false, err
|
||||
}
|
||||
name := ""
|
||||
for r := range held.Heads {
|
||||
if trunk.SameRepository(r, repository) {
|
||||
name = r
|
||||
}
|
||||
}
|
||||
if name == "" || held.Heads[name] == head {
|
||||
return false, nil // not named, or no change: no revision and no source-moved (A-464c)
|
||||
}
|
||||
was := held.Heads[name]
|
||||
// Only forward (A-464c): the head held must be contained in the merge, asked of the seat git.
|
||||
if h.Forge == nil {
|
||||
return false, errors.New("this gate reads no forge, so it cannot tell whether the merge is ahead of the head")
|
||||
}
|
||||
ahead, err := h.Forge.Contains(name, was, head)
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("whether %.12s is in %.12s of %s cannot be read, so the head stays: %w", was, head, name, err)
|
||||
}
|
||||
if !ahead {
|
||||
h.logf("[mesh-delivery] source.%s keeps %s at %.12s: the merge %.12s does not contain it (heard again, late or out of order), left without effect", branch, name, was, head)
|
||||
return false, nil
|
||||
}
|
||||
rev, err := h.Source.Set(branch, func(prev *Source) (Source, bool) {
|
||||
moved = false
|
||||
if prev == nil {
|
||||
return Source{}, false
|
||||
}
|
||||
name := ""
|
||||
for r := range prev.Heads {
|
||||
if trunk.SameRepository(r, repository) {
|
||||
name = r
|
||||
}
|
||||
}
|
||||
if name == "" || prev.Heads[name] == head {
|
||||
return Source{}, false
|
||||
if prev == nil || prev.Heads[name] != was {
|
||||
return Source{}, false // another writer moved it first
|
||||
}
|
||||
next := Source{Group: group, Heads: map[string]string{}}
|
||||
for r, c := range prev.Heads {
|
||||
|
||||
@@ -1,11 +1,13 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
meshcatalog "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog"
|
||||
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-forge-bridge/forgemsg"
|
||||
stdio "git.novox.be/novox/mesh-sdk/go"
|
||||
)
|
||||
@@ -35,7 +37,7 @@ func TestAMergedMessageMovesTheSourceOfItsBranchWhoeverMadeTheMerge(t *testing.T
|
||||
src := &memSource{vals: map[string]Source{"main": {Group: "g", Heads: map[string]string{"novox/mesh-contracts": old}}}}
|
||||
var said []string
|
||||
var logged []string
|
||||
h := &Holder{Now: time.Now, Source: src,
|
||||
h := &Holder{Now: time.Now, Source: src, Forge: &fakeForge{},
|
||||
Logf: func(f string, a ...any) { logged = append(logged, f) },
|
||||
SaySeat: func(subject string, _ any, id string) error { said = append(said, id); return nil }}
|
||||
if err := h.ForgeMerged(body(c.repository, c.base, "")); err != nil {
|
||||
@@ -95,9 +97,55 @@ func TestTheGateTakesTheForgeBridgesMergedEventOfAnyRepository(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
src := &memSource{vals: map[string]Source{"main": {Heads: map[string]string{"novox/mesh-contracts": strings.Repeat("a", 40)}}}}
|
||||
h := &Holder{Now: time.Now, Source: src, Logf: func(string, ...any) {}}
|
||||
h := &Holder{Now: time.Now, Source: src, Forge: &fakeForge{}, Logf: func(string, ...any) {}}
|
||||
key := strings.Replace(strings.Replace(forgeMergedEvent, "*", "novox", 1), "*", "mesh-contracts", 1)
|
||||
if err := take(h, stdio.Envelope{Key: key, Body: data}); err != nil || src.vals["main"].Heads["novox/mesh-contracts"] != merge {
|
||||
t.Fatalf("event %q: %v, source %+v", key, err, src.vals["main"])
|
||||
}
|
||||
}
|
||||
|
||||
// Design 47 G A-464c: a head moves only forward and only when it changes. A merged message for a commit that does not contain
|
||||
// the held head (replayed, late, out of order) moves nothing and is said; a second message for the same head writes no
|
||||
// revision; and a take-in.taken for a head a merged already moved writes none and says no source-moved.
|
||||
func TestAHeadMovesOnlyForwardAndOnlyWhenItChanges(t *testing.T) {
|
||||
oldC, newC := strings.Repeat("a", 40), strings.Repeat("b", 40)
|
||||
msg := func(merge string) []byte {
|
||||
data, err := forgemsg.EncodeForge(forgemsg.Forge{V: 1, Kind: forgemsg.ForgeMerged, Repository: "novox/app", Base: "main", Merge: merge, Head: merge, Number: 3, Root: strings.Repeat("d", 40)})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return data
|
||||
}
|
||||
src := &memSource{vals: map[string]Source{"main": {Heads: map[string]string{"novox/app": newC}}}}
|
||||
var said, logged []string
|
||||
// newC contains oldC, not the other way round.
|
||||
forge := &fakeForge{contains: func(commit, in string) bool { return commit == in || (commit == oldC && in == newC) }}
|
||||
h := &Holder{Now: time.Now, Source: src, Forge: forge, Logf: func(f string, a ...any) { logged = append(logged, f) },
|
||||
SaySeat: func(_ string, _ any, id string) error { said = append(said, id); return nil }}
|
||||
if err := h.ForgeMerged(msg(oldC)); err != nil || src.vals["main"].Heads["novox/app"] != newC || src.rev != 0 || len(said) != 0 || len(logged) == 0 {
|
||||
t.Fatalf("a replayed old merge: %v, source %+v, revisions %d, said %v, logged %v: want nothing moved and a log line", err, src.vals["main"], src.rev, said, logged)
|
||||
}
|
||||
if err := h.ForgeMerged(msg(newC)); err != nil || src.rev != 0 || len(said) != 0 {
|
||||
t.Fatalf("a second message for the same head wrote revision %d, said %v", src.rev, said)
|
||||
}
|
||||
// A take-in.taken naming the head a merged moved already writes no revision and says nothing.
|
||||
body := []byte(`{"v":1,"kind":"taken","key":"g.feat","what":"group","group":"feat","branch":"main","source":7,` +
|
||||
`"merges":[{"repository":"novox/app","number":3,"merge":"` + newC + `"}],"entries":[]}`)
|
||||
if err := h.moveSource(mustTaken(t, body)); err != nil || src.rev != 0 || len(said) != 0 {
|
||||
t.Fatalf("take-in for a head already moved: %v, revision %d, said %v", err, src.rev, said)
|
||||
}
|
||||
// A real forward move still writes one revision.
|
||||
src.vals["main"] = Source{Heads: map[string]string{"novox/app": oldC}}
|
||||
if err := h.ForgeMerged(msg(newC)); err != nil || src.rev != 1 || len(said) != 1 {
|
||||
t.Fatalf("a forward move: %v, revision %d, said %v", err, src.rev, said)
|
||||
}
|
||||
}
|
||||
|
||||
func mustTaken(t *testing.T, body []byte) meshcatalog.TakeInTaken {
|
||||
t.Helper()
|
||||
var v meshcatalog.TakeInTaken
|
||||
if err := json.Unmarshal(body, &v); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return v
|
||||
}
|
||||
|
||||
@@ -55,6 +55,18 @@ func (s seatGit) Branch(repository, branch string) (gitseat.BranchAnswer, error)
|
||||
return a, s.call(gitseat.VerbBranch, gitseat.BranchInput{Repository: repository, Branch: branch}, &a)
|
||||
}
|
||||
|
||||
// Contains is whether commit is the commit in or an ancestor of it.
|
||||
func (s seatGit) Contains(repository, commit, in string) (bool, error) {
|
||||
var a gitseat.ContainsAnswer
|
||||
if err := s.call(gitseat.VerbContains, gitseat.ContainsInput{Repository: repository, Commit: commit, In: in}, &a); err != nil {
|
||||
return false, err
|
||||
}
|
||||
if a.Error != "" {
|
||||
return false, errors.New(a.Error)
|
||||
}
|
||||
return a.Contains, nil
|
||||
}
|
||||
|
||||
// watchedRepositories are the repositories each watched branch is registered for, each once, sorted.
|
||||
func watchedRepositories(regs []meshcatalog.Registration) map[string][]string {
|
||||
seen := map[string]map[string]bool{}
|
||||
|
||||
@@ -107,9 +107,20 @@ func (h *Holder) moveSource(t meshcatalog.TakeInTaken) error {
|
||||
for _, m := range t.Merges {
|
||||
heads[m.Repository] = m.Merge
|
||||
}
|
||||
wrote := false
|
||||
rev, err := h.Source.Set(t.Branch, func(prev *Source) (Source, bool) {
|
||||
wrote = false
|
||||
next := Source{Group: t.Group, Heads: heads}
|
||||
if prev != nil {
|
||||
same := true
|
||||
for r, c := range heads {
|
||||
if prev.Heads[r] != c {
|
||||
same = false
|
||||
}
|
||||
}
|
||||
if same {
|
||||
return Source{}, false // a merged message moved these heads already: no revision (A-464c)
|
||||
}
|
||||
next.Heads = map[string]string{}
|
||||
for r, c := range prev.Heads {
|
||||
next.Heads[r] = c
|
||||
@@ -118,12 +129,13 @@ func (h *Holder) moveSource(t meshcatalog.TakeInTaken) error {
|
||||
next.Heads[r] = c
|
||||
}
|
||||
}
|
||||
wrote = true
|
||||
return next, true
|
||||
})
|
||||
if err != nil {
|
||||
return fmt.Errorf("source.%s cannot be moved: %w", t.Branch, err)
|
||||
}
|
||||
if h.SaySeat == nil {
|
||||
if h.SaySeat == nil || !wrote {
|
||||
return nil
|
||||
}
|
||||
at := h.Now().UTC()
|
||||
|
||||
@@ -84,7 +84,7 @@ func TestATakeInTakenOpensOneRecordAndMovesItsBranchsSource(t *testing.T) {
|
||||
if s := src.vals["main"]; s.Group != "feat" || s.Heads["novox/app"] != merge {
|
||||
t.Fatalf("source.main %+v", s)
|
||||
}
|
||||
if len(events) != 2 || !strings.HasSuffix(events[0].subject, ".event.source-moved.main") {
|
||||
if len(events) != 1 || !strings.HasSuffix(events[0].subject, ".event.source-moved.main") {
|
||||
t.Fatalf("said %+v", events)
|
||||
}
|
||||
drop := json.RawMessage(`{"v":1,"kind":"taken","key":"m.00000000000000000002","what":"revision","dropped":true,"branch":"main","source":8,` +
|
||||
@@ -92,7 +92,7 @@ func TestATakeInTakenOpensOneRecordAndMovesItsBranchsSource(t *testing.T) {
|
||||
if err := c.Hear(stdio.Envelope{Key: "mesh-delivery-indexer.take-in.taken", Body: drop}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if kept, _ := h.Records.Kept(); len(kept) != 1 || len(events) != 2 {
|
||||
if kept, _ := h.Records.Kept(); len(kept) != 1 || len(events) != 1 {
|
||||
t.Fatalf("a hand drop opened %d records and said %d events", len(kept), len(events))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user