gunk-dev/the-valley

render.go

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