gunk-dev/the-valley

render.go

source7309e1d6b8bbmain's published tip history
9.0 KiB
1package main
2
3import (
4 "bytes"
5 "encoding/json"
6 "fmt"
7 "io"
8
9 "the-valley/change"
10)
11
12// A memo records a decision and not why, so a current decision says so.
13const noReason = "The stored outcome has no detailed reason. Ask the instance operator for the integrator report.\n"
14
15// writeHuman prints the state as the sentences an operator reads, and how
16// to act on it.
17func writeHuman(g git, s *status, w io.Writer) int {
18 p := func(format string, args ...any) { fmt.Fprintf(w, format, args...) }
19 if r := s.recorded; r != nil {
20 p("Change %s v%d on %s, owned by %s: %s\n", r.handle, r.version, s.branch, r.owner, r.title)
21 if r.landedAs != 0 && r.landedAs != r.version {
22 p("Its landing: %s, as v%d.\n", r.landing, r.landedAs)
23 } else {
24 p("Its landing: %s.\n", r.landing)
25 }
26 writeRestack(s, w)
27 }
28 switch s.state {
29 case "integrated":
30 p("%s (%s): integrated into origin/main.\n", s.label(), short(s.head))
31 if s.request != "" {
32 p("The request for %s is still present at %s.\n", short(s.request), s.requestRef)
33 }
34 return writeCheckout(g, s.tip, w)
35 case "integrated-recorded":
36 p("%s (%s): the integrator recorded it integrated into origin/main.\n", s.label(), short(s.head))
37 return writeCheckout(g, s.tip, w)
38 case "outdated-request":
39 p("%s (%s): latest commit not approved.\n", s.label(), short(s.head))
40 p("An older request for %s is still pending at %s.\n", short(s.request), s.requestRef)
41 p("Review the latest commit: valley review %s\n", shellQuote(s.name()))
42 return 0
43 case "outcome-unreadable":
44 p("%s: the stored outcome is unreadable; status is unknown.\n", s.label())
45 p("Ask the instance operator to inspect %s.\n", s.outcomeRef)
46 return 0
47 case "voided-withdrawn":
48 p("%s (%s): not approved; the integrator voided its request because the change was withdrawn.\n", s.label(), short(s.head))
49 p("Approve it again once it is ready: valley review %s\n", shellQuote(s.name()))
50 return 0
51 case "not-requested":
52 p("%s: not approved; no pending request for this commit.\n", s.label())
53 if s.memo.decision != "" {
54 p("Last recorded decision: %s for %s.\n", s.memo.decision, short(s.memo.head))
55 }
56 p("Approve it through review: valley review %s\n", shellQuote(s.name()))
57 return 0
58 }
59
60 p("%s (%s): ", s.label(), short(s.head))
61 switch s.state {
62 case "awaiting-decision":
63 p("awaiting an integrator decision.\n")
64 case "awaiting-new-decision":
65 p("awaiting a new decision; the target or evidence has changed.\n")
66 p("Last recorded decision: %s.\n", s.memo.decision)
67 case "stale":
68 p("needs attention; the integrator reported stale.\n")
69 p("The change conflicts with main, or required evidence does not transfer.\n")
70 p(noReason)
71 case "rejected":
72 p("the integrator rejected the evidence.\n")
73 p(noReason)
74 case "refused-not-ready":
75 p("the integrator refused to land it, because a record of its head did not say ready.\n")
76 p("A request lands only while every change whose version it would bring is ready. The request stands, and the integrator judges it again on every pass, so it lands once the change is readied again.\n")
77 case "refused-merge":
78 p("the integrator refused to land it, because its commits from the merge base with main include a merge.\n")
79 p("A change is a line of commits. Make one without the merge, and approve that head.\n")
80 case "failed":
81 p("the integrator failed while processing the request (%s).\n", s.memo.failedAt)
82 p("This does not prove that nothing landed; recording can fail after main advances.\n")
83 p(noReason)
84 }
85 p("Request: %s\n", s.requestRef)
86 p("Check again: valley status %s\n", shellQuote(s.name()))
87 return 0
88}
89
90// writeRestack says what the change's restack report says, and whether it
91// still applies. A historical report is named as one, so it is never read
92// as the change's state now.
93func writeRestack(s *status, w io.Writer) {
94 switch {
95 case s.restack == nil:
96 case s.restack.Current:
97 fmt.Fprintf(w, "Its restack report: %s.\n", readable(s.restackSays))
98 if s.restack.Report.Result == change.RestackConflict {
99 fmt.Fprintf(w, "Rebase it by hand: valley rebase %s\n", shellQuote(s.name()))
100 }
101 default:
102 fmt.Fprintf(w, "Its last restack report is historical: %s. main, or a record, branch or request it read, has changed since.\n", readable(s.restackSays))
103 }
104}
105
106// writeCheckout says how the local checkout stands against the main a
107// change was integrated into, and how to bring it up to date. A checkout
108// whose HEAD cannot be read ends the read with git's own status, after
109// git has said why.
110func writeCheckout(g git, tip string, w io.Writer) int {
111 p := func(format string, args ...any) { fmt.Fprintf(w, format, args...) }
112 if bare, err := g.output(false, "rev-parse", "--is-bare-repository"); err == nil && bare == "true" {
113 p("This is a bare repository; there is no local checkout to update.\n")
114 return 0
115 }
116 current, err := g.output(false, "symbolic-ref", "--quiet", "--short", "HEAD")
117 if err != nil {
118 current = "detached HEAD"
119 }
120 localHead, err := g.output(false, "rev-parse", "--short", "HEAD")
121 if err != nil {
122 return exitCode(err)
123 }
124 p("Local checkout: %s at %s.\n", current, localHead)
125 switch {
126 case g.succeeds(false, "merge-base", "--is-ancestor", tip, "HEAD"):
127 p("Your local checkout already contains origin/main.\n")
128 case !g.succeeds(false, "show-ref", "--verify", "--quiet", "refs/heads/main"):
129 p("To create a local main checkout:\n")
130 p(" git fetch origin\n git switch --track -c main origin/main\n")
131 case g.succeeds(false, "merge-base", "--is-ancestor", tip, "refs/heads/main"):
132 p("Local main already contains origin/main. To view it:\n git switch main\n")
133 case !g.succeeds(false, "merge-base", "--is-ancestor", "refs/heads/main", tip):
134 p("Local main has diverged from origin/main. Inspect the commits before updating:\n")
135 p(" git fetch origin\n git log --oneline --left-right main...origin/main\n")
136 default:
137 p("To update your local main checkout:\n")
138 if current != "main" {
139 p(" git switch main\n")
140 }
141 p(" git pull --ff-only origin main\n")
142 }
143 return 0
144}
145
146func short(oid string) string {
147 if len(oid) > 7 {
148 return oid[:7]
149 }
150 return oid
151}
152
153// The valley-status/v1 object. docs/architecture.md documents it, and a
154// change to any field's name or meaning is a new schema version. For a
155// branch with no record, the fields from change_id on are null, except
156// knots, which is an empty slice.
157type jsonStatus struct {
158 Schema string `json:"schema"`
159 Change string `json:"change"`
160 RequestName string `json:"request_name"`
161 Target string `json:"target"`
162 TargetTip string `json:"target_tip"`
163 Head *string `json:"head"`
164 Request *jsonRequest `json:"request"`
165 Outcome *jsonOutcome `json:"outcome"`
166 State string `json:"state"`
167 ChangeID *string `json:"change_id"`
168 Handle *string `json:"handle"`
169 Version *int `json:"version"`
170 Title *string `json:"title"`
171 Owner *string `json:"owner"`
172 Landing *string `json:"landing"`
173 LandedAs *int `json:"landed_version"`
174 Restack *jsonRestack `json:"restack"`
175 Knots []knotRef `json:"knots"`
176}
177
178type jsonRequest struct {
179 Ref string `json:"ref"`
180 Commit string `json:"commit"`
181}
182
183// An outcome record's head and tip are given only when they are object
184// ids, and failed_at only for a failed pass: an unknown record is reported
185// as present and unread, never as half of a decision.
186type jsonOutcome struct {
187 Decision string `json:"decision"`
188 Head *string `json:"head"`
189 Tip *string `json:"tip"`
190 FailedAt *string `json:"failed_at"`
191}
192
193// writeJSON prints the state as one valley-status/v1 object on one line.
194// It is encoded whole before anything is written, so a reader gets the
195// object or nothing.
196func writeJSON(s *status, w, stderr io.Writer) int {
197 out := jsonStatus{
198 Schema: "valley-status/v1",
199 Change: s.branch,
200 RequestName: s.change,
201 Target: target,
202 TargetTip: s.tip,
203 Head: optional(s.head),
204 State: s.state,
205 Knots: s.knots,
206 }
207 if out.Knots == nil {
208 out.Knots = []knotRef{}
209 }
210 if s.request != "" {
211 out.Request = &jsonRequest{Ref: s.requestRef, Commit: s.request}
212 }
213 if r := s.recorded; r != nil {
214 out.ChangeID, out.Handle, out.Version = &r.id, &r.handle, &r.version
215 out.Title, out.Owner, out.Landing = &r.title, &r.owner, &r.landing
216 if r.landedAs != 0 {
217 out.LandedAs = &r.landedAs
218 }
219 }
220 out.Restack = s.restack
221 if s.outcome != "" {
222 o := &jsonOutcome{Decision: s.memo.decision, FailedAt: optional(s.memo.failedAt)}
223 if isObjectID(s.memo.head) {
224 o.Head = &s.memo.head
225 }
226 if isObjectID(s.memo.tip) {
227 o.Tip = &s.memo.tip
228 }
229 out.Outcome = o
230 }
231 var buf bytes.Buffer
232 enc := json.NewEncoder(&buf)
233 enc.SetEscapeHTML(false)
234 if err := enc.Encode(out); err != nil {
235 fmt.Fprintf(stderr, "valley: could not encode the status: %v\n", err)
236 return 1
237 }
238 if _, err := w.Write(buf.Bytes()); err != nil {
239 fmt.Fprintf(stderr, "valley: could not write the status: %v\n", err)
240 return 1
241 }
242 return 0
243}
244
245func optional(s string) *string {
246 if s == "" {
247 return nil
248 }
249 return &s
250}