| 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]int // main's first-parent history, down to its first merge, and each commit's number of parents; nil until read |
| 122 | reachesOf map[string]bool |
| 123 | reachOf map[string]reached |
| 124 | ancestorOf map[[2]string]bool // "is a an ancestor of b", as git answered it |
| 125 | history *history // every commit main reaches; nil until read, or when it does not read |
| 126 | historyRead bool |
| 127 | digestOf map[string]string |
| 128 | transfers []transferStatement |
| 129 | read bool // whether transfers has been read |
| 130 | |
| 131 | // unverified is set when a transfer statement named a version and |
| 132 | // could not be checked, so its landing may be stronger than shown. |
| 133 | unverified bool |
| 134 | } |
| 135 | |
| 136 | // snapshotTries is how many times ReadSnapshot lists origin and fetches |
| 137 | // what the listing names before it gives up. |
| 138 | const snapshotTries = 3 |
| 139 | |
| 140 | // ReadSnapshot lists origin once and fetches every object the snapshot |
| 141 | // reads: main, every branch tip, every record commit, every outcome and |
| 142 | // landing record, and every restack report. A record commit has its |
| 143 | // version heads as parents, so they come with it. |
| 144 | // |
| 145 | // A ref can move between the listing and the fetch. Origin may then no |
| 146 | // longer hold the object the listing named, as when main was rewritten and |
| 147 | // its old tip pruned, and the fetch fails. The read is then made again |
| 148 | // from a new listing, up to snapshotTries times in all, and a fetch that |
| 149 | // still fails is the snapshot's error. |
| 150 | func ReadSnapshot(ctx context.Context, g Git, v Verifier) (*Snapshot, error) { |
| 151 | for try := 1; ; try++ { |
| 152 | o, err := ListOrigin(ctx, g, InventoryPatterns...) |
| 153 | if err != nil { |
| 154 | return nil, err |
| 155 | } |
| 156 | s, err := fetchSnapshot(ctx, g, o, v) |
| 157 | if err == nil || try == snapshotTries || ctx.Err() != nil { |
| 158 | return s, err |
| 159 | } |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | // fetchSnapshot fetches every object a snapshot of the listing reads, and |
| 164 | // reads every record it names. |
| 165 | func fetchSnapshot(ctx context.Context, g Git, o Origin, v Verifier) (*Snapshot, error) { |
| 166 | var want []string |
| 167 | for ref, oid := range o.Refs { |
| 168 | if strings.HasPrefix(ref, "refs/heads/") || strings.HasPrefix(ref, OutcomePrefix) || |
| 169 | strings.HasPrefix(ref, LandingPrefix) || strings.HasPrefix(ref, RestackPrefix) { |
| 170 | want = append(want, oid) |
| 171 | } |
| 172 | } |
| 173 | if err := Fetch(ctx, g, want...); err != nil { |
| 174 | return nil, err |
| 175 | } |
| 176 | all, unread, err := ReadAll(ctx, g, o) |
| 177 | if err != nil { |
| 178 | return nil, err |
| 179 | } |
| 180 | s := NewSnapshot(ctx, g, o, all, v) |
| 181 | s.Unread = unread |
| 182 | return s, nil |
| 183 | } |
| 184 | |
| 185 | // NewSnapshot is a snapshot of what o lists, with every record in all |
| 186 | // already read and every object it names already fetched. |
| 187 | func NewSnapshot(ctx context.Context, g Git, o Origin, all []Recorded, v Verifier) *Snapshot { |
| 188 | s := &Snapshot{ |
| 189 | Origin: o, Tip: o.Refs["refs/heads/"+Target], Changes: all, |
| 190 | ctx: ctx, g: g, verify: v, |
| 191 | outcomes: map[string]Outcome{}, landed: map[string]landingAt{}, reports: map[string]Report{}, landings: map[string][2]string{}, |
| 192 | reachesOf: map[string]bool{}, reachOf: map[string]reached{}, ancestorOf: map[[2]string]bool{}, digestOf: map[string]string{}, |
| 193 | } |
| 194 | var records []string |
| 195 | for ref, oid := range o.Refs { |
| 196 | if strings.HasPrefix(ref, OutcomePrefix) { |
| 197 | records = append(records, oid) |
| 198 | } |
| 199 | } |
| 200 | read := g.blobs(ctx, records) |
| 201 | for ref, oid := range o.Refs { |
| 202 | if strings.HasPrefix(ref, OutcomePrefix) { |
| 203 | data, ok := read[oid] |
| 204 | if !ok { |
| 205 | var err error |
| 206 | if data, err = g.output(ctx, true, "cat-file", "blob", oid); err != nil { |
| 207 | data = "" |
| 208 | } |
| 209 | } |
| 210 | s.outcomes[ref] = ParseOutcome(data) |
| 211 | } |
| 212 | } |
| 213 | return s |
| 214 | } |
| 215 | |
| 216 | // Unverified reports whether a transfer statement named a version and |
| 217 | // could not be checked, so that version's landing may be stronger than |
| 218 | // the snapshot says. |
| 219 | func (s *Snapshot) Unverified() bool { return s.unverified } |
| 220 | |
| 221 | // OnMain reports whether a commit is on main's first-parent history (main |
| 222 | // itself, its first parent, that commit's first parent, and so on) with no |
| 223 | // merge between it and main's tip: whether |
| 224 | // `git rev-list --first-parent --merges <oid>..main` would list nothing. |
| 225 | // This is where a landing puts a change. The history is read down to its |
| 226 | // first merge, which is itself the last commit counted, since no merge is |
| 227 | // above it. |
| 228 | func (s *Snapshot) OnMain(oid string) bool { |
| 229 | if s.Tip == "" || oid == "" { |
| 230 | return false |
| 231 | } |
| 232 | if s.firstParent == nil { |
| 233 | s.firstParent = map[string]int{} |
| 234 | if out, err := s.g.output(s.ctx, true, "rev-list", "--first-parent", "--parents", s.Tip); err == nil { |
| 235 | for _, line := range strings.Split(out, "\n") { |
| 236 | fields := strings.Fields(line) |
| 237 | if len(fields) == 0 { |
| 238 | continue |
| 239 | } |
| 240 | s.firstParent[fields[0]] = len(fields) - 1 |
| 241 | if len(fields) > 2 { |
| 242 | break |
| 243 | } |
| 244 | } |
| 245 | } |
| 246 | } |
| 247 | _, on := s.firstParent[oid] |
| 248 | return on |
| 249 | } |
| 250 | |
| 251 | // Reaches reports whether main reaches a commit by any path: whether the |
| 252 | // commit is an ancestor of main, or main itself. That is ancestry, which |
| 253 | // is not landing (OnMain). |
| 254 | // |
| 255 | // Main's history answers, once read. A commit outside it that Reach lists |
| 256 | // is one main does not reach. Anything else, such as a name that is not a |
| 257 | // commit's ID, git answers, once. |
| 258 | func (s *Snapshot) Reaches(oid string) bool { |
| 259 | if s.Tip == "" || oid == "" { |
| 260 | return false |
| 261 | } |
| 262 | if h := s.mainHistory(); h != nil { |
| 263 | if h.has(oid) { |
| 264 | return true |
| 265 | } |
| 266 | if s.Reach(oid)[oid] { |
| 267 | return false |
| 268 | } |
| 269 | } |
| 270 | r, seen := s.reachesOf[oid] |
| 271 | if !seen { |
| 272 | r = s.g.succeeds(s.ctx, true, "merge-base", "--is-ancestor", oid, s.Tip) |
| 273 | s.reachesOf[oid] = r |
| 274 | } |
| 275 | return r |
| 276 | } |
| 277 | |
| 278 | // Request is the ref of the integration request origin holds for a |
| 279 | // version: one filed under either of its names, at its head. |
| 280 | func (s *Snapshot) Request(id string, v change.Version) (ref string, ok bool) { |
| 281 | for _, name := range RequestNames(id, v) { |
| 282 | if ref := RequestPrefix + name; s.Refs[ref] == HeadOf(v) { |
| 283 | return ref, true |
| 284 | } |
| 285 | } |
| 286 | return "", false |
| 287 | } |
| 288 | |
| 289 | // OutcomeOf is the outcome record origin holds under a request name, as |
| 290 | // ParseOutcome reads it, and whether origin holds one. |
| 291 | func (s *Snapshot) OutcomeOf(name string) (Outcome, bool) { |
| 292 | m, ok := s.outcomes[OutcomePrefix+name] |
| 293 | return m, ok |
| 294 | } |
| 295 | |
| 296 | // Outcome is the integrator's last decision about a version: the outcome |
| 297 | // record under either of its names whose head is the version's head. |
| 298 | func (s *Snapshot) Outcome(id string, v change.Version) (name string, m Outcome, found bool) { |
| 299 | for _, name := range RequestNames(id, v) { |
| 300 | if m, ok := s.outcomes[OutcomePrefix+name]; ok && m.Head == HeadOf(v) { |
| 301 | return name, m, true |
| 302 | } |
| 303 | } |
| 304 | return "", Outcome{}, false |
| 305 | } |
| 306 | |
| 307 | // Landing is the landing evidence a version has, and the commit on main it |
| 308 | // landed as, when it has. Each version is decided once per snapshot. |
| 309 | func (s *Snapshot) Landing(id string, v change.Version) (kind, commit string) { |
| 310 | key := fmt.Sprintf("%s %d", id, v.N) |
| 311 | if l, ok := s.landings[key]; ok { |
| 312 | return l[0], l[1] |
| 313 | } |
| 314 | kind, commit = s.decideLanding(id, v) |
| 315 | s.landings[key] = [2]string{kind, commit} |
| 316 | return kind, commit |
| 317 | } |
| 318 | |
| 319 | func (s *Snapshot) decideLanding(id string, v change.Version) (kind, commit string) { |
| 320 | head := HeadOf(v) |
| 321 | if s.OnMain(head) { |
| 322 | return Landed, head |
| 323 | } |
| 324 | if commit, ok := s.recordedLanding(id, v); ok { |
| 325 | return LandedRecorded, commit |
| 326 | } |
| 327 | if name, m, ok := s.Outcome(id, v); ok { |
| 328 | consumed := m.Decision == "land" && s.Refs[RequestPrefix+name] != head |
| 329 | if consumed || m.Decision == "failed" { |
| 330 | return LandingUncertain, "" |
| 331 | } |
| 332 | } |
| 333 | if s.Refs[LandingPrefix+head] != "" { |
| 334 | return LandingUncertain, "" |
| 335 | } |
| 336 | return NotLanded, "" |
| 337 | } |
| 338 | |
| 339 | // IsLanded reports whether a version has landed by the first two rules. |
| 340 | func (s *Snapshot) IsLanded(id string, v change.Version) bool { |
| 341 | kind, _ := s.Landing(id, v) |
| 342 | return kind == Landed || kind == LandedRecorded |
| 343 | } |
| 344 | |
| 345 | // LandedAs is the version a change landed as, and whether it has: the |
| 346 | // newest of its versions that landed by the first two rules. A change |
| 347 | // counts as landed once any of its versions has, so a version restacked |
| 348 | // while an earlier one was approved, and still active when that one |
| 349 | // landed, is no work left to land. An active version whose head reaches |
| 350 | // that landing was made on a main that holds it, and is the change's next |
| 351 | // round: the change reads unlanded until that version lands. |
| 352 | func (s *Snapshot) LandedAs(n Recorded) (change.Version, bool) { |
| 353 | active := n.Record.ActiveVersion() |
| 354 | for i := len(n.Record.Versions) - 1; i >= 0; i-- { |
| 355 | v := n.Record.Versions[i] |
| 356 | kind, commit := s.Landing(n.ID, v) |
| 357 | if kind != Landed && kind != LandedRecorded { |
| 358 | continue |
| 359 | } |
| 360 | if v.N != active.N && s.StrictAncestor(commit, HeadOf(active)) { |
| 361 | return change.Version{}, false |
| 362 | } |
| 363 | return v, true |
| 364 | } |
| 365 | return change.Version{}, false |
| 366 | } |
| 367 | |
| 368 | // Unlanded reports whether a change, in any state, has not landed as any |
| 369 | // of its versions (LandedAs): the changes a stack's layers are among. |
| 370 | func (s *Snapshot) Unlanded(n Recorded) bool { |
| 371 | _, landed := s.LandedAs(n) |
| 372 | return !landed |
| 373 | } |
| 374 | |
| 375 | // recordedLanding looks for a transfer statement that records the version |
| 376 | // landing as a commit on main, and verifies the first that matches. |
| 377 | func (s *Snapshot) recordedLanding(id string, v change.Version) (string, bool) { |
| 378 | names := map[string]bool{} |
| 379 | for _, n := range RequestNames(id, v) { |
| 380 | names[n] = true |
| 381 | } |
| 382 | head := HeadOf(v) |
| 383 | for _, t := range s.transferStatements() { |
| 384 | if !names[t.change] || !IsObjectID(t.landed) || !s.OnMain(t.landed) || !s.singleParent(t.landed) { |
| 385 | continue |
| 386 | } |
| 387 | if !s.boundTo(t, head) { |
| 388 | continue |
| 389 | } |
| 390 | digest, ok := s.digest(head) |
| 391 | if !ok { |
| 392 | s.unverified = true |
| 393 | continue |
| 394 | } |
| 395 | if t.base != digest || !s.mergesTo(head, t.landed) { |
| 396 | continue |
| 397 | } |
| 398 | if s.verified(t) { |
| 399 | return t.landed, true |
| 400 | } |
| 401 | } |
| 402 | return "", false |
| 403 | } |
| 404 | |
| 405 | // boundTo reports whether the integrator recorded landing head as the |
| 406 | // statement's commit, under the statement's request name. When origin |
| 407 | // holds a landing record for the head, that record alone answers. A |
| 408 | // landing made before landing records were written has none, and its |
| 409 | // outcome record answers instead. |
| 410 | func (s *Snapshot) boundTo(t transferStatement, head string) bool { |
| 411 | if at, found := s.landingRecord(head); found { |
| 412 | return at.read && at.Landed == t.landed && at.Request == t.change && at.Head == head |
| 413 | } |
| 414 | m, ok := s.outcomes[OutcomePrefix+t.change] |
| 415 | return ok && m.Decision == "land" && m.Head == head |
| 416 | } |
| 417 | |
| 418 | // landingAt is a landing record as a snapshot read it, and whether it |
| 419 | // read. |
| 420 | type landingAt struct { |
| 421 | change.Landing |
| 422 | read bool |
| 423 | } |
| 424 | |
| 425 | // landingRecord reads the landing record origin holds for head, once. |
| 426 | // found is false when origin holds none. A record that does not read is |
| 427 | // found, and says nothing. |
| 428 | func (s *Snapshot) landingRecord(head string) (landingAt, bool) { |
| 429 | oid := s.Refs[LandingPrefix+head] |
| 430 | if oid == "" { |
| 431 | return landingAt{}, false |
| 432 | } |
| 433 | if at, ok := s.landed[head]; ok { |
| 434 | return at, true |
| 435 | } |
| 436 | var at landingAt |
| 437 | if err := Fetch(s.ctx, s.g, oid); err != nil { |
| 438 | s.unverified = true |
| 439 | } else if data, err := s.g.output(s.ctx, true, "cat-file", "blob", oid+":"+change.LandingFile); err == nil { |
| 440 | if l, err := change.ParseLanding([]byte(data + "\n")); err == nil { |
| 441 | at = landingAt{Landing: l, read: true} |
| 442 | } |
| 443 | } |
| 444 | s.landed[head] = at |
| 445 | return at, true |
| 446 | } |
| 447 | |
| 448 | // singleParent reports whether a commit has exactly one parent, as every |
| 449 | // commit the integrator lands as does. For a commit on main (OnMain), the |
| 450 | // read of main's first-parent history has its parents. |
| 451 | func (s *Snapshot) singleParent(oid string) bool { |
| 452 | if n, on := s.firstParent[oid]; on { |
| 453 | return n == 1 |
| 454 | } |
| 455 | out, err := s.g.output(s.ctx, true, "rev-list", "--parents", "-n", "1", oid) |
| 456 | return err == nil && len(strings.Fields(out)) == 2 |
| 457 | } |
| 458 | |
| 459 | // mergesTo reports whether the integrator's merge of head onto landed's |
| 460 | // parent gives landed's tree. It is the merge integrator/git.go's |
| 461 | // mergeDelta makes: merge-tree onto the tip, with the merge base of the |
| 462 | // tip and the head as the base. A head that merges to another tree, or |
| 463 | // not at all, is not what landed. |
| 464 | func (s *Snapshot) mergesTo(head, landed string) bool { |
| 465 | tip, err := s.g.output(s.ctx, true, "rev-parse", "--verify", "--quiet", landed+"^1^{commit}") |
| 466 | if err != nil { |
| 467 | return false |
| 468 | } |
| 469 | want, err := s.g.output(s.ctx, true, "rev-parse", "--verify", "--quiet", landed+"^{tree}") |
| 470 | if err != nil { |
| 471 | return false |
| 472 | } |
| 473 | base, err := s.g.output(s.ctx, true, "merge-base", tip, head) |
| 474 | if err != nil { |
| 475 | return false |
| 476 | } |
| 477 | out, err := s.g.output(s.ctx, true, "merge-tree", "--write-tree", "--merge-base="+base, tip, head) |
| 478 | if err != nil { |
| 479 | return false |
| 480 | } |
| 481 | tree, _, _ := strings.Cut(out, "\n") |
| 482 | return strings.TrimSpace(tree) == want |
| 483 | } |
| 484 | |
| 485 | // transferStatements reads every transfer statement under origin's |
| 486 | // attestation refs, once. A transfer statement is a note at |
| 487 | // change.TransferPath of a landed commit, under the transfer.d directory |
| 488 | // at the top of an attestation ref's tree, or a single transfer.note at |
| 489 | // the top of the tree, as a ref written before statements were kept per |
| 490 | // landing holds it. Anything unreadable is skipped: what is not read is |
| 491 | // not believed. |
| 492 | func (s *Snapshot) transferStatements() []transferStatement { |
| 493 | if s.read { |
| 494 | return s.transfers |
| 495 | } |
| 496 | s.read = true |
| 497 | var trees []string |
| 498 | for ref, oid := range s.Refs { |
| 499 | if strings.HasPrefix(ref, AttestationRefs) { |
| 500 | trees = append(trees, oid) |
| 501 | } |
| 502 | } |
| 503 | if len(trees) == 0 { |
| 504 | return nil |
| 505 | } |
| 506 | sort.Strings(trees) |
| 507 | if err := Fetch(s.ctx, s.g, trees...); err != nil { |
| 508 | s.unverified = true |
| 509 | return nil |
| 510 | } |
| 511 | var query bytes.Buffer |
| 512 | for _, tree := range trees { |
| 513 | fmt.Fprintf(&query, "%s:%s\n%s:%s\n", tree, change.TransferNote, tree, change.TransferDir) |
| 514 | } |
| 515 | out, err := s.g.input(s.ctx, true, &query, "cat-file", "--batch-check=%(objectname) %(objecttype)") |
| 516 | if err != nil { |
| 517 | s.unverified = true |
| 518 | return nil |
| 519 | } |
| 520 | var blobs []string |
| 521 | for _, line := range strings.Split(out, "\n") { |
| 522 | oid, kind, _ := strings.Cut(line, " ") |
| 523 | switch kind { |
| 524 | case "blob": |
| 525 | blobs = append(blobs, oid) |
| 526 | case "tree": |
| 527 | listing, err := s.g.output(s.ctx, true, "ls-tree", "-r", "-z", oid) |
| 528 | if err != nil { |
| 529 | s.unverified = true |
| 530 | continue |
| 531 | } |
| 532 | for _, entry := range strings.Split(listing, "\x00") { |
| 533 | meta, path, ok := strings.Cut(entry, "\t") |
| 534 | if fields := strings.Fields(meta); ok && len(fields) == 3 && fields[1] == "blob" && isTransferPath(path) { |
| 535 | blobs = append(blobs, fields[2]) |
| 536 | } |
| 537 | } |
| 538 | } |
| 539 | } |
| 540 | read := s.g.blobs(s.ctx, blobs) |
| 541 | for _, oid := range blobs { |
| 542 | note, ok := read[oid] |
| 543 | if !ok { |
| 544 | var err error |
| 545 | if note, err = s.g.output(s.ctx, true, "cat-file", "blob", oid); err != nil { |
| 546 | continue |
| 547 | } |
| 548 | } |
| 549 | note += "\n" |
| 550 | text, ok := noteText(note) |
| 551 | if !ok || statementValue(text, "predicateType") != predicateTransfer { |
| 552 | continue |
| 553 | } |
| 554 | s.transfers = append(s.transfers, transferStatement{ |
| 555 | note: []byte(note), |
| 556 | change: statementValue(text, "predicate.change"), |
| 557 | base: statementValue(text, "predicate.base.valley-tree-v1"), |
| 558 | landed: statementValue(text, "subject.digest.git-sha1"), |
| 559 | }) |
| 560 | } |
| 561 | return s.transfers |
| 562 | } |
| 563 | |
| 564 | // isTransferPath reports whether a path within the transfer.d directory is |
| 565 | // where change.TransferPath puts the statement for some commit: |
| 566 | // <2 hex>/<2 hex>/<rest>.note, the three parts a full object ID. |
| 567 | func isTransferPath(path string) bool { |
| 568 | parts := strings.Split(strings.TrimSuffix(path, ".note"), "/") |
| 569 | if len(parts) != 3 || len(parts[0]) != 2 || len(parts[1]) != 2 || !strings.HasSuffix(path, ".note") { |
| 570 | return false |
| 571 | } |
| 572 | return IsObjectID(parts[0] + parts[1] + parts[2]) |
| 573 | } |
| 574 | |
| 575 | // noteText is the statement text of a note: everything before the last |
| 576 | // blank line, which separates the text from its signatures. |
| 577 | func noteText(note string) (string, bool) { |
| 578 | split := strings.LastIndex(note, "\n\n") |
| 579 | if split < 0 { |
| 580 | return "", false |
| 581 | } |
| 582 | return note[:split+1], true |
| 583 | } |
| 584 | |
| 585 | // statementValue reads one key's value from statement text, where each |
| 586 | // line is a key, one space and a value. It is a lookup, and it decides |
| 587 | // nothing: what is read here only chooses which statement to verify. |
| 588 | func statementValue(text, key string) string { |
| 589 | for _, line := range strings.Split(text, "\n") { |
| 590 | if k, v, ok := strings.Cut(line, " "); ok && k == key { |
| 591 | return v |
| 592 | } |
| 593 | } |
| 594 | return "" |
| 595 | } |
| 596 | |
| 597 | // repoDir is the directory attest is pointed at. |
| 598 | func (s *Snapshot) repoDir() string { |
| 599 | if s.g.Dir == "" { |
| 600 | return "." |
| 601 | } |
| 602 | return s.g.Dir |
| 603 | } |
| 604 | |
| 605 | // digest is the valley-tree-v1 digest of a commit's tree, from attest, |
| 606 | // which defines the scheme. |
| 607 | func (s *Snapshot) digest(oid string) (string, bool) { |
| 608 | if d, ok := s.digestOf[oid]; ok { |
| 609 | return d, d != "" |
| 610 | } |
| 611 | out, err := s.g.tool(s.ctx, s.verify.Attest, "digest", "--repo", s.repoDir(), "--rev", oid).Output() |
| 612 | d := "" |
| 613 | if scheme, value, ok := strings.Cut(strings.TrimSpace(string(out)), ":"); finished(err) == nil && ok && scheme == "valley-tree-v1" { |
| 614 | d = value |
| 615 | } |
| 616 | s.digestOf[oid] = d |
| 617 | return d, d != "" |
| 618 | } |
| 619 | |
| 620 | // verified runs attest verify over a transfer statement: its signature |
| 621 | // must be the integrator's, by a key the known-signers file holds, and |
| 622 | // its subject must be the tree of the commit it says landed. A name the |
| 623 | // signer projects file restricts is not the integrator's, so nothing |
| 624 | // signed under it is verified. |
| 625 | func (s *Snapshot) verified(t transferStatement) bool { |
| 626 | if s.verify.Resolve != nil { |
| 627 | s.verify.Keys, s.verify.Signer, s.verify.SignerProjects = s.verify.Resolve() |
| 628 | s.verify.Resolve = nil |
| 629 | } |
| 630 | if _, worker := s.verify.SignerProjects[s.verify.Signer]; worker || s.verify.Keys == "" || s.verify.Signer == "" { |
| 631 | s.unverified = true |
| 632 | return false |
| 633 | } |
| 634 | file, err := os.CreateTemp("", "valley-transfer-*.note") |
| 635 | if err != nil { |
| 636 | s.unverified = true |
| 637 | return false |
| 638 | } |
| 639 | defer os.Remove(file.Name()) |
| 640 | _, err = file.Write(t.note) |
| 641 | if cerr := file.Close(); err != nil || cerr != nil { |
| 642 | s.unverified = true |
| 643 | return false |
| 644 | } |
| 645 | cmd := s.g.tool(s.ctx, s.verify.Attest, "verify", "--note", file.Name(), |
| 646 | "--known-keys", s.verify.Keys, "--signer", s.verify.Signer, |
| 647 | "--repo", s.repoDir(), "--rev", t.landed) |
| 648 | if err := finished(cmd.Run()); err != nil { |
| 649 | // attest refusing the note is an answer. Failing to run it is not. |
| 650 | var exit *exec.ExitError |
| 651 | if !errors.As(err, &exit) { |
| 652 | s.unverified = true |
| 653 | } |
| 654 | return false |
| 655 | } |
| 656 | return true |
| 657 | } |
| 658 | |
| 659 | // reached is what a commit reaches, as one rev-list read it. |
| 660 | type reached struct { |
| 661 | commits map[string]bool // every commit it reaches that main does not |
| 662 | // below is each commit main reaches that is a parent of one in |
| 663 | // commits: where its own history meets main's. |
| 664 | below []string |
| 665 | } |
| 666 | |
| 667 | // Reach is every commit reachable from oid that main does not reach. |
| 668 | func (s *Snapshot) Reach(oid string) map[string]bool { |
| 669 | return s.reached(oid).commits |
| 670 | } |
| 671 | |
| 672 | // reached reads what oid reaches, once. |
| 673 | func (s *Snapshot) reached(oid string) reached { |
| 674 | if r, ok := s.reachOf[oid]; ok { |
| 675 | return r |
| 676 | } |
| 677 | args := []string{"rev-list", "--parents", oid} |
| 678 | if s.Tip != "" { |
| 679 | args = append(args, "^"+s.Tip) |
| 680 | } |
| 681 | r := reached{commits: map[string]bool{}} |
| 682 | if out, err := s.g.output(s.ctx, true, args...); err == nil { |
| 683 | var parents []string |
| 684 | for _, line := range strings.Split(out, "\n") { |
| 685 | if fields := strings.Fields(line); len(fields) > 0 { |
| 686 | r.commits[fields[0]] = true |
| 687 | parents = append(parents, fields[1:]...) |
| 688 | } |
| 689 | } |
| 690 | seen := map[string]bool{} |
| 691 | for _, p := range parents { |
| 692 | if !r.commits[p] && !seen[p] { |
| 693 | seen[p] = true |
| 694 | r.below = append(r.below, p) |
| 695 | } |
| 696 | } |
| 697 | } |
| 698 | s.reachOf[oid] = r |
| 699 | return r |
| 700 | } |
| 701 | |
| 702 | // StrictAncestor reports whether a is an ancestor of b, and not b itself. |
| 703 | // A commit main does not reach is an ancestor of b exactly when it is in |
| 704 | // Reach(b). A commit main reaches is one exactly when b is a commit main |
| 705 | // reaches and has it in its history, or when a commit where b's own |
| 706 | // history meets main's has it. Main's history answers that. git is asked |
| 707 | // only when the snapshot cannot tell, and each pair once. |
| 708 | func (s *Snapshot) StrictAncestor(a, b string) bool { |
| 709 | switch { |
| 710 | case a == b: |
| 711 | return false |
| 712 | case s.Reach(b)[a]: |
| 713 | return true |
| 714 | case !s.Reaches(a): |
| 715 | return false |
| 716 | } |
| 717 | if r, known := s.mainAncestor(a, b); known { |
| 718 | return r |
| 719 | } |
| 720 | key := [2]string{a, b} |
| 721 | r, seen := s.ancestorOf[key] |
| 722 | if !seen { |
| 723 | r = s.g.succeeds(s.ctx, true, "merge-base", "--is-ancestor", a, b) |
| 724 | s.ancestorOf[key] = r |
| 725 | } |
| 726 | return r |
| 727 | } |
| 728 | |
| 729 | // mainAncestor answers, from main's history, whether a, a commit main |
| 730 | // reaches, is an ancestor of b. known is false when it cannot tell: when |
| 731 | // main's history did not read, or b is not a commit Reach(b) or main's |
| 732 | // history lists. |
| 733 | func (s *Snapshot) mainAncestor(a, b string) (r, known bool) { |
| 734 | h := s.mainHistory() |
| 735 | if h == nil || !h.has(a) { |
| 736 | return false, false |
| 737 | } |
| 738 | if h.has(b) { |
| 739 | return h.reaches(b, a), true |
| 740 | } |
| 741 | at := s.reached(b) |
| 742 | if !at.commits[b] { |
| 743 | return false, false |
| 744 | } |
| 745 | for _, c := range at.below { |
| 746 | if !h.has(c) { |
| 747 | return false, false |
| 748 | } |
| 749 | } |
| 750 | for _, c := range at.below { |
| 751 | if h.reaches(c, a) { |
| 752 | return true, true |
| 753 | } |
| 754 | } |
| 755 | return false, true |
| 756 | } |
| 757 | |
| 758 | // mainHistory is every commit main reaches, read once. It is nil when |
| 759 | // origin has no main, or its history does not read. |
| 760 | func (s *Snapshot) mainHistory() *history { |
| 761 | if !s.historyRead && s.Tip != "" { |
| 762 | s.historyRead = true |
| 763 | s.history, _ = readHistory(s.ctx, s.g, s.Tip) |
| 764 | } |
| 765 | return s.history |
| 766 | } |
| 767 | |
| 768 | // BranchHead is a branch on origin and the commit at its tip. |
| 769 | type BranchHead struct { |
| 770 | Branch, Head string |
| 771 | } |
| 772 | |
| 773 | // Unrecorded is every branch on origin, other than main, whose tip main |
| 774 | // does not reach, that is not the head of any change's active version, and |
| 775 | // that no landed version's head reaches, in the order of their names. |
| 776 | // |
| 777 | // A version lands as one new commit on main, so main does not reach the |
| 778 | // version's own commits, nor a branch below them. Their content is on |
| 779 | // main, so such a branch is no work waiting to land: it is left out, as |
| 780 | // the rebase engine cuts a landed version's commits from a family |
| 781 | // (LandedUnder). |
| 782 | func (s *Snapshot) Unrecorded() []BranchHead { |
| 783 | active := map[string]bool{} |
| 784 | for _, n := range s.Changes { |
| 785 | active[HeadOf(n.Record.ActiveVersion())] = true |
| 786 | } |
| 787 | var branches []BranchHead |
| 788 | for ref, oid := range s.Refs { |
| 789 | name, ok := strings.CutPrefix(ref, "refs/heads/") |
| 790 | if !ok || name == Target || active[oid] || s.Reaches(oid) || s.underLanded(oid) { |
| 791 | continue |
| 792 | } |
| 793 | branches = append(branches, BranchHead{Branch: name, Head: oid}) |
| 794 | } |
| 795 | sort.Slice(branches, func(i, j int) bool { return branches[i].Branch < branches[j].Branch }) |
| 796 | return branches |
| 797 | } |
| 798 | |
| 799 | // underLanded reports whether a landed version's head is oid or reaches |
| 800 | // it. Each version's landing is decided once per snapshot, before |
| 801 | // anything is walked, and only a landed version's head has its reach |
| 802 | // read, once per snapshot. |
| 803 | func (s *Snapshot) underLanded(oid string) bool { |
| 804 | for _, n := range s.Changes { |
| 805 | for _, v := range n.Record.Versions { |
| 806 | if h := HeadOf(v); s.IsLanded(n.ID, v) && (h == oid || s.Reach(h)[oid]) { |
| 807 | return true |
| 808 | } |
| 809 | } |
| 810 | } |
| 811 | return false |
| 812 | } |
| 813 | |
| 814 | // LandedUnder is the head of every version that has landed, by the first |
| 815 | // two rules, and that head reaches while main does not, head itself |
| 816 | // included: work a landing took onto main as one new commit, which main |
| 817 | // does not reach. They are in the order of the changes' IDs and their |
| 818 | // versions. Each version's landing is decided first, and head's reach is |
| 819 | // read only once some version has landed. |
| 820 | func (s *Snapshot) LandedUnder(head string) []string { |
| 821 | var reach map[string]bool |
| 822 | var heads []string |
| 823 | for _, n := range s.Changes { |
| 824 | for _, v := range n.Record.Versions { |
| 825 | if !s.IsLanded(n.ID, v) { |
| 826 | continue |
| 827 | } |
| 828 | if reach == nil { |
| 829 | reach = s.Reach(head) |
| 830 | } |
| 831 | if h := HeadOf(v); reach[h] { |
| 832 | heads = append(heads, h) |
| 833 | } |
| 834 | } |
| 835 | } |
| 836 | return heads |
| 837 | } |
| 838 | |
| 839 | // Node is one head among those Nest stacks: a change's active head or a |
| 840 | // branch's tip, with the commit time it is ordered by and the name ties |
| 841 | // are broken by. |
| 842 | type Node struct { |
| 843 | Name string |
| 844 | Head string |
| 845 | When int64 |
| 846 | } |
| 847 | |
| 848 | // Nesting is how a set of nodes stack, by ancestry alone. Each slice is |
| 849 | // indexed as the nodes were given. |
| 850 | type Nesting struct { |
| 851 | // Parent is the node each is nested under: the nearest node whose head |
| 852 | // strictly contains its own, or -1 for none. Where two are equally |
| 853 | // near, the one with the later commit is taken. |
| 854 | Parent []int |
| 855 | // Base is the nearest node whose head its own head strictly contains, |
| 856 | // or -1 for main. Where two are equally near, the one nested under it |
| 857 | // is taken, and otherwise the one with the later commit. |
| 858 | Base []int |
| 859 | // Inside is every node whose head strictly contains its own, in the |
| 860 | // order given. |
| 861 | Inside [][]int |
| 862 | } |
| 863 | |
| 864 | // Nest stacks the nodes: valley pending nests its entries this way, and |
| 865 | // a stack's layers each sit on their base. |
| 866 | func (s *Snapshot) Nest(nodes []Node) Nesting { |
| 867 | n := len(nodes) |
| 868 | in := make([][]bool, n) // in[i][j]: node i's head strictly contains node j's |
| 869 | nest := Nesting{Parent: make([]int, n), Base: make([]int, n), Inside: make([][]int, n)} |
| 870 | for i := range nodes { |
| 871 | in[i] = make([]bool, n) |
| 872 | for j := range nodes { |
| 873 | in[i][j] = i != j && s.StrictAncestor(nodes[j].Head, nodes[i].Head) |
| 874 | if in[i][j] { |
| 875 | nest.Inside[j] = append(nest.Inside[j], i) |
| 876 | } |
| 877 | } |
| 878 | } |
| 879 | // nearest is the nodes i contains, or that contain i, with none |
| 880 | // between, the latest commit first. |
| 881 | nearest := func(i int, above bool) []int { |
| 882 | var near []int |
| 883 | for j := range nodes { |
| 884 | outer, inner := j, i |
| 885 | if !above { |
| 886 | outer, inner = i, j |
| 887 | } |
| 888 | if !in[outer][inner] { |
| 889 | continue |
| 890 | } |
| 891 | between := false |
| 892 | for k := range nodes { |
| 893 | between = between || in[outer][k] && in[k][inner] |
| 894 | } |
| 895 | if !between { |
| 896 | near = append(near, j) |
| 897 | } |
| 898 | } |
| 899 | sort.SliceStable(near, func(a, b int) bool { |
| 900 | x, y := nodes[near[a]], nodes[near[b]] |
| 901 | if x.When != y.When { |
| 902 | return x.When > y.When |
| 903 | } |
| 904 | return x.Name < y.Name |
| 905 | }) |
| 906 | return near |
| 907 | } |
| 908 | for i := range nodes { |
| 909 | nest.Parent[i] = -1 |
| 910 | if above := nearest(i, true); len(above) > 0 { |
| 911 | nest.Parent[i] = above[0] |
| 912 | } |
| 913 | } |
| 914 | for i := range nodes { |
| 915 | nest.Base[i] = -1 |
| 916 | below := nearest(i, false) |
| 917 | for _, d := range below { |
| 918 | if nest.Parent[d] == i { |
| 919 | nest.Base[i] = d |
| 920 | break |
| 921 | } |
| 922 | } |
| 923 | if nest.Base[i] == -1 && len(below) > 0 { |
| 924 | nest.Base[i] = below[0] |
| 925 | } |
| 926 | } |
| 927 | return nest |
| 928 | } |