gunk-dev/the-valley

render.go

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