worktree

package
v2.6.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 28, 2026 License: MIT Imports: 11 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package worktree owns the git worktree lifecycle and diff capture, all through the Executor interface so it works identically on every target kind.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddExcludes

func AddExcludes(ctx context.Context, ex executor.Executor, repoDir string) error

AddExcludes keeps our runtime dir and verify-run artifacts out of git status/diff/commit. Verify artifacts leaking into `git add -A` was a real bug found only on a live dispatch.

func BranchName

func BranchName(taskID int64, attemptN int) string

BranchName is the branch one attempt works on.

func Commit added in v2.6.0

func Commit(ctx context.Context, ex executor.Executor, dir, branch string, o CommitOptions) ([]map[string]any, error)

Commit is CommitPushPR with the review workspace's options. The commit step fails as a StepError (409) exactly like CommitPushPR's.

func CommitPushPR added in v2.4.0

func CommitPushPR(ctx context.Context, ex executor.Executor, dir, branch, message string,
	push, pr bool, prTitle, prBody string) ([]map[string]any, error)

CommitPushPR runs `git add -A && git commit`, an optional push and an optional `gh pr create`, in dir on branch. It is the one place the task and session Commit buttons and the CI loop's own fix commits touch git, so a fix to one path fixes all of them. A push or PR step that runs but fails is recorded in steps and does not abort, except a failed push always cancels a requested PR (never open a PR for a branch that didn't reach origin).

func DefaultWorkroot

func DefaultWorkroot(repoPath string) string

DefaultWorkroot puts worktrees beside the repo, not inside it.

func Ensure

func Ensure(ctx context.Context, ex executor.Executor, repo, baseBranch, branch, path string) error

Ensure creates the attempt's worktree, tolerating one that already exists — follow-up attempts and the reviewer gate deliberately reuse it.

func ForcePushWithLease added in v2.6.0

func ForcePushWithLease(ctx context.Context, ex executor.Executor, dir, branch, lease string) (map[string]any, error)

ForcePushWithLease pushes branch with --force-with-lease, pinned to lease (the remote commit the caller last saw) when one is given, so a push that would discard someone else's newer work fails instead.

func IntentToAddUntracked added in v2.6.0

func IntentToAddUntracked(dir string) string

IntentToAddUntracked is the command that marks every untracked, not ignored file in dir as intent-to-add, and nothing else.

func IsGitRepo added in v2.4.0

func IsGitRepo(ctx context.Context, ex executor.Executor, dir string) bool

IsGitRepo reports whether dir is inside a git working tree. Used before a live diff or commit so a plain (non-git) workdir gets a clear error instead of a confusing git failure three commands later.

func MergeBase added in v2.4.0

func MergeBase(ctx context.Context, ex executor.Executor, wt, baseRef string) (string, error)

MergeBase resolves the commit a worktree's HEAD shares with baseRef, so a live diff shows only what the worktree actually changed even if baseRef has moved on since the worktree branched from it. Returns "" (not an error) when git can't resolve one — an unborn branch, or baseRef missing entirely — so callers can fall back to diffing against baseRef directly.

func NamespacedBranch

func NamespacedBranch(branch, namespace string) string

NamespacedBranch scopes an automatically-created branch to one runtime.

func NamespacedWorkroot

func NamespacedWorkroot(workroot, namespace string) string

NamespacedWorkroot keeps automatically-created allocations from separate control-plane instances out of one another's default directory.

func Path

func Path(workroot string, taskID int64, attemptN int) string

Path is where one attempt's worktree lives under a workroot.

func Push added in v2.4.0

func Push(ctx context.Context, ex executor.Executor, dir, branch string) (map[string]any, error)

Push runs `git push -u origin <branch>` in dir and reports it as a step. A push that runs and fails is a step with a non-zero "rc", not an error.

func PushedRefs added in v2.6.0

func PushedRefs(ctx context.Context, ex executor.Executor, dir string) ([]string, error)

PushedRefs lists the remote-tracking branches that already contain HEAD. A commit on any of them is published; amending it rewrites history someone else may have.

func Remove

func Remove(ctx context.Context, ex executor.Executor, repo, path string) error

Remove reclaims a worktree.

func RunInteractive

func RunInteractive(ctx context.Context, ex executor.Executor, action string, plan *Interactive) error

func RunInteractiveWithTimeout

func RunInteractiveWithTimeout(ctx context.Context, ex executor.Executor, action string, plan *Interactive, timeout float64) error

Types

type CommitOptions added in v2.6.0

type CommitOptions struct {
	Message string
	// StageAll runs `git add -A` first; otherwise only what is staged is
	// committed.
	StageAll bool
	Amend    bool
	// AllowPushedAmend overrides the guard that refuses to amend a commit a
	// remote branch already contains.
	AllowPushedAmend bool
	Push             bool
	// ForceWithLease pushes with --force-with-lease. Lease, when set, is the
	// remote commit the caller last saw; the push fails if origin moved.
	ForceWithLease bool
	Lease          string
	PR             bool
	PRTitle        string
	PRBody         string
}

CommitOptions is the review workspace's commit: the index as staged or everything, optionally amending HEAD, then an optional push (plain or force-with-lease) and PR.

type ErrAmendPushed added in v2.6.0

type ErrAmendPushed struct{ Refs []string }

ErrAmendPushed is the amend guard's refusal. Refs names the remote branches that already contain HEAD.

func (*ErrAmendPushed) Error added in v2.6.0

func (e *ErrAmendPushed) Error() string

type FilePatch

type FilePatch struct {
	Path  string `json:"path"`
	Patch string `json:"patch"`
}

FilePatch is one file's slice of a unified diff, for the mobile viewer.

func SplitPatch

func SplitPatch(patch string) []FilePatch

SplitPatch slices a unified diff into per-file chunks.

type FileStat

type FileStat struct {
	Path      string `json:"path"`
	Additions int    `json:"additions"`
	Deletions int    `json:"deletions"`
}

FileStat is one file's line churn in a captured diff.

func CaptureDiff

func CaptureDiff(ctx context.Context, ex executor.Executor, wt, baseBranch string) (string, []FileStat, error)

CaptureDiff returns the full patch plus per-file stats of everything the attempt changed against its base branch.

type Interactive

type Interactive struct {
	OperationActive bool                  `json:"operation_active,omitempty"`
	ControlToken    string                `json:"control_token,omitempty"`
	SetupCommand    string                `json:"setup_command,omitempty"`
	SetupEnv        map[string]string     `json:"setup_env,omitempty"`
	SetupState      string                `json:"setup_state,omitempty"`
	SetupOutput     string                `json:"setup_output,omitempty"`
	Repo            string                `json:"repo"`
	Path            string                `json:"path"`
	Branch          string                `json:"branch"`
	Base            string                `json:"base"`
	Commit          string                `json:"commit"`
	Token           string                `json:"token,omitempty"`
	State           string                `json:"state"`
	Error           string                `json:"error,omitempty"`
	Repositories    []WorkspaceRepository `json:"repositories,omitempty"`
}

Interactive records an allocation before any remote Git mutation. A failed launch keeps this record so the directory is still discoverable and removable.

func PlanInteractive

func PlanInteractive(repo string, id int64, o InteractiveOptions) *Interactive

func PlanMultiWorkspace

func PlanMultiWorkspace(sources []RepositorySource, id int64, options InteractiveOptions) (*Interactive, error)

PlanMultiWorkspace creates only a plan. It never touches Git or the filesystem. Each repository has its own ownership token, base and allocation state.

func PlanWorkspaceExtension

func PlanWorkspaceExtension(existing *Interactive, source RepositorySource, id int64) (*Interactive, error)

PlanWorkspaceExtension preserves existing ownership and stages one new child. The worker must validate the complete plan against the live root receipt.

func (*Interactive) HasSetupCommand

func (p *Interactive) HasSetupCommand() bool

HasSetupCommand gives package installation a longer overall setup deadline.

func (*Interactive) RedactOwnership

func (p *Interactive) RedactOwnership()

RedactOwnership removes all allocation tokens before an API response. It is applied to a decoded copy, never to the persisted ownership records.

type InteractiveOptions

type InteractiveOptions struct {
	Base              string                `json:"base"`
	Branch            string                `json:"branch"`
	Namespace         string                `json:"-"`
	Workroot          string                `json:"-"`
	ExtraRepositories []RepositorySelection `json:"extra_repositories,omitempty"`
}

type RepositorySelection

type RepositorySelection struct {
	ProjectID int64  `json:"project_id"`
	Base      string `json:"base,omitempty"`
}

type RepositorySource

type RepositorySource struct {
	Name, Repo, Base string
	SetupCommand     string
	SetupEnv         map[string]string
	ProjectID        *int64
}

RepositorySource is resolved from registered projects on one target before planning; paths and Git identities must still be verified on that target.

type StepError added in v2.4.0

type StepError struct {
	Status int
	Msg    string
}

StepError is a git/gh step that ran and failed on its own terms (bad commit, a real merge conflict) as opposed to the executor itself failing — the former is the caller's to fix and maps to 409, the latter is ours and maps through respondErr like any other infrastructure error.

func (*StepError) Error added in v2.4.0

func (e *StepError) Error() string

type WorkspaceRepository

type WorkspaceRepository struct {
	Name      string       `json:"name"`
	ProjectID *int64       `json:"project_id,omitempty"`
	Worktree  *Interactive `json:"worktree"`
}

WorkspaceRepository records each allocation independently, including a failed or removed one. The enclosing workspace owns the root directory separately.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL