| 1 | // valley pending: what is waiting to land, for a person to read. |
| 2 | // |
| 3 | // It lists the ready changes whose active version has not landed, by the |
| 4 | // first two landing rules (inventory.go), one line each: |
| 5 | // |
| 6 | // nxvp v2 | the active version's title | includes xunx | uncertain |
| 7 | // |
| 8 | // A change includes another when its active head strictly contains the |
| 9 | // other's active head, and the other has not landed either. That is |
| 10 | // ancestry alone: approving the change lands the other's content too. |
| 11 | // Only the nearest is named. uncertain is shown when the active version |
| 12 | // may have landed without its evidence being stored. With --owes, each |
| 13 | // line ends with how many checks the change owes, by cost. |
| 14 | // |
| 15 | // Two sections follow. branches lists each branch on origin, other than |
| 16 | // main, that main does not reach and that is no change's active head. |
| 17 | // drafts lists the changes whose record says they are drafts, at the head |
| 18 | // of each active version. |
| 19 | // |
| 20 | // With --json it prints valley-pending/v1: valley-changes/v1's shape, with |
| 21 | // the pending changes alone in changes, the drafts in an array of their |
| 22 | // own, drafts, and the same branches. |
| 23 | |
| 24 | package main |
| 25 | |
| 26 | import ( |
| 27 | "fmt" |
| 28 | "io" |
| 29 | "os" |
| 30 | "os/exec" |
| 31 | "strings" |
| 32 | "unicode/utf8" |
| 33 | |
| 34 | "the-valley/change" |
| 35 | ) |
| 36 | |
| 37 | // pendingChange is a ready change whose active version has not landed. |
| 38 | func pendingChange(s *snapshot, n recorded) bool { |
| 39 | return n.record.State == change.Ready && !s.isLanded(n.id, n.record.ActiveVersion()) |
| 40 | } |
| 41 | |
| 42 | // unlanded is a change in any state whose active version has not landed: |
| 43 | // the changes a pending change may include. |
| 44 | func unlanded(s *snapshot, n recorded) bool { |
| 45 | return !s.isLanded(n.id, n.record.ActiveVersion()) |
| 46 | } |
| 47 | |
| 48 | func isDraft(_ *snapshot, n recorded) bool { |
| 49 | return n.record.State == change.Draft |
| 50 | } |
| 51 | |
| 52 | // pendingView is a repository as valley-pending/v1 writes it: the pending |
| 53 | // changes in changes and the drafts in drafts, each with includes among |
| 54 | // the changes that have not landed. |
| 55 | func (l listed) pendingView(h handleMap) jsonRepo { |
| 56 | r := l.repoView(h, pendingChange, unlanded) |
| 57 | drafts := []jsonChange{} |
| 58 | if l.err == nil { |
| 59 | drafts = l.repoView(h, isDraft, unlanded).Changes |
| 60 | } |
| 61 | r.Drafts = &drafts |
| 62 | return r |
| 63 | } |
| 64 | |
| 65 | func runPending(g git, args []string, stdout, stderr io.Writer) error { |
| 66 | l, err := parseListing("pending", args, true) |
| 67 | if err != nil { |
| 68 | return err |
| 69 | } |
| 70 | list, host, err := sources(g, l, stderr) |
| 71 | if err != nil { |
| 72 | return err |
| 73 | } |
| 74 | repos := readSources(list, verifierFromEnv()) |
| 75 | h := handles(repos) |
| 76 | defer warnUnverified(repos, stderr) |
| 77 | if l.asJSON { |
| 78 | doc := jsonChanges{Schema: pendingSchema, Host: host, Repos: []jsonRepo{}} |
| 79 | for _, r := range repos { |
| 80 | view := r.pendingView(h) |
| 81 | utf8Branches(&view, stderr) |
| 82 | doc.Repos = append(doc.Repos, view) |
| 83 | } |
| 84 | return writeDocument(stdout, doc) |
| 85 | } |
| 86 | for i, r := range repos { |
| 87 | if i > 0 { |
| 88 | fmt.Fprintln(stdout) |
| 89 | } |
| 90 | if err := writePending(r, h, l, stdout, len(repos) > 1 || l.all || l.repo != ""); err != nil { |
| 91 | return err |
| 92 | } |
| 93 | } |
| 94 | return nil |
| 95 | } |
| 96 | |
| 97 | // writePending prints one repository's pending changes, then its |
| 98 | // unrecorded branches and its drafts. Several repositories are each |
| 99 | // headed by their name. |
| 100 | func writePending(r listed, h handleMap, l listing, w io.Writer, headed bool) error { |
| 101 | p := func(format string, args ...any) { fmt.Fprintf(w, format, args...) } |
| 102 | heading := func(section string) { |
| 103 | switch { |
| 104 | case headed && section == "": |
| 105 | p("%s\n", r.name) |
| 106 | case headed: |
| 107 | p("\n%s %s\n", r.name, section) |
| 108 | case section != "": |
| 109 | p("\n%s\n", section) |
| 110 | } |
| 111 | } |
| 112 | if r.err != nil { |
| 113 | p("%s: %v\n", r.name, r.err) |
| 114 | return nil |
| 115 | } |
| 116 | view := r.pendingView(h) |
| 117 | heading("") |
| 118 | for _, c := range view.Changes { |
| 119 | v := c.Versions[c.Active-1] |
| 120 | p("%s v%d | %s", c.Handle, v.N, v.Title) |
| 121 | if len(c.Includes) > 0 { |
| 122 | var named []string |
| 123 | for _, id := range c.Includes { |
| 124 | named = append(named, h[handleKey{r.name, c.Target, id}]) |
| 125 | } |
| 126 | p(" | includes %s", strings.Join(named, ", ")) |
| 127 | } |
| 128 | if v.Landing == uncertain { |
| 129 | p(" | uncertain") |
| 130 | } |
| 131 | if l.owes { |
| 132 | p(" | %s", r.snap.owes(v.Head)) |
| 133 | } |
| 134 | p("\n") |
| 135 | } |
| 136 | if len(view.Changes) == 0 { |
| 137 | p("no change pending\n") |
| 138 | } |
| 139 | if len(view.Branches) > 0 { |
| 140 | heading("branches") |
| 141 | for _, b := range view.Branches { |
| 142 | p("%s\n", r.snap.pendingLine(b.Branch, b.Head)) |
| 143 | } |
| 144 | } |
| 145 | if len(*view.Drafts) > 0 { |
| 146 | heading("drafts") |
| 147 | } |
| 148 | for _, c := range *view.Drafts { |
| 149 | v := c.Versions[c.Active-1] |
| 150 | label := fmt.Sprintf("%s v%d", c.Handle, v.N) |
| 151 | if v.Branch != "" { |
| 152 | label += " on " + v.Branch |
| 153 | } |
| 154 | p("%s\n", r.snap.pendingLine(label, v.Head)) |
| 155 | } |
| 156 | return nil |
| 157 | } |
| 158 | |
| 159 | // pendingLine is one line of pending for a commit: the label, then the |
| 160 | // commit's subject, age and author, how far it is ahead of main, and its |
| 161 | // diffstat against the merge base. |
| 162 | func (s *snapshot) pendingLine(label, oid string) string { |
| 163 | out, _ := s.g.output(true, "log", "-1", "--format=%s%x00%cr%x00%an", oid) |
| 164 | fields := strings.SplitN(out, "\x00", 3) |
| 165 | for len(fields) < 3 { |
| 166 | fields = append(fields, "") |
| 167 | } |
| 168 | subject := fields[0] |
| 169 | if utf8.RuneCountInString(subject) > 60 { |
| 170 | subject = string([]rune(subject)[:57]) + "..." |
| 171 | } |
| 172 | ahead, stat := "?", "" |
| 173 | if s.tip != "" { |
| 174 | ahead, _ = s.g.output(true, "rev-list", "--count", s.tip+".."+oid) |
| 175 | stat, _ = s.g.output(true, "diff", "--shortstat", s.tip+"..."+oid) |
| 176 | } |
| 177 | stat = strings.TrimPrefix(stat, " ") |
| 178 | if stat == "" { |
| 179 | stat = "no file changes" |
| 180 | } |
| 181 | return fmt.Sprintf("%s | %s | %s | %s | ahead %s | %s", label, subject, fields[1], fields[2], ahead, stat) |
| 182 | } |
| 183 | |
| 184 | // owes says how many checks a head owes under the policy, by cost, as |
| 185 | // valley checks derives them against the merge base with main. It runs |
| 186 | // the valley command bin/valley names in VALLEY_COMMAND, inside the |
| 187 | // repository the change is in. |
| 188 | func (s *snapshot) owes(head string) string { |
| 189 | valley := os.Getenv("VALLEY_COMMAND") |
| 190 | if valley == "" { |
| 191 | valley = "valley" |
| 192 | } |
| 193 | base, err := s.g.output(true, "merge-base", s.tip, head) |
| 194 | if err != nil { |
| 195 | return "owes ? (no merge base with main)" |
| 196 | } |
| 197 | cmd := exec.Command(valley, "checks", base, head) |
| 198 | cmd.Dir = s.g.dir |
| 199 | cmd.Env = s.g.env |
| 200 | out, err := cmd.Output() |
| 201 | if err != nil { |
| 202 | return "owes ? (the policy did not derive)" |
| 203 | } |
| 204 | count := func(prefix string) int { |
| 205 | for _, line := range strings.Split(string(out), "\n") { |
| 206 | if rest, ok := strings.CutPrefix(line, prefix); ok && rest != "none" { |
| 207 | return len(strings.Split(rest, ", ")) |
| 208 | } |
| 209 | } |
| 210 | return 0 |
| 211 | } |
| 212 | quick, slow := count("quick: "), count("slow: ") |
| 213 | if quick+slow == 0 { |
| 214 | return "owes nothing" |
| 215 | } |
| 216 | return fmt.Sprintf("owes %d quick, %d slow", quick, slow) |
| 217 | } |