gunk-dev/the-valley

main.go

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