gunk-dev/the-valley

reviewpage.go

source734bfb035238main's published tip history
22.0 KiB
1package main
2
3// What review shows of a change: the page, set against the change's
4// approved version; the pager it is read in; the notes the reviewer
5// inserts into it; and the checks line above the prompt.
6//
7// The page is set against the change's approved version: the greatest
8// version below it whose head a request or an outcome on origin names
9// (compare.go). none has no approved version to compare with, so review
10// pages the whole diff, under any note compare gives, such as an approved
11// version it could not compare. clean has the approved version's file
12// delta: review prints that in a few lines, with the landings between the
13// two bases, and pages nothing. changed pages the files whose change
14// differs. When a commit was edited, that is what the shown version
15// changes beyond the approved version carried onto main, and each path in
16// conflict with main; -v adds the range-diff, whose hunks carry no line
17// numbers, so a note taken beside it is unanchored, as is one beside the
18// approved version's own diff of a conflicted path, which is indented
19// since its new side is not the shown head. Every other diff on the page
20// is in git's ordinary form with the shown head on its new side, so a
21// note taken there keeps its file and line. [v]iew full diff pages
22// the whole diff at every prompt. The comparison files no request and
23// skips no check.
24//
25// The page is a file rather than a pipe, so less's v key has something to
26// hand the editor: editing it is how notes are written. Whatever comes
27// back with lines inserted is read as notes: no marker, no syntax, no
28// mode. An inserted line is a note because it was not there before, and
29// it is anchored to the file and the new-side line number of the diff
30// line above it, which is the pair a reviewer would otherwise copy out by
31// hand. Notes are kept in $XDG_STATE_HOME/valley/notes, since they are
32// neither config nor cache, and losing them loses judgment.
33
34import (
35 "bytes"
36 "fmt"
37 "io"
38 "os"
39 "os/exec"
40 "path/filepath"
41 "regexp"
42 "strconv"
43 "strings"
44 "syscall"
45 "time"
46)
47
48// showReview shows a change alone. A change that is not ready says so
49// first, before anything else shown of it, and that notice becomes the
50// heading, so the change's knots follow it.
51func (r *review) showReview(branch string) error {
52 head := r.changeHead(branch)
53 // [a]pprove approves exactly the head shown here (bind).
54 if r.c.id == "" {
55 r.approveHead = head
56 }
57 held := notReady(r.c.handle, r.c.version, r.c.state, r.c.owner)
58 if _, err := r.newScratch(); err != nil {
59 return err
60 }
61 kind, compared := r.compareShown(r.c.id, r.c.version, head)
62 if err := r.showCompared(branch, head, held, r.doesOf(r.c.id), kind, compared, func(w io.Writer) error { return r.fullDiff(w, branch, head) }); err != nil {
63 return err
64 }
65 r.checksClause = r.checksLine(r.c.id, false)
66 return nil
67}
68
69// viewFull is [v]iew full diff for a change alone: its whole diff paged,
70// and the notes taken in it kept.
71func (r *review) viewFull(branch string) error {
72 head := r.changeHead(branch)
73 dir, err := r.newScratch()
74 if err != nil {
75 return err
76 }
77 var b bytes.Buffer
78 if err := r.fullDiff(&b, branch, head); err != nil {
79 return err
80 }
81 if err := r.writePage(dir, b.Bytes()); err != nil {
82 return err
83 }
84 return r.pageBuffer(branch, r.short(head))
85}
86
87// compareShown sets the version shown against the change's approved
88// version, against main's tip as the fetch left it. It returns none,
89// clean or changed, and the text to show. A plain branch is none, and so
90// is a comparison that could not be made, with a warning.
91func (r *review) compareShown(id, version, head string) (kind, text string) {
92 if id == "" {
93 return compareNone, ""
94 }
95 var out bytes.Buffer
96 compared := false
97 if tip := r.ref("refs/remotes/origin/main"); tip != "" {
98 args := []string{"--tip", tip, id, version, head}
99 if r.verbose {
100 args = append([]string{"-v"}, args...)
101 }
102 if err := runCompare(r.g, args, &out); err != nil {
103 fmt.Fprintf(r.stderr, "valley: %s\n", err)
104 } else {
105 compared = true
106 }
107 }
108 if !compared {
109 fmt.Fprintf(r.stderr, "valley: warning: could not compare v%s of change %s with its approved version; reviewing it in full\n", version, id)
110 return compareNone, ""
111 }
112 kind, text, _ = strings.Cut(out.String(), "\n")
113 return kind, text
114}
115
116// showCompared shows what compareShown found, under the heading when
117// there is one. A clean restack is printed, and nothing is paged. A
118// changed version pages the comparison. Otherwise the writer writes the
119// whole diff, and it is paged. Notes taken in a page are kept under key,
120// against head.
121//
122// Knots follow the heading when there is one. Otherwise they follow the
123// first line of the comparison or the whole diff, which names the change.
124func (r *review) showCompared(key, head, heading, knots, kind, compared string, writer func(io.Writer) error) error {
125 sha := r.short(head)
126 var body bytes.Buffer
127 body.WriteString(compared)
128 if kind == compareNone {
129 if err := writer(&body); err != nil {
130 return err
131 }
132 }
133 var page bytes.Buffer
134 if heading != "" {
135 page.WriteString(heading + "\n")
136 if knots != "" {
137 page.WriteString(knots + "\n")
138 }
139 page.Write(body.Bytes())
140 } else if body.Len() > 0 {
141 first, rest, _ := strings.Cut(body.String(), "\n")
142 page.WriteString(first + "\n")
143 if knots != "" {
144 page.WriteString(knots + "\n")
145 }
146 page.WriteString(rest)
147 }
148 r.mu.Lock()
149 dir := r.scratch
150 r.mu.Unlock()
151 if err := r.writePage(dir, page.Bytes()); err != nil {
152 return err
153 }
154 if kind == compareClean {
155 f, err := os.Open(filepath.Join(dir, "review.diff"))
156 if err == nil {
157 runStyle(nil, f, r.stdout)
158 f.Close()
159 }
160 r.dropScratch()
161 return nil
162 }
163 return r.pageBuffer(key, sha)
164}
165
166// writePage writes the page to review.diff in dir.
167func (r *review) writePage(dir string, page []byte) error {
168 if err := os.WriteFile(filepath.Join(dir, "review.diff"), page, 0o600); err != nil {
169 return r.die(fmt.Sprintf("could not write the page: %v", err))
170 }
171 return nil
172}
173
174// fullDiff writes the whole diff of a change alone: its log and its diff
175// against origin/main. main is read by its exact name, since git would
176// take a tag named origin/main for it. A main that cannot be read ends the
177// review, as a page it cannot write does (gitTo).
178func (r *review) fullDiff(w io.Writer, branch, head string) error {
179 sha := r.short(head)
180 tip := r.ref("refs/remotes/origin/main")
181 if tip == "" {
182 return reviewEnded{1}
183 }
184 if r.c.head != "" {
185 fmt.Fprintf(w, "change %s v%s at %s, on branch %s, log against origin/main:\n\n", r.c.handle, r.c.version, sha, branch)
186 } else {
187 fmt.Fprintf(w, "branch %s, log against origin/main:\n\n", branch)
188 }
189 if err := r.commitLog(w, tip+".."+head); err != nil {
190 return err
191 }
192 return r.gitTo(w, "--no-pager", "diff", tip+"..."+head)
193}
194
195// commitLog writes each commit in the range, newest first: its short hash
196// and subject, its body indented four spaces, and a blank line. The whole
197// message is shown because a change lands with its head commit's. The
198// indent keeps a body line from reading as a diff header, to the notes'
199// anchors and to the diff pager alike.
200func (r *review) commitLog(w io.Writer, revs string) error {
201 return r.gitTo(w, "--no-pager", "log", "--format=%h %s%n%w(0,4,4)%b", revs)
202}
203
204// gitTo runs git with its output written to w. A git that fails ends the
205// review with git's status, having said why: the page would be incomplete,
206// and no answer is asked about a page the operator could not read whole.
207func (r *review) gitTo(w io.Writer, args ...string) error {
208 cmd := r.g.command(args...)
209 cmd.Stdout, cmd.Stderr = w, r.stderr
210 processStarted()
211 if err := cmd.Run(); err != nil {
212 return reviewEnded{exitCode(err)}
213 }
214 return nil
215}
216
217// pageBuffer pages the page written to the scratch directory, keeps the
218// notes taken in it under key, against the commit shown, and removes it.
219func (r *review) pageBuffer(key, sha string) error {
220 r.mu.Lock()
221 dir := r.scratch
222 r.mu.Unlock()
223 page := filepath.Join(dir, "review.diff")
224 orig := filepath.Join(dir, "review.orig")
225 if data, err := os.ReadFile(page); err == nil {
226 if err := os.WriteFile(orig, data, 0o600); err != nil {
227 return r.die(fmt.Sprintf("could not keep the page: %v", err))
228 }
229 }
230 r.page(page)
231 r.collectNotes(key, sha, orig, page)
232 r.dropScratch()
233 return nil
234}
235
236// diffPager is the pager git would use for a diff: pager.diff first, as
237// per-command pager configurations set it and leave core.pager alone,
238// then git var GIT_PAGER (core.pager, GIT_PAGER, PAGER, less). pager.diff
239// may legally be a boolean; true, false and an empty value are taken as
240// unset. The value is a shell command line.
241func (r *review) diffPager() string {
242 pager, _ := r.g.output(false, "config", "--get", "pager.diff")
243 switch pager {
244 case "", "true", "false":
245 pager, _ = r.g.output(false, "var", "GIT_PAGER")
246 }
247 return pager
248}
249
250// pageEdit is what less's v key runs (LESSEDIT): the editor less's %E
251// names, VISUAL, then EDITOR, then vi, each a shell command line, on the
252// raw page, and then the rendering again, so the view less reopens shows
253// the notes in place. Paths reach it through the environment, since
254// LESSEDIT is a prompt string in which % ? . : and \ are special.
255const pageEdit = `eval "${VISUAL:-${EDITOR:-vi}} \"\$@\" \"\$VALLEY_PAGE_RAW\""
256 sh -c "$VALLEY_PAGE_RENDER" < "$VALLEY_PAGE_RAW" > "$VALLEY_PAGE_SHOWN"`
257
258// page shows file to the operator. Off a terminal it is printed.
259//
260// The configured diff pager is the renderer, run once as a filter into a
261// sibling file that less pages, with style (style.go) after it, so the
262// pager never reads style's escapes and the raw page has none. v runs
263// pageEdit. What this costs is the line number: v opens the editor at
264// +%lm, a line of the rendered view, so a filter that restructures the
265// diff, as delta's default rendering does, opens it near the cursor
266// rather than on it. A structure-preserving filter (pager.diff = `delta
267// --color-only`) makes it exact. No anchor depends on it: a note is
268// anchored by where it was inserted, not by where the editor opened.
269//
270// A pager that is less already keeps its options, and style alone renders.
271// Without less there is no editor key, and the pager reads the page
272// through a pipe.
273func (r *review) page(file string) {
274 if !terminalOf(r.stdout).live {
275 if data, err := os.ReadFile(file); err == nil {
276 r.stdout.Write(data)
277 }
278 return
279 }
280 shown := strings.TrimSuffix(file, ".diff") + ".shown"
281 pager, view := r.diffPager(), "less"
282 exe, exeErr := os.Executable()
283 first := pager
284 if i := strings.IndexAny(pager, " \t\n\v\f\r"); i >= 0 {
285 first = pager[:i]
286 }
287 if first == "less" || strings.HasSuffix(first, "/less") {
288 if exeErr != nil {
289 r.shell(pager+` "$1"`, nil, nil, nil, file)
290 return
291 }
292 view, pager = pager, "cat"
293 }
294 if _, err := exec.LookPath("less"); err != nil {
295 in, err := os.Open(file)
296 if err == nil {
297 r.shell(pager, in, nil, nil)
298 in.Close()
299 }
300 return
301 }
302 if exeErr == nil {
303 pager += " | " + shellQuote(exe) + " style --paged"
304 }
305 in, err := os.Open(file)
306 if err != nil {
307 return
308 }
309 out, err := os.Create(shown)
310 if err == nil {
311 r.shell(pager, in, out, nil)
312 out.Close()
313 }
314 in.Close()
315 // -R because the filter's colour is the whole point of running it. No
316 // LESSOPEN, since the filter has run and less answers v on a file read
317 // through one with a warning, then runs the editor on whatever key comes
318 // next, q included; and so no LESSCLOSE.
319 env := []string{}
320 for _, kv := range r.g.env {
321 name, _, _ := strings.Cut(kv, "=")
322 switch name {
323 case "LESSOPEN", "LESSCLOSE", "LESSEDIT", "VALLEY_PAGE_RAW", "VALLEY_PAGE_SHOWN", "VALLEY_PAGE_RENDER", "VALLEY_PAGE_EDIT":
324 continue
325 }
326 env = append(env, kv)
327 }
328 env = append(env, "VALLEY_PAGE_RAW="+file, "VALLEY_PAGE_SHOWN="+shown, "VALLEY_PAGE_RENDER="+pager,
329 "VALLEY_PAGE_EDIT="+pageEdit, `LESSEDIT=sh -c "$VALLEY_PAGE_EDIT" sh ?lm+%lm.`)
330 r.shell(view+` -R -- "$1"`, nil, nil, env, shown)
331}
332
333// shell runs a shell command line in the foreground, with the arguments
334// given as $1 and on, stdin and stdout the terminal's unless given, in the
335// repository and the environment of the review's, or env.
336func (r *review) shell(line string, stdin io.Reader, stdout io.Writer, env []string, args ...string) {
337 cmd := r.g.program("sh", append([]string{"-c", line, "sh"}, args...)...)
338 cmd.Stdin, cmd.Stdout, cmd.Stderr = r.stdin, r.stdout, r.stderr
339 if stdin != nil {
340 cmd.Stdin = stdin
341 }
342 if stdout != nil {
343 cmd.Stdout = stdout
344 }
345 if env != nil {
346 cmd.Env = env
347 }
348 r.inForeground(func() { _ = cmd.Run() })
349}
350
351// anchor is where a line of the page sits: the file it belongs to, and
352// the new-side line number. No path is above every file (the log); line 0
353// is a file header, above every hunk.
354type anchor struct {
355 path string
356 line int
357}
358
359// diffHeader is a diff's file header, its b/ side the name a rename will
360// have.
361var diffHeader = regexp.MustCompile(`^diff --git a/(.*) b/(.*)$`)
362
363// pageLines are the lines of data that end in a newline.
364func pageLines(data []byte) []string {
365 s := string(data)
366 if i := strings.LastIndexByte(s, '\n'); i >= 0 {
367 return strings.Split(s[:i], "\n")
368 }
369 return nil
370}
371
372// leadingNumber is the number s starts with, or 0.
373func leadingNumber(s string) int {
374 end := 0
375 for end < len(s) && s[end] >= '0' && s[end] <= '9' {
376 end++
377 }
378 n, _ := strconv.Atoi(s[:end])
379 return n
380}
381
382// anchors is the anchor of every line of the page, by line, built in one
383// pass so that anchoring a note is a lookup.
384func anchors(page []byte) []anchor {
385 var all []anchor
386 path, at, next, inHunk := "", 0, 0, false
387 for _, line := range pageLines(page) {
388 // Header lines are only headers outside a hunk: inside one, every
389 // line carries a +/-/space prefix, so a diff of a diff cannot be
390 // misread.
391 if inHunk {
392 switch {
393 case line == "" || line[0] == ' ' || line[0] == '+':
394 at = next // this line's own new-side number
395 next++
396 case line[0] == '-' || line[0] == '\\':
397 // a removal or the no-newline marker: no new-side line
398 default:
399 inHunk = false
400 }
401 }
402 if !inHunk {
403 switch {
404 case styleHeading.MatchString(line):
405 // A section heading ends the diff above it.
406 path, at = "", 0
407 case strings.HasPrefix(line, "diff --git "):
408 path = ""
409 if m := diffHeader.FindStringSubmatch(line); m != nil {
410 path = m[2]
411 }
412 at = 0
413 case strings.HasPrefix(line, "@@ "):
414 _, rest, _ := strings.Cut(line, "+") // -12,7 +34,9 @@ -> 34,9 @@
415 at = leadingNumber(rest)
416 next, inHunk = at, true
417 }
418 }
419 all = append(all, anchor{path, at})
420 }
421 return all
422}
423
424// anchorOf is the anchor of a note inserted after page line n, as it is
425// written down.
426func anchorOf(all []anchor, n int) string {
427 if n < 1 || n > len(all) || all[n-1].path == "" {
428 return "(unanchored)"
429 }
430 if a := all[n-1]; a.line != 0 {
431 return fmt.Sprintf("%s:%d", a.path, a.line)
432 }
433 return all[n-1].path
434}
435
436// notesIn is the notes in an edited page, one `anchor: text` line each.
437// git says where the reviewer's lines landed: a hunk that removes nothing
438// is an insertion, and the line it was inserted after is the anchor. A
439// hunk that removes something is an edit of the diff itself, which
440// anchors to nothing, since the line it would name is the line it
441// changed, so it is dropped with a count.
442func (r *review) notesIn(orig, edited string) []string {
443 page, _ := os.ReadFile(orig)
444 all := anchors(page)
445 cmd := r.g.command("--no-pager", "diff", "--no-ext-diff", "--no-index", "-U0", "--", orig, edited)
446 cmd.Stderr = r.stderr
447 processStarted()
448 out, _ := cmd.Output()
449 var notes []string
450 at, insert, touched := 0, false, 0
451 for _, line := range pageLines(out) {
452 switch {
453 case strings.HasPrefix(line, "@@ "):
454 rest := strings.TrimPrefix(line, "@@ -") // 12,0 +13,2 @@
455 at = leadingNumber(rest)
456 rest = strings.TrimLeft(rest, "0123456789")
457 old := 1
458 if strings.HasPrefix(rest, ",") {
459 old = leadingNumber(rest[1:])
460 }
461 insert = old == 0
462 if !insert {
463 touched += old
464 }
465 case strings.HasPrefix(line, "+"):
466 // Only inside an insertion hunk: the +++ of git's own header is
467 // not a note, and neither is the new side of an edit.
468 if !insert {
469 continue
470 }
471 text := strings.TrimLeft(line[1:], " \t\n\v\f\r")
472 if text == "" { // a blank line says nothing
473 continue
474 }
475 notes = append(notes, anchorOf(all, at)+": "+text)
476 }
477 }
478 if touched != 0 {
479 fmt.Fprintf(r.stderr, "valley: warning: %d changed or deleted diff line(s) ignored — a note is a line you insert\n", touched)
480 }
481 return notes
482}
483
484// notesFile is where the notes taken under key live.
485func notesFile(key string) string {
486 state := os.Getenv("XDG_STATE_HOME")
487 if state == "" {
488 state = os.Getenv("HOME") + "/.local/state"
489 }
490 return state + "/valley/notes/" + key + ".md"
491}
492
493// collectNotes reads the notes out of the paged page and keeps them:
494// appended to the notes file under key, under the commit they were taken
495// against, and held for the print that follows the verdict. A notes file
496// that cannot be written is a warning, never a failure: the notes are
497// still printed, and a review is not undone by a missing directory.
498func (r *review) collectNotes(key, sha, orig, edited string) {
499 found := r.notesIn(orig, edited)
500 if len(found) == 0 {
501 return
502 }
503 r.notes = append(r.notes, found...)
504 file := notesFile(key)
505 text := fmt.Sprintf("## %s at %s\n\n%s\n\n", key, sha, strings.Join(found, "\n"))
506 if os.MkdirAll(filepath.Dir(file), 0o777) == nil {
507 if f, err := os.OpenFile(file, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0o666); err == nil {
508 _, err = f.WriteString(text)
509 if f.Close() == nil && err == nil {
510 return
511 }
512 }
513 }
514 fmt.Fprintf(r.stderr, "valley: warning: could not write %s — the notes are printed only\n", file)
515}
516
517// printNotes prints the notes taken under key, after the verdict.
518func (r *review) printNotes(key string, notes []string) {
519 if len(notes) == 0 {
520 return
521 }
522 fmt.Fprintf(r.stdout, "\n%d note(s) on %s, appended to %s:\n", len(notes), key, notesFile(key))
523 for _, n := range notes {
524 fmt.Fprintf(r.stdout, " %s\n", n)
525 }
526}
527
528// readDoes reads the plan graph once for every change in the review, for
529// the knots each does, and starts reading their checks. A missing or
530// unreadable graph shows no knots; -v warns on stderr.
531func (r *review) readDoes() {
532 var ids []string
533 if len(r.layers) > 1 {
534 for _, l := range r.layers {
535 if l.id != "" {
536 ids = append(ids, l.id)
537 }
538 }
539 } else if r.c.id != "" {
540 ids = []string{r.c.id}
541 }
542 if len(ids) == 0 {
543 return
544 }
545 r.does = readDoers(r.g, project(r.g), r.verbose, r.stderr)
546 r.startChecksRead(&checksRead{ids: ids, verbose: r.verbose})
547}
548
549// doesOf is the `does ...` lines of one change.
550func (r *review) doesOf(id string) string {
551 if id == "" {
552 return ""
553 }
554 var lines []string
555 for _, k := range r.does[id] {
556 lines = append(lines, k.line())
557 }
558 return strings.Join(lines, "\n")
559}
560
561// checksRead is the read of the changes' checks lines, which takes
562// seconds: it reads the policy and the host's telemetry. It runs this
563// program's checkline (versionchecks.go) in the background, in a process
564// group of its own, so that a Ctrl-C the operator gives the pager does not
565// reach it, while the page is shown, and the line is printed just before
566// the prompt when the read has finished. It never waits for the read,
567// unless VALLEY_REVIEW_CHECKS_WAIT gives the seconds to wait for it. The
568// read has a TMPDIR of its own, removed once it has ended, so a read
569// stopped part way leaves nothing behind.
570type checksRead struct {
571 ids []string
572 verbose bool // pass on what the read said on stderr
573 cmd *exec.Cmd
574 out, err bytes.Buffer
575 done chan struct{} // closed once the read has ended and its TMPDIR is removed
576 shown map[string]bool // the changes whose line is not shown again
577}
578
579// startChecksRead starts the read c names, in place of any read started.
580func (r *review) startChecksRead(c *checksRead) {
581 r.mu.Lock()
582 old := r.checks
583 r.checks = nil
584 r.mu.Unlock()
585 old.stop()
586 c = &checksRead{ids: c.ids, verbose: c.verbose, done: make(chan struct{}), shown: map[string]bool{}}
587 exe, err := os.Executable()
588 var tmp string
589 if err == nil {
590 tmp, err = os.MkdirTemp("", "valley-review-checks-")
591 }
592 if err != nil {
593 close(c.done)
594 } else {
595 c.cmd = r.g.program(exe, append([]string{"checkline"}, c.ids...)...)
596 c.cmd.Env = append(c.cmd.Env, "TMPDIR="+tmp)
597 c.cmd.Stdout, c.cmd.Stderr = &c.out, &c.err
598 c.cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
599 processStarted()
600 if c.cmd.Start() != nil {
601 c.cmd = nil
602 os.RemoveAll(tmp)
603 close(c.done)
604 } else {
605 go func() {
606 _ = c.cmd.Wait()
607 os.RemoveAll(tmp)
608 close(c.done)
609 }()
610 }
611 }
612 r.mu.Lock()
613 exiting := r.exiting
614 if !exiting {
615 r.checks = c
616 }
617 r.mu.Unlock()
618 if exiting {
619 c.stop()
620 }
621}
622
623// restartChecksRead reads the checks again, as for heads that moved, when
624// a read was started.
625func (r *review) restartChecksRead() {
626 r.mu.Lock()
627 c := r.checks
628 r.mu.Unlock()
629 if c != nil {
630 r.startChecksRead(c)
631 }
632}
633
634// stop stops the read, if it still runs, with everything it started, and
635// returns once it has ended and its TMPDIR is removed.
636func (c *checksRead) stop() {
637 if c == nil || c.cmd == nil {
638 return
639 }
640 select {
641 case <-c.done:
642 default:
643 _ = syscall.Kill(-c.cmd.Process.Pid, syscall.SIGTERM)
644 <-c.done
645 }
646}
647
648// checksLine is the `checks: …` line of one change, if the read has
649// finished and gave it one, or "". mark says it is not shown again.
650func (r *review) checksLine(id string, mark bool) string {
651 r.mu.Lock()
652 c := r.checks
653 r.mu.Unlock()
654 if c == nil || id == "" || c.shown[id] {
655 return ""
656 }
657 // Normally the read has had the whole time the page was read in. A host
658 // that wants the line at the prompt can have review wait for it.
659 wait := os.Getenv("VALLEY_REVIEW_CHECKS_WAIT")
660 seconds, err := strconv.Atoi(wait)
661 if err != nil || strings.Trim(wait, "0123456789") != "" {
662 seconds = 0
663 }
664 select {
665 case <-c.done:
666 default:
667 select {
668 case <-c.done:
669 case <-time.After(time.Duration(seconds) * time.Second):
670 return ""
671 }
672 }
673 if c.verbose && c.err.Len() > 0 {
674 r.stderr.Write(c.err.Bytes())
675 c.err.Reset()
676 }
677 var found []string
678 for _, line := range pageLines(c.out.Bytes()) {
679 if changeID, text, _ := strings.Cut(line, "\t"); changeID == id {
680 found = append(found, text)
681 if mark {
682 c.shown[id] = true
683 }
684 }
685 }
686 return strings.Join(found, "\n")
687}