gunk-dev/the-valley

does.go

source29699b69fbf8main's published tip history
4.5 KiB
1// The knots a change does: every knot in its project's plan graph whose
2// changes lists the change, as valley:<project>/<change ID>. review shows
3// them above a change's diff (runDoes), and changes, pending and status
4// list them (list.go, pending.go, render.go).
5//
6// The change record holds no knot field. The plan lives in project
7// storage, and the record holds the change's workflow, so the link is read
8// from the graph's side alone. The graph is read once per command, however
9// many changes it lists. A graph that is missing or does not read links no
10// change. That is never an error: with -v, the command says so on stderr.
11
12package main
13
14import (
15 "fmt"
16 "io"
17 "sort"
18 "strings"
19
20 "the-valley/change"
21 "the-valley/knot"
22)
23
24// knotRef is a knot as a listing names it.
25type knotRef struct {
26 ID string `json:"id"`
27 Type string `json:"type"`
28 Status string `json:"status"`
29 Title string `json:"title"`
30
31 active bool
32}
33
34// line is how review shows a knot the change does:
35//
36// does oc-6335749 (open): Ready changes stay landable as main moves
37//
38// The graph holds no control character, and a title holds no newline or
39// tab. The title is made visible all the same, so the line is one line
40// whatever the graph holds.
41func (k knotRef) line() string {
42 title := strings.Map(func(r rune) rune {
43 if r == '\n' || r == '\t' {
44 return ' '
45 }
46 return r
47 }, k.Title)
48 return fmt.Sprintf("does %s (%s): %s", visible(k.ID), visible(k.Status), visible(title))
49}
50
51// doers is one read of a project's graph: the knots that link each change,
52// by change ID.
53type doers map[string][]knotRef
54
55// readDoers reads the project's graph at refs/the-valley/knots on the
56// origin of g, and finds the knots that link each change in the project.
57// A link names the project and the full change ID, and links to any other
58// project or to a pull request are passed over. A missing graph, or one
59// that does not read, gives no knots, and says why on stderr when verbose.
60func readDoers(g git, project string, verbose bool, stderr io.Writer) doers {
61 if !verbose {
62 g.stderr = io.Discard
63 }
64 s, err := remote{g: g, name: project}.read()
65 switch {
66 case err != nil:
67 if verbose {
68 fmt.Fprintf(stderr, "valley: %s's plan graph could not be read, so no change shows a knot: %v\n", project, err)
69 }
70 return doers{}
71 case s.tip == "":
72 if verbose {
73 fmt.Fprintf(stderr, "valley: %s has no plan graph at %s, so no change shows a knot\n", project, knot.Ref)
74 }
75 return doers{}
76 }
77 prefix := "valley:" + project + "/"
78 d := doers{}
79 for _, id := range s.graph.IDs() {
80 k := s.graph.Knots[id]
81 ref := knotRef{ID: k.ID, Type: k.Type, Status: k.Status, Title: k.Title, active: k.Active()}
82 seen := map[string]bool{}
83 for _, link := range k.Changes {
84 cid, ok := strings.CutPrefix(link, prefix)
85 if !ok || !change.ValidID(cid) || seen[cid] {
86 continue
87 }
88 seen[cid] = true
89 d[cid] = append(d[cid], ref)
90 }
91 }
92 for _, list := range d {
93 // IDs() gives each list in ID order. The stable sort puts active
94 // knots ahead of the ones that have ended, so the first knot is
95 // one the work still serves.
96 sort.SliceStable(list, func(i, j int) bool { return list[i].active && !list[j].active })
97 }
98 return d
99}
100
101// of is the knots that link the change: active knots first, each group in
102// ID order. It is never nil, so JSON writes an empty list.
103func (d doers) of(id string) []knotRef {
104 if list := d[id]; list != nil {
105 return list
106 }
107 return []knotRef{}
108}
109
110// runDoes prints the knots each change named does, for review, which
111// shows them after the line that names the change and before its diff.
112// The graph is that of this repository's project, read once. One line per
113// knot, in the order the changes are given, each change's knots in the
114// order of:
115//
116// <change ID>\tdoes <knot ID> (<status>): <title>
117//
118// A change no knot links prints nothing. -v says why on stderr when the
119// graph is missing or does not read.
120func runDoes(g git, args []string, stdout, stderr io.Writer) error {
121 verbose := false
122 var ids []string
123 for _, a := range args {
124 switch {
125 case a == "-v":
126 verbose = true
127 case change.ValidID(a):
128 ids = append(ids, a)
129 default:
130 return failure("usage: valley-go does [-v] <change ID>...")
131 }
132 }
133 if !g.succeeds(true, "rev-parse", "--git-dir") {
134 return failure("not a git repository")
135 }
136 d := readDoers(g, project(g), verbose, stderr)
137 var b strings.Builder
138 for _, id := range ids {
139 for _, k := range d.of(id) {
140 fmt.Fprintf(&b, "%s\t%s\n", id, k.line())
141 }
142 }
143 _, err := io.WriteString(stdout, b.String())
144 return err
145}