gunk-dev/the-valley

main.go

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