gunk-dev/the-valley

pending.go

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