gunk-dev/the-valley

main.go

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