Add deadlines to git network ops and lock merges per project
This commit is contained in:
+41
-11
@@ -1,6 +1,7 @@
|
||||
package git
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
@@ -10,6 +11,8 @@ import (
|
||||
"slices"
|
||||
"strconv"
|
||||
"strings"
|
||||
"syscall"
|
||||
"time"
|
||||
)
|
||||
|
||||
type RepoInfo struct {
|
||||
@@ -228,10 +231,17 @@ func SquashCommit(repo, worktree, target, message string, env []string) (string,
|
||||
|
||||
const Remote = "origin"
|
||||
|
||||
const networkTimeout = 2 * time.Minute
|
||||
|
||||
var networkEnv = []string{
|
||||
"GIT_TERMINAL_PROMPT=0",
|
||||
"GIT_SSH_COMMAND=ssh -o BatchMode=yes -o ConnectTimeout=30",
|
||||
}
|
||||
|
||||
var ErrRemoteMoved = errors.New("the remote's branch has moved")
|
||||
|
||||
func Push(repo, commit, branch, notes string) error {
|
||||
out, stderr, code, err := runCode(repo, nil, "push", "--atomic", "--porcelain", Remote, commit+":refs/heads/"+branch, notes+":"+NotesRef)
|
||||
func Push(ctx context.Context, repo, commit, branch, notes string) error {
|
||||
out, stderr, code, err := runNetwork(ctx, repo, "push", "--atomic", "--porcelain", Remote, commit+":refs/heads/"+branch, notes+":"+NotesRef)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -251,13 +261,19 @@ func FastForward(repo, rev string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
func Fetch(repo string) error {
|
||||
_, err := run(repo, "fetch", Remote)
|
||||
return err
|
||||
func Fetch(ctx context.Context, repo string) error {
|
||||
_, stderr, code, err := runNetwork(ctx, repo, "fetch", Remote)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if code != 0 {
|
||||
return fmt.Errorf("git fetch %s: exit status %d: %s", Remote, code, stderr)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func FetchFastForward(repo, branch string) error {
|
||||
err := Fetch(repo)
|
||||
func FetchFastForward(ctx context.Context, repo, branch string) error {
|
||||
err := Fetch(ctx, repo)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -336,7 +352,7 @@ func runEnv(dir string, env []string, args ...string) (string, error) {
|
||||
}
|
||||
|
||||
func runInput(dir string, env []string, input string, args ...string) (string, error) {
|
||||
out, stderr, code, err := runCodeInput(dir, env, strings.NewReader(input), args...)
|
||||
out, stderr, code, err := runCodeInput(context.Background(), dir, env, strings.NewReader(input), args...)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
@@ -347,17 +363,31 @@ func runInput(dir string, env []string, input string, args ...string) (string, e
|
||||
}
|
||||
|
||||
func runCode(dir string, env []string, args ...string) (string, string, int, error) {
|
||||
return runCodeInput(dir, env, nil, args...)
|
||||
return runCodeInput(context.Background(), dir, env, nil, args...)
|
||||
}
|
||||
|
||||
func runCodeInput(dir string, env []string, stdin io.Reader, args ...string) (string, string, int, error) {
|
||||
cmd := exec.Command("git", args...)
|
||||
func runNetwork(ctx context.Context, dir string, args ...string) (string, string, int, error) {
|
||||
ctx, cancel := context.WithTimeout(ctx, networkTimeout)
|
||||
defer cancel()
|
||||
return runCodeInput(ctx, dir, networkEnv, nil, args...)
|
||||
}
|
||||
|
||||
func runCodeInput(ctx context.Context, dir string, env []string, stdin io.Reader, args ...string) (string, string, int, error) {
|
||||
cmd := exec.CommandContext(ctx, "git", args...)
|
||||
cmd.Dir = dir
|
||||
cmd.Env = append(os.Environ(), env...)
|
||||
cmd.Stdin = stdin
|
||||
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
|
||||
cmd.Cancel = func() error {
|
||||
return syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL)
|
||||
}
|
||||
cmd.WaitDelay = 5 * time.Second
|
||||
stderr := &strings.Builder{}
|
||||
cmd.Stderr = stderr
|
||||
out, err := cmd.Output()
|
||||
if ctx.Err() != nil {
|
||||
return "", "", 0, fmt.Errorf("git %s: %w: %s", strings.Join(args, " "), context.Cause(ctx), strings.TrimSpace(stderr.String()))
|
||||
}
|
||||
exit := &exec.ExitError{}
|
||||
if errors.As(err, &exit) {
|
||||
return strings.TrimSpace(string(out)), strings.TrimSpace(stderr.String()), exit.ExitCode(), nil
|
||||
|
||||
Reference in New Issue
Block a user