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