gunk-dev/the-valley

main.go

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