| 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 | |
| 12 | package main |
| 13 | |
| 14 | import ( |
| 15 | "context" |
| 16 | "sort" |
| 17 | |
| 18 | "the-valley/change" |
| 19 | "the-valley/rebase" |
| 20 | ) |
| 21 | |
| 22 | // The landing evidence a version can have. |
| 23 | const ( |
| 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. |
| 32 | var 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 directory what was verified is |
| 37 | // remembered in. newVerifier finds them (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. |
| 40 | type verifier = rebase.Verifier |
| 41 | |
| 42 | // verifierHint says what a transfer statement is verified with, for a |
| 43 | // command that could not verify one. |
| 44 | const 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" |
| 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. |
| 48 | type 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 | policy tipPolicy // the policy at tip, read when first asked for |
| 59 | } |
| 60 | |
| 61 | // readSnapshot lists origin once and fetches every object the snapshot |
| 62 | // reads: main, every branch tip, every record commit and every outcome |
| 63 | // record. |
| 64 | func readSnapshot(g git, v verifier) (*snapshot, error) { |
| 65 | land, err := rebase.ReadSnapshot(context.Background(), g.engine(), v) |
| 66 | if err != nil { |
| 67 | return nil, err |
| 68 | } |
| 69 | s := wrapSnapshot(g, land) |
| 70 | for _, u := range land.Unread { |
| 71 | s.unread = append(s.unread, unreadRecord{ref: u.Ref, commit: u.Commit, why: u.Why}) |
| 72 | } |
| 73 | return s, nil |
| 74 | } |
| 75 | |
| 76 | // newSnapshot is a snapshot of what o lists, with every record in all |
| 77 | // already read and every object it names already fetched. |
| 78 | func newSnapshot(g git, o origin, all []recorded, v verifier) *snapshot { |
| 79 | var read []rebase.Recorded |
| 80 | for _, n := range all { |
| 81 | read = append(read, n.engine()) |
| 82 | } |
| 83 | return wrapSnapshot(g, rebase.NewSnapshot(context.Background(), g.engine(), o.engine(), read, v)) |
| 84 | } |
| 85 | |
| 86 | func wrapSnapshot(g git, land *rebase.Snapshot) *snapshot { |
| 87 | return &snapshot{land: land, g: g, o: origin{refs: land.Refs}, tip: land.Tip, all: fromEngine(land.Changes)} |
| 88 | } |
| 89 | |
| 90 | // headOf is a version's head as an object ID. |
| 91 | func headOf(v change.Version) string { return rebase.HeadOf(v) } |
| 92 | |
| 93 | // unverified reports whether a transfer statement named a version and |
| 94 | // could not be checked, so its landing may be stronger than shown. |
| 95 | func (s *snapshot) unverified() bool { return s.land.Unverified() } |
| 96 | |
| 97 | // reaches reports whether main reaches a commit by any path. |
| 98 | func (s *snapshot) reaches(oid string) bool { return s.land.Reaches(oid) } |
| 99 | |
| 100 | // request is the integration request origin holds for a version: one |
| 101 | // filed under either of its names, at its head. |
| 102 | func (s *snapshot) request(id string, v change.Version) *jsonRequest { |
| 103 | if ref, ok := s.land.Request(id, v); ok { |
| 104 | return &jsonRequest{Ref: ref, Commit: headOf(v)} |
| 105 | } |
| 106 | return nil |
| 107 | } |
| 108 | |
| 109 | // outcome is the integrator's last decision about a version: the outcome |
| 110 | // record under either of its names whose head is the version's head. |
| 111 | func (s *snapshot) outcome(id string, v change.Version) (name string, m memo, found bool) { |
| 112 | name, o, found := s.land.Outcome(id, v) |
| 113 | return name, memoOf(o), found |
| 114 | } |
| 115 | |
| 116 | // landing is the landing evidence a version has, and the commit on main it |
| 117 | // landed as, when it has. |
| 118 | func (s *snapshot) landing(id string, v change.Version) (kind, commit string) { |
| 119 | return s.land.Landing(id, v) |
| 120 | } |
| 121 | |
| 122 | // landedAs is the version a change landed as, and whether it has |
| 123 | // (rebase.Snapshot.LandedAs). |
| 124 | func (s *snapshot) landedAs(n recorded) (change.Version, bool) { return s.land.LandedAs(n.engine()) } |
| 125 | |
| 126 | // isLanded reports whether a change has landed as any of its versions. |
| 127 | func (s *snapshot) isLanded(n recorded) bool { |
| 128 | _, landed := s.landedAs(n) |
| 129 | return landed |
| 130 | } |
| 131 | |
| 132 | // reach is every commit reachable from oid that main does not reach. |
| 133 | func (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. |
| 136 | func (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. |
| 142 | func (s *snapshot) includes(n recorded, candidates []recorded) []string { |
| 143 | head := headOf(n.record.ActiveVersion()) |
| 144 | heads := []string{head} |
| 145 | for _, c := range candidates { |
| 146 | heads = append(heads, headOf(c.record.ActiveVersion())) |
| 147 | } |
| 148 | s.land.ReadReach(heads) |
| 149 | var below []recorded |
| 150 | for _, c := range candidates { |
| 151 | if c.id != n.id && s.strictAncestor(headOf(c.record.ActiveVersion()), head) { |
| 152 | below = append(below, c) |
| 153 | } |
| 154 | } |
| 155 | ids := []string{} |
| 156 | for _, c := range below { |
| 157 | nearest := true |
| 158 | for _, d := range below { |
| 159 | if s.strictAncestor(headOf(c.record.ActiveVersion()), headOf(d.record.ActiveVersion())) { |
| 160 | nearest = false |
| 161 | break |
| 162 | } |
| 163 | } |
| 164 | if nearest { |
| 165 | ids = append(ids, c.id) |
| 166 | } |
| 167 | } |
| 168 | sort.Strings(ids) |
| 169 | return ids |
| 170 | } |
| 171 | |
| 172 | // unrecorded is every branch on origin, other than main, whose tip main |
| 173 | // does not reach, that is not the head of any change's active version, and |
| 174 | // that no landed version's head reaches, in the order of their names. |
| 175 | func (s *snapshot) unrecorded() []jsonBranch { |
| 176 | branches := []jsonBranch{} |
| 177 | for _, b := range s.land.Unrecorded() { |
| 178 | branches = append(branches, jsonBranch{Branch: b.Branch, Head: b.Head}) |
| 179 | } |
| 180 | return branches |
| 181 | } |