gunk-dev/the-valley

does.go

sourcec03fa16c50eemain's published tip history
4.3 KiB
1// The knots a change does: every knot in its project's plan graph whose
2// changes field lists valley:<project>/<change ID>. review shows them above
3// a change's diff (runDoes), and changes, pending and status --json include
4// them (list.go, pending.go, render.go).
5//
6// The plan graph in project storage holds the links from knots to changes.
7// Each command reads a project's graph once, however many changes it
8// inspects. When the graph is missing or unreadable, every change gets no
9// knots, and -v warns on stderr.
10
11package main
12
13import (
14 "fmt"
15 "io"
16 "sort"
17 "strings"
18
19 "the-valley/change"
20 "the-valley/knot"
21)
22
23// knotRef is a knot that links a change, as JSON listings and review use it.
24type knotRef struct {
25 ID string `json:"id"`
26 Type string `json:"type"`
27 Status string `json:"status"`
28 Title string `json:"title"`
29
30 active bool
31}
32
33// line formats a knot for review:
34//
35// does oc-6335749 (open): Ready changes stay landable as main moves
36//
37// It strips newlines, tabs and control characters even though the graph
38// schema forbids them, so a malformed knot cannot break runDoes's
39// tab-separated output or write escape sequences to the terminal.
40func (k knotRef) line() string {
41 title := strings.Map(func(r rune) rune {
42 if r == '\n' || r == '\t' {
43 return ' '
44 }
45 return r
46 }, k.Title)
47 return fmt.Sprintf("does %s (%s): %s", visible(k.ID), visible(k.Status), visible(title))
48}
49
50// doers maps each change ID in a project to the knots that link it.
51type doers map[string][]knotRef
52
53// readDoers reads the project's plan graph at refs/the-valley/knots on g's
54// origin and indexes the knots by linked change ID. It keeps only links of
55// the form valley:<project>/<change ID>, ignoring links to other projects
56// or pull requests. When the graph is missing or unreadable, it returns an
57// empty map and, if verbose, warns on stderr.
58func readDoers(g git, project string, verbose bool, stderr io.Writer) doers {
59 if !verbose {
60 g.stderr = io.Discard
61 }
62 s, err := remote{g: g, name: project}.read()
63 switch {
64 case err != nil:
65 if verbose {
66 fmt.Fprintf(stderr, "valley: %s's plan graph could not be read, so no change shows a knot: %v\n", project, err)
67 }
68 return doers{}
69 case s.tip == "":
70 if verbose {
71 fmt.Fprintf(stderr, "valley: %s has no plan graph at %s, so no change shows a knot\n", project, knot.Ref)
72 }
73 return doers{}
74 }
75 prefix := "valley:" + project + "/"
76 d := doers{}
77 for _, id := range s.graph.IDs() {
78 k := s.graph.Knots[id]
79 ref := knotRef{ID: k.ID, Type: k.Type, Status: k.Status, Title: k.Title, active: k.Active()}
80 seen := map[string]bool{}
81 for _, link := range k.Changes {
82 cid, ok := strings.CutPrefix(link, prefix)
83 if !ok || !change.ValidID(cid) || seen[cid] {
84 continue
85 }
86 seen[cid] = true
87 d[cid] = append(d[cid], ref)
88 }
89 }
90 for _, list := range d {
91 // IDs() visits knots in ID order; a stable sort puts active knots
92 // before terminal ones while keeping each group in ID order.
93 sort.SliceStable(list, func(i, j int) bool { return list[i].active && !list[j].active })
94 }
95 return d
96}
97
98// of returns the knots that link change id, or an empty slice so JSON
99// encodes [] instead of null.
100func (d doers) of(id string) []knotRef {
101 if list := d[id]; list != nil {
102 return list
103 }
104 return []knotRef{}
105}
106
107// runDoes implements `valley-go does`, which `valley review` calls to find
108// the knots each change in the review does. It reads this project's plan
109// graph once and prints one tab-separated line per knot, in the order the
110// change IDs are given:
111//
112// <change ID>\tdoes <knot ID> (<status>): <title>
113//
114// Changes with no linked knots produce no lines. -v warns on stderr when
115// the graph is missing or unreadable.
116func runDoes(g git, args []string, stdout, stderr io.Writer) error {
117 verbose := false
118 var ids []string
119 for _, a := range args {
120 switch {
121 case a == "-v":
122 verbose = true
123 case change.ValidID(a):
124 ids = append(ids, a)
125 default:
126 return failure("usage: valley-go does [-v] <change ID>...")
127 }
128 }
129 if !g.succeeds(true, "rev-parse", "--git-dir") {
130 return failure("not a git repository")
131 }
132 d := readDoers(g, project(g), verbose, stderr)
133 var b strings.Builder
134 for _, id := range ids {
135 for _, k := range d.of(id) {
136 fmt.Fprintf(&b, "%s\t%s\n", id, k.line())
137 }
138 }
139 _, err := io.WriteString(stdout, b.String())
140 return err
141}