gunk-dev/the-valley

main.go

sourcea769d6211b91main's published tip history
9.4 KiB
1// valley-go is the part of the valley command written in Go: the verbs
2// that read a change's state from origin, and the verbs that write its
3// change record there. bin/valley runs it for status, ready, unready and
4// withdraw, for changes and pending, which list every change across the
5// repositories a host serves, and for resolve, which turns the name of a
6// change into its branch for the verbs bin/valley runs itself, for
7// stack, which lists the layers under a change for review (stack.go), for
8// rebase, which rebases a change's whole stack onto main, and brings, which
9// lists the changes a head would bring that have not landed, for approve
10// (rebase.go), for does, which lists the knots each change does, for review
11// (does.go), for knot, the plan graph's API (knot.go), which reads and
12// writes a project's knots at refs/the-valley/knots, for plan, which
13// shows that graph as a view (plan.go), for compare, which sets a
14// version review shows against the change's approved version
15// (compare.go), and for self-update, which installs the tip of a branch of
16// the-valley into the user's nix profile (selfupdate.go). checks prints
17// which checks a change requires, as the Go deriver derives them
18// (checks.go); no verb of bin/valley runs it. coverage says which of a
19// head's required nix checks already have evidence on origin that approve
20// can reuse rather than build (coverage.go).
21//
22// status reads one change's integration status (status.go). changes and
23// pending read every record and how far each version has landed
24// (inventory.go, pending.go). ready,
25// unready and withdraw write the change's record,
26// refs/the-valley/changes/main/<change ID> (ready.go), in the format the
27// change module defines and the push hook guards. The integrator reads
28// records only to refuse: it lands no request that would bring a version
29// of a change that is not ready, and it voids a withdrawn change's
30// requests. The integration request stays the only way
31// to main. versions lists a change's versions, for approve.
32//
33// A change is named by its full change ID, by its handle (the shortest
34// prefix of the ID, four letters at least, that no other change on origin
35// shares), or by its branch (changes.go).
36package main
37
38import (
39 "errors"
40 "fmt"
41 "io"
42 "os"
43 "os/exec"
44 "strings"
45 "time"
46
47 "the-valley/rebase"
48)
49
50const usage = `usage: valley-go status [--json] [-v] <change>
51 valley-go ready [--head <sha>] [--change <id>] [--if-active <sha>] [--stamp] [<branch>]
52 valley-go unready <change>
53 valley-go withdraw <change>
54 valley-go resolve <change>
55 valley-go stack <change>
56 valley-go does [-v] <change ID>...
57 valley-go rebase [--head <sha>] <change>
58 valley-go brings [--change <id>] <head>
59 valley-go versions <change ID>
60 valley-go compare --tip <commit> <change ID> <version> <head>
61 valley-go checks [--schema <file>] --instance <dir> --project <dir> <base> <head>
62 valley-go coverage --head <sha> [--check <name>=<attribute>]...
63 valley-go changes --json [--all] [--repo <name>] [-v]
64 valley-go pending [--json] [--all] [--repo <name>] [--checks] [-v]
65 valley-go knot <verb> [args] (valley-go knot for its verbs)
66 valley-go plan [--project <name> | --all] [--owner <name>] [--json]
67 valley-go self-update [-v] [--retry | --rollback | --pin | --unpin] [--repo <url>] [--branch <name>]
68
69status, stack, rebase, brings, changes, pending and plan read how far a
70version has landed, and also take --known-signers <file> and
71--integrator-name <name>: what an integrator's transfer statement is
72verified with, ahead of VALLEY_KNOWN_SIGNERS, VALLEY_INTEGRATOR_SIGNER and
73what the serving host publishes at refs/the-valley/signers.`
74
75func main() {
76 os.Exit(run(os.Args[1:], os.Stdout, os.Stderr))
77}
78
79// failure is a verb that ends without doing what it was asked: a message
80// for stderr and nothing on stdout. Exit status 1, as bin/valley's die
81// gives.
82type failure string
83
84func (f failure) Error() string { return string(f) }
85
86func run(args []string, stdout, stderr io.Writer) int {
87 g := git{env: gitEnvironment(os.Environ()), stderr: stderr}
88 verb := ""
89 if len(args) > 0 {
90 verb, args = args[0], args[1:]
91 }
92 // What this run verifies a transfer statement with (signers.go). Only
93 // the verbs that read how far a version has landed take the flags.
94 verification = &signerSource{g: g, stderr: stderr}
95 defer verification.done()
96 if signerVerbs[verb] {
97 var err error
98 if args, verification.keys, verification.name, err = takeSignerFlags(args); err != nil {
99 fmt.Fprintf(stderr, "valley: %s\n", err)
100 return 1
101 }
102 }
103 var err error
104 switch verb {
105 case "status":
106 return runStatus(g, args, stdout, stderr)
107 case "ready":
108 err = runReady(g, args, stdout)
109 case "unready":
110 err = runUnready(g, args, stdout, stderr)
111 case "withdraw":
112 err = runWithdraw(g, args, stdout, stderr)
113 case "resolve":
114 err = runResolve(g, args, stdout)
115 case "stack":
116 err = runStack(g, args, stdout)
117 case "does":
118 err = runDoes(g, args, stdout, stderr)
119 case "rebase":
120 return runRebase(g, args, stdout, stderr)
121 case "brings":
122 err = runBrings(g, args, stdout)
123 case "versions":
124 err = runVersions(g, args, stdout)
125 case "compare":
126 err = runCompare(g, args, stdout)
127 case "checks":
128 err = runChecks(g, args, stdout)
129 case "coverage":
130 err = runCoverage(g, args, stdout, stderr)
131 case "changes":
132 err = runChanges(g, args, stdout, stderr)
133 case "pending":
134 err = runPending(g, args, stdout, stderr)
135 case "self-update":
136 return runSelfUpdate(args, stdout, stderr)
137 case "knot", "plan":
138 if _, err := clockSetting(); err != nil {
139 fmt.Fprintf(stderr, "valley: %s\n", err)
140 return 2
141 }
142 if verb == "knot" {
143 return runKnot(g, args, os.Stdin, stdout, stderr)
144 }
145 err = runPlan(g, args, stdout, stderr)
146 default:
147 fmt.Fprintln(stderr, usage)
148 return 2
149 }
150 if err != nil {
151 fmt.Fprintf(stderr, "valley: %s\n", err)
152 return 1
153 }
154 return 0
155}
156
157// git runs git in a repository, in the environment bin/valley gives every
158// git it runs.
159type git struct {
160 env []string
161 stderr io.Writer // where git's own diagnostics go, unless quiet
162 dir string // the repository's directory; "" for the current one
163}
164
165// engine is g as the rebase engine runs git: in the same repository and
166// environment, with git's own diagnostics on the same stderr.
167func (g git) engine() rebase.Git {
168 return rebase.Git{Dir: g.dir, Env: g.env, Stderr: g.stderr}
169}
170
171// gitEnvironment is the caller's environment with replacement refs off.
172// A replacement ref makes one object stand in for another wherever git
173// looks it up, so a fetched or planted refs/replace/* could change which
174// commit this read takes for a change's head. git is told to ignore them,
175// and the variables that point it at other objects or at another
176// replacement namespace are dropped. The person's own configuration is
177// kept: reaching origin needs it, and it does not change what an object
178// id reads as once replacement refs are off.
179func gitEnvironment(inherited []string) []string {
180 var kept []string
181 for _, kv := range inherited {
182 name, _, _ := strings.Cut(kv, "=")
183 switch name {
184 case "GIT_NO_REPLACE_OBJECTS", "GIT_REPLACE_REF_BASE", "GIT_GRAFT_FILE",
185 "GIT_OBJECT_DIRECTORY", "GIT_ALTERNATE_OBJECT_DIRECTORIES":
186 continue
187 }
188 kept = append(kept, kv)
189 }
190 return append(kept, "GIT_NO_REPLACE_OBJECTS=1")
191}
192
193// output runs git and returns what it printed, less trailing newlines, as
194// a shell's command substitution reads it. A quiet run discards git's
195// diagnostics; any other passes them on.
196func (g git) output(quiet bool, args ...string) (string, error) {
197 return g.input(quiet, nil, args...)
198}
199
200// input is output with stdin.
201func (g git) input(quiet bool, stdin io.Reader, args ...string) (string, error) {
202 cmd := exec.Command("git", args...)
203 cmd.Env = g.env
204 cmd.Dir = g.dir
205 cmd.Stdin = stdin
206 if !quiet {
207 cmd.Stderr = g.stderr
208 }
209 out, err := cmd.Output()
210 return strings.TrimRight(string(out), "\n"), err
211}
212
213// succeeds reports whether git exits zero. A failure to run git at all is
214// a no as well, as it is to a shell condition.
215func (g git) succeeds(quiet bool, args ...string) bool {
216 _, err := g.output(quiet, args...)
217 return err == nil
218}
219
220// clock is the time the knot and plan verbs take as now: the time
221// VALLEY_NOW holds, when it is set, or else the system's clock. A knot's
222// created date, a claim's time and a comment's stamp are written at it,
223// and a claim's age is read against it. Checks set it, so the dates a
224// graph records and the ages a view shows are the same on every run. It
225// grants nothing: the push hook holds none of those times to its own
226// clock, so a writer could record any of them by writing the file.
227func clock() time.Time {
228 if t, err := clockSetting(); err == nil && !t.IsZero() {
229 return t
230 }
231 return time.Now().UTC()
232}
233
234// clockSetting is the time VALLEY_NOW holds, in UTC, or the zero time when
235// it is unset. A value that is no RFC 3339 time is refused.
236func clockSetting() (time.Time, error) {
237 v := os.Getenv("VALLEY_NOW")
238 if v == "" {
239 return time.Time{}, nil
240 }
241 t, err := time.Parse(time.RFC3339, v)
242 if err != nil {
243 return time.Time{}, failure(fmt.Sprintf("VALLEY_NOW is %q, and it takes an RFC 3339 time, such as 2026-10-15T09:30:00Z", v))
244 }
245 return t.UTC(), nil
246}
247
248// exitCode is the status a failed git exited with, for passing it on.
249func exitCode(err error) int {
250 var exit *exec.ExitError
251 if errors.As(err, &exit) && exit.ExitCode() > 0 {
252 return exit.ExitCode()
253 }
254 return 1
255}