| 1 | package rebase |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "io" |
| 7 | "os/exec" |
| 8 | "strings" |
| 9 | "time" |
| 10 | ) |
| 11 | |
| 12 | // Git runs git in one repository. Every command runs under the context it |
| 13 | // is given, so a cancelled context ends the git it is running, and what |
| 14 | // that git started. |
| 15 | // |
| 16 | // The engine never changes the environment it is given. A caller that |
| 17 | // must not run the repository's hooks or filters says so in Env, as git |
| 18 | // configuration. |
| 19 | type Git struct { |
| 20 | Dir string // the repository's directory; "" for the current one |
| 21 | Env []string // git's environment; nil for this process's own |
| 22 | Stdout io.Writer // where a rebase prints as it goes; nil discards it |
| 23 | Stderr io.Writer // where git's own diagnostics go, unless quiet; nil discards them |
| 24 | |
| 25 | // Unattended is git that nobody is at a terminal for, as a service's |
| 26 | // is. Every command runs in a process group of its own, a command that |
| 27 | // reaches origin included, so a cancelled context kills what that |
| 28 | // command started too, such as ssh. A caller that sets it never lets git |
| 29 | // or ssh ask a question: see command. |
| 30 | Unattended bool |
| 31 | } |
| 32 | |
| 33 | // waitDelay is how long a wait for git's output may go on after git has |
| 34 | // exited, or after a cancelled context has killed it. Something git |
| 35 | // started may still hold the output open. |
| 36 | const waitDelay = 5 * time.Second |
| 37 | |
| 38 | // reachesOrigin is the git commands that talk to origin. |
| 39 | var reachesOrigin = map[string]bool{"ls-remote": true, "fetch": true, "push": true} |
| 40 | |
| 41 | // command is git with the arguments given, run in g's repository and |
| 42 | // environment under ctx. |
| 43 | // |
| 44 | // A cancelled context kills git and everything git started, such as a |
| 45 | // hook or a filter. Killing git alone is not enough: a hook it started |
| 46 | // can keep git's output open, and a wait for that output would not |
| 47 | // return. So git runs in a process group of its own, and the whole group |
| 48 | // is killed. A wait for git's output is also bounded by waitDelay, for a |
| 49 | // process that has left the group. |
| 50 | // |
| 51 | // A command that reaches origin stays in the caller's process group, and |
| 52 | // is killed alone, unless g is Unattended. A process group of its own is |
| 53 | // not the terminal's foreground group, and a process outside that group |
| 54 | // that reads the terminal is stopped. ssh reads the terminal to ask for a |
| 55 | // passphrase, and git reads it to ask for credentials. |
| 56 | func (g Git) command(ctx context.Context, args ...string) *exec.Cmd { |
| 57 | cmd := exec.CommandContext(ctx, "git", args...) |
| 58 | cmd.Env, cmd.Dir, cmd.WaitDelay = g.Env, g.Dir, waitDelay |
| 59 | if g.Unattended || len(args) == 0 || !reachesOrigin[args[0]] { |
| 60 | ownGroup(cmd) |
| 61 | } |
| 62 | return cmd |
| 63 | } |
| 64 | |
| 65 | // Command is git with the arguments given, run as the engine runs its |
| 66 | // own: in g's repository and environment, killed with what it started |
| 67 | // once ctx is done, and with the wait for its output bounded. A caller |
| 68 | // that runs git beside the engine runs it this way, so its git is bounded |
| 69 | // as the engine's is. |
| 70 | func (g Git) Command(ctx context.Context, args ...string) *exec.Cmd { |
| 71 | return g.command(ctx, args...) |
| 72 | } |
| 73 | |
| 74 | // tool is a program other than git, such as attest, run in g's |
| 75 | // environment. It never reads the terminal, so it always runs in a |
| 76 | // process group of its own, killed whole once ctx is done, and the wait |
| 77 | // for its output is bounded as git's is. |
| 78 | func (g Git) tool(ctx context.Context, name string, args ...string) *exec.Cmd { |
| 79 | cmd := exec.CommandContext(ctx, name, args...) |
| 80 | cmd.Env, cmd.WaitDelay = g.Env, waitDelay |
| 81 | ownGroup(cmd) |
| 82 | return cmd |
| 83 | } |
| 84 | |
| 85 | // cleanupBound is the longest cleanup may run: removing a worktree, |
| 86 | // aborting its rebase, and deleting the refs that kept a prepared family. |
| 87 | var cleanupBound = time.Minute |
| 88 | |
| 89 | // cleanupContext is the context cleanup runs under. It is not cancelled |
| 90 | // with ctx, since cleanup has to run once the caller's work is cancelled |
| 91 | // or past its deadline. It is done after cleanupBound, so cleanup cannot |
| 92 | // hang on a hook or a filter that never returns. |
| 93 | func cleanupContext(ctx context.Context) (context.Context, context.CancelFunc) { |
| 94 | return context.WithTimeout(context.WithoutCancel(ctx), cleanupBound) |
| 95 | } |
| 96 | |
| 97 | // finished is the error of git that has ended. git that exits zero has |
| 98 | // succeeded even when the wait for its output was cut short: only |
| 99 | // something git started can still hold the output then. |
| 100 | func finished(err error) error { |
| 101 | if errors.Is(err, exec.ErrWaitDelay) { |
| 102 | return nil |
| 103 | } |
| 104 | return err |
| 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. |
| 110 | func (g Git) output(ctx context.Context, quiet bool, args ...string) (string, error) { |
| 111 | return g.input(ctx, quiet, nil, args...) |
| 112 | } |
| 113 | |
| 114 | // input is output with stdin. |
| 115 | func (g Git) input(ctx context.Context, quiet bool, stdin io.Reader, args ...string) (string, error) { |
| 116 | cmd := g.command(ctx, args...) |
| 117 | cmd.Stdin = stdin |
| 118 | if !quiet { |
| 119 | cmd.Stderr = g.Stderr |
| 120 | } |
| 121 | out, err := cmd.Output() |
| 122 | return strings.TrimRight(string(out), "\n"), finished(err) |
| 123 | } |
| 124 | |
| 125 | // succeeds reports whether git exits zero. A failure to run git at all is |
| 126 | // a no as well, as it is to a shell condition. |
| 127 | func (g Git) succeeds(ctx context.Context, quiet bool, args ...string) bool { |
| 128 | _, err := g.output(ctx, quiet, args...) |
| 129 | return err == nil |
| 130 | } |
| 131 | |
| 132 | // passing runs git with its output on Stdout and its diagnostics on |
| 133 | // Stderr. |
| 134 | func (g Git) passing(ctx context.Context, args ...string) error { |
| 135 | cmd := g.command(ctx, args...) |
| 136 | cmd.Stdout, cmd.Stderr = g.Stdout, g.Stderr |
| 137 | return finished(cmd.Run()) |
| 138 | } |
| 139 | |
| 140 | // in is g run in another directory. |
| 141 | func (g Git) in(dir string) Git { |
| 142 | g.Dir = dir |
| 143 | return g |
| 144 | } |
| 145 | |
| 146 | // short is an object ID cut to seven digits, as messages name a commit. |
| 147 | func short(oid string) string { |
| 148 | if len(oid) > 7 { |
| 149 | return oid[:7] |
| 150 | } |
| 151 | return oid |
| 152 | } |
| 153 | |
| 154 | // IsObjectID reports whether s is a full SHA-1 or SHA-256 object ID, |
| 155 | // lowercase. |
| 156 | func IsObjectID(s string) bool { |
| 157 | return isHex(s, 40) || isHex(s, 64) |
| 158 | } |
| 159 | |
| 160 | func isHex(s string, n int) bool { |
| 161 | if len(s) != n { |
| 162 | return false |
| 163 | } |
| 164 | for _, c := range []byte(s) { |
| 165 | if (c < '0' || c > '9') && (c < 'a' || c > 'f') { |
| 166 | return false |
| 167 | } |
| 168 | } |
| 169 | return true |
| 170 | } |