| 1 | // valley pending: what is waiting to land, for a person to read. |
| 2 | // |
| 3 | // It prints three sections, in this order. ready holds the ready changes |
| 4 | // whose active version has not landed, by the first two landing rules |
| 5 | // (inventory.go). branches holds each branch on origin, other than main, |
| 6 | // that main does not reach, that is no change's active head, and that no |
| 7 | // landed version's head reaches. drafts |
| 8 | // holds the changes whose record says they are drafts. When no ready |
| 9 | // change is at the top of a stack, one line says so in place of ready, |
| 10 | // and says where any ready change inside another section's stack is. |
| 11 | // |
| 12 | // Each entry is a block. Its first line is its name, with the age and |
| 13 | // author of its head: the handle and version of a change, the name of a |
| 14 | // branch, and for a draft its handle and version on its branch. The lines |
| 15 | // indented under it give its title, then the commits and the diff the |
| 16 | // entry adds to its base: |
| 17 | // |
| 18 | // act-on-a-change-by-handle-key-its-1902807a 9 hours ago · alice-agent-vm |
| 19 | // change: share the record bounds and the c-<change ID> request name |
| 20 | // 10 commits · 31 files +2412 −404 |
| 21 | // approving it lands the 2 below too |
| 22 | // ├ find-changes-across-repos-valley-changes-1e788c4f 10 hours ago · alice-… |
| 23 | // │ valley: changes --json and pending by handle, across a host's projects |
| 24 | // │ 9 commits · 17 files +2701 −137 |
| 25 | // └ a-draft-state-valley-unready-and-what-56ed34ac 12 hours ago · alice-ag… |
| 26 | // change: read a draft as a third state |
| 27 | // 14 commits · 20 files +1207 −219 |
| 28 | // |
| 29 | // A change's title is its record's, for the active version. A branch's |
| 30 | // title is the subject of its first own commit: the oldest commit on its |
| 31 | // first-parent line that its base does not hold. When a knot in the plan |
| 32 | // graph links a change, the first knot's ID follows the title, as |
| 33 | // "<title> · oc-6335749" (does.go). |
| 34 | // |
| 35 | // An entry's base is the nearest other entry its head strictly contains, |
| 36 | // or main when there is none. That is the ancestry includes reads |
| 37 | // (inventory.go). Its commits and diff are counted from the nearer of its |
| 38 | // base and the head of a landed version its head contains: a landed |
| 39 | // version's commits are not on main, since it landed as one new commit, |
| 40 | // and its content is. A stack is an entry that no other entry contains, the |
| 41 | // top, then every entry the top contains, as one list at one indentation, |
| 42 | // however deep the stack is. Each entry is listed before the entries it |
| 43 | // contains. The top's block says how many entries approving it lands with |
| 44 | // it. Each entry is on the entry listed right after it, and says |
| 45 | // "on <base>" where it is not, as at a fork, where two entries are on one |
| 46 | // base and the base is listed under only one of them. An entry the top |
| 47 | // does not contain is never listed under it. A stack is printed in the |
| 48 | // section of its top, and a listed entry from another section says which |
| 49 | // it is in. |
| 50 | // |
| 51 | // A change may say more, each on a line of its own. includes <handle> |
| 52 | // names a change its head contains that is not an entry here, such as a |
| 53 | // withdrawn one. uncertain says the active version may have landed without |
| 54 | // its evidence being stored. With --checks, it says how many checks the |
| 55 | // policy requires of the change, by cost, and for a ready change how many |
| 56 | // of its required checks passed, failed and are pending, as valley |
| 57 | // changes --checks lists them (versionchecks.go), naming those that |
| 58 | // failed: "checks: 18 passed, 1 failed (valley-request)". When the host's |
| 59 | // worker telemetry holds the version's builds, the line goes on with what |
| 60 | // they cost and the checks that grew against their recent cost on the |
| 61 | // same capacity |
| 62 | // (checkcost.go): "checks: 21 passed in 9m10s (eval 24s); worker-e2e |
| 63 | // 431s, +68s against its recent 363s". --json then carries the same |
| 64 | // checks objects. |
| 65 | // |
| 66 | // Within a section, the stack with the latest commit comes first, and |
| 67 | // within a stack the entry with the latest commit does. Every line fits |
| 68 | // stdout's terminal, or 80 columns when stdout is no terminal or does not |
| 69 | // know its width. A title or an author is cut short with …; a handle or a |
| 70 | // branch never is. On a terminal, names are bold, unless NO_COLOR is set. |
| 71 | // |
| 72 | // With --json it prints valley-pending/v1: valley-changes/v1's shape, with |
| 73 | // the pending changes alone in changes, the drafts in an array of their |
| 74 | // own, drafts, and the same branches. |
| 75 | |
| 76 | package main |
| 77 | |
| 78 | import ( |
| 79 | "fmt" |
| 80 | "io" |
| 81 | "os" |
| 82 | "os/exec" |
| 83 | "sort" |
| 84 | "strconv" |
| 85 | "strings" |
| 86 | "unicode" |
| 87 | |
| 88 | "the-valley/change" |
| 89 | "the-valley/rebase" |
| 90 | ) |
| 91 | |
| 92 | // pendingChange is a ready change whose active version has not landed. |
| 93 | func pendingChange(s *snapshot, n recorded) bool { |
| 94 | return n.record.State == change.Ready && !s.isLanded(n.id, n.record.ActiveVersion()) |
| 95 | } |
| 96 | |
| 97 | // unlanded is a change in any state whose active version has not landed: |
| 98 | // the changes a pending change may include. |
| 99 | func unlanded(s *snapshot, n recorded) bool { |
| 100 | return !s.isLanded(n.id, n.record.ActiveVersion()) |
| 101 | } |
| 102 | |
| 103 | func isDraft(_ *snapshot, n recorded) bool { |
| 104 | return n.record.State == change.Draft |
| 105 | } |
| 106 | |
| 107 | // pendingView is a repository as valley-pending/v1 writes it: the pending |
| 108 | // changes in changes and the drafts in drafts, each with includes among |
| 109 | // the changes that have not landed. |
| 110 | func (l listed) pendingView(h handleMap) jsonRepo { |
| 111 | r := l.repoView(h, pendingChange, unlanded) |
| 112 | drafts := []jsonChange{} |
| 113 | if l.err == nil { |
| 114 | drafts = l.repoView(h, isDraft, unlanded).Changes |
| 115 | } |
| 116 | r.Drafts = &drafts |
| 117 | return r |
| 118 | } |
| 119 | |
| 120 | func runPending(g git, args []string, stdout, stderr io.Writer) error { |
| 121 | l, err := parseListing("pending", args, true) |
| 122 | if err != nil { |
| 123 | return err |
| 124 | } |
| 125 | list, host, err := sources(g, l, stderr) |
| 126 | if err != nil { |
| 127 | return err |
| 128 | } |
| 129 | repos := readSources(list, newVerifier()) |
| 130 | readKnots(repos, l.verbose, stderr) |
| 131 | h := handles(repos) |
| 132 | writeHandleCache(g, repos, h) |
| 133 | defer warnUnverified(repos, stderr) |
| 134 | if l.asJSON { |
| 135 | doc := jsonChanges{Schema: pendingSchema, Host: host, Repos: []jsonRepo{}} |
| 136 | c := newCosts() |
| 137 | for _, r := range repos { |
| 138 | view := r.pendingView(h) |
| 139 | if l.checks { |
| 140 | r.addChecks(&view, c, stderr) |
| 141 | } |
| 142 | utf8Branches(&view, stderr) |
| 143 | doc.Repos = append(doc.Repos, view) |
| 144 | } |
| 145 | return writeDocument(stdout, doc) |
| 146 | } |
| 147 | t := terminalOf(stdout) |
| 148 | c := newCosts() |
| 149 | for i, r := range repos { |
| 150 | if i > 0 { |
| 151 | fmt.Fprintln(stdout) |
| 152 | } |
| 153 | writePending(r, h, l, c, stdout, stderr, len(repos) > 1 || l.all || l.repo != "", t) |
| 154 | } |
| 155 | return nil |
| 156 | } |
| 157 | |
| 158 | // sections are valley pending's sections, in the order they are printed, |
| 159 | // each with the word a nested entry from it is marked with. |
| 160 | var sections = []struct{ name, kind string }{ |
| 161 | {"ready", "ready"}, {"branches", "branch"}, {"drafts", "draft"}, |
| 162 | } |
| 163 | |
| 164 | // entry is one block of valley pending: a ready change, a branch no record |
| 165 | // is on, or a draft. |
| 166 | type entry struct { |
| 167 | section string |
| 168 | name string // what the block's first line names |
| 169 | ref string // what the entry is typed as: its handle, or its branch |
| 170 | head string // the head commit |
| 171 | title string // the record's title; for a branch, read by measure |
| 172 | knot string // ID of the first knot that links the change (does.go) |
| 173 | includes []string // what includes names that is no entry here |
| 174 | notes []string // uncertain and checks |
| 175 | |
| 176 | when int64 // the head's commit time |
| 177 | age, author string |
| 178 | layer string // the commits and the diff it adds to its base |
| 179 | |
| 180 | parent *entry // the entry it is listed under, if any |
| 181 | kids []*entry // the entries nested right under it |
| 182 | base *entry // nil for main |
| 183 | inside []*entry // every entry whose head strictly contains its own |
| 184 | below []*entry // for the top of a stack, every entry listed under it |
| 185 | latest int64 // for the top of a stack, the latest commit time in it |
| 186 | } |
| 187 | |
| 188 | // writePending prints one repository's entries, section by section. |
| 189 | // Several repositories are each headed by their name. |
| 190 | func writePending(r listed, h handleMap, l listing, c *costs, w, stderr io.Writer, headed bool, t terminal) { |
| 191 | p := func(format string, args ...any) { fmt.Fprintf(w, format, args...) } |
| 192 | if r.err != nil { |
| 193 | p("%s: %v\n", r.name, r.err) |
| 194 | return |
| 195 | } |
| 196 | if headed { |
| 197 | p("%s\n%s\n", r.name, strings.Repeat("─", min(columns(r.name), t.width))) |
| 198 | } |
| 199 | view := r.pendingView(h) |
| 200 | if l.checks { |
| 201 | r.addChecks(&view, c, stderr) |
| 202 | } |
| 203 | all := r.entries(view, h, l) |
| 204 | tops := r.snap.stack(all) |
| 205 | gap := false |
| 206 | for _, sec := range sections { |
| 207 | var here []*entry |
| 208 | for _, e := range tops { |
| 209 | if e.section == sec.name { |
| 210 | here = append(here, e) |
| 211 | } |
| 212 | } |
| 213 | if sec.name == "ready" && len(here) == 0 { |
| 214 | p("%s\n", readyElsewhere(tops)) |
| 215 | gap = true |
| 216 | continue |
| 217 | } |
| 218 | if len(here) == 0 { |
| 219 | continue |
| 220 | } |
| 221 | if gap { |
| 222 | p("\n") |
| 223 | } |
| 224 | p("%s\n", sec.name) |
| 225 | for i, e := range here { |
| 226 | if i > 0 { |
| 227 | p("\n") |
| 228 | } |
| 229 | for _, line := range t.drawStack(e) { |
| 230 | p("%s\n", line) |
| 231 | } |
| 232 | } |
| 233 | gap = true |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | // entries are a repository's entries, as its pending view lists them, each |
| 238 | // with its head's commit time, age and author. |
| 239 | func (r listed) entries(view jsonRepo, h handleMap, l listing) []*entry { |
| 240 | s := r.snap |
| 241 | states := map[handleKey]string{} |
| 242 | for _, n := range s.all { |
| 243 | states[handleKey{r.name, n.record.Target, n.id}] = n.record.State |
| 244 | } |
| 245 | var all []*entry |
| 246 | recorded := func(section string, c jsonChange) { |
| 247 | v := c.Versions[c.Active-1] |
| 248 | e := &entry{section: section, name: fmt.Sprintf("%s v%d", c.Handle, v.N), ref: c.Handle, head: v.Head, title: readable(v.Title)} |
| 249 | if len(c.Knots) > 0 { |
| 250 | e.knot = readable(c.Knots[0].ID) |
| 251 | } |
| 252 | if section == "drafts" && v.Branch != "" { |
| 253 | e.name += " on " + branchName(v.Branch) |
| 254 | } |
| 255 | for _, id := range c.Includes { |
| 256 | k := handleKey{r.name, c.Target, id} |
| 257 | if st := states[k]; st != change.Ready && st != change.Draft { |
| 258 | e.includes = append(e.includes, fmt.Sprintf("includes %s (%s)", h[k], st)) |
| 259 | } |
| 260 | } |
| 261 | if v.Landing == uncertain { |
| 262 | e.notes = append(e.notes, "uncertain: it may have landed without its evidence being stored") |
| 263 | } |
| 264 | if c.Restack != nil && c.Restack.Current { |
| 265 | name := func(id string) string { return h[handleKey{r.name, c.Target, id}] } |
| 266 | e.notes = append(e.notes, "restack: "+readable(restackSays(c.Restack.Report, name))) |
| 267 | } |
| 268 | if l.checks { |
| 269 | e.notes = append(e.notes, s.checks(v.Head)) |
| 270 | } |
| 271 | if v.Checks != nil { |
| 272 | e.notes = append(e.notes, checksLine(v.Checks)) |
| 273 | } |
| 274 | all = append(all, e) |
| 275 | } |
| 276 | for _, c := range view.Changes { |
| 277 | recorded("ready", c) |
| 278 | } |
| 279 | for _, b := range view.Branches { |
| 280 | name := branchName(b.Branch) |
| 281 | all = append(all, &entry{section: "branches", name: name, ref: name, head: b.Head}) |
| 282 | } |
| 283 | for _, c := range *view.Drafts { |
| 284 | recorded("drafts", c) |
| 285 | } |
| 286 | for _, e := range all { |
| 287 | out, _ := s.g.output(true, "log", "-1", "--format=%ct%x00%cr%x00%an", e.head) |
| 288 | if fields := strings.SplitN(out, "\x00", 3); len(fields) == 3 { |
| 289 | e.when, _ = strconv.ParseInt(fields[0], 10, 64) |
| 290 | e.age, e.author = fields[1], readable(fields[2]) |
| 291 | } |
| 292 | } |
| 293 | return all |
| 294 | } |
| 295 | |
| 296 | // stack nests each entry under the nearest entry whose head strictly |
| 297 | // contains its own, and gives each entry its base: the nearest entry its |
| 298 | // own head strictly contains, as the rebase engine stacks layers |
| 299 | // (rebase.Snapshot.Nest). Where two are equally near, the one with the |
| 300 | // later commit is taken. It returns the entries nested under none, each the |
| 301 | // top of a stack, the stack with the latest commit first. Each top lists |
| 302 | // every entry nested under it, at any depth, in its below. |
| 303 | func (s *snapshot) stack(all []*entry) []*entry { |
| 304 | nodes := make([]rebase.Node, len(all)) |
| 305 | for i, e := range all { |
| 306 | nodes[i] = rebase.Node{Name: e.name, Head: e.head, When: e.when} |
| 307 | } |
| 308 | nest := s.land.Nest(nodes) |
| 309 | for i, e := range all { |
| 310 | for _, j := range nest.Inside[i] { |
| 311 | e.inside = append(e.inside, all[j]) |
| 312 | } |
| 313 | if p := nest.Parent[i]; p >= 0 { |
| 314 | e.parent = all[p] |
| 315 | e.parent.kids = append(e.parent.kids, e) |
| 316 | } |
| 317 | } |
| 318 | var tops []*entry |
| 319 | for i, e := range all { |
| 320 | if b := nest.Base[i]; b >= 0 { |
| 321 | e.base = all[b] |
| 322 | } |
| 323 | s.measure(e) |
| 324 | if e.parent == nil { |
| 325 | tops = append(tops, e) |
| 326 | } |
| 327 | } |
| 328 | for _, top := range tops { |
| 329 | top.list() |
| 330 | } |
| 331 | byActivity(tops, func(e *entry) int64 { return e.latest }) |
| 332 | return tops |
| 333 | } |
| 334 | |
| 335 | // list puts every entry nested under the top of a stack in its below, each |
| 336 | // before the entries it contains. Where it can, it lists next the base of |
| 337 | // the entry it listed last, so a chain is listed unbroken. Otherwise it |
| 338 | // lists next the entry with the latest commit. It sets latest, the latest |
| 339 | // commit time in the stack. |
| 340 | func (top *entry) list() { |
| 341 | nested := map[*entry]bool{} |
| 342 | var walk func(e *entry) |
| 343 | walk = func(e *entry) { |
| 344 | for _, k := range e.kids { |
| 345 | nested[k] = true |
| 346 | walk(k) |
| 347 | } |
| 348 | } |
| 349 | walk(top) |
| 350 | top.latest = top.when |
| 351 | for last := top; len(nested) > 0; { |
| 352 | var free []*entry // those every entry containing them is listed before |
| 353 | for e := range nested { |
| 354 | listable := true |
| 355 | for _, c := range e.inside { |
| 356 | listable = listable && !nested[c] |
| 357 | } |
| 358 | if listable { |
| 359 | free = append(free, e) |
| 360 | } |
| 361 | } |
| 362 | byActivity(free, func(e *entry) int64 { return e.when }) |
| 363 | next := free[0] |
| 364 | for _, e := range free { |
| 365 | if e == last.base { |
| 366 | next = e |
| 367 | } |
| 368 | } |
| 369 | delete(nested, next) |
| 370 | top.below = append(top.below, next) |
| 371 | top.latest = max(top.latest, next.when) |
| 372 | last = next |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | // readyElsewhere is what the ready section says when no ready change is at |
| 377 | // the top of a stack: that nothing is ready, or where the ready changes in |
| 378 | // other sections' stacks are. |
| 379 | func readyElsewhere(tops []*entry) string { |
| 380 | n, stacks := 0, 0 |
| 381 | var where []string |
| 382 | for _, sec := range sections { |
| 383 | found := false |
| 384 | for _, top := range tops { |
| 385 | held := 0 |
| 386 | for _, e := range top.below { |
| 387 | if e.section == "ready" { |
| 388 | held++ |
| 389 | } |
| 390 | } |
| 391 | if top.section == sec.name && held > 0 { |
| 392 | n, stacks, found = n+held, stacks+1, true |
| 393 | } |
| 394 | } |
| 395 | if found { |
| 396 | where = append(where, sec.name) |
| 397 | } |
| 398 | } |
| 399 | if n == 0 { |
| 400 | return "nothing is ready" |
| 401 | } |
| 402 | verb, in := "are", fmt.Sprintf("%d stacks", stacks) |
| 403 | if n == 1 { |
| 404 | verb = "is" |
| 405 | } |
| 406 | if stacks == 1 { |
| 407 | in = "a stack" |
| 408 | } |
| 409 | return fmt.Sprintf("nothing is ready on its own; %s %s in %s under %s", counted(n, "ready change"), verb, in, strings.Join(where, " and ")) |
| 410 | } |
| 411 | |
| 412 | // byActivity orders entries latest first, and then by name. |
| 413 | func byActivity(list []*entry, when func(*entry) int64) { |
| 414 | sort.SliceStable(list, func(i, j int) bool { |
| 415 | if a, b := when(list[i]), when(list[j]); a != b { |
| 416 | return a > b |
| 417 | } |
| 418 | return list[i].name < list[j].name |
| 419 | }) |
| 420 | } |
| 421 | |
| 422 | // measure reads what an entry adds to what it stands on: how many |
| 423 | // commits, and the diff. It stands on the nearest of its base and every |
| 424 | // landed version's head below it, as the rebase engine cuts a landed |
| 425 | // version's commits from a family, or on its merge base with main when |
| 426 | // there is none. A landed version's commits are not on main, since it |
| 427 | // landed as one new commit, and its content is. A branch's title is the |
| 428 | // subject of the first of the entry's own commits on its first-parent |
| 429 | // line. |
| 430 | func (s *snapshot) measure(e *entry) { |
| 431 | var below []string |
| 432 | for _, h := range s.land.LandedUnder(e.head) { |
| 433 | if h != e.head { |
| 434 | below = append(below, h) |
| 435 | } |
| 436 | } |
| 437 | if e.base != nil { |
| 438 | below = append(below, e.base.head) |
| 439 | } |
| 440 | var not []string |
| 441 | from := s.tip |
| 442 | for _, h := range below { |
| 443 | not = append(not, "^"+h) |
| 444 | // The heads below are all the entry's ancestors, and the nearest |
| 445 | // is the one that contains every other. |
| 446 | if from == s.tip || s.strictAncestor(from, h) { |
| 447 | from = h |
| 448 | } |
| 449 | } |
| 450 | if s.tip != "" { |
| 451 | not = append(not, "^"+s.tip) |
| 452 | } |
| 453 | if from == "" { |
| 454 | from, _ = s.g.input(true, strings.NewReader(""), "hash-object", "-t", "tree", "--stdin") |
| 455 | } |
| 456 | commits := "? commits" |
| 457 | if out, err := s.g.output(true, append([]string{"rev-list", "--count", e.head}, not...)...); err == nil { |
| 458 | n, _ := strconv.Atoi(out) |
| 459 | commits = counted(n, "commit") |
| 460 | } |
| 461 | files, added, deleted := 0, 0, 0 |
| 462 | out, _ := s.g.output(true, "diff", "--numstat", from+"..."+e.head) |
| 463 | for _, line := range strings.Split(out, "\n") { |
| 464 | if f := strings.SplitN(line, "\t", 3); len(f) == 3 { |
| 465 | a, _ := strconv.Atoi(f[0]) // a binary file's counts are -, read as 0 |
| 466 | d, _ := strconv.Atoi(f[1]) |
| 467 | files, added, deleted = files+1, added+a, deleted+d |
| 468 | } |
| 469 | } |
| 470 | diff := "no file changes" |
| 471 | if files > 0 { |
| 472 | diff = fmt.Sprintf("%s +%d −%d", counted(files, "file"), added, deleted) |
| 473 | } |
| 474 | e.layer = commits + " · " + diff |
| 475 | if e.section == "branches" { |
| 476 | e.title = s.branchTitle(e.head, not) |
| 477 | } |
| 478 | } |
| 479 | |
| 480 | // branchTitle is a branch's title: the subject of the first of its own |
| 481 | // commits on its first-parent line, those its head reaches and not leaves |
| 482 | // out. |
| 483 | func (s *snapshot) branchTitle(head string, not []string) string { |
| 484 | out, _ := s.g.output(true, append([]string{"log", "--first-parent", "--reverse", "--format=%s", head}, not...)...) |
| 485 | first, _, _ := strings.Cut(out, "\n") |
| 486 | return readable(change.Title(first)) |
| 487 | } |
| 488 | |
| 489 | func counted(n int, noun string) string { |
| 490 | switch n { |
| 491 | case 0: |
| 492 | return "no " + noun + "s" |
| 493 | case 1: |
| 494 | return "1 " + noun |
| 495 | } |
| 496 | return fmt.Sprintf("%d %ss", n, noun) |
| 497 | } |
| 498 | |
| 499 | // drawStack is the lines of a stack: the top entry's block, then the |
| 500 | // block of each entry listed under it, at one indentation however deep the |
| 501 | // stack is. Each listed entry is on the entry listed right after it, and |
| 502 | // says "on <base>" where it is not. |
| 503 | func (t terminal) drawStack(top *entry) []string { |
| 504 | list := append([]*entry{top}, top.below...) |
| 505 | var lines []string |
| 506 | for i, e := range list { |
| 507 | var next *entry |
| 508 | if i+1 < len(list) { |
| 509 | next = list[i+1] |
| 510 | } |
| 511 | first, rest := "", " " |
| 512 | switch { |
| 513 | case i > 0 && next != nil: |
| 514 | first, rest = " ├ ", " │ " |
| 515 | case i > 0: |
| 516 | first, rest = " └ ", " " |
| 517 | } |
| 518 | suffix := "" |
| 519 | if e.section != top.section { |
| 520 | for _, sec := range sections { |
| 521 | if sec.name == e.section { |
| 522 | suffix = " (" + sec.kind + ")" |
| 523 | } |
| 524 | } |
| 525 | } |
| 526 | lines = append(lines, t.nameLine(first, e.name, suffix, e, rest)...) |
| 527 | lines = append(lines, rest+t.titled(e, rest), rest+t.fit(e.layer, rest)) |
| 528 | switch { |
| 529 | case e.base == next: |
| 530 | case e.base != nil: |
| 531 | lines = append(lines, rest+"on "+e.base.ref) |
| 532 | default: |
| 533 | lines = append(lines, rest+"on "+recordTarget) |
| 534 | } |
| 535 | lines = append(lines, prefixed(rest, e.includes)...) |
| 536 | for _, note := range e.notes { |
| 537 | lines = append(lines, rest+t.fit(note, rest)) |
| 538 | } |
| 539 | if i == 0 && len(top.below) > 0 { |
| 540 | lands := fmt.Sprintf("approving it lands the %d below too", len(top.below)) |
| 541 | if len(top.below) == 1 { |
| 542 | lands = "approving it lands the one below too" |
| 543 | } |
| 544 | lines = append(lines, rest+t.fit(lands, rest)) |
| 545 | } |
| 546 | } |
| 547 | return lines |
| 548 | } |
| 549 | |
| 550 | // titled formats e's title line after lead, appending " · <knot ID>" when a |
| 551 | // knot links the change. Long titles are truncated so the knot ID fits in |
| 552 | // full. |
| 553 | func (t terminal) titled(e *entry, lead string) string { |
| 554 | if e.knot == "" { |
| 555 | return t.fit(e.title, lead) |
| 556 | } |
| 557 | tail := " · " + e.knot |
| 558 | return fit(e.title, t.width-columns(lead+tail)) + tail |
| 559 | } |
| 560 | |
| 561 | func prefixed(lead string, lines []string) []string { |
| 562 | var out []string |
| 563 | for _, l := range lines { |
| 564 | out = append(out, lead+l) |
| 565 | } |
| 566 | return out |
| 567 | } |
| 568 | |
| 569 | // nameLine is an entry's first line: its name, in bold on a terminal, and |
| 570 | // the head's age and author at the right margin. When that leaves too |
| 571 | // little room, the author is cut short. When even that leaves too little, |
| 572 | // the age and author go on a line of their own, indented as rest is. |
| 573 | func (t terminal) nameLine(lead, name, suffix string, e *entry, rest string) []string { |
| 574 | left := lead + t.strong(name) + suffix |
| 575 | used := columns(lead + name + suffix) |
| 576 | if e.age == "" && e.author == "" { |
| 577 | return []string{left} |
| 578 | } |
| 579 | right := e.age + " · " + e.author |
| 580 | if room := t.width - used - 2; columns(right) > room { |
| 581 | short := room - columns(e.age+" · ") |
| 582 | if short < 4 { |
| 583 | return []string{left, rest + t.fit(right, rest)} |
| 584 | } |
| 585 | right = e.age + " · " + fit(e.author, short) |
| 586 | } |
| 587 | return []string{left + strings.Repeat(" ", t.width-used-columns(right)) + right} |
| 588 | } |
| 589 | |
| 590 | // branchName is a branch's name as printed: as it is, or, when it holds a |
| 591 | // character that is not printable or bytes that are not UTF-8, in the |
| 592 | // $'...' form a shell reads back as the same name. |
| 593 | func branchName(b string) string { |
| 594 | if printable(b) { |
| 595 | return b |
| 596 | } |
| 597 | return shellQuote(b) |
| 598 | } |
| 599 | |
| 600 | // readable is s as text to print: valid UTF-8, with every control |
| 601 | // character, which a terminal could act on, made a space. |
| 602 | func readable(s string) string { |
| 603 | return strings.Map(func(r rune) rune { |
| 604 | if unicode.IsControl(r) { |
| 605 | return ' ' |
| 606 | } |
| 607 | return r |
| 608 | }, strings.ToValidUTF8(s, "�")) |
| 609 | } |
| 610 | |
| 611 | // checks says how many checks the policy requires of a head, by cost, as |
| 612 | // valley checks derives them against the merge base with main. It runs |
| 613 | // the valley command bin/valley names in VALLEY_COMMAND, inside the |
| 614 | // repository the change is in. |
| 615 | func (s *snapshot) checks(head string) string { |
| 616 | valley := os.Getenv("VALLEY_COMMAND") |
| 617 | if valley == "" { |
| 618 | valley = "valley" |
| 619 | } |
| 620 | base, err := s.g.output(true, "merge-base", s.tip, head) |
| 621 | if err != nil { |
| 622 | return "requires ? (no merge base with main)" |
| 623 | } |
| 624 | cmd := exec.Command(valley, "checks", base, head) |
| 625 | cmd.Dir = s.g.dir |
| 626 | cmd.Env = s.g.env |
| 627 | out, err := cmd.Output() |
| 628 | if err != nil { |
| 629 | return "requires ? (the policy did not derive)" |
| 630 | } |
| 631 | count := func(prefix string) int { |
| 632 | for _, line := range strings.Split(string(out), "\n") { |
| 633 | if rest, ok := strings.CutPrefix(line, prefix); ok && rest != "none" { |
| 634 | return len(strings.Split(rest, ", ")) |
| 635 | } |
| 636 | } |
| 637 | return 0 |
| 638 | } |
| 639 | quick, slow := count("quick: "), count("slow: ") |
| 640 | if quick+slow == 0 { |
| 641 | return "requires no checks" |
| 642 | } |
| 643 | noun := "checks" |
| 644 | if quick == 1 { |
| 645 | noun = "check" |
| 646 | } |
| 647 | return fmt.Sprintf("requires %d quick %s, %d slow", quick, noun, slow) |
| 648 | } |