gunk-dev/the-valley

inventory.go

source8724de39a028main's published tip history
6.2 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/rebase"
20)
21
22// The landing evidence a version can have.
23const (
24 landed = rebase.Landed
25 landedRecorded = rebase.LandedRecorded
26 uncertain = rebase.LandingUncertain
27 notLanded = rebase.NotLanded
28)
29
30// inventoryPatterns are the ls-remote patterns of every ref a snapshot
31// reads.
32var inventoryPatterns = rebase.InventoryPatterns
33
34// verifier is what a transfer statement is checked with: the verifier
35// keys of the identity registry's known-signers file, the name the
36// integrator signs under, and the attest helper that checks a note.
37// newVerifier finds the first two (signers.go). Without both, no transfer
38// statement is believed, and a version that only one could show landed
39// reads as uncertain or not-landed.
40type verifier = rebase.Verifier
41
42// verifierHint says what a transfer statement is verified with, for a
43// command that could not verify one.
44const verifierHint = "Verifying one takes attest on PATH, 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"
45
46// snapshot is one read of a repository's origin: the engine's snapshot,
47// which decides each landing, with the records as this program holds them.
48type snapshot struct {
49 land *rebase.Snapshot
50 g git
51 o origin
52 tip string // main on origin; "" when origin has none
53 all []recorded
54
55 // unread is each record that does not read, which no listing shows.
56 unread []unreadRecord
57}
58
59// readSnapshot lists origin once and fetches every object the snapshot
60// reads: main, every branch tip, every record commit and every outcome
61// record.
62func readSnapshot(g git, v verifier) (*snapshot, error) {
63 land, err := rebase.ReadSnapshot(context.Background(), g.engine(), v)
64 if err != nil {
65 return nil, err
66 }
67 s := wrapSnapshot(g, land)
68 for _, u := range land.Unread {
69 s.unread = append(s.unread, unreadRecord{ref: u.Ref, commit: u.Commit, why: u.Why})
70 }
71 return s, nil
72}
73
74// newSnapshot is a snapshot of what o lists, with every record in all
75// already read and every object it names already fetched.
76func newSnapshot(g git, o origin, all []recorded, v verifier) *snapshot {
77 var read []rebase.Recorded
78 for _, n := range all {
79 read = append(read, n.engine())
80 }
81 return wrapSnapshot(g, rebase.NewSnapshot(context.Background(), g.engine(), o.engine(), read, v))
82}
83
84func wrapSnapshot(g git, land *rebase.Snapshot) *snapshot {
85 return &snapshot{land: land, g: g, o: origin{refs: land.Refs}, tip: land.Tip, all: fromEngine(land.Changes)}
86}
87
88// headOf is a version's head as an object ID.
89func headOf(v change.Version) string { return rebase.HeadOf(v) }
90
91// unverified reports whether a transfer statement named a version and
92// could not be checked, so its landing may be stronger than shown.
93func (s *snapshot) unverified() bool { return s.land.Unverified() }
94
95// reaches reports whether main reaches a commit by any path.
96func (s *snapshot) reaches(oid string) bool { return s.land.Reaches(oid) }
97
98// request is the integration request origin holds for a version: one
99// filed under either of its names, at its head.
100func (s *snapshot) request(id string, v change.Version) *jsonRequest {
101 if ref, ok := s.land.Request(id, v); ok {
102 return &jsonRequest{Ref: ref, Commit: headOf(v)}
103 }
104 return nil
105}
106
107// outcome is the integrator's last decision about a version: the outcome
108// record under either of its names whose head is the version's head.
109func (s *snapshot) outcome(id string, v change.Version) (name string, m memo, found bool) {
110 name, o, found := s.land.Outcome(id, v)
111 return name, memoOf(o), found
112}
113
114// landing is the landing evidence a version has, and the commit on main it
115// landed as, when it has.
116func (s *snapshot) landing(id string, v change.Version) (kind, commit string) {
117 return s.land.Landing(id, v)
118}
119
120// isLanded reports whether a version has landed by the first two rules.
121func (s *snapshot) isLanded(id string, v change.Version) bool { return s.land.IsLanded(id, v) }
122
123// reach is every commit reachable from oid that main does not reach.
124func (s *snapshot) reach(oid string) map[string]bool { return s.land.Reach(oid) }
125
126// strictAncestor reports whether a is an ancestor of b, and not b itself.
127func (s *snapshot) strictAncestor(a, b string) bool { return s.land.StrictAncestor(a, b) }
128
129// includes is the change IDs among candidates whose active head is a
130// strict ancestor of n's active head, nearest only: one that is itself an
131// ancestor of another such change's head is left out. It is ancestry
132// alone, so a change that includes another lands the other's content too.
133func (s *snapshot) includes(n recorded, candidates []recorded) []string {
134 head := headOf(n.record.ActiveVersion())
135 var below []recorded
136 for _, c := range candidates {
137 if c.id != n.id && s.strictAncestor(headOf(c.record.ActiveVersion()), head) {
138 below = append(below, c)
139 }
140 }
141 ids := []string{}
142 for _, c := range below {
143 nearest := true
144 for _, d := range below {
145 if s.strictAncestor(headOf(c.record.ActiveVersion()), headOf(d.record.ActiveVersion())) {
146 nearest = false
147 break
148 }
149 }
150 if nearest {
151 ids = append(ids, c.id)
152 }
153 }
154 sort.Strings(ids)
155 return ids
156}
157
158// unrecorded is every branch on origin, other than main, whose tip main
159// does not reach and that is not the head of any change's active version,
160// in the order of their names.
161func (s *snapshot) unrecorded() []jsonBranch {
162 branches := []jsonBranch{}
163 for _, b := range s.land.Unrecorded() {
164 branches = append(branches, jsonBranch{Branch: b.Branch, Head: b.Head})
165 }
166 return branches
167}