| 1 | // Every change a repository holds a record of, and how far each version |
| 2 | // has landed. |
| 3 | // |
| 4 | // A snapshot is one read of origin: every branch, every record, every |
| 5 | // integration request, outcome record and landing record, every |
| 6 | // attestation ref, and every restack report. From it, for each version of |
| 7 | // each change, it decides the landing evidence the version has. The first |
| 8 | // rule that applies is the answer: |
| 9 | // |
| 10 | // 1. landed: the version's head is on main's first-parent history, and |
| 11 | // no commit on that history between the head and main's tip is a |
| 12 | // merge. The integrator never writes a merge, and it never puts a |
| 13 | // change's own commits on that history. This rule matches a head |
| 14 | // written to main directly, or landed directly by an earlier |
| 15 | // integrator. A merge above the head |
| 16 | // could have dropped the head's content, whichever parent the head is |
| 17 | // reached through, so it leaves the head not landed by this rule. |
| 18 | // 2. landed-recorded: a commit L on main's first-parent history, with one |
| 19 | // parent, carries a transfer statement, signed by the integrator, |
| 20 | // whose change is a request name of the version and whose base is |
| 21 | // the valley-tree-v1 digest of the version's tree. The integrator |
| 22 | // writes one when it lands a change (integrator/transfer.go). The |
| 23 | // integrator recorded landing this version's head as L under that |
| 24 | // request name: a statement about trees does not name the exact head, |
| 25 | // and the integrator's records do. Only the integrator writes them. |
| 26 | // The landing record for the head (change/landing.go) says so when |
| 27 | // origin holds one. A landing made before the integrator wrote |
| 28 | // landing records has none, and then the outcome record for the |
| 29 | // request name must say land at this version's head. And the |
| 30 | // integrator's own merge of the head onto L's parent gives L's tree. |
| 31 | // 3. uncertain: a request for the version was consumed, or the outcome |
| 32 | // record says the integrator failed on it, or a landing record names |
| 33 | // its head, and neither of the above holds. The integrator records a |
| 34 | // landing in the transaction that moves main (integrator/refs.go), so |
| 35 | // only a landing made before it did, or one cut short inside git's |
| 36 | // commit of that transaction, exists without its evidence. |
| 37 | // 4. not-landed: otherwise. |
| 38 | // |
| 39 | // An outcome record or a landing record alone is never proof of a |
| 40 | // landing. An outcome record is kept per request name, so a later pass |
| 41 | // can overwrite it: a landing that only its outcome record bound to its |
| 42 | // head, and whose record a later pass overwrote, reads uncertain, never |
| 43 | // landed-recorded. A landing record is kept per head, so a later landing |
| 44 | // under the same request name leaves it in place. |
| 45 | |
| 46 | package rebase |
| 47 | |
| 48 | import ( |
| 49 | "bytes" |
| 50 | "context" |
| 51 | "errors" |
| 52 | "fmt" |
| 53 | "os" |
| 54 | "os/exec" |
| 55 | "sort" |
| 56 | "strings" |
| 57 | |
| 58 | "the-valley/change" |
| 59 | ) |
| 60 | |
| 61 | // The landing evidence a version can have. |
| 62 | const ( |
| 63 | Landed = "landed" |
| 64 | LandedRecorded = "landed-recorded" |
| 65 | LandingUncertain = "uncertain" |
| 66 | NotLanded = "not-landed" |
| 67 | ) |
| 68 | |
| 69 | const predicateTransfer = "the-valley/integration/transfer/v1" |
| 70 | |
| 71 | // Verifier is what a transfer statement is checked with: the verifier |
| 72 | // keys of the identity registry's known-signers file, the name the |
| 73 | // integrator signs under, and the attest helper that checks a note. |
| 74 | // Without the first two, no transfer statement is believed, and a version |
| 75 | // that only one could show landed reads as uncertain or not-landed. |
| 76 | // |
| 77 | // SignerProjects is the signer projects file the host's controllers |
| 78 | // read (note/signerprojects.go): each signer it names, and the projects |
| 79 | // that signer's check evidence counts in. A signer it names, a worker or |
| 80 | // another controller, is never this host's integrator, so while it names |
| 81 | // Signer no transfer statement is believed, in any project. |
| 82 | // |
| 83 | // Resolve, when set, finds the keys, the name and the signer projects |
| 84 | // file when they are first needed: a snapshot calls it once, before it |
| 85 | // verifies its first statement, and checks with what it returns in place |
| 86 | // of Keys, Signer and SignerProjects. A snapshot that verifies no |
| 87 | // statement never calls it. |
| 88 | type Verifier struct { |
| 89 | Keys, Signer, Attest string |
| 90 | SignerProjects map[string]map[string]bool |
| 91 | Resolve func() (keys, signer string, signerProjects map[string]map[string]bool) |
| 92 | } |
| 93 | |
| 94 | // transferStatement is one transfer statement found under an attestation |
| 95 | // ref, with the values a landing is matched on, read before it is |
| 96 | // verified. |
| 97 | type transferStatement struct { |
| 98 | note []byte |
| 99 | change string // predicate.change: the request's change name |
| 100 | base string // predicate.base.valley-tree-v1: the tree the evidence was over |
| 101 | landed string // subject.digest.git-sha1: the commit that landed |
| 102 | } |
| 103 | |
| 104 | // Snapshot is one read of a repository's origin. Its landing decisions, |
| 105 | // and the ancestry it reads, are read from git when first asked for, under |
| 106 | // the context the snapshot was made with, and kept. |
| 107 | type Snapshot struct { |
| 108 | Origin |
| 109 | Tip string // main on origin; "" when origin has none |
| 110 | Changes []Recorded // every record that reads, in the order of their IDs |
| 111 | Unread []UnreadRecord // each record that does not read, which no reader shows |
| 112 | |
| 113 | ctx context.Context |
| 114 | g Git |
| 115 | verify Verifier |
| 116 | |
| 117 | outcomes map[string]Outcome // outcome ref → the record it holds |
| 118 | landed map[string]landingAt // head → its landing record, once read |
| 119 | reports map[string]Report // change ID → its restack report, once read |
| 120 | landings map[string][2]string // "<change ID> <n>" → landing and commit |
| 121 | firstParent map[string]bool // main's first-parent history, down to its first merge; nil until read |
| 122 | reachesOf map[string]bool |
| 123 | reachOf map[string]map[string]bool |
| 124 | digestOf map[string]string |
| 125 | transfers []transferStatement |
| 126 | read bool // whether transfers has been read |
| 127 | |
| 128 | // unverified is set when a transfer statement named a version and |
| 129 | // could not be checked, so its landing may be stronger than shown. |
| 130 | unverified bool |
| 131 | } |
| 132 | |
| 133 | // snapshotTries is how many times ReadSnapshot lists origin and fetches |
| 134 | // what the listing names before it gives up. |
| 135 | const snapshotTries = 3 |
| 136 | |
| 137 | // ReadSnapshot lists origin once and fetches every object the snapshot |
| 138 | // reads: main, every branch tip, every record commit, every outcome and |
| 139 | // landing record, and every restack report. A record commit has its |
| 140 | // version heads as parents, so they come with it. |
| 141 | // |
| 142 | // A ref can move between the listing and the fetch. Origin may then no |
| 143 | // longer hold the object the listing named, as when main was rewritten and |
| 144 | // its old tip pruned, and the fetch fails. The read is then made again |
| 145 | // from a new listing, up to snapshotTries times in all, and a fetch that |
| 146 | // still fails is the snapshot's error. |
| 147 | func ReadSnapshot(ctx context.Context, g Git, v Verifier) (*Snapshot, error) { |
| 148 | for try := 1; ; try++ { |
| 149 | o, err := ListOrigin(ctx, g, InventoryPatterns...) |
| 150 | if err != nil { |
| 151 | return nil, err |
| 152 | } |
| 153 | s, err := fetchSnapshot(ctx, g, o, v) |
| 154 | if err == nil || try == snapshotTries || ctx.Err() != nil { |
| 155 | return s, err |
| 156 | } |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | // fetchSnapshot fetches every object a snapshot of the listing reads, and |
| 161 | // reads every record it names. |
| 162 | func fetchSnapshot(ctx context.Context, g Git, o Origin, v Verifier) (*Snapshot, error) { |
| 163 | var want []string |
| 164 | for ref, oid := range o.Refs { |
| 165 | if strings.HasPrefix(ref, "refs/heads/") || strings.HasPrefix(ref, OutcomePrefix) || |
| 166 | strings.HasPrefix(ref, LandingPrefix) || strings.HasPrefix(ref, RestackPrefix) { |
| 167 | want = append(want, oid) |
| 168 | } |
| 169 | } |
| 170 | if err := Fetch(ctx, g, want...); err != nil { |
| 171 | return nil, err |
| 172 | } |
| 173 | all, unread, err := ReadAll(ctx, g, o) |
| 174 | if err != nil { |
| 175 | return nil, err |
| 176 | } |
| 177 | s := NewSnapshot(ctx, g, o, all, v) |
| 178 | s.Unread = unread |
| 179 | return s, nil |
| 180 | } |
| 181 | |
| 182 | // NewSnapshot is a snapshot of what o lists, with every record in all |
| 183 | // already read and every object it names already fetched. |
| 184 | func NewSnapshot(ctx context.Context, g Git, o Origin, all []Recorded, v Verifier) *Snapshot { |
| 185 | s := &Snapshot{ |
| 186 | Origin: o, Tip: o.Refs["refs/heads/"+Target], Changes: all, |
| 187 | ctx: ctx, g: g, verify: v, |
| 188 | outcomes: map[string]Outcome{}, landed: map[string]landingAt{}, reports: map[string]Report{}, landings: map[string][2]string{}, |
| 189 | reachesOf: map[string]bool{}, reachOf: map[string]map[string]bool{}, digestOf: map[string]string{}, |
| 190 | } |
| 191 | for ref, oid := range o.Refs { |
| 192 | if strings.HasPrefix(ref, OutcomePrefix) { |
| 193 | data, err := g.output(ctx, true, "cat-file", "blob", oid) |
| 194 | if err != nil { |
| 195 | data = "" |
| 196 | } |
| 197 | s.outcomes[ref] = ParseOutcome(data) |
| 198 | } |
| 199 | } |
| 200 | return s |
| 201 | } |
| 202 | |
| 203 | // Unverified reports whether a transfer statement named a version and |
| 204 | // could not be checked, so that version's landing may be stronger than |
| 205 | // the snapshot says. |
| 206 | func (s *Snapshot) Unverified() bool { return s.unverified } |
| 207 | |
| 208 | // OnMain reports whether a commit is on main's first-parent history (main |
| 209 | // itself, its first parent, that commit's first parent, and so on) with no |
| 210 | // merge between it and main's tip: whether |
| 211 | // `git rev-list --first-parent --merges <oid>..main` would list nothing. |
| 212 | // This is where a landing puts a change. The history is read down to its |
| 213 | // first merge, which is itself the last commit counted, since no merge is |
| 214 | // above it. |
| 215 | func (s *Snapshot) OnMain(oid string) bool { |
| 216 | if s.Tip == "" || oid == "" { |
| 217 | return false |
| 218 | } |
| 219 | if s.firstParent == nil { |
| 220 | s.firstParent = map[string]bool{} |
| 221 | if out, err := s.g.output(s.ctx, true, "rev-list", "--first-parent", "--parents", s.Tip); err == nil { |
| 222 | for _, line := range strings.Split(out, "\n") { |
| 223 | fields := strings.Fields(line) |
| 224 | if len(fields) == 0 { |
| 225 | continue |
| 226 | } |
| 227 | s.firstParent[fields[0]] = true |
| 228 | if len(fields) > 2 { |
| 229 | break |
| 230 | } |
| 231 | } |
| 232 | } |
| 233 | } |
| 234 | return s.firstParent[oid] |
| 235 | } |
| 236 | |
| 237 | // Reaches reports whether main reaches a commit by any path: whether the |
| 238 | // commit is an ancestor of main, or main itself. That is ancestry, which |
| 239 | // is not landing (OnMain). |
| 240 | func (s *Snapshot) Reaches(oid string) bool { |
| 241 | if s.Tip == "" || oid == "" { |
| 242 | return false |
| 243 | } |
| 244 | r, seen := s.reachesOf[oid] |
| 245 | if !seen { |
| 246 | r = s.g.succeeds(s.ctx, true, "merge-base", "--is-ancestor", oid, s.Tip) |
| 247 | s.reachesOf[oid] = r |
| 248 | } |
| 249 | return r |
| 250 | } |
| 251 | |
| 252 | // Request is the ref of the integration request origin holds for a |
| 253 | // version: one filed under either of its names, at its head. |
| 254 | func (s *Snapshot) Request(id string, v change.Version) (ref string, ok bool) { |
| 255 | for _, name := range RequestNames(id, v) { |
| 256 | if ref := RequestPrefix + name; s.Refs[ref] == HeadOf(v) { |
| 257 | return ref, true |
| 258 | } |
| 259 | } |
| 260 | return "", false |
| 261 | } |
| 262 | |
| 263 | // OutcomeOf is the outcome record origin holds under a request name, as |
| 264 | // ParseOutcome reads it, and whether origin holds one. |
| 265 | func (s *Snapshot) OutcomeOf(name string) (Outcome, bool) { |
| 266 | m, ok := s.outcomes[OutcomePrefix+name] |
| 267 | return m, ok |
| 268 | } |
| 269 | |
| 270 | // Outcome is the integrator's last decision about a version: the outcome |
| 271 | // record under either of its names whose head is the version's head. |
| 272 | func (s *Snapshot) Outcome(id string, v change.Version) (name string, m Outcome, found bool) { |
| 273 | for _, name := range RequestNames(id, v) { |
| 274 | if m, ok := s.outcomes[OutcomePrefix+name]; ok && m.Head == HeadOf(v) { |
| 275 | return name, m, true |
| 276 | } |
| 277 | } |
| 278 | return "", Outcome{}, false |
| 279 | } |
| 280 | |
| 281 | // Landing is the landing evidence a version has, and the commit on main it |
| 282 | // landed as, when it has. Each version is decided once per snapshot. |
| 283 | func (s *Snapshot) Landing(id string, v change.Version) (kind, commit string) { |
| 284 | key := fmt.Sprintf("%s %d", id, v.N) |
| 285 | if l, ok := s.landings[key]; ok { |
| 286 | return l[0], l[1] |
| 287 | } |
| 288 | kind, commit = s.decideLanding(id, v) |
| 289 | s.landings[key] = [2]string{kind, commit} |
| 290 | return kind, commit |
| 291 | } |
| 292 | |
| 293 | func (s *Snapshot) decideLanding(id string, v change.Version) (kind, commit string) { |
| 294 | head := HeadOf(v) |
| 295 | if s.OnMain(head) { |
| 296 | return Landed, head |
| 297 | } |
| 298 | if commit, ok := s.recordedLanding(id, v); ok { |
| 299 | return LandedRecorded, commit |
| 300 | } |
| 301 | if name, m, ok := s.Outcome(id, v); ok { |
| 302 | consumed := m.Decision == "land" && s.Refs[RequestPrefix+name] != head |
| 303 | if consumed || m.Decision == "failed" { |
| 304 | return LandingUncertain, "" |
| 305 | } |
| 306 | } |
| 307 | if s.Refs[LandingPrefix+head] != "" { |
| 308 | return LandingUncertain, "" |
| 309 | } |
| 310 | return NotLanded, "" |
| 311 | } |
| 312 | |
| 313 | // IsLanded reports whether a version has landed by the first two rules. |
| 314 | func (s *Snapshot) IsLanded(id string, v change.Version) bool { |
| 315 | kind, _ := s.Landing(id, v) |
| 316 | return kind == Landed || kind == LandedRecorded |
| 317 | } |
| 318 | |
| 319 | // Unlanded reports whether a change, in any state, has an active version |
| 320 | // that has not landed: the changes a stack's layers are among. |
| 321 | func (s *Snapshot) Unlanded(n Recorded) bool { |
| 322 | return !s.IsLanded(n.ID, n.Record.ActiveVersion()) |
| 323 | } |
| 324 | |
| 325 | // recordedLanding looks for a transfer statement that records the version |
| 326 | // landing as a commit on main, and verifies the first that matches. |
| 327 | func (s *Snapshot) recordedLanding(id string, v change.Version) (string, bool) { |
| 328 | names := map[string]bool{} |
| 329 | for _, n := range RequestNames(id, v) { |
| 330 | names[n] = true |
| 331 | } |
| 332 | head := HeadOf(v) |
| 333 | for _, t := range s.transferStatements() { |
| 334 | if !names[t.change] || !IsObjectID(t.landed) || !s.OnMain(t.landed) || !s.singleParent(t.landed) { |
| 335 | continue |
| 336 | } |
| 337 | if !s.boundTo(t, head) { |
| 338 | continue |
| 339 | } |
| 340 | digest, ok := s.digest(head) |
| 341 | if !ok { |
| 342 | s.unverified = true |
| 343 | continue |
| 344 | } |
| 345 | if t.base != digest || !s.mergesTo(head, t.landed) { |
| 346 | continue |
| 347 | } |
| 348 | if s.verified(t) { |
| 349 | return t.landed, true |
| 350 | } |
| 351 | } |
| 352 | return "", false |
| 353 | } |
| 354 | |
| 355 | // boundTo reports whether the integrator recorded landing head as the |
| 356 | // statement's commit, under the statement's request name. When origin |
| 357 | // holds a landing record for the head, that record alone answers. A |
| 358 | // landing made before landing records were written has none, and its |
| 359 | // outcome record answers instead. |
| 360 | func (s *Snapshot) boundTo(t transferStatement, head string) bool { |
| 361 | if at, found := s.landingRecord(head); found { |
| 362 | return at.read && at.Landed == t.landed && at.Request == t.change && at.Head == head |
| 363 | } |
| 364 | m, ok := s.outcomes[OutcomePrefix+t.change] |
| 365 | return ok && m.Decision == "land" && m.Head == head |
| 366 | } |
| 367 | |
| 368 | // landingAt is a landing record as a snapshot read it, and whether it |
| 369 | // read. |
| 370 | type landingAt struct { |
| 371 | change.Landing |
| 372 | read bool |
| 373 | } |
| 374 | |
| 375 | // landingRecord reads the landing record origin holds for head, once. |
| 376 | // found is false when origin holds none. A record that does not read is |
| 377 | // found, and says nothing. |
| 378 | func (s *Snapshot) landingRecord(head string) (landingAt, bool) { |
| 379 | oid := s.Refs[LandingPrefix+head] |
| 380 | if oid == "" { |
| 381 | return landingAt{}, false |
| 382 | } |
| 383 | if at, ok := s.landed[head]; ok { |
| 384 | return at, true |
| 385 | } |
| 386 | var at landingAt |
| 387 | if err := Fetch(s.ctx, s.g, oid); err != nil { |
| 388 | s.unverified = true |
| 389 | } else if data, err := s.g.output(s.ctx, true, "cat-file", "blob", oid+":"+change.LandingFile); err == nil { |
| 390 | if l, err := change.ParseLanding([]byte(data + "\n")); err == nil { |
| 391 | at = landingAt{Landing: l, read: true} |
| 392 | } |
| 393 | } |
| 394 | s.landed[head] = at |
| 395 | return at, true |
| 396 | } |
| 397 | |
| 398 | // singleParent reports whether a commit has exactly one parent, as every |
| 399 | // commit the integrator lands as does. |
| 400 | func (s *Snapshot) singleParent(oid string) bool { |
| 401 | out, err := s.g.output(s.ctx, true, "rev-list", "--parents", "-n", "1", oid) |
| 402 | return err == nil && len(strings.Fields(out)) == 2 |
| 403 | } |
| 404 | |
| 405 | // mergesTo reports whether the integrator's merge of head onto landed's |
| 406 | // parent gives landed's tree. It is the merge integrator/git.go's |
| 407 | // mergeDelta makes: merge-tree onto the tip, with the merge base of the |
| 408 | // tip and the head as the base. A head that merges to another tree, or |
| 409 | // not at all, is not what landed. |
| 410 | func (s *Snapshot) mergesTo(head, landed string) bool { |
| 411 | tip, err := s.g.output(s.ctx, true, "rev-parse", "--verify", "--quiet", landed+"^1^{commit}") |
| 412 | if err != nil { |
| 413 | return false |
| 414 | } |
| 415 | want, err := s.g.output(s.ctx, true, "rev-parse", "--verify", "--quiet", landed+"^{tree}") |
| 416 | if err != nil { |
| 417 | return false |
| 418 | } |
| 419 | base, err := s.g.output(s.ctx, true, "merge-base", tip, head) |
| 420 | if err != nil { |
| 421 | return false |
| 422 | } |
| 423 | out, err := s.g.output(s.ctx, true, "merge-tree", "--write-tree", "--merge-base="+base, tip, head) |
| 424 | if err != nil { |
| 425 | return false |
| 426 | } |
| 427 | tree, _, _ := strings.Cut(out, "\n") |
| 428 | return strings.TrimSpace(tree) == want |
| 429 | } |
| 430 | |
| 431 | // transferStatements reads every transfer statement under origin's |
| 432 | // attestation refs, once. A transfer statement is a note at |
| 433 | // change.TransferPath of a landed commit, under the transfer.d directory |
| 434 | // at the top of an attestation ref's tree, or a single transfer.note at |
| 435 | // the top of the tree, as a ref written before statements were kept per |
| 436 | // landing holds it. Anything unreadable is skipped: what is not read is |
| 437 | // not believed. |
| 438 | func (s *Snapshot) transferStatements() []transferStatement { |
| 439 | if s.read { |
| 440 | return s.transfers |
| 441 | } |
| 442 | s.read = true |
| 443 | var trees []string |
| 444 | for ref, oid := range s.Refs { |
| 445 | if strings.HasPrefix(ref, AttestationRefs) { |
| 446 | trees = append(trees, oid) |
| 447 | } |
| 448 | } |
| 449 | if len(trees) == 0 { |
| 450 | return nil |
| 451 | } |
| 452 | sort.Strings(trees) |
| 453 | if err := Fetch(s.ctx, s.g, trees...); err != nil { |
| 454 | s.unverified = true |
| 455 | return nil |
| 456 | } |
| 457 | var query bytes.Buffer |
| 458 | for _, tree := range trees { |
| 459 | fmt.Fprintf(&query, "%s:%s\n%s:%s\n", tree, change.TransferNote, tree, change.TransferDir) |
| 460 | } |
| 461 | out, err := s.g.input(s.ctx, true, &query, "cat-file", "--batch-check=%(objectname) %(objecttype)") |
| 462 | if err != nil { |
| 463 | s.unverified = true |
| 464 | return nil |
| 465 | } |
| 466 | var blobs []string |
| 467 | for _, line := range strings.Split(out, "\n") { |
| 468 | oid, kind, _ := strings.Cut(line, " ") |
| 469 | switch kind { |
| 470 | case "blob": |
| 471 | blobs = append(blobs, oid) |
| 472 | case "tree": |
| 473 | listing, err := s.g.output(s.ctx, true, "ls-tree", "-r", "-z", oid) |
| 474 | if err != nil { |
| 475 | s.unverified = true |
| 476 | continue |
| 477 | } |
| 478 | for _, entry := range strings.Split(listing, "\x00") { |
| 479 | meta, path, ok := strings.Cut(entry, "\t") |
| 480 | if fields := strings.Fields(meta); ok && len(fields) == 3 && fields[1] == "blob" && isTransferPath(path) { |
| 481 | blobs = append(blobs, fields[2]) |
| 482 | } |
| 483 | } |
| 484 | } |
| 485 | } |
| 486 | for _, oid := range blobs { |
| 487 | note, err := s.g.output(s.ctx, true, "cat-file", "blob", oid) |
| 488 | if err != nil { |
| 489 | continue |
| 490 | } |
| 491 | note += "\n" |
| 492 | text, ok := noteText(note) |
| 493 | if !ok || statementValue(text, "predicateType") != predicateTransfer { |
| 494 | continue |
| 495 | } |
| 496 | s.transfers = append(s.transfers, transferStatement{ |
| 497 | note: []byte(note), |
| 498 | change: statementValue(text, "predicate.change"), |
| 499 | base: statementValue(text, "predicate.base.valley-tree-v1"), |
| 500 | landed: statementValue(text, "subject.digest.git-sha1"), |
| 501 | }) |
| 502 | } |
| 503 | return s.transfers |
| 504 | } |
| 505 | |
| 506 | // isTransferPath reports whether a path within the transfer.d directory is |
| 507 | // where change.TransferPath puts the statement for some commit: |
| 508 | // <2 hex>/<2 hex>/<rest>.note, the three parts a full object ID. |
| 509 | func isTransferPath(path string) bool { |
| 510 | parts := strings.Split(strings.TrimSuffix(path, ".note"), "/") |
| 511 | if len(parts) != 3 || len(parts[0]) != 2 || len(parts[1]) != 2 || !strings.HasSuffix(path, ".note") { |
| 512 | return false |
| 513 | } |
| 514 | return IsObjectID(parts[0] + parts[1] + parts[2]) |
| 515 | } |
| 516 | |
| 517 | // noteText is the statement text of a note: everything before the last |
| 518 | // blank line, which separates the text from its signatures. |
| 519 | func noteText(note string) (string, bool) { |
| 520 | split := strings.LastIndex(note, "\n\n") |
| 521 | if split < 0 { |
| 522 | return "", false |
| 523 | } |
| 524 | return note[:split+1], true |
| 525 | } |
| 526 | |
| 527 | // statementValue reads one key's value from statement text, where each |
| 528 | // line is a key, one space and a value. It is a lookup, and it decides |
| 529 | // nothing: what is read here only chooses which statement to verify. |
| 530 | func statementValue(text, key string) string { |
| 531 | for _, line := range strings.Split(text, "\n") { |
| 532 | if k, v, ok := strings.Cut(line, " "); ok && k == key { |
| 533 | return v |
| 534 | } |
| 535 | } |
| 536 | return "" |
| 537 | } |
| 538 | |
| 539 | // repoDir is the directory attest is pointed at. |
| 540 | func (s *Snapshot) repoDir() string { |
| 541 | if s.g.Dir == "" { |
| 542 | return "." |
| 543 | } |
| 544 | return s.g.Dir |
| 545 | } |
| 546 | |
| 547 | // digest is the valley-tree-v1 digest of a commit's tree, from attest, |
| 548 | // which defines the scheme. |
| 549 | func (s *Snapshot) digest(oid string) (string, bool) { |
| 550 | if d, ok := s.digestOf[oid]; ok { |
| 551 | return d, d != "" |
| 552 | } |
| 553 | out, err := s.g.tool(s.ctx, s.verify.Attest, "digest", "--repo", s.repoDir(), "--rev", oid).Output() |
| 554 | d := "" |
| 555 | if scheme, value, ok := strings.Cut(strings.TrimSpace(string(out)), ":"); finished(err) == nil && ok && scheme == "valley-tree-v1" { |
| 556 | d = value |
| 557 | } |
| 558 | s.digestOf[oid] = d |
| 559 | return d, d != "" |
| 560 | } |
| 561 | |
| 562 | // verified runs attest verify over a transfer statement: its signature |
| 563 | // must be the integrator's, by a key the known-signers file holds, and |
| 564 | // its subject must be the tree of the commit it says landed. A name the |
| 565 | // signer projects file restricts is not the integrator's, so nothing |
| 566 | // signed under it is verified. |
| 567 | func (s *Snapshot) verified(t transferStatement) bool { |
| 568 | if s.verify.Resolve != nil { |
| 569 | s.verify.Keys, s.verify.Signer, s.verify.SignerProjects = s.verify.Resolve() |
| 570 | s.verify.Resolve = nil |
| 571 | } |
| 572 | if _, worker := s.verify.SignerProjects[s.verify.Signer]; worker || s.verify.Keys == "" || s.verify.Signer == "" { |
| 573 | s.unverified = true |
| 574 | return false |
| 575 | } |
| 576 | file, err := os.CreateTemp("", "valley-transfer-*.note") |
| 577 | if err != nil { |
| 578 | s.unverified = true |
| 579 | return false |
| 580 | } |
| 581 | defer os.Remove(file.Name()) |
| 582 | _, err = file.Write(t.note) |
| 583 | if cerr := file.Close(); err != nil || cerr != nil { |
| 584 | s.unverified = true |
| 585 | return false |
| 586 | } |
| 587 | cmd := s.g.tool(s.ctx, s.verify.Attest, "verify", "--note", file.Name(), |
| 588 | "--known-keys", s.verify.Keys, "--signer", s.verify.Signer, |
| 589 | "--repo", s.repoDir(), "--rev", t.landed) |
| 590 | if err := finished(cmd.Run()); err != nil { |
| 591 | // attest refusing the note is an answer. Failing to run it is not. |
| 592 | var exit *exec.ExitError |
| 593 | if !errors.As(err, &exit) { |
| 594 | s.unverified = true |
| 595 | } |
| 596 | return false |
| 597 | } |
| 598 | return true |
| 599 | } |
| 600 | |
| 601 | // Reach is every commit reachable from oid that main does not reach. |
| 602 | func (s *Snapshot) Reach(oid string) map[string]bool { |
| 603 | if r, ok := s.reachOf[oid]; ok { |
| 604 | return r |
| 605 | } |
| 606 | args := []string{"rev-list", oid} |
| 607 | if s.Tip != "" { |
| 608 | args = append(args, "^"+s.Tip) |
| 609 | } |
| 610 | r := map[string]bool{} |
| 611 | if out, err := s.g.output(s.ctx, true, args...); err == nil { |
| 612 | for _, c := range strings.Fields(out) { |
| 613 | r[c] = true |
| 614 | } |
| 615 | } |
| 616 | s.reachOf[oid] = r |
| 617 | return r |
| 618 | } |
| 619 | |
| 620 | // StrictAncestor reports whether a is an ancestor of b, and not b itself. |
| 621 | // A commit main does not reach is an ancestor of b exactly when it is in |
| 622 | // Reach(b), so git is asked only about a commit main reaches. |
| 623 | func (s *Snapshot) StrictAncestor(a, b string) bool { |
| 624 | switch { |
| 625 | case a == b: |
| 626 | return false |
| 627 | case s.Reach(b)[a]: |
| 628 | return true |
| 629 | case s.Reaches(a): |
| 630 | return s.g.succeeds(s.ctx, true, "merge-base", "--is-ancestor", a, b) |
| 631 | } |
| 632 | return false |
| 633 | } |
| 634 | |
| 635 | // BranchHead is a branch on origin and the commit at its tip. |
| 636 | type BranchHead struct { |
| 637 | Branch, Head string |
| 638 | } |
| 639 | |
| 640 | // Unrecorded is every branch on origin, other than main, whose tip main |
| 641 | // does not reach, that is not the head of any change's active version, and |
| 642 | // that no landed version's head reaches, in the order of their names. |
| 643 | // |
| 644 | // A version lands as one new commit on main, so main does not reach the |
| 645 | // version's own commits, nor a branch below them. Their content is on |
| 646 | // main, so such a branch is no work waiting to land: it is left out, as |
| 647 | // the rebase engine cuts a landed version's commits from a family |
| 648 | // (LandedUnder). |
| 649 | func (s *Snapshot) Unrecorded() []BranchHead { |
| 650 | active := map[string]bool{} |
| 651 | for _, n := range s.Changes { |
| 652 | active[HeadOf(n.Record.ActiveVersion())] = true |
| 653 | } |
| 654 | var branches []BranchHead |
| 655 | for ref, oid := range s.Refs { |
| 656 | name, ok := strings.CutPrefix(ref, "refs/heads/") |
| 657 | if !ok || name == Target || active[oid] || s.Reaches(oid) || s.underLanded(oid) { |
| 658 | continue |
| 659 | } |
| 660 | branches = append(branches, BranchHead{Branch: name, Head: oid}) |
| 661 | } |
| 662 | sort.Slice(branches, func(i, j int) bool { return branches[i].Branch < branches[j].Branch }) |
| 663 | return branches |
| 664 | } |
| 665 | |
| 666 | // underLanded reports whether a landed version's head is oid or reaches |
| 667 | // it. Each version's landing is decided once per snapshot, before |
| 668 | // anything is walked, and only a landed version's head has its reach |
| 669 | // read, once per snapshot. |
| 670 | func (s *Snapshot) underLanded(oid string) bool { |
| 671 | for _, n := range s.Changes { |
| 672 | for _, v := range n.Record.Versions { |
| 673 | if h := HeadOf(v); s.IsLanded(n.ID, v) && (h == oid || s.Reach(h)[oid]) { |
| 674 | return true |
| 675 | } |
| 676 | } |
| 677 | } |
| 678 | return false |
| 679 | } |
| 680 | |
| 681 | // LandedUnder is the head of every version that has landed, by the first |
| 682 | // two rules, and that head reaches while main does not, head itself |
| 683 | // included: work a landing took onto main as one new commit, which main |
| 684 | // does not reach. They are in the order of the changes' IDs and their |
| 685 | // versions. Each version's landing is decided first, and head's reach is |
| 686 | // read only once some version has landed. |
| 687 | func (s *Snapshot) LandedUnder(head string) []string { |
| 688 | var reach map[string]bool |
| 689 | var heads []string |
| 690 | for _, n := range s.Changes { |
| 691 | for _, v := range n.Record.Versions { |
| 692 | if !s.IsLanded(n.ID, v) { |
| 693 | continue |
| 694 | } |
| 695 | if reach == nil { |
| 696 | reach = s.Reach(head) |
| 697 | } |
| 698 | if h := HeadOf(v); reach[h] { |
| 699 | heads = append(heads, h) |
| 700 | } |
| 701 | } |
| 702 | } |
| 703 | return heads |
| 704 | } |
| 705 | |
| 706 | // Node is one head among those Nest stacks: a change's active head or a |
| 707 | // branch's tip, with the commit time it is ordered by and the name ties |
| 708 | // are broken by. |
| 709 | type Node struct { |
| 710 | Name string |
| 711 | Head string |
| 712 | When int64 |
| 713 | } |
| 714 | |
| 715 | // Nesting is how a set of nodes stack, by ancestry alone. Each slice is |
| 716 | // indexed as the nodes were given. |
| 717 | type Nesting struct { |
| 718 | // Parent is the node each is nested under: the nearest node whose head |
| 719 | // strictly contains its own, or -1 for none. Where two are equally |
| 720 | // near, the one with the later commit is taken. |
| 721 | Parent []int |
| 722 | // Base is the nearest node whose head its own head strictly contains, |
| 723 | // or -1 for main. Where two are equally near, the one nested under it |
| 724 | // is taken, and otherwise the one with the later commit. |
| 725 | Base []int |
| 726 | // Inside is every node whose head strictly contains its own, in the |
| 727 | // order given. |
| 728 | Inside [][]int |
| 729 | } |
| 730 | |
| 731 | // Nest stacks the nodes: valley pending nests its entries this way, and |
| 732 | // a stack's layers each sit on their base. |
| 733 | func (s *Snapshot) Nest(nodes []Node) Nesting { |
| 734 | n := len(nodes) |
| 735 | in := make([][]bool, n) // in[i][j]: node i's head strictly contains node j's |
| 736 | nest := Nesting{Parent: make([]int, n), Base: make([]int, n), Inside: make([][]int, n)} |
| 737 | for i := range nodes { |
| 738 | in[i] = make([]bool, n) |
| 739 | for j := range nodes { |
| 740 | in[i][j] = i != j && s.StrictAncestor(nodes[j].Head, nodes[i].Head) |
| 741 | if in[i][j] { |
| 742 | nest.Inside[j] = append(nest.Inside[j], i) |
| 743 | } |
| 744 | } |
| 745 | } |
| 746 | // nearest is the nodes i contains, or that contain i, with none |
| 747 | // between, the latest commit first. |
| 748 | nearest := func(i int, above bool) []int { |
| 749 | var near []int |
| 750 | for j := range nodes { |
| 751 | outer, inner := j, i |
| 752 | if !above { |
| 753 | outer, inner = i, j |
| 754 | } |
| 755 | if !in[outer][inner] { |
| 756 | continue |
| 757 | } |
| 758 | between := false |
| 759 | for k := range nodes { |
| 760 | between = between || in[outer][k] && in[k][inner] |
| 761 | } |
| 762 | if !between { |
| 763 | near = append(near, j) |
| 764 | } |
| 765 | } |
| 766 | sort.SliceStable(near, func(a, b int) bool { |
| 767 | x, y := nodes[near[a]], nodes[near[b]] |
| 768 | if x.When != y.When { |
| 769 | return x.When > y.When |
| 770 | } |
| 771 | return x.Name < y.Name |
| 772 | }) |
| 773 | return near |
| 774 | } |
| 775 | for i := range nodes { |
| 776 | nest.Parent[i] = -1 |
| 777 | if above := nearest(i, true); len(above) > 0 { |
| 778 | nest.Parent[i] = above[0] |
| 779 | } |
| 780 | } |
| 781 | for i := range nodes { |
| 782 | nest.Base[i] = -1 |
| 783 | below := nearest(i, false) |
| 784 | for _, d := range below { |
| 785 | if nest.Parent[d] == i { |
| 786 | nest.Base[i] = d |
| 787 | break |
| 788 | } |
| 789 | } |
| 790 | if nest.Base[i] == -1 && len(below) > 0 { |
| 791 | nest.Base[i] = below[0] |
| 792 | } |
| 793 | } |
| 794 | return nest |
| 795 | } |