gunk-dev/the-valley

main.go

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