gunk-dev/the-valley

main.go

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