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