gunk-dev/the-valley

inventory.go

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