gunk-dev/the-valley

render.go

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