| 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. The first rule |
| 8 | // 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 a fast-forward puts |
| 13 | // every commit of a change on that history. A merge above the head |
| 14 | // could have dropped the head's content, whichever parent the head is |
| 15 | // reached through, so it leaves the head not landed by this rule. |
| 16 | // 2. landed-recorded: a commit L on main's first-parent history, with one |
| 17 | // parent, carries a transfer statement, signed by the integrator, |
| 18 | // whose change is a request name of the version and whose base is |
| 19 | // the valley-tree-v1 digest of the version's tree. The integrator |
| 20 | // writes one when it lands a change (integrator/transfer.go). The |
| 21 | // integrator's outcome record for that request name says land, at |
| 22 | // this version's head: only the integrator writes outcome records, |
| 23 | // and the record names the exact head it landed, which a statement |
| 24 | // about trees does not. And the integrator's own merge of the head |
| 25 | // onto L's parent gives L's tree. |
| 26 | // 3. uncertain: a request for the version was consumed, or the outcome |
| 27 | // record says the integrator failed on it, and neither of the above |
| 28 | // holds. The integrator moves main before it stores its evidence |
| 29 | // (integrator/refs.go), so a landing can exist without its evidence. |
| 30 | // 4. not-landed: otherwise. |
| 31 | // |
| 32 | // The outcome record alone is never proof of a landing, because a later |
| 33 | // pass can overwrite it. A record a later pass overwrote leaves a landing |
| 34 | // uncertain, never landed-recorded. |
| 35 | |
| 36 | package main |
| 37 | |
| 38 | import ( |
| 39 | "bytes" |
| 40 | "errors" |
| 41 | "fmt" |
| 42 | "os" |
| 43 | "os/exec" |
| 44 | "sort" |
| 45 | "strings" |
| 46 | |
| 47 | "the-valley/change" |
| 48 | ) |
| 49 | |
| 50 | // The landing evidence a version can have. |
| 51 | const ( |
| 52 | landed = "landed" |
| 53 | landedRecorded = "landed-recorded" |
| 54 | uncertain = "uncertain" |
| 55 | notLanded = "not-landed" |
| 56 | ) |
| 57 | |
| 58 | const ( |
| 59 | transferNote = "transfer.note" |
| 60 | predicateTransfer = "the-valley/integration/transfer/v1" |
| 61 | ) |
| 62 | |
| 63 | // inventoryPatterns are the ls-remote patterns of every ref a snapshot |
| 64 | // reads. |
| 65 | var inventoryPatterns = []string{ |
| 66 | "refs/heads/*", |
| 67 | change.RefPrefix + recordTarget + "/*", |
| 68 | requestPrefix + "*", |
| 69 | outcomePrefix + "*", |
| 70 | attestationRefs + "*", |
| 71 | } |
| 72 | |
| 73 | // verifier is what a transfer statement is checked with: the verifier |
| 74 | // keys of the identity registry's known-signers file, the name the |
| 75 | // integrator signs under, and the attest helper that checks a note. |
| 76 | // VALLEY_KNOWN_SIGNERS and VALLEY_INTEGRATOR_SIGNER name the first two. |
| 77 | // Without both, no transfer statement is believed, and a version that |
| 78 | // only one could show landed reads as uncertain or not-landed. |
| 79 | type verifier struct { |
| 80 | keys, signer, attest string |
| 81 | } |
| 82 | |
| 83 | // verifierHint says what a transfer statement is verified with, for a |
| 84 | // command that could not verify one. |
| 85 | const verifierHint = "Set VALLEY_KNOWN_SIGNERS to the known-signers file and VALLEY_INTEGRATOR_SIGNER to the integrator's signing name, with attest on PATH" |
| 86 | |
| 87 | func verifierFromEnv() verifier { |
| 88 | return verifier{ |
| 89 | keys: os.Getenv("VALLEY_KNOWN_SIGNERS"), |
| 90 | signer: os.Getenv("VALLEY_INTEGRATOR_SIGNER"), |
| 91 | attest: "attest", |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | // transferStatement is one transfer statement found under an attestation |
| 96 | // ref, with the values a landing is matched on, read before it is |
| 97 | // verified. |
| 98 | type transferStatement struct { |
| 99 | note []byte |
| 100 | change string // predicate.change: the request's change name |
| 101 | base string // predicate.base.valley-tree-v1: the tree the evidence was over |
| 102 | landed string // subject.digest.git-sha1: the commit that landed |
| 103 | } |
| 104 | |
| 105 | // snapshot is one read of a repository's origin. |
| 106 | type snapshot struct { |
| 107 | g git |
| 108 | o origin |
| 109 | tip string // main on origin; "" when origin has none |
| 110 | all []recorded |
| 111 | verify verifier |
| 112 | |
| 113 | outcomes map[string]memo // outcome ref → the record it holds |
| 114 | landings map[string][2]string // "<change ID> <n>" → landing and commit |
| 115 | firstParent map[string]bool // main's first-parent history, down to its first merge; nil until read |
| 116 | reachesOf map[string]bool |
| 117 | reachOf map[string]map[string]bool |
| 118 | digestOf map[string]string |
| 119 | transfers []transferStatement |
| 120 | read bool // whether transfers has been read |
| 121 | |
| 122 | // unverified is set when a transfer statement named a version and |
| 123 | // could not be checked, so its landing may be stronger than shown. |
| 124 | unverified bool |
| 125 | |
| 126 | // unread is each record that does not read, which no listing shows. |
| 127 | unread []unreadRecord |
| 128 | } |
| 129 | |
| 130 | // readSnapshot lists origin once and fetches every object the snapshot |
| 131 | // reads: main, every branch tip, every record commit and every outcome |
| 132 | // record. A record commit has its version heads as parents, so they come |
| 133 | // with it. |
| 134 | func readSnapshot(g git, v verifier) (*snapshot, error) { |
| 135 | o, err := listOrigin(g, inventoryPatterns...) |
| 136 | if err != nil { |
| 137 | return nil, err |
| 138 | } |
| 139 | var want []string |
| 140 | for ref, oid := range o.refs { |
| 141 | if strings.HasPrefix(ref, "refs/heads/") || strings.HasPrefix(ref, outcomePrefix) { |
| 142 | want = append(want, oid) |
| 143 | } |
| 144 | } |
| 145 | if err := fetch(g, want...); err != nil { |
| 146 | return nil, err |
| 147 | } |
| 148 | all, unread, err := readAll(g, o) |
| 149 | if err != nil { |
| 150 | return nil, err |
| 151 | } |
| 152 | s := newSnapshot(g, o, all, v) |
| 153 | s.unread = unread |
| 154 | return s, nil |
| 155 | } |
| 156 | |
| 157 | // newSnapshot is a snapshot of what o lists, with every record in all |
| 158 | // already read and every object it names already fetched. |
| 159 | func newSnapshot(g git, o origin, all []recorded, v verifier) *snapshot { |
| 160 | s := &snapshot{ |
| 161 | g: g, o: o, tip: o.refs["refs/heads/"+recordTarget], all: all, verify: v, |
| 162 | outcomes: map[string]memo{}, landings: map[string][2]string{}, |
| 163 | reachesOf: map[string]bool{}, reachOf: map[string]map[string]bool{}, digestOf: map[string]string{}, |
| 164 | } |
| 165 | for ref, oid := range o.refs { |
| 166 | if strings.HasPrefix(ref, outcomePrefix) { |
| 167 | data, err := g.output(true, "cat-file", "blob", oid) |
| 168 | if err != nil { |
| 169 | data = "" |
| 170 | } |
| 171 | s.outcomes[ref] = parseMemo(data) |
| 172 | } |
| 173 | } |
| 174 | return s |
| 175 | } |
| 176 | |
| 177 | // headOf is a version's head as an object ID. |
| 178 | func headOf(v change.Version) string { |
| 179 | return strings.TrimPrefix(v.Head, change.HeadPrefix) |
| 180 | } |
| 181 | |
| 182 | // onMain reports whether a commit is on main's first-parent history (main |
| 183 | // itself, its first parent, that commit's first parent, and so on) with no |
| 184 | // merge between it and main's tip: whether |
| 185 | // `git rev-list --first-parent --merges <oid>..main` would list nothing. |
| 186 | // This is where a landing puts a change. The history is read down to its |
| 187 | // first merge, which is itself the last commit counted, since no merge is |
| 188 | // above it. |
| 189 | func (s *snapshot) onMain(oid string) bool { |
| 190 | if s.tip == "" || oid == "" { |
| 191 | return false |
| 192 | } |
| 193 | if s.firstParent == nil { |
| 194 | s.firstParent = map[string]bool{} |
| 195 | if out, err := s.g.output(true, "rev-list", "--first-parent", "--parents", s.tip); err == nil { |
| 196 | for _, line := range strings.Split(out, "\n") { |
| 197 | fields := strings.Fields(line) |
| 198 | if len(fields) == 0 { |
| 199 | continue |
| 200 | } |
| 201 | s.firstParent[fields[0]] = true |
| 202 | if len(fields) > 2 { |
| 203 | break |
| 204 | } |
| 205 | } |
| 206 | } |
| 207 | } |
| 208 | return s.firstParent[oid] |
| 209 | } |
| 210 | |
| 211 | // reaches reports whether main reaches a commit by any path: whether the |
| 212 | // commit is an ancestor of main, or main itself. That is ancestry, which |
| 213 | // is not landing (onMain). |
| 214 | func (s *snapshot) reaches(oid string) bool { |
| 215 | if s.tip == "" || oid == "" { |
| 216 | return false |
| 217 | } |
| 218 | r, seen := s.reachesOf[oid] |
| 219 | if !seen { |
| 220 | r = s.g.succeeds(true, "merge-base", "--is-ancestor", oid, s.tip) |
| 221 | s.reachesOf[oid] = r |
| 222 | } |
| 223 | return r |
| 224 | } |
| 225 | |
| 226 | // requestNames are the names a request for a version is filed under: its |
| 227 | // branch, the way approve names one, and c-<change ID>. |
| 228 | func requestNames(id string, v change.Version) []string { |
| 229 | var names []string |
| 230 | if v.Branch != "" { |
| 231 | names = append(names, changeName(v.Branch)) |
| 232 | } |
| 233 | return append(names, "c-"+id) |
| 234 | } |
| 235 | |
| 236 | // request is the integration request origin holds for a version: one |
| 237 | // filed under either of its names, at its head. |
| 238 | func (s *snapshot) request(id string, v change.Version) *jsonRequest { |
| 239 | for _, name := range requestNames(id, v) { |
| 240 | if ref := requestPrefix + name; s.o.refs[ref] == headOf(v) { |
| 241 | return &jsonRequest{Ref: ref, Commit: headOf(v)} |
| 242 | } |
| 243 | } |
| 244 | return nil |
| 245 | } |
| 246 | |
| 247 | // outcome is the integrator's last decision about a version: the outcome |
| 248 | // record under either of its names whose head is the version's head. |
| 249 | func (s *snapshot) outcome(id string, v change.Version) (name string, m memo, found bool) { |
| 250 | for _, name := range requestNames(id, v) { |
| 251 | if m, ok := s.outcomes[outcomePrefix+name]; ok && m.head == headOf(v) { |
| 252 | return name, m, true |
| 253 | } |
| 254 | } |
| 255 | return "", memo{}, false |
| 256 | } |
| 257 | |
| 258 | // landing is the landing evidence a version has, and the commit on main it |
| 259 | // landed as, when it has. Each version is decided once per snapshot. |
| 260 | func (s *snapshot) landing(id string, v change.Version) (kind, commit string) { |
| 261 | key := fmt.Sprintf("%s %d", id, v.N) |
| 262 | if l, ok := s.landings[key]; ok { |
| 263 | return l[0], l[1] |
| 264 | } |
| 265 | kind, commit = s.decideLanding(id, v) |
| 266 | s.landings[key] = [2]string{kind, commit} |
| 267 | return kind, commit |
| 268 | } |
| 269 | |
| 270 | func (s *snapshot) decideLanding(id string, v change.Version) (kind, commit string) { |
| 271 | head := headOf(v) |
| 272 | if s.onMain(head) { |
| 273 | return landed, head |
| 274 | } |
| 275 | if commit, ok := s.recordedLanding(id, v); ok { |
| 276 | return landedRecorded, commit |
| 277 | } |
| 278 | if name, m, ok := s.outcome(id, v); ok { |
| 279 | consumed := m.decision == "land" && s.o.refs[requestPrefix+name] != head |
| 280 | if consumed || m.decision == "failed" { |
| 281 | return uncertain, "" |
| 282 | } |
| 283 | } |
| 284 | return notLanded, "" |
| 285 | } |
| 286 | |
| 287 | // isLanded reports whether a version has landed by the first two rules. |
| 288 | func (s *snapshot) isLanded(id string, v change.Version) bool { |
| 289 | kind, _ := s.landing(id, v) |
| 290 | return kind == landed || kind == landedRecorded |
| 291 | } |
| 292 | |
| 293 | // recordedLanding looks for a transfer statement that records the version |
| 294 | // landing as a commit on main, and verifies the first that matches. |
| 295 | func (s *snapshot) recordedLanding(id string, v change.Version) (string, bool) { |
| 296 | names := map[string]bool{} |
| 297 | for _, n := range requestNames(id, v) { |
| 298 | names[n] = true |
| 299 | } |
| 300 | head := headOf(v) |
| 301 | for _, t := range s.transferStatements() { |
| 302 | if !names[t.change] || !isObjectID(t.landed) || !s.onMain(t.landed) || !s.singleParent(t.landed) { |
| 303 | continue |
| 304 | } |
| 305 | if m, ok := s.outcomes[outcomePrefix+t.change]; !ok || m.decision != "land" || m.head != head { |
| 306 | continue |
| 307 | } |
| 308 | digest, ok := s.digest(headOf(v)) |
| 309 | if !ok { |
| 310 | s.unverified = true |
| 311 | continue |
| 312 | } |
| 313 | if t.base != digest || !s.mergesTo(headOf(v), t.landed) { |
| 314 | continue |
| 315 | } |
| 316 | if s.verified(t) { |
| 317 | return t.landed, true |
| 318 | } |
| 319 | } |
| 320 | return "", false |
| 321 | } |
| 322 | |
| 323 | // singleParent reports whether a commit has exactly one parent, as every |
| 324 | // commit the integrator lands as does. |
| 325 | func (s *snapshot) singleParent(oid string) bool { |
| 326 | out, err := s.g.output(true, "rev-list", "--parents", "-n", "1", oid) |
| 327 | return err == nil && len(strings.Fields(out)) == 2 |
| 328 | } |
| 329 | |
| 330 | // mergesTo reports whether the integrator's merge of head onto landed's |
| 331 | // parent gives landed's tree. It is the merge integrator/git.go's |
| 332 | // mergeDelta makes: merge-tree onto the tip, with the merge base of the |
| 333 | // tip and the head as the base. A head that merges to another tree, or |
| 334 | // not at all, is not what landed. |
| 335 | func (s *snapshot) mergesTo(head, landed string) bool { |
| 336 | tip, err := s.g.output(true, "rev-parse", "--verify", "--quiet", landed+"^1^{commit}") |
| 337 | if err != nil { |
| 338 | return false |
| 339 | } |
| 340 | want, err := s.g.output(true, "rev-parse", "--verify", "--quiet", landed+"^{tree}") |
| 341 | if err != nil { |
| 342 | return false |
| 343 | } |
| 344 | base, err := s.g.output(true, "merge-base", tip, head) |
| 345 | if err != nil { |
| 346 | return false |
| 347 | } |
| 348 | out, err := s.g.output(true, "merge-tree", "--write-tree", "--merge-base="+base, tip, head) |
| 349 | if err != nil { |
| 350 | return false |
| 351 | } |
| 352 | tree, _, _ := strings.Cut(out, "\n") |
| 353 | return strings.TrimSpace(tree) == want |
| 354 | } |
| 355 | |
| 356 | // transferStatements reads every transfer statement under origin's |
| 357 | // attestation refs, once. A transfer statement is the transfer.note at |
| 358 | // the top of an attestation ref's tree. Anything unreadable is skipped: |
| 359 | // what is not read is not believed. |
| 360 | func (s *snapshot) transferStatements() []transferStatement { |
| 361 | if s.read { |
| 362 | return s.transfers |
| 363 | } |
| 364 | s.read = true |
| 365 | var trees []string |
| 366 | for ref, oid := range s.o.refs { |
| 367 | if strings.HasPrefix(ref, attestationRefs) { |
| 368 | trees = append(trees, oid) |
| 369 | } |
| 370 | } |
| 371 | if len(trees) == 0 { |
| 372 | return nil |
| 373 | } |
| 374 | sort.Strings(trees) |
| 375 | if err := fetch(s.g, trees...); err != nil { |
| 376 | s.unverified = true |
| 377 | return nil |
| 378 | } |
| 379 | var query bytes.Buffer |
| 380 | for _, tree := range trees { |
| 381 | fmt.Fprintf(&query, "%s:%s\n", tree, transferNote) |
| 382 | } |
| 383 | out, err := s.g.input(true, &query, "cat-file", "--batch-check=%(objectname) %(objecttype)") |
| 384 | if err != nil { |
| 385 | s.unverified = true |
| 386 | return nil |
| 387 | } |
| 388 | for _, line := range strings.Split(out, "\n") { |
| 389 | oid, kind, _ := strings.Cut(line, " ") |
| 390 | if kind != "blob" { |
| 391 | continue |
| 392 | } |
| 393 | note, err := s.g.output(true, "cat-file", "blob", oid) |
| 394 | if err != nil { |
| 395 | continue |
| 396 | } |
| 397 | note += "\n" |
| 398 | text, ok := noteText(note) |
| 399 | if !ok || statementValue(text, "predicateType") != predicateTransfer { |
| 400 | continue |
| 401 | } |
| 402 | s.transfers = append(s.transfers, transferStatement{ |
| 403 | note: []byte(note), |
| 404 | change: statementValue(text, "predicate.change"), |
| 405 | base: statementValue(text, "predicate.base.valley-tree-v1"), |
| 406 | landed: statementValue(text, "subject.digest.git-sha1"), |
| 407 | }) |
| 408 | } |
| 409 | return s.transfers |
| 410 | } |
| 411 | |
| 412 | // noteText is the statement text of a note: everything before the last |
| 413 | // blank line, which separates the text from its signatures. |
| 414 | func noteText(note string) (string, bool) { |
| 415 | split := strings.LastIndex(note, "\n\n") |
| 416 | if split < 0 { |
| 417 | return "", false |
| 418 | } |
| 419 | return note[:split+1], true |
| 420 | } |
| 421 | |
| 422 | // statementValue reads one key's value from statement text, where each |
| 423 | // line is a key, one space and a value. It is a lookup, and it decides |
| 424 | // nothing: what is read here only chooses which statement to verify. |
| 425 | func statementValue(text, key string) string { |
| 426 | for _, line := range strings.Split(text, "\n") { |
| 427 | if k, v, ok := strings.Cut(line, " "); ok && k == key { |
| 428 | return v |
| 429 | } |
| 430 | } |
| 431 | return "" |
| 432 | } |
| 433 | |
| 434 | // repoDir is the directory attest is pointed at. |
| 435 | func (s *snapshot) repoDir() string { |
| 436 | if s.g.dir == "" { |
| 437 | return "." |
| 438 | } |
| 439 | return s.g.dir |
| 440 | } |
| 441 | |
| 442 | // digest is the valley-tree-v1 digest of a commit's tree, from attest, |
| 443 | // which defines the scheme. |
| 444 | func (s *snapshot) digest(oid string) (string, bool) { |
| 445 | if d, ok := s.digestOf[oid]; ok { |
| 446 | return d, d != "" |
| 447 | } |
| 448 | cmd := exec.Command(s.verify.attest, "digest", "--repo", s.repoDir(), "--rev", oid) |
| 449 | cmd.Env = s.g.env |
| 450 | out, err := cmd.Output() |
| 451 | d := "" |
| 452 | if scheme, value, ok := strings.Cut(strings.TrimSpace(string(out)), ":"); err == nil && ok && scheme == "valley-tree-v1" { |
| 453 | d = value |
| 454 | } |
| 455 | s.digestOf[oid] = d |
| 456 | return d, d != "" |
| 457 | } |
| 458 | |
| 459 | // verified runs attest verify over a transfer statement: its signature |
| 460 | // must be the integrator's, by a key the known-signers file holds, and |
| 461 | // its subject must be the tree of the commit it says landed. |
| 462 | func (s *snapshot) verified(t transferStatement) bool { |
| 463 | if s.verify.keys == "" || s.verify.signer == "" { |
| 464 | s.unverified = true |
| 465 | return false |
| 466 | } |
| 467 | file, err := os.CreateTemp("", "valley-transfer-*.note") |
| 468 | if err != nil { |
| 469 | s.unverified = true |
| 470 | return false |
| 471 | } |
| 472 | defer os.Remove(file.Name()) |
| 473 | _, err = file.Write(t.note) |
| 474 | if cerr := file.Close(); err != nil || cerr != nil { |
| 475 | s.unverified = true |
| 476 | return false |
| 477 | } |
| 478 | cmd := exec.Command(s.verify.attest, "verify", "--note", file.Name(), |
| 479 | "--known-keys", s.verify.keys, "--signer", s.verify.signer, |
| 480 | "--repo", s.repoDir(), "--rev", t.landed) |
| 481 | cmd.Env = s.g.env |
| 482 | if err := cmd.Run(); err != nil { |
| 483 | // attest refusing the note is an answer. Failing to run it is not. |
| 484 | var exit *exec.ExitError |
| 485 | if !errors.As(err, &exit) { |
| 486 | s.unverified = true |
| 487 | } |
| 488 | return false |
| 489 | } |
| 490 | return true |
| 491 | } |
| 492 | |
| 493 | // reach is every commit reachable from oid that main does not reach. |
| 494 | func (s *snapshot) reach(oid string) map[string]bool { |
| 495 | if r, ok := s.reachOf[oid]; ok { |
| 496 | return r |
| 497 | } |
| 498 | args := []string{"rev-list", oid} |
| 499 | if s.tip != "" { |
| 500 | args = append(args, "^"+s.tip) |
| 501 | } |
| 502 | r := map[string]bool{} |
| 503 | if out, err := s.g.output(true, args...); err == nil { |
| 504 | for _, c := range strings.Fields(out) { |
| 505 | r[c] = true |
| 506 | } |
| 507 | } |
| 508 | s.reachOf[oid] = r |
| 509 | return r |
| 510 | } |
| 511 | |
| 512 | // strictAncestor reports whether a is an ancestor of b, and not b itself. |
| 513 | // A commit main does not reach is an ancestor of b exactly when it is in |
| 514 | // reach(b), so git is asked only about a commit main reaches. |
| 515 | func (s *snapshot) strictAncestor(a, b string) bool { |
| 516 | switch { |
| 517 | case a == b: |
| 518 | return false |
| 519 | case s.reach(b)[a]: |
| 520 | return true |
| 521 | case s.reaches(a): |
| 522 | return s.g.succeeds(true, "merge-base", "--is-ancestor", a, b) |
| 523 | } |
| 524 | return false |
| 525 | } |
| 526 | |
| 527 | // includes is the change IDs among candidates whose active head is a |
| 528 | // strict ancestor of n's active head, nearest only: one that is itself an |
| 529 | // ancestor of another such change's head is left out. It is ancestry |
| 530 | // alone, so a change that includes another lands the other's content too. |
| 531 | func (s *snapshot) includes(n recorded, candidates []recorded) []string { |
| 532 | head := headOf(n.record.ActiveVersion()) |
| 533 | var below []recorded |
| 534 | for _, c := range candidates { |
| 535 | if c.id != n.id && s.strictAncestor(headOf(c.record.ActiveVersion()), head) { |
| 536 | below = append(below, c) |
| 537 | } |
| 538 | } |
| 539 | ids := []string{} |
| 540 | for _, c := range below { |
| 541 | nearest := true |
| 542 | for _, d := range below { |
| 543 | if s.strictAncestor(headOf(c.record.ActiveVersion()), headOf(d.record.ActiveVersion())) { |
| 544 | nearest = false |
| 545 | break |
| 546 | } |
| 547 | } |
| 548 | if nearest { |
| 549 | ids = append(ids, c.id) |
| 550 | } |
| 551 | } |
| 552 | sort.Strings(ids) |
| 553 | return ids |
| 554 | } |
| 555 | |
| 556 | // unrecorded is every branch on origin, other than main, whose tip main |
| 557 | // does not reach and that is not the head of any change's active version, |
| 558 | // in the order of their names. |
| 559 | func (s *snapshot) unrecorded() []jsonBranch { |
| 560 | active := map[string]bool{} |
| 561 | for _, n := range s.all { |
| 562 | active[headOf(n.record.ActiveVersion())] = true |
| 563 | } |
| 564 | branches := []jsonBranch{} |
| 565 | for ref, oid := range s.o.refs { |
| 566 | name, ok := strings.CutPrefix(ref, "refs/heads/") |
| 567 | if !ok || name == recordTarget || active[oid] || s.reaches(oid) { |
| 568 | continue |
| 569 | } |
| 570 | branches = append(branches, jsonBranch{Branch: name, Head: oid}) |
| 571 | } |
| 572 | sort.Slice(branches, func(i, j int) bool { return branches[i].Branch < branches[j].Branch }) |
| 573 | return branches |
| 574 | } |