gunk-dev/the-valley

main.go

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