| 1 | package main |
| 2 | |
| 3 | import ( |
| 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. |
| 13 | const 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. |
| 17 | func 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 "refused-approval": |
| 81 | p("the integrator refused to land it, because it found no approval of this head that it accepts.\n") |
| 82 | case "failed": |
| 83 | p("the integrator failed while processing the request (%s).\n", s.memo.failedAt) |
| 84 | p("This does not prove that nothing landed; recording can fail after main advances.\n") |
| 85 | p(noReason) |
| 86 | } |
| 87 | p("Request: %s\n", s.requestRef) |
| 88 | p("Check again: valley status %s\n", shellQuote(s.name())) |
| 89 | return 0 |
| 90 | } |
| 91 | |
| 92 | // writeRestack says what the change's restack report says, and whether it |
| 93 | // still applies. A historical report is named as one, so it is never read |
| 94 | // as the change's state now. |
| 95 | func writeRestack(s *status, w io.Writer) { |
| 96 | switch { |
| 97 | case s.restack == nil: |
| 98 | case s.restack.Current: |
| 99 | fmt.Fprintf(w, "Its restack report: %s.\n", readable(s.restackSays)) |
| 100 | if s.restack.Report.Result == change.RestackConflict { |
| 101 | fmt.Fprintf(w, "Rebase it by hand: valley rebase %s\n", shellQuote(s.name())) |
| 102 | } |
| 103 | default: |
| 104 | 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)) |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | // writeCheckout says how the local checkout stands against the main a |
| 109 | // change was integrated into, and how to bring it up to date. A checkout |
| 110 | // whose HEAD cannot be read ends the read with git's own status, after |
| 111 | // git has said why. |
| 112 | func writeCheckout(g git, tip string, w io.Writer) int { |
| 113 | p := func(format string, args ...any) { fmt.Fprintf(w, format, args...) } |
| 114 | if bare, err := g.output(false, "rev-parse", "--is-bare-repository"); err == nil && bare == "true" { |
| 115 | p("This is a bare repository; there is no local checkout to update.\n") |
| 116 | return 0 |
| 117 | } |
| 118 | current, err := g.output(false, "symbolic-ref", "--quiet", "--short", "HEAD") |
| 119 | if err != nil { |
| 120 | current = "detached HEAD" |
| 121 | } |
| 122 | localHead, err := g.output(false, "rev-parse", "--short", "HEAD") |
| 123 | if err != nil { |
| 124 | return exitCode(err) |
| 125 | } |
| 126 | p("Local checkout: %s at %s.\n", current, localHead) |
| 127 | switch { |
| 128 | case g.succeeds(false, "merge-base", "--is-ancestor", tip, "HEAD"): |
| 129 | p("Your local checkout already contains origin/main.\n") |
| 130 | case !g.succeeds(false, "show-ref", "--verify", "--quiet", "refs/heads/main"): |
| 131 | p("To create a local main checkout:\n") |
| 132 | p(" git fetch origin\n git switch --track -c main origin/main\n") |
| 133 | case g.succeeds(false, "merge-base", "--is-ancestor", tip, "refs/heads/main"): |
| 134 | p("Local main already contains origin/main. To view it:\n git switch main\n") |
| 135 | case !g.succeeds(false, "merge-base", "--is-ancestor", "refs/heads/main", tip): |
| 136 | p("Local main has diverged from origin/main. Inspect the commits before updating:\n") |
| 137 | p(" git fetch origin\n git log --oneline --left-right main...origin/main\n") |
| 138 | default: |
| 139 | p("To update your local main checkout:\n") |
| 140 | if current != "main" { |
| 141 | p(" git switch main\n") |
| 142 | } |
| 143 | p(" git pull --ff-only origin main\n") |
| 144 | } |
| 145 | return 0 |
| 146 | } |
| 147 | |
| 148 | func short(oid string) string { |
| 149 | if len(oid) > 7 { |
| 150 | return oid[:7] |
| 151 | } |
| 152 | return oid |
| 153 | } |
| 154 | |
| 155 | // The valley-status/v1 object. docs/architecture.md documents it, and a |
| 156 | // change to any field's name or meaning is a new schema version. For a |
| 157 | // branch with no record, the fields from change_id on are null, except |
| 158 | // knots, which is an empty slice. |
| 159 | type jsonStatus struct { |
| 160 | Schema string `json:"schema"` |
| 161 | Change string `json:"change"` |
| 162 | RequestName string `json:"request_name"` |
| 163 | Target string `json:"target"` |
| 164 | TargetTip string `json:"target_tip"` |
| 165 | Head *string `json:"head"` |
| 166 | Request *jsonRequest `json:"request"` |
| 167 | Outcome *jsonOutcome `json:"outcome"` |
| 168 | State string `json:"state"` |
| 169 | ChangeID *string `json:"change_id"` |
| 170 | Handle *string `json:"handle"` |
| 171 | Version *int `json:"version"` |
| 172 | Title *string `json:"title"` |
| 173 | Owner *string `json:"owner"` |
| 174 | Landing *string `json:"landing"` |
| 175 | LandedAs *int `json:"landed_version"` |
| 176 | Restack *jsonRestack `json:"restack"` |
| 177 | Knots []knotRef `json:"knots"` |
| 178 | } |
| 179 | |
| 180 | type jsonRequest struct { |
| 181 | Ref string `json:"ref"` |
| 182 | Commit string `json:"commit"` |
| 183 | } |
| 184 | |
| 185 | // An outcome record's head and tip are given only when they are object |
| 186 | // ids, and failed_at only for a failed pass: an unknown record is reported |
| 187 | // as present and unread, never as half of a decision. |
| 188 | type jsonOutcome struct { |
| 189 | Decision string `json:"decision"` |
| 190 | Head *string `json:"head"` |
| 191 | Tip *string `json:"tip"` |
| 192 | FailedAt *string `json:"failed_at"` |
| 193 | } |
| 194 | |
| 195 | // writeJSON prints the state as one valley-status/v1 object on one line. |
| 196 | // It is encoded whole before anything is written, so a reader gets the |
| 197 | // object or nothing. |
| 198 | func writeJSON(s *status, w, stderr io.Writer) int { |
| 199 | out := jsonStatus{ |
| 200 | Schema: "valley-status/v1", |
| 201 | Change: s.branch, |
| 202 | RequestName: s.change, |
| 203 | Target: target, |
| 204 | TargetTip: s.tip, |
| 205 | Head: optional(s.head), |
| 206 | State: s.state, |
| 207 | Knots: s.knots, |
| 208 | } |
| 209 | if out.Knots == nil { |
| 210 | out.Knots = []knotRef{} |
| 211 | } |
| 212 | if s.request != "" { |
| 213 | out.Request = &jsonRequest{Ref: s.requestRef, Commit: s.request} |
| 214 | } |
| 215 | if r := s.recorded; r != nil { |
| 216 | out.ChangeID, out.Handle, out.Version = &r.id, &r.handle, &r.version |
| 217 | out.Title, out.Owner, out.Landing = &r.title, &r.owner, &r.landing |
| 218 | if r.landedAs != 0 { |
| 219 | out.LandedAs = &r.landedAs |
| 220 | } |
| 221 | } |
| 222 | out.Restack = s.restack |
| 223 | if s.outcome != "" { |
| 224 | o := &jsonOutcome{Decision: s.memo.decision, FailedAt: optional(s.memo.failedAt)} |
| 225 | if isObjectID(s.memo.head) { |
| 226 | o.Head = &s.memo.head |
| 227 | } |
| 228 | if isObjectID(s.memo.tip) { |
| 229 | o.Tip = &s.memo.tip |
| 230 | } |
| 231 | out.Outcome = o |
| 232 | } |
| 233 | var buf bytes.Buffer |
| 234 | enc := json.NewEncoder(&buf) |
| 235 | enc.SetEscapeHTML(false) |
| 236 | if err := enc.Encode(out); err != nil { |
| 237 | fmt.Fprintf(stderr, "valley: could not encode the status: %v\n", err) |
| 238 | return 1 |
| 239 | } |
| 240 | if _, err := w.Write(buf.Bytes()); err != nil { |
| 241 | fmt.Fprintf(stderr, "valley: could not write the status: %v\n", err) |
| 242 | return 1 |
| 243 | } |
| 244 | return 0 |
| 245 | } |
| 246 | |
| 247 | func optional(s string) *string { |
| 248 | if s == "" { |
| 249 | return nil |
| 250 | } |
| 251 | return &s |
| 252 | } |