gunk-dev/the-valley

render.go

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