gunk-dev/the-valley

main.go

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