Run every pipeline run in its own worktree
A fresh run started from a repository's main checkout gets its own worktree and branch next to the repository, so runs never share a tree or see the operator's uncommitted work, and several can run at once. Retries and runs started in a linked worktree keep their directory. A clean, landed run tree is removed after success; anything else is kept. The delivery gate commits what the land stage left uncommitted in a run worktree, and no longer requires the operator's main checkout to be clean: Git's fast-forward refuses safely when a local change conflicts.
This commit is contained in:
@@ -35,7 +35,14 @@ const deliveryCleanupTimeout = 10 * time.Second
|
||||
// fast-forwards main. A conflicting merge is aborted, leaving the workspace
|
||||
// exactly as it was. The returned note is non-empty when a catch-up merge
|
||||
// happened, so the caller can record it.
|
||||
func verifyDelivery(ctx context.Context, workspace string, access stage.LandingAccess, checks []string) (string, error) {
|
||||
//
|
||||
// Work the land stage left uncommitted in the item workspace is committed
|
||||
// first (commitMessage): the workspace is the run's own tree, so everything in
|
||||
// it is the run's work, and a partial file list must never drop part of it.
|
||||
// The operator's main checkout need not be clean: the fast-forward is Git's
|
||||
// own `merge --ff-only`, which refuses without touching anything when a local
|
||||
// change or untracked file would be overwritten.
|
||||
func verifyDelivery(ctx context.Context, workspace string, access stage.LandingAccess, checks []string, commitMessage string) (string, error) {
|
||||
lock, err := acquireDeliveryLock(ctx, access.LockPath)
|
||||
if err != nil {
|
||||
return "", err
|
||||
@@ -48,14 +55,15 @@ func verifyDelivery(ctx context.Context, workspace string, access stage.LandingA
|
||||
if current != access {
|
||||
return "", fmt.Errorf("delivery access changed during stage")
|
||||
}
|
||||
for _, directory := range []string{workspace, access.MainWorktree} {
|
||||
status, err := gitOutput(directory, "status", "--porcelain=v1", "--untracked-files=all")
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("delivery checkout status: %w", err)
|
||||
}
|
||||
if status != "" {
|
||||
return "", fmt.Errorf("delivery checkout is not clean: %s", strings.Split(status, "\n")[0])
|
||||
}
|
||||
if err := commitDeliveryLeftovers(ctx, workspace, commitMessage); err != nil {
|
||||
return "", err
|
||||
}
|
||||
status, err := gitOutput(workspace, "status", "--porcelain=v1", "--untracked-files=all")
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("delivery checkout status: %w", err)
|
||||
}
|
||||
if status != "" {
|
||||
return "", fmt.Errorf("delivery checkout is not clean: %s", strings.Split(status, "\n")[0])
|
||||
}
|
||||
itemHead, err := gitOutput(workspace, "rev-parse", "HEAD")
|
||||
if err != nil {
|
||||
@@ -84,16 +92,14 @@ func verifyDelivery(ctx context.Context, workspace string, access stage.LandingA
|
||||
return note, fmt.Errorf("delivery item commit cannot fast-forward canonical main")
|
||||
}
|
||||
if output, err := exec.CommandContext(ctx, "git", "-C", access.MainWorktree, "merge", "--ff-only", itemHead).CombinedOutput(); err != nil {
|
||||
return note, fmt.Errorf("delivery fast-forward canonical main: %w: %s", err, strings.TrimSpace(string(output)))
|
||||
return note, fmt.Errorf("delivery fast-forward canonical main (the main checkout has local changes that conflict with the item; the item is checked and ready on its branch): %w: %s", err, strings.TrimSpace(string(output)))
|
||||
}
|
||||
for _, directory := range []string{workspace, access.MainWorktree} {
|
||||
status, err := gitOutput(directory, "status", "--porcelain=v1", "--untracked-files=all")
|
||||
if err != nil {
|
||||
return note, fmt.Errorf("delivery settlement status: %w", err)
|
||||
}
|
||||
if status != "" {
|
||||
return note, fmt.Errorf("delivery checkout is not clean after checks: %s", strings.Split(status, "\n")[0])
|
||||
}
|
||||
status, err = gitOutput(workspace, "status", "--porcelain=v1", "--untracked-files=all")
|
||||
if err != nil {
|
||||
return note, fmt.Errorf("delivery settlement status: %w", err)
|
||||
}
|
||||
if status != "" {
|
||||
return note, fmt.Errorf("delivery checkout is not clean after checks: %s", strings.Split(status, "\n")[0])
|
||||
}
|
||||
// Re-derive at the settlement boundary. A success means exactly the checked
|
||||
// item commit is now reachable from the same canonical main worktree.
|
||||
@@ -111,6 +117,26 @@ func verifyDelivery(ctx context.Context, workspace string, access stage.LandingA
|
||||
return note, nil
|
||||
}
|
||||
|
||||
// commitDeliveryLeftovers commits everything still uncommitted in the item
|
||||
// workspace (tracked and untracked, respecting .gitignore). An empty message
|
||||
// or a clean tree commits nothing.
|
||||
func commitDeliveryLeftovers(ctx context.Context, workspace, message string) error {
|
||||
status, err := gitOutput(workspace, "status", "--porcelain=v1", "--untracked-files=all")
|
||||
if err != nil {
|
||||
return fmt.Errorf("delivery checkout status: %w", err)
|
||||
}
|
||||
if status == "" || strings.TrimSpace(message) == "" {
|
||||
return nil
|
||||
}
|
||||
if output, err := boundedCommandOutput(ctx, workspace, "add", "--all"); err != nil {
|
||||
return fmt.Errorf("delivery commit of remaining changes: %w: %s", err, strings.TrimSpace(string(output)))
|
||||
}
|
||||
if output, err := boundedCommandOutput(ctx, workspace, "commit", "--no-verify", "-m", message); err != nil {
|
||||
return fmt.Errorf("delivery commit of remaining changes: %w: %s", err, strings.TrimSpace(string(output)))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// catchUpDeliveryItem merges canonical main into the clean item workspace with a
|
||||
// normal merge commit and returns the new item HEAD. It never rewrites history:
|
||||
// a failed merge is aborted and the workspace is verified to be back at
|
||||
|
||||
Reference in New Issue
Block a user