gunk-dev/the-valley

reviewstack.go

source7309e1d6b8bbmain's published tip history
13.3 KiB
1package main
2
3// A stack, a change with layers under it that have not landed, is
4// reviewed one layer at a time, bottom first, each against the layer below
5// it, the way it was built. Approving the top lands every layer under it,
6// so reading the top against main alone would ask the operator to judge
7// the whole stack as one diff.
8//
9// The layers are stack.go's, bottom first, the change itself last. A
10// layer's notes are kept under its branch: a branch's own name, or the
11// branch a change's active version names.
12
13import (
14 "bytes"
15 "fmt"
16 "io"
17 "os"
18 "strconv"
19)
20
21// reviewLayer is one layer of the stack under review.
22type reviewLayer struct {
23 stackLayer
24 id, version, branch, owner string
25 notes []string // taken on the layer, for the print after the verdict
26}
27
28// readStack reads the stack under the change review resolved. Each change
29// under the top is read again with resolve, for its ID, version, branch
30// and owner, and must still be at the head the stack names. The top must
31// be what review resolved. It reports false, having said why, when either
32// does not hold or the stack cannot be read.
33func (r *review) readStack() bool {
34 r.layers = nil
35 name := r.c.id
36 if name == "" {
37 name = "origin/" + r.c.branch
38 }
39 read, err := stackLayers(r.g, name)
40 if err != nil {
41 fmt.Fprintf(r.stderr, "valley: %s\n", err)
42 return false
43 }
44 var layers []reviewLayer
45 for _, l := range read {
46 layers = append(layers, reviewLayer{stackLayer: l, branch: l.name})
47 }
48 last := len(layers) - 1
49 kind := "branch"
50 if r.c.id != "" {
51 kind = "change"
52 }
53 if last < 0 || layers[last].head != r.changeHead(r.c.branch) || layers[last].kind != kind {
54 return r.refuse(r.topName + " moved while its stack was read")
55 }
56 top := &layers[last]
57 top.id, top.version, top.branch, top.owner = r.c.id, r.c.version, r.c.branch, r.c.owner
58 for k := range layers[:last] {
59 l := &layers[k]
60 if l.kind != "change" {
61 continue
62 }
63 res, err := resolveRecorded(r.g, l.name)
64 if err != nil {
65 fmt.Fprintf(r.stderr, "valley: %s\n", err)
66 return false
67 }
68 if res.id == "" || res.head != l.head {
69 return r.refuse("change " + l.name + ", under " + r.topName + ", moved while the stack was read")
70 }
71 l.id, l.version, l.branch, l.state, l.owner = res.id, strconv.Itoa(res.version), res.branch, res.state, res.owner
72 }
73 r.layers = layers
74 return true
75}
76
77// useLayer acts on layer k as review acts on a change it was given,
78// binding [a]pprove to the head the stack shows. A branch under the top is
79// read again as origin/<branch>, which no handle can shadow.
80func (r *review) useLayer(k int) {
81 l := r.layers[k]
82 r.c = shownChange{name: "origin/" + l.name, id: l.id, version: l.version, branch: l.branch, state: l.state, owner: l.owner}
83 if k == len(r.layers)-1 {
84 r.c.name = r.topName
85 }
86 if l.id != "" {
87 r.c.name, r.c.handle, r.c.head = l.name, l.name, l.head
88 }
89 r.bind()
90 if l.id == "" {
91 r.approveHead = l.head
92 }
93}
94
95// notesKey is the branch layer k's notes are kept under. A change whose
96// active version names no branch keeps them under its handle.
97func (r *review) notesKey(k int) string {
98 if b := r.layers[k].branch; b != "" {
99 return b
100 }
101 return r.layers[k].name
102}
103
104// layerSkipped reports a layer below the top that is approved, which asks
105// nothing of the reviewer: the valley carries it to landing once its
106// checks pass. The review does not page it. The top is the change review
107// was named, so it is always paged.
108func (r *review) layerSkipped(k int) bool {
109 return k < len(r.layers)-1 && r.layers[k].approved == "approved"
110}
111
112// layerLabel is how a layer is named in the list: its name, then branch,
113// or the change's state.
114func (r *review) layerLabel(k int) string {
115 l := r.layers[k]
116 switch {
117 case l.kind == "branch":
118 return l.name + " (branch)"
119 case r.layerSkipped(k):
120 return l.name + " (approved, not paged)"
121 }
122 return l.name + " (" + l.state + ")"
123}
124
125// aState is a state that is not ready, as a sentence names it.
126func aState(state string) string {
127 if state == "draft" {
128 return "a draft"
129 }
130 return "withdrawn"
131}
132
133// summarizeStack says what is coming: the layers bottom first, the lower
134// layers the integrator would refuse, and what each answer lands.
135func (r *review) summarizeStack() {
136 n := len(r.layers)
137 top := r.layers[n-1].name
138 fmt.Fprintf(r.stdout, "%s is a stack of %d changes; reviewing from the bottom, each against the one below it:\n", top, n)
139 for k, l := range r.layers {
140 fmt.Fprintf(r.stdout, " %d. %s: %s\n", k+1, r.layerLabel(k), l.title)
141 }
142 for k, l := range r.layers[:n-1] {
143 if l.state == "draft" || l.state == "withdrawn" {
144 fmt.Fprintf(r.stdout, "warning: %s, change %d of %d, is %s. The integrator refuses to land it or anything above it (refused-not-ready).\n",
145 l.name, k+1, n, aState(l.state))
146 }
147 }
148 fmt.Fprintf(r.stdout, "read each layer and go on with [n]; approving %s at the top lands all %d. [a] on a lower layer lands it and the layers under it, and ends the review.\n", top, n)
149}
150
151// baseStack offers a stack whose bottom does not fast-forward from main,
152// before any layer is paged, [b]ase, which rebases every layer as the
153// rebase verb does (rebase.go), [v]iew diff, which pages the top's whole
154// diff, or [s]kip. After a rebase the stack is read again and listed
155// again, so the heads paged are the heads approved. A rebase refused or
156// left unfinished has said what happened, and only [v]iew diff and [s]kip
157// are offered then. It reports false when the review stops here.
158func (r *review) baseStack() bool {
159 clause, stopped := "behind main", "; nothing done"
160 for !r.fastForwards(r.layers[0].head) {
161 n := len(r.layers)
162 keys, answers := []string{"[b]ase onto main", "[v]iew diff", "[s]kip"}, "b, v, or s"
163 if clause != "behind main" {
164 keys, answers = []string{"[v]iew diff", "[s]kip"}, "v or s"
165 }
166 r.prompt([]string{r.layers[n-1].name, fmt.Sprintf("stack of %d", n), clause}, keys...)
167 switch r.mustAnswer() {
168 case "b":
169 switch {
170 case clause != "behind main":
171 fmt.Fprintln(r.stdout, "answer "+answers)
172 case r.rebaseShown(r.layers[n-1].head):
173 _ = fetchOrigin(r.g, r.stdout, r.stderr)
174 if r.c.id != "" {
175 handle := r.c.handle
176 if err := r.resolve(r.c.id); err != nil {
177 fmt.Fprintf(r.stderr, "valley: %s\n", err)
178 r.die("could not read change " + handle + " again after the rebase")
179 }
180 }
181 r.bind()
182 if !r.readStack() {
183 r.die("could not read the stack under " + r.topName + " again after the rebase")
184 }
185 r.summarizeStack()
186 default:
187 clause, stopped, r.walkRefused = "not rebased", "", true
188 }
189 case "v":
190 r.viewFull(r.c.branch)
191 case "s":
192 fmt.Fprintf(r.stdout, "stopped reviewing %s before its first layer%s\n", r.layers[n-1].name, stopped)
193 r.skipped()
194 return false
195 default:
196 fmt.Fprintln(r.stdout, "answer "+answers)
197 }
198 }
199 return true
200}
201
202// rebaseShown is [b]ase onto main for the stack under review, while its
203// top is still at head, the head review showed of it. It is the rebase
204// verb (rebase.go): it rebases the change's whole stack in a throwaway
205// worktree of its own, never the user's checkout, pushes every layer's
206// branch in one atomic push, leased on the tips it read, and adds each
207// change's rebased head to its record as a new version. The judgment on
208// the rebased diff stays with the operator, back at the prompt.
209func (r *review) rebaseShown(head string) bool {
210 name := r.c.id
211 if name == "" {
212 name = r.c.name
213 }
214 status := 0
215 r.inForeground(func() {
216 status = rebaseCancelledBy(r.g, []string{"--head", head, name}, r.stdout, r.stderr, os.Interrupt)
217 })
218 if status != 0 {
219 return false
220 }
221 // What the checks read was for the heads before the rebase.
222 r.restartChecksRead()
223 return true
224}
225
226// layerHeading is the line that heads layer k wherever it is shown.
227func (r *review) layerHeading(k int) string {
228 below := "main"
229 if k > 0 {
230 below = r.layers[k-1].name
231 }
232 return fmt.Sprintf("change %d of %d: %s on %s", k+1, len(r.layers), r.layers[k].name, below)
233}
234
235// layerHeld is why layer k can't be approved, when it or a layer under it
236// is not ready, or "". A layer that is not ready says so as a change alone
237// does (notReady). A layer above one says which: approving it would land
238// that layer too, and the integrator lands nothing that would bring a
239// version of a change that is not ready.
240func (r *review) layerHeld(k int) string {
241 l := r.layers[k]
242 if held := notReady(l.name, l.version, l.state, l.owner); held != "" {
243 return held
244 }
245 for j := k - 1; j >= 0; j-- {
246 if s := r.layers[j].state; s == "draft" || s == "withdrawn" {
247 return fmt.Sprintf("it would bring %s, change %d of %d, which is %s, so it can't be approved yet.",
248 r.layers[j].name, j+1, len(r.layers), aState(s))
249 }
250 }
251 return ""
252}
253
254// pageLayer shows layer k, as showReview shows a change alone. The layer
255// is compared with its own change's approved version, so a lower layer is
256// set against its own approval, not the top's. Its whole diff is against
257// the layer below it. The notes taken in it are kept under its own branch.
258// When it can't be approved, layerHeld's line follows its heading, before
259// the layer's knots.
260func (r *review) pageLayer(k int) {
261 r.notes = nil
262 l := r.layers[k]
263 heading := r.layerHeading(k)
264 if held := r.layerHeld(k); held != "" {
265 heading += "\n" + held
266 }
267 r.newScratch()
268 kind, compared := r.compareShown(l.id, l.version, l.head)
269 r.showCompared(r.notesKey(k), l.head, heading, r.doesOf(l.id), kind, compared, func(w io.Writer) { r.layerDiff(w, k) })
270 if line := r.checksLine(l.id, true); line != "" {
271 fmt.Fprintln(r.stdout, line)
272 }
273 r.keepLayerNotes(k)
274}
275
276// viewLayer is [v]iew full diff for layer k: its whole diff against the
277// layer below it.
278func (r *review) viewLayer(k int) {
279 r.notes = nil
280 dir := r.newScratch()
281 var b bytes.Buffer
282 b.WriteString(r.layerHeading(k) + "\n")
283 r.layerDiff(&b, k)
284 r.writePage(dir, b.Bytes())
285 r.pageBuffer(r.notesKey(k), r.short(r.layers[k].head))
286 r.keepLayerNotes(k)
287}
288
289// keepLayerNotes adds the notes just taken to layer k's.
290func (r *review) keepLayerNotes(k int) {
291 r.layers[k].notes = append(r.layers[k].notes, r.notes...)
292}
293
294// layerDiff writes layer k's whole diff: its log and its diff against the
295// layer below it.
296func (r *review) layerDiff(w io.Writer, k int) {
297 l := r.layers[k]
298 below := "main"
299 if k > 0 {
300 below = r.layers[k-1].name
301 }
302 sha := r.short(l.head)
303 on := below + " at " + r.short(l.base)
304 switch {
305 case l.kind == "branch":
306 fmt.Fprintf(w, "branch %s, log against %s:\n\n", l.name, on)
307 case l.branch != "":
308 fmt.Fprintf(w, "change %s v%s at %s, on branch %s, log against %s:\n\n", l.name, l.version, sha, l.branch, on)
309 default:
310 fmt.Fprintf(w, "change %s v%s at %s, log against %s:\n\n", l.name, l.version, sha, on)
311 }
312 r.commitLog(w, l.base+".."+l.head)
313 r.gitTo(w, "--no-pager", "diff", l.base+"..."+l.head)
314}
315
316// reviewStack shows each layer under the top and asks after each, naming
317// the layer and its place: [n]ext layer goes on to the layer above,
318// [a]pprove approves that layer at the head shown, which lands it and
319// every layer under it, as the prompt names them, [v]iew full diff pages
320// the layer's whole diff, and [s]top ends the review. [r]eject is not
321// offered under the top: deleting a lower layer's branch would leave the
322// layers above still holding it. [a]pprove is not offered at or above a
323// layer that is not ready, and the prompt says why in its place
324// (layerHeld). After the top, its verdict is taken as for any change, with
325// a prompt that names the top and says approving it lands all n.
326func (r *review) reviewStack() {
327 r.summarizeStack()
328 if !r.baseStack() {
329 return
330 }
331 fmt.Fprintln(r.stdout)
332 n, top := len(r.layers), len(r.layers)-1
333 for k := 0; k < top; k++ {
334 if r.layerSkipped(k) {
335 continue
336 }
337 r.pageLayer(k)
338 l := r.layers[k]
339 var lands string
340 switch k {
341 case 0:
342 lands = r.layers[0].name + " alone"
343 case 1:
344 lands = r.layers[0].name + " and " + r.layers[1].name
345 default:
346 lands = fmt.Sprintf("the %d up to %s", k+1, l.name)
347 }
348 held := r.layerHeld(k)
349 answering:
350 for {
351 failed, answers := "", "n, a, v, or s"
352 if r.checksFailedAt == l.head {
353 failed = r.short(l.head)
354 }
355 switch {
356 case held != "":
357 fmt.Fprintf(r.stdout, "%s, %d of %d: %s [n]ext layer, [v]iew full diff, [s]top? ", l.name, k+1, n, held)
358 answers = "n, v, or s"
359 case failed != "":
360 fmt.Fprintf(r.stdout, "%s, %d of %d: its checks failed at %s, so approving needs a new head: [n]ext layer, [v]iew full diff, [s]top? ", l.name, k+1, n, failed)
361 answers = "n, v, or s"
362 default:
363 fmt.Fprintf(r.stdout, "%s, %d of %d: [n]ext layer, [a]pprove %s, [v]iew full diff, [s]top? ", l.name, k+1, n, lands)
364 }
365 switch r.mustAnswer() {
366 case "n":
367 break answering
368 case "a":
369 if failed != "" || held != "" {
370 fmt.Fprintln(r.stdout, "answer "+answers)
371 continue
372 }
373 r.useLayer(k)
374 if r.moved(true) || r.approve(r.c.branch) {
375 return
376 }
377 r.useLayer(top)
378 case "v":
379 r.viewLayer(k)
380 case "s":
381 fmt.Fprintf(r.stdout, "stopped reviewing %s at change %d of %d, %s; nothing done\n", r.layers[top].name, k+1, n, l.name)
382 r.skipped()
383 return
384 default:
385 fmt.Fprintln(r.stdout, "answer "+answers)
386 }
387 }
388 }
389 r.pageLayer(top)
390 r.useLayer(top)
391 r.decide(r.layers[top].branch, r.layers[top].name, n)
392}
393
394// printStackNotes prints the notes taken on every layer, each under its
395// own branch.
396func (r *review) printStackNotes() {
397 for k, l := range r.layers {
398 r.printNotes(r.notesKey(k), l.notes)
399 }
400}