gunk-dev/the-valley

reviewstack.go

source28abc5737165main's published tip history
14.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, r.verifier(), 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 and keeps the notes taken in it on the top layer, or [s]kip. After
155// a rebase the stack is read again and listed again, so the heads paged
156// are the heads approved, and the top keeps its notes. A rebase refused or
157// left unfinished has said what happened, and only [v]iew diff and [s]kip
158// are offered then. It reports false when the review stops here.
159func (r *review) baseStack() (bool, error) {
160 clause, stopped := "behind main", "; nothing done"
161 for !r.fastForwards(r.layers[0].head) {
162 n := len(r.layers)
163 keys, answers := []string{"[b]ase onto main", "[v]iew diff", "[s]kip"}, "b, v, or s"
164 if clause != "behind main" {
165 keys, answers = []string{"[v]iew diff", "[s]kip"}, "v or s"
166 }
167 r.prompt([]string{r.layers[n-1].name, fmt.Sprintf("stack of %d", n), clause}, keys...)
168 choice, err := r.mustAnswer()
169 if err != nil {
170 return false, err
171 }
172 switch choice {
173 case "b":
174 switch {
175 case clause != "behind main":
176 fmt.Fprintln(r.stdout, "answer "+answers)
177 case r.rebaseShown(r.layers[n-1].head):
178 _ = fetchOrigin(r.g, r.stdout, r.stderr)
179 if r.c.id != "" {
180 handle := r.c.handle
181 if err := r.resolve(r.c.id); err != nil {
182 fmt.Fprintf(r.stderr, "valley: %s\n", err)
183 return false, r.die("could not read change " + handle + " again after the rebase")
184 }
185 }
186 r.bind()
187 notes := r.layers[n-1].notes
188 if !r.readStack() {
189 return false, r.die("could not read the stack under " + r.topName + " again after the rebase")
190 }
191 r.layers[len(r.layers)-1].notes = notes
192 r.summarizeStack()
193 default:
194 clause, stopped, r.refused = "not rebased", "", true
195 }
196 case "v":
197 r.notes = nil
198 if err := r.viewFull(r.c.branch); err != nil {
199 return false, err
200 }
201 r.keepLayerNotes(n - 1)
202 case "s":
203 fmt.Fprintf(r.stdout, "stopped reviewing %s before its first layer%s\n", r.layers[n-1].name, stopped)
204 r.skipped()
205 return false, nil
206 default:
207 fmt.Fprintln(r.stdout, "answer "+answers)
208 }
209 }
210 return true, nil
211}
212
213// rebaseShown is [b]ase onto main for the stack under review, while its
214// top is still at head, the head review showed of it. It is the rebase
215// verb (rebase.go): it rebases the change's whole stack in a throwaway
216// worktree of its own, never the user's checkout, pushes every layer's
217// branch in one atomic push, leased on the tips it read, and adds each
218// change's rebased head to its record as a new version. The judgment on
219// the rebased diff stays with the operator, back at the prompt.
220func (r *review) rebaseShown(head string) bool {
221 name := r.c.id
222 if name == "" {
223 name = r.c.name
224 }
225 status := 0
226 r.inForeground(func() {
227 status = rebaseCancelledBy(r.g, r.verifier(), []string{"--head", head, name}, r.stdout, r.stderr, os.Interrupt)
228 })
229 if status != 0 {
230 return false
231 }
232 // What the checks read was for the heads before the rebase.
233 r.restartChecksRead()
234 return true
235}
236
237// layerHeading is the line that heads layer k wherever it is shown.
238func (r *review) layerHeading(k int) string {
239 below := "main"
240 if k > 0 {
241 below = r.layers[k-1].name
242 }
243 return fmt.Sprintf("change %d of %d: %s on %s", k+1, len(r.layers), r.layers[k].name, below)
244}
245
246// layerHeld is why layer k can't be approved, when it or a layer under it
247// is not ready, or "". A layer that is not ready says so as a change alone
248// does (notReady). A layer above one says which: approving it would land
249// that layer too, and the integrator lands nothing that would bring a
250// version of a change that is not ready.
251func (r *review) layerHeld(k int) string {
252 l := r.layers[k]
253 if held := notReady(l.name, l.version, l.state, l.owner); held != "" {
254 return held
255 }
256 for j := k - 1; j >= 0; j-- {
257 if s := r.layers[j].state; s == "draft" || s == "withdrawn" {
258 return fmt.Sprintf("it would bring %s, change %d of %d, which is %s, so it can't be approved yet.",
259 r.layers[j].name, j+1, len(r.layers), aState(s))
260 }
261 }
262 return ""
263}
264
265// pageLayer shows layer k, as showReview shows a change alone. The layer
266// is compared with its own change's approved version, so a lower layer is
267// set against its own approval, not the top's. Its whole diff is against
268// the layer below it. The notes taken in it are kept under its own branch.
269// When it can't be approved, layerHeld's line follows its heading, before
270// the layer's knots.
271func (r *review) pageLayer(k int) error {
272 r.notes = nil
273 l := r.layers[k]
274 heading := r.layerHeading(k)
275 if held := r.layerHeld(k); held != "" {
276 heading += "\n" + held
277 }
278 if _, err := r.newScratch(); err != nil {
279 return err
280 }
281 kind, compared := r.compareShown(l.id, l.version, l.head)
282 if err := r.showCompared(r.notesKey(k), l.head, heading, r.doesOf(l.id), kind, compared, func(w io.Writer) error { return r.layerDiff(w, k) }); err != nil {
283 return err
284 }
285 if line := r.checksLine(l.id, true); line != "" {
286 fmt.Fprintln(r.stdout, line)
287 }
288 r.keepLayerNotes(k)
289 return nil
290}
291
292// viewLayer is [v]iew full diff for layer k: its whole diff against the
293// layer below it.
294func (r *review) viewLayer(k int) error {
295 r.notes = nil
296 dir, err := r.newScratch()
297 if err != nil {
298 return err
299 }
300 var b bytes.Buffer
301 b.WriteString(r.layerHeading(k) + "\n")
302 if err := r.layerDiff(&b, k); err != nil {
303 return err
304 }
305 if err := r.writePage(dir, b.Bytes()); err != nil {
306 return err
307 }
308 if err := r.pageBuffer(r.notesKey(k), r.short(r.layers[k].head)); err != nil {
309 return err
310 }
311 r.keepLayerNotes(k)
312 return nil
313}
314
315// keepLayerNotes adds the notes just taken to layer k's.
316func (r *review) keepLayerNotes(k int) {
317 r.layers[k].notes = append(r.layers[k].notes, r.notes...)
318}
319
320// layerDiff writes layer k's whole diff: its log and its diff against the
321// layer below it.
322func (r *review) layerDiff(w io.Writer, k int) error {
323 l := r.layers[k]
324 below := "main"
325 if k > 0 {
326 below = r.layers[k-1].name
327 }
328 sha := r.short(l.head)
329 on := below + " at " + r.short(l.base)
330 switch {
331 case l.kind == "branch":
332 fmt.Fprintf(w, "branch %s, log against %s:\n\n", l.name, on)
333 case l.branch != "":
334 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)
335 default:
336 fmt.Fprintf(w, "change %s v%s at %s, log against %s:\n\n", l.name, l.version, sha, on)
337 }
338 if err := r.commitLog(w, l.base+".."+l.head); err != nil {
339 return err
340 }
341 return r.gitTo(w, "--no-pager", "diff", l.base+"..."+l.head)
342}
343
344// reviewStack shows each layer under the top and asks after each, naming
345// the layer and its place: [n]ext layer goes on to the layer above,
346// [a]pprove approves that layer at the head shown, which lands it and
347// every layer under it, as the prompt names them, [v]iew full diff pages
348// the layer's whole diff, and [s]top ends the review. [r]eject is not
349// offered under the top: deleting a lower layer's branch would leave the
350// layers above still holding it. [a]pprove is not offered at or above a
351// layer that is not ready, and the prompt says why in its place
352// (layerHeld). After the top, its verdict is taken as for any change, with
353// a prompt that names the top and says approving it lands all n.
354func (r *review) reviewStack() error {
355 r.summarizeStack()
356 if ok, err := r.baseStack(); !ok {
357 return err
358 }
359 fmt.Fprintln(r.stdout)
360 n, top := len(r.layers), len(r.layers)-1
361 for k := 0; k < top; k++ {
362 if r.layerSkipped(k) {
363 continue
364 }
365 if err := r.pageLayer(k); err != nil {
366 return err
367 }
368 l := r.layers[k]
369 var lands string
370 switch k {
371 case 0:
372 lands = r.layers[0].name + " alone"
373 case 1:
374 lands = r.layers[0].name + " and " + r.layers[1].name
375 default:
376 lands = fmt.Sprintf("the %d up to %s", k+1, l.name)
377 }
378 held := r.layerHeld(k)
379 answering:
380 for {
381 failed, answers := "", "n, a, v, or s"
382 if r.checksFailedAt == l.head {
383 failed = r.short(l.head)
384 }
385 switch {
386 case held != "":
387 fmt.Fprintf(r.stdout, "%s, %d of %d: %s [n]ext layer, [v]iew full diff, [s]top? ", l.name, k+1, n, held)
388 answers = "n, v, or s"
389 case failed != "":
390 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)
391 answers = "n, v, or s"
392 default:
393 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)
394 }
395 choice, err := r.mustAnswer()
396 if err != nil {
397 return err
398 }
399 switch choice {
400 case "n":
401 break answering
402 case "a":
403 if failed != "" || held != "" {
404 fmt.Fprintln(r.stdout, "answer "+answers)
405 continue
406 }
407 r.useLayer(k)
408 moved, err := r.moved(true)
409 if err != nil {
410 return err
411 }
412 if moved || r.approve(r.c.branch) {
413 return nil
414 }
415 r.useLayer(top)
416 case "v":
417 if err := r.viewLayer(k); err != nil {
418 return err
419 }
420 case "s":
421 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)
422 r.skipped()
423 return nil
424 default:
425 fmt.Fprintln(r.stdout, "answer "+answers)
426 }
427 }
428 }
429 if err := r.pageLayer(top); err != nil {
430 return err
431 }
432 r.useLayer(top)
433 return r.decide(r.layers[top].branch, r.layers[top].name, n)
434}
435
436// printStackNotes prints the notes taken on every layer, each under its
437// own branch.
438func (r *review) printStackNotes() {
439 for k, l := range r.layers {
440 r.printNotes(r.notesKey(k), l.notes)
441 }
442}