gunk-dev/the-valley

main.go

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