| 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 | |
| 26 | // waitDelay is how long a wait for git's output may go on after git has |
| 27 | // exited, or after a cancelled context has killed it. Something git |
| 28 | // started may still hold the output open. |
| 29 | const waitDelay = 5 * time.Second |
| 30 | |
| 31 | // reachesOrigin is the git commands that talk to origin. |
| 32 | var reachesOrigin = map[string]bool{"ls-remote": true, "fetch": true, "push": true} |
| 33 | |
| 34 | // command is git with the arguments given, run in g's repository and |
| 35 | // environment under ctx. |
| 36 | // |
| 37 | // A cancelled context kills git and everything git started, such as a |
| 38 | // hook or a filter. Killing git alone is not enough: a hook it started |
| 39 | // can keep git's output open, and a wait for that output would not |
| 40 | // return. So git runs in a process group of its own, and the whole group |
| 41 | // is killed. A wait for git's output is also bounded by waitDelay, for a |
| 42 | // process that has left the group. |
| 43 | // |
| 44 | // A command that reaches origin stays in the caller's process group, and |
| 45 | // is killed alone. A process group of its own is not the terminal's |
| 46 | // foreground group, and a process outside that group that reads the |
| 47 | // terminal is stopped. ssh reads the terminal to ask for a passphrase, |
| 48 | // and git reads it to ask for credentials. |
| 49 | func (g Git) command(ctx context.Context, args ...string) *exec.Cmd { |
| 50 | cmd := exec.CommandContext(ctx, "git", args...) |
| 51 | cmd.Env, cmd.Dir, cmd.WaitDelay = g.Env, g.Dir, waitDelay |
| 52 | if len(args) == 0 || !reachesOrigin[args[0]] { |
| 53 | ownGroup(cmd) |
| 54 | } |
| 55 | return cmd |
| 56 | } |
| 57 | |
| 58 | // finished is the error of git that has ended. git that exits zero has |
| 59 | // succeeded even when the wait for its output was cut short: only |
| 60 | // something git started can still hold the output then. |
| 61 | func finished(err error) error { |
| 62 | if errors.Is(err, exec.ErrWaitDelay) { |
| 63 | return nil |
| 64 | } |
| 65 | return err |
| 66 | } |
| 67 | |
| 68 | // output runs git and returns what it printed, less trailing newlines, as |
| 69 | // a shell's command substitution reads it. A quiet run discards git's |
| 70 | // diagnostics; any other passes them on. |
| 71 | func (g Git) output(ctx context.Context, quiet bool, args ...string) (string, error) { |
| 72 | return g.input(ctx, quiet, nil, args...) |
| 73 | } |
| 74 | |
| 75 | // input is output with stdin. |
| 76 | func (g Git) input(ctx context.Context, quiet bool, stdin io.Reader, args ...string) (string, error) { |
| 77 | cmd := g.command(ctx, args...) |
| 78 | cmd.Stdin = stdin |
| 79 | if !quiet { |
| 80 | cmd.Stderr = g.Stderr |
| 81 | } |
| 82 | out, err := cmd.Output() |
| 83 | return strings.TrimRight(string(out), "\n"), finished(err) |
| 84 | } |
| 85 | |
| 86 | // succeeds reports whether git exits zero. A failure to run git at all is |
| 87 | // a no as well, as it is to a shell condition. |
| 88 | func (g Git) succeeds(ctx context.Context, quiet bool, args ...string) bool { |
| 89 | _, err := g.output(ctx, quiet, args...) |
| 90 | return err == nil |
| 91 | } |
| 92 | |
| 93 | // passing runs git with its output on Stdout and its diagnostics on |
| 94 | // Stderr. |
| 95 | func (g Git) passing(ctx context.Context, args ...string) error { |
| 96 | cmd := g.command(ctx, args...) |
| 97 | cmd.Stdout, cmd.Stderr = g.Stdout, g.Stderr |
| 98 | return finished(cmd.Run()) |
| 99 | } |
| 100 | |
| 101 | // in is g run in another directory. |
| 102 | func (g Git) in(dir string) Git { |
| 103 | g.Dir = dir |
| 104 | return g |
| 105 | } |
| 106 | |
| 107 | // short is an object ID cut to seven digits, as messages name a commit. |
| 108 | func short(oid string) string { |
| 109 | if len(oid) > 7 { |
| 110 | return oid[:7] |
| 111 | } |
| 112 | return oid |
| 113 | } |
| 114 | |
| 115 | // IsObjectID reports whether s is a full SHA-1 or SHA-256 object ID, |
| 116 | // lowercase. |
| 117 | func IsObjectID(s string) bool { |
| 118 | return isHex(s, 40) || isHex(s, 64) |
| 119 | } |
| 120 | |
| 121 | func isHex(s string, n int) bool { |
| 122 | if len(s) != n { |
| 123 | return false |
| 124 | } |
| 125 | for _, c := range []byte(s) { |
| 126 | if (c < '0' || c > '9') && (c < 'a' || c > 'f') { |
| 127 | return false |
| 128 | } |
| 129 | } |
| 130 | return true |
| 131 | } |