gunk-dev/the-valley

main.go

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