| 1 | // valley-status reads one change's integration status from origin: the |
| 2 | // change's branch head, its integration request, and the integrator's |
| 3 | // last recorded outcome for it. It is `valley status`; bin/valley runs it. |
| 4 | // |
| 5 | // It writes nothing. The objects it reads are fetched without writing |
| 6 | // FETCH_HEAD or moving a branch, so a status read leaves the checkout and |
| 7 | // its working tree exactly as it found them. No check runs and no request |
| 8 | // is filed. |
| 9 | // |
| 10 | // Without flags it prints the sentences an operator reads. With --json it |
| 11 | // prints one object, valley-status/v1, which is a contract front ends |
| 12 | // read; docs/architecture.md documents it. Both are rendered from one |
| 13 | // decision (decide), so the two forms cannot disagree about a change. |
| 14 | package main |
| 15 | |
| 16 | import ( |
| 17 | "errors" |
| 18 | "fmt" |
| 19 | "io" |
| 20 | "os" |
| 21 | "os/exec" |
| 22 | "strings" |
| 23 | "unicode/utf8" |
| 24 | ) |
| 25 | |
| 26 | const usage = "usage: valley status [--json] <branch>" |
| 27 | |
| 28 | func main() { |
| 29 | os.Exit(run(os.Args[1:], os.Stdout, os.Stderr)) |
| 30 | } |
| 31 | |
| 32 | // failure is a read that ends without a status: a message for stderr and |
| 33 | // nothing on stdout. Exit status 1, as bin/valley's die gives. |
| 34 | type failure string |
| 35 | |
| 36 | func (f failure) Error() string { return string(f) } |
| 37 | |
| 38 | func run(args []string, stdout, stderr io.Writer) int { |
| 39 | g := git{env: gitEnvironment(os.Environ()), stderr: stderr} |
| 40 | asJSON, branch, err := parseArgs(args) |
| 41 | if err == nil && asJSON && !utf8.ValidString(branch) { |
| 42 | err = failure(branch + ": not UTF-8, so JSON cannot carry the change name") |
| 43 | } |
| 44 | var s *status |
| 45 | if err == nil { |
| 46 | s, err = read(g, branch) |
| 47 | } |
| 48 | if err != nil { |
| 49 | fmt.Fprintf(stderr, "valley: %s\n", err) |
| 50 | return 1 |
| 51 | } |
| 52 | if asJSON { |
| 53 | return writeJSON(s, stdout, stderr) |
| 54 | } |
| 55 | return writeHuman(g, s, stdout) |
| 56 | } |
| 57 | |
| 58 | // parseArgs takes one change and an optional --json, in either order. The |
| 59 | // change is the branch name, and an origin/ prefix is dropped once, the |
| 60 | // way a remote-tracking name would be written. |
| 61 | func parseArgs(args []string) (asJSON bool, branch string, err error) { |
| 62 | var rest []string |
| 63 | for _, a := range args { |
| 64 | if a == "--json" { |
| 65 | asJSON = true |
| 66 | continue |
| 67 | } |
| 68 | rest = append(rest, a) |
| 69 | } |
| 70 | if len(rest) != 1 { |
| 71 | return false, "", failure(usage) |
| 72 | } |
| 73 | return asJSON, strings.TrimPrefix(rest[0], "origin/"), nil |
| 74 | } |
| 75 | |
| 76 | // git runs git in the current directory, in the environment bin/valley |
| 77 | // gives every git it runs. |
| 78 | type git struct { |
| 79 | env []string |
| 80 | stderr io.Writer // where git's own diagnostics go, unless quiet |
| 81 | } |
| 82 | |
| 83 | // gitEnvironment is the caller's environment with replacement refs off. |
| 84 | // A replacement ref makes one object stand in for another wherever git |
| 85 | // looks it up, so a fetched or planted refs/replace/* could change which |
| 86 | // commit this read takes for a change's head. git is told to ignore them, |
| 87 | // and the variables that point it at other objects or at another |
| 88 | // replacement namespace are dropped. The person's own configuration is |
| 89 | // kept: reaching origin needs it, and it does not change what an object |
| 90 | // id reads as once replacement refs are off. |
| 91 | func gitEnvironment(inherited []string) []string { |
| 92 | var kept []string |
| 93 | for _, kv := range inherited { |
| 94 | name, _, _ := strings.Cut(kv, "=") |
| 95 | switch name { |
| 96 | case "GIT_NO_REPLACE_OBJECTS", "GIT_REPLACE_REF_BASE", "GIT_GRAFT_FILE", |
| 97 | "GIT_OBJECT_DIRECTORY", "GIT_ALTERNATE_OBJECT_DIRECTORIES": |
| 98 | continue |
| 99 | } |
| 100 | kept = append(kept, kv) |
| 101 | } |
| 102 | return append(kept, "GIT_NO_REPLACE_OBJECTS=1") |
| 103 | } |
| 104 | |
| 105 | // output runs git and returns what it printed, less trailing newlines, as |
| 106 | // a shell's command substitution reads it. A quiet run discards git's |
| 107 | // diagnostics; any other passes them on. |
| 108 | func (g git) output(quiet bool, args ...string) (string, error) { |
| 109 | cmd := exec.Command("git", args...) |
| 110 | cmd.Env = g.env |
| 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. |
| 120 | func (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. |
| 126 | func 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 | } |