gunk-dev/the-valley

main.go

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