gunk-dev/the-valley

main.go

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