gunk-dev/the-valley

inventory.go

source6a7605f70901main's published tip history
7.4 KiB
1// Every change a repository holds a record of, and how far each version
2// has landed.
3//
4// changes and pending read one snapshot of origin for each repository
5// they list: every branch, every record, every integration request and
6// outcome, and every attestation ref. From it they decide, for each
7// version of each change, the landing evidence it has: landed,
8// landed-recorded, uncertain or not-landed. The rules are the rebase
9// engine's (../rebase/snapshot.go), so a listing and a rebase read one
10// landing the same way.
11
12package main
13
14import (
15 "context"
16 "sort"
17
18 "the-valley/change"
19 "the-valley/knot"
20 "the-valley/rebase"
21)
22
23// The landing evidence a version can have.
24const (
25 landed = rebase.Landed
26 landedRecorded = rebase.LandedRecorded
27 uncertain = rebase.LandingUncertain
28 notLanded = rebase.NotLanded
29)
30
31// inventoryPatterns are the ls-remote patterns of every ref a snapshot
32// reads.
33var inventoryPatterns = rebase.InventoryPatterns
34
35// verifier is what a transfer statement is checked with: the verifier
36// keys of the identity registry's known-signers file, the name the
37// integrator signs under, and the directory what was verified is
38// remembered in. newVerifier finds them (signers.go). Without both, no transfer
39// statement is believed, and a version that only one could show landed
40// reads as uncertain or not-landed.
41type verifier = rebase.Verifier
42
43// verifierHint says what a transfer statement is verified with, for a
44// command that could not verify one.
45const verifierHint = "Verifying one takes the known-signers file and the integrator's signing name. The serving host publishes the file and the name at " + signersRef + " in its instance repository, and --known-signers and --integrator-name, or VALLEY_KNOWN_SIGNERS and VALLEY_INTEGRATOR_SIGNER, give them instead"
46
47// snapshot is one read of a repository's origin: the engine's snapshot,
48// which decides each landing, with the records as this program holds them.
49type snapshot struct {
50 land *rebase.Snapshot
51 g git
52 o origin
53 tip string // main on origin; "" when origin has none
54 all []recorded
55
56 // unread is each record that does not read, which no listing shows.
57 unread []unreadRecord
58
59 policy tipPolicy // the policy at tip, read when first asked for
60
61 // inventory says o is a listing of inventoryPatterns, which covers
62 // every attestation ref and the plan graph's ref, so a reader of either
63 // in the same command lists origin no more.
64 inventory bool
65}
66
67// evidence is every attestation ref origin holds, as the snapshot's own
68// listing gives them, or nil when that listing did not cover them.
69func (s *snapshot) evidence() map[string]string {
70 if !s.inventory {
71 return nil
72 }
73 return s.o.refs
74}
75
76// knotsTip is the commit origin's plan graph ref names, as the snapshot's
77// own listing gives it, "" for none; listed is false when that listing did
78// not cover the ref.
79func (s *snapshot) knotsTip() (tip string, listed bool) {
80 if !s.inventory {
81 return "", false
82 }
83 return s.o.refs[knot.Ref], true
84}
85
86// readSnapshot lists origin once and fetches every object the snapshot
87// reads: main, every branch tip, every record commit and every outcome
88// record.
89func readSnapshot(g git, v verifier) (*snapshot, error) {
90 land, err := rebase.ReadSnapshot(context.Background(), g.engine(), v)
91 if err != nil {
92 return nil, err
93 }
94 s := wrapSnapshot(g, land)
95 s.inventory = true
96 for _, u := range land.Unread {
97 s.unread = append(s.unread, unreadRecord{ref: u.Ref, commit: u.Commit, why: u.Why})
98 }
99 return s, nil
100}
101
102// newSnapshot is a snapshot of what o lists, with every record in all
103// already read and every object it names already fetched.
104func newSnapshot(g git, o origin, all []recorded, v verifier) *snapshot {
105 var read []rebase.Recorded
106 for _, n := range all {
107 read = append(read, n.engine())
108 }
109 return wrapSnapshot(g, rebase.NewSnapshot(context.Background(), g.engine(), o.engine(), read, v))
110}
111
112func wrapSnapshot(g git, land *rebase.Snapshot) *snapshot {
113 return &snapshot{land: land, g: g, o: origin{refs: land.Refs}, tip: land.Tip, all: fromEngine(land.Changes)}
114}
115
116// headOf is a version's head as an object ID.
117func headOf(v change.Version) string { return rebase.HeadOf(v) }
118
119// unverified reports whether a transfer statement named a version and
120// could not be checked, so its landing may be stronger than shown.
121func (s *snapshot) unverified() bool { return s.land.Unverified() }
122
123// reaches reports whether main reaches a commit by any path.
124func (s *snapshot) reaches(oid string) bool { return s.land.Reaches(oid) }
125
126// request is the integration request origin holds for a version: one
127// filed under either of its names, at its head.
128func (s *snapshot) request(id string, v change.Version) *jsonRequest {
129 if ref, ok := s.land.Request(id, v); ok {
130 return &jsonRequest{Ref: ref, Commit: headOf(v)}
131 }
132 return nil
133}
134
135// outcome is the integrator's last decision about a version: the outcome
136// record under either of its names whose head is the version's head.
137func (s *snapshot) outcome(id string, v change.Version) (name string, m memo, found bool) {
138 name, o, found := s.land.Outcome(id, v)
139 return name, memoOf(o), found
140}
141
142// landing is the landing evidence a version has, and the commit on main it
143// landed as, when it has.
144func (s *snapshot) landing(id string, v change.Version) (kind, commit string) {
145 return s.land.Landing(id, v)
146}
147
148// landedAs is the version a change landed as, and whether it has
149// (rebase.Snapshot.LandedAs).
150func (s *snapshot) landedAs(n recorded) (change.Version, bool) { return s.land.LandedAs(n.engine()) }
151
152// isLanded reports whether a change has landed as any of its versions.
153func (s *snapshot) isLanded(n recorded) bool {
154 _, landed := s.landedAs(n)
155 return landed
156}
157
158// reach is every commit reachable from oid that main does not reach.
159func (s *snapshot) reach(oid string) map[string]bool { return s.land.Reach(oid) }
160
161// strictAncestor reports whether a is an ancestor of b, and not b itself.
162func (s *snapshot) strictAncestor(a, b string) bool { return s.land.StrictAncestor(a, b) }
163
164// includes is the change IDs among candidates whose active head is a
165// strict ancestor of n's active head, nearest only: one that is itself an
166// ancestor of another such change's head is left out. It is ancestry
167// alone, so a change that includes another lands the other's content too.
168func (s *snapshot) includes(n recorded, candidates []recorded) []string {
169 head := headOf(n.record.ActiveVersion())
170 heads := []string{head}
171 for _, c := range candidates {
172 heads = append(heads, headOf(c.record.ActiveVersion()))
173 }
174 s.land.ReadReach(heads)
175 var below []recorded
176 for _, c := range candidates {
177 if c.id != n.id && s.strictAncestor(headOf(c.record.ActiveVersion()), head) {
178 below = append(below, c)
179 }
180 }
181 ids := []string{}
182 for _, c := range below {
183 nearest := true
184 for _, d := range below {
185 if s.strictAncestor(headOf(c.record.ActiveVersion()), headOf(d.record.ActiveVersion())) {
186 nearest = false
187 break
188 }
189 }
190 if nearest {
191 ids = append(ids, c.id)
192 }
193 }
194 sort.Strings(ids)
195 return ids
196}
197
198// unrecorded is every branch on origin, other than main, whose tip main
199// does not reach, that is not the head of any change's active version, and
200// that no landed version's head reaches, in the order of their names.
201func (s *snapshot) unrecorded() []jsonBranch {
202 branches := []jsonBranch{}
203 for _, b := range s.land.Unrecorded() {
204 branches = append(branches, jsonBranch{Branch: b.Branch, Head: b.Head})
205 }
206 return branches
207}