Add deadlines to git network ops and lock merges per project
This commit is contained in:
@@ -59,8 +59,8 @@ type Manager struct {
|
||||
snapMu sync.Mutex
|
||||
snaps map[string]*snapState
|
||||
|
||||
mergeMu sync.Mutex
|
||||
mergeHolders map[string]string
|
||||
mergeMu sync.Mutex
|
||||
merges map[string]*projectMerge
|
||||
|
||||
reviewMu sync.Mutex
|
||||
|
||||
@@ -130,7 +130,7 @@ func New(st *store.Store, worktreeRoot, leadRoot, codexHome string) *Manager {
|
||||
finishing: map[string]bool{},
|
||||
signals: make(chan []api.Event, 1024),
|
||||
snaps: map[string]*snapState{},
|
||||
mergeHolders: map[string]string{},
|
||||
merges: map[string]*projectMerge{},
|
||||
bad: newBadLines(),
|
||||
watchStop: make(chan struct{}),
|
||||
}
|
||||
|
||||
+51
-30
@@ -8,6 +8,7 @@ import (
|
||||
"log/slog"
|
||||
"slices"
|
||||
"strings"
|
||||
"sync"
|
||||
"unicode/utf8"
|
||||
|
||||
"g.fc.run/misc/byre/internal/api"
|
||||
@@ -65,10 +66,11 @@ func (m *Manager) Merge(ctx context.Context, task api.Task, project api.Project,
|
||||
return api.Task{}, err
|
||||
}
|
||||
|
||||
m.mergeMu.Lock()
|
||||
defer m.mergeMu.Unlock()
|
||||
pm := m.projectMerge(project.Id)
|
||||
pm.mu.Lock()
|
||||
defer pm.mu.Unlock()
|
||||
|
||||
if m.mergeHolders[project.Id] == task.Id {
|
||||
if pm.holder == task.Id {
|
||||
return api.Task{}, fmt.Errorf("%w: task %s is already merging", ErrConflict, task.Id)
|
||||
}
|
||||
if title != task.MergeTitle {
|
||||
@@ -79,9 +81,9 @@ func (m *Manager) Merge(ctx context.Context, task api.Task, project api.Project,
|
||||
task.MergeTitle = title
|
||||
}
|
||||
|
||||
switch m.mergeHolders[project.Id] {
|
||||
switch pm.holder {
|
||||
case "":
|
||||
err := m.startMerge(ctx, task, project, user)
|
||||
err := m.startMerge(ctx, pm, task, project, user)
|
||||
if err != nil {
|
||||
return api.Task{}, err
|
||||
}
|
||||
@@ -97,8 +99,24 @@ func (m *Manager) Merge(ctx context.Context, task api.Task, project api.Project,
|
||||
return m.store.GetTask(ctx, task.Id)
|
||||
}
|
||||
|
||||
func (m *Manager) startMerge(ctx context.Context, task api.Task, project api.Project, user api.User) error {
|
||||
err := git.FetchFastForward(project.RepoPath, project.DefaultBranch)
|
||||
type projectMerge struct {
|
||||
mu sync.Mutex
|
||||
holder string
|
||||
}
|
||||
|
||||
func (m *Manager) projectMerge(projectID string) *projectMerge {
|
||||
m.mergeMu.Lock()
|
||||
defer m.mergeMu.Unlock()
|
||||
pm := m.merges[projectID]
|
||||
if pm == nil {
|
||||
pm = &projectMerge{}
|
||||
m.merges[projectID] = pm
|
||||
}
|
||||
return pm
|
||||
}
|
||||
|
||||
func (m *Manager) startMerge(ctx context.Context, pm *projectMerge, task api.Task, project api.Project, user api.User) error {
|
||||
err := git.FetchFastForward(ctx, project.RepoPath, project.DefaultBranch)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -110,13 +128,13 @@ func (m *Manager) startMerge(ctx context.Context, task api.Task, project api.Pro
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
m.mergeHolders[project.Id] = task.Id
|
||||
pm.holder = task.Id
|
||||
|
||||
prompt := mergeInPrompt(project.DefaultBranch) + fmt.Sprintf(" When this turn ends, byre squashes this branch onto %s and pushes it; if you stop, call stop_merge first so it doesn't. ", project.DefaultBranch) + summaryRequest
|
||||
_, err = m.sendMergeIn(ctx, task, user, prompt)
|
||||
if err != nil {
|
||||
m.takeMerge(task.Id)
|
||||
delete(m.mergeHolders, project.Id)
|
||||
pm.holder = ""
|
||||
}
|
||||
return err
|
||||
}
|
||||
@@ -127,10 +145,11 @@ func (m *Manager) recoverMerges(ctx context.Context) error {
|
||||
return err
|
||||
}
|
||||
|
||||
m.mergeMu.Lock()
|
||||
defer m.mergeMu.Unlock()
|
||||
for _, t := range merging {
|
||||
m.mergeHolders[t.ProjectId] = t.Id
|
||||
pm := m.projectMerge(t.ProjectId)
|
||||
pm.mu.Lock()
|
||||
pm.holder = t.Id
|
||||
pm.mu.Unlock()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -177,7 +196,7 @@ func (m *Manager) resumeFinalizing(ctx context.Context, f store.FinalizingMerge)
|
||||
return errors.New("nothing was pushed. Merge again to retry")
|
||||
}
|
||||
|
||||
landed, err := pushLanded(project, f.Commit)
|
||||
landed, err := pushLanded(ctx, project, f.Commit)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -187,9 +206,9 @@ func (m *Manager) resumeFinalizing(ctx context.Context, f store.FinalizingMerge)
|
||||
return m.closePushed(ctx, f.Task, project, f.Commit, f.Notes, f.By)
|
||||
}
|
||||
|
||||
func pushLanded(project api.Project, commit string) (bool, error) {
|
||||
func pushLanded(ctx context.Context, project api.Project, commit string) (bool, error) {
|
||||
target := git.Remote + "/" + project.DefaultBranch
|
||||
err := git.Fetch(project.RepoPath)
|
||||
err := git.Fetch(ctx, project.RepoPath)
|
||||
if err != nil {
|
||||
return false, &unfinishedMergeError{fmt.Errorf("checking whether %s reached %s failed: %w", commit, target, err)}
|
||||
}
|
||||
@@ -231,22 +250,24 @@ func (m *Manager) abandonMerge(taskID, projectID string) {
|
||||
}
|
||||
|
||||
func (m *Manager) releaseMerge(projectID, taskID string) {
|
||||
m.mergeMu.Lock()
|
||||
defer m.mergeMu.Unlock()
|
||||
if m.mergeHolders[projectID] == taskID {
|
||||
delete(m.mergeHolders, projectID)
|
||||
pm := m.projectMerge(projectID)
|
||||
pm.mu.Lock()
|
||||
defer pm.mu.Unlock()
|
||||
if pm.holder == taskID {
|
||||
pm.holder = ""
|
||||
}
|
||||
m.startNextMerge(projectID)
|
||||
m.startNextMerge(pm, projectID)
|
||||
}
|
||||
|
||||
func (m *Manager) nextMerge(projectID string) {
|
||||
m.mergeMu.Lock()
|
||||
defer m.mergeMu.Unlock()
|
||||
m.startNextMerge(projectID)
|
||||
pm := m.projectMerge(projectID)
|
||||
pm.mu.Lock()
|
||||
defer pm.mu.Unlock()
|
||||
m.startNextMerge(pm, projectID)
|
||||
}
|
||||
|
||||
func (m *Manager) startNextMerge(projectID string) {
|
||||
if m.mergeHolders[projectID] != "" {
|
||||
func (m *Manager) startNextMerge(pm *projectMerge, projectID string) {
|
||||
if pm.holder != "" {
|
||||
return
|
||||
}
|
||||
|
||||
@@ -278,7 +299,7 @@ func (m *Manager) startNextMerge(projectID string) {
|
||||
continue
|
||||
}
|
||||
|
||||
err = m.startHeldMerge(ctx, task, login)
|
||||
err = m.startHeldMerge(ctx, pm, task, login)
|
||||
if err == nil {
|
||||
slog.Info("started held merge", "task", task.Id, "by", login)
|
||||
return
|
||||
@@ -288,7 +309,7 @@ func (m *Manager) startNextMerge(projectID string) {
|
||||
}
|
||||
}
|
||||
|
||||
func (m *Manager) startHeldMerge(ctx context.Context, task api.Task, login string) error {
|
||||
func (m *Manager) startHeldMerge(ctx context.Context, pm *projectMerge, task api.Task, login string) error {
|
||||
project, err := m.store.GetProject(ctx, task.ProjectId)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -300,7 +321,7 @@ func (m *Manager) startHeldMerge(ctx context.Context, task api.Task, login strin
|
||||
if user.GitName == "" || user.GitEmail == "" {
|
||||
return ErrNoGitIdentity
|
||||
}
|
||||
return m.startMerge(ctx, task, project, user)
|
||||
return m.startMerge(ctx, pm, task, project, user)
|
||||
}
|
||||
|
||||
func (m *Manager) fileTaskError(ctx context.Context, task api.Task, sessions []api.AgentSession, msg string) {
|
||||
@@ -396,12 +417,12 @@ func (m *Manager) squashAndClose(ctx context.Context, taskID, login string) erro
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = git.Push(project.RepoPath, commit, project.DefaultBranch, notes)
|
||||
err = git.Push(ctx, project.RepoPath, commit, project.DefaultBranch, notes)
|
||||
if errors.Is(err, git.ErrRemoteMoved) {
|
||||
return err
|
||||
}
|
||||
if err != nil {
|
||||
landed, lerr := pushLanded(project, commit)
|
||||
landed, lerr := pushLanded(ctx, project, commit)
|
||||
if lerr != nil {
|
||||
return &unfinishedMergeError{fmt.Errorf("%w, and %w", err, errors.Unwrap(lerr))}
|
||||
}
|
||||
|
||||
+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