gunk-dev/the-valley

main.go

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