gunk-dev/the-valley

reviewstack.go

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