gunk-dev/the-valley

main.go

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