git

package
v0.24.0 Latest Latest
Warning

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

Go to latest
Published: Nov 7, 2026 License: MIT Imports: 17 Imported by: 1 module, 1 package

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 git is the core module's git sandbox backend: the in-place materializer over the caller's repository and the detached-worktree isolated mode, both addressable through the sandbox package's registry as "git" (the backend self-registers on import). The Sandbox contract, the capability model, and OpenSandbox live in the parent sandbox package; this package owns git state only.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CleanPolicy added in v0.22.0

type CleanPolicy int

CleanPolicy selects what the apply (rewind) sequence does with untracked content in the workspace.

The apply sequence has always run `git clean -fd`, which deletes every untracked path — including content created after the checkpoint was captured: orchestrator scratch files, freshly authored role definitions, empty scratch directories, sub-agent output directories. For a workspace whose owner treats the tree as a live host workspace rather than a disposable checkout, that is losing data the rewind was never meant to reclaim.

The policy does not change the git commands Apply runs; it decides what those commands are allowed to see first. Under CleanPreserveUntracked the untracked content absent from the target state is evacuated into a stash directory under the git dir before the clean, and moved back once the apply has finished, so `git clean -fd` reclaims exactly what the captured state holds and nothing else.

const (
	// CleanAll is the default and the historical behavior: `git clean -fd`
	// runs over the full worktree, so untracked content is deleted by a
	// rewind. Choosing it keeps Apply byte-identical to a sandbox built
	// without WithCleanPolicy.
	CleanAll CleanPolicy = iota

	// CleanPreserveUntracked keeps untracked content the target state does
	// not contain. Preservation is lossless-or-loud: content that cannot be
	// moved back — because the applied state put something else at its
	// path, or because a parent component is no longer a plain directory —
	// stays in the stash under the git dir and Apply reports both the stash
	// location and the reason. Nothing is ever destroyed silently.
	CleanPreserveUntracked
)

func (CleanPolicy) String added in v0.22.0

func (p CleanPolicy) String() string

String renders the policy for logs and error messages. An unrecognized value renders as the default: only CleanPreserveUntracked opts into the new behavior, so a misconfigured policy fails safe onto the byte-identical path.

type GitSandbox

type GitSandbox struct {
	// contains filtered or unexported fields
}

GitSandbox materializes a git working tree on the host filesystem.

It has two modes, distinguished only by lifecycle:

  • In-place (NewLocalGitSandbox): operates directly on the caller's repository. It is a materializer, not an isolation boundary, so Isolated is false and trace.Containment is trace.ContainUncontained. Create validates that the path is a repository and Destroy is a no-op — a sandbox must never delete a user's repository.

  • Worktree (NewWorktreeSandbox): allocates a detached `git worktree` for the scope. Create and Destroy manage real resources, Isolated is true, and trace.Containment is trace.ContainBuffered: effects are held in a separate checkout and only graduate when the caller applies that state to the parent's in-place sandbox.

Capture, Apply, and Diff are identical across modes and are shared: git worktrees share the object store, so a tree captured in a worktree can be applied or merged in the main tree of the same repository.

Exec, ReadFile, and WriteFile return sbx.ErrUnsupported. Agent processes are the caller's concern; this backend owns git state only.

func NewLocalGitSandbox

func NewLocalGitSandbox(repoPath string, opts ...Option) *GitSandbox

NewLocalGitSandbox returns an in-place sandbox over repoPath.

func NewWorktreeSandbox

func NewWorktreeSandbox(repoPath, worktreePath string, opts ...Option) *GitSandbox

NewWorktreeSandbox returns an isolated sandbox that checks out repoPath at a detached worktree in worktreePath. repoPath remains the source of truth for git administrative commands; worktreePath is where workspace operations run. A relative worktreePath is resolved against the repository — the same way `git worktree add` resolves it, since Create runs with the repository as its working directory — never against the process working directory, so the recorded address matches where the worktree actually lands. The recorded address is normalized to an absolute path.

func (*GitSandbox) Address

func (g *GitSandbox) Address() string

Address implements sbx.Addressed: the durable workspace address, a normalized absolute path — the worktree path when isolated, the repository path otherwise. Recovery records it in the fork payload and hands it back to the host's resolver, so an isolated child's substrate is re-adoptable from the trace alone.

func (*GitSandbox) AnnotateDeltaBase

func (g *GitSandbox) AnnotateDeltaBase(ctx context.Context, delta *trace.WorkspaceState, baseline trace.WorkspaceState) error

AnnotateDeltaBase implements sbx.DeltaBaseAnnotator: it resolves the fork baseline's tree identity, records it on a freshly captured delta as base_tree_sha, and pins both trees with refs (pinTree) so garbage collection cannot reclaim them between seal and settle.

The baseline's own data decides the resolution: tree_sha for states recorded after plan 06; a legacy dirty baseline's stash_sha resolves the minted stash commit's tree; a legacy clean baseline (empty stash_sha — `git stash create` reports nothing for a clean tree) resolves the recorded HEAD's tree, which is the same content. A legacy dirty baseline whose minted stash has since been pruned fails here with an actionable error — the fork can no longer settle over its dirty baseline, at seal time rather than mid-settle.

func (*GitSandbox) Apply

func (g *GitSandbox) Apply(ctx context.Context, ws trace.WorkspaceState) error

Apply resets the workspace to a captured state. The state is reusable: Apply consumes nothing, so the same value may be applied repeatedly (this is what fork-and-choose needs).

Tree-identified states (tree_sha) are restored exactly, including deletions. Legacy states recorded before tree_sha still carry stash_sha and keep the stash-apply path, so old traces keep applying.

func (*GitSandbox) ApplyDelta

func (g *GitSandbox) ApplyDelta(ctx context.Context, delta trace.WorkspaceState) error

ApplyDelta merges a captured state's changes onto the current workspace without resetting it — the three-way path for settlement's apply verb.

The delta is baseline-relative (plan 06, U1c): its data carries base_tree_sha, the fork baseline's tree recorded by Seal, and tree_sha, the sealed output's full tree. The merge base is the baseline, so the baseline's own uncommitted content appears on neither side and only the branch's actual changes land. git worktrees share the object store, so trees captured in one worktree merge in any checkout of the same repository.

Shape rules, so no implementation silently reintroduces the historical no-op bug (an empty stash_sha was treated as "clean capture, nothing to apply" even when the caller had handed it a state it could not read):

  • tree_sha == base_tree_sha: the output changed nothing; a no-op.
  • tree_sha without base_tree_sha: malformed — the merge has no base, and falling back to HEAD would read the baseline's own content as branch changes (U1b again). An error.
  • neither key present: malformed. An error, never a silent success.
  • a legacy stash_sha-carrying delta (recorded before tree_sha) keeps the stash-apply path; an empty stash is a legacy clean capture, a legitimate no-op — distinguished by key presence, not value.

A conflicting delta fails and leaves the workspace untouched.

func (*GitSandbox) Backend

func (g *GitSandbox) Backend() string

Backend identifies the backend.

func (*GitSandbox) Capabilities

func (g *GitSandbox) Capabilities() sbx.SandboxCapabilities

Capabilities reports the git backend's support matrix.

Exec is reported only when a containment backend was supplied via WithExec, and Containment is honest about the resolved enforcement: ContainContained only when a backend is actually available and the placement is not advisory, ContainBuffered for an isolated worktree, ContainUncontained otherwise. A backend must not claim containment it cannot provide (see the package doc on reporting capabilities honestly).

func (*GitSandbox) Capture

func (g *GitSandbox) Capture(ctx context.Context) (trace.WorkspaceState, error)

Capture records the current workspace state as a reusable trace.WorkspaceState.

The working tree — including untracked files — is staged into a scratch index (never the caller's) and the index's tree is written out with `git write-tree`. The recorded tree_sha is the content itself, not a per-capture artifact: two captures of an identical tree at the same HEAD — clean or dirty — digest equal, which is the digest-stability contract on Sandbox.Capture and what Settle(Select)'s precondition needs (plan 06, U1a). A capture after the timeline moved (a commit, even one whose tree matches the prior content) records a different state by design. Nothing is committed and the working tree is unchanged, so a caller that had staged changes keeps them staged.

func (*GitSandbox) CleanPolicy added in v0.22.0

func (g *GitSandbox) CleanPolicy() CleanPolicy

CleanPolicy reports the policy Apply uses with this sandbox. A host that adopts preserve semantics can surface the resolved policy in its own diagnostics; the zero value is CleanAll.

func (*GitSandbox) Create

func (g *GitSandbox) Create(ctx context.Context, spec sbx.SandboxSpec) error

Create validates the repository, and in worktree mode allocates the worktree.

The worktree is created detached (`--detach`) so sibling variants cannot contend over a branch. Stale administrative entries from a previous crash are pruned first, which is a no-op when there is nothing to prune.

func (*GitSandbox) Destroy

func (g *GitSandbox) Destroy(ctx context.Context) error

Destroy removes the worktree this sandbox allocated. In-place mode is a no-op so a scope can never delete the user's repository.

The os.RemoveAll fallback is gated on the repository actually registering this path as one of its worktrees — and on that worktree not being the repository's main worktree: the Sandbox contract is that Destroy must never destroy resources the backend did not create (sandbox.Sandbox), so a misconfigured or failed-Create worktree path pointing at a directory someone else owns is never removed. A registry-check failure is reported, not swallowed.

On Windows a leftover handle (editor, shell, indexer, antivirus) can make `git worktree remove` fail; for a path the repository registers as one of its worktrees, the directory is then removed directly and the administrative entry pruned so the worktree does not become unusable.

func (*GitSandbox) Diff

func (g *GitSandbox) Diff(ctx context.Context, ws trace.WorkspaceState, maxLines int) (string, []string, error)

Diff returns the unified diff and changed file paths between ws and the current workspace.

Untracked files are made visible through a scratch index, so Diff is non-mutating: it never touches the caller's index, matching Capture.

func (*GitSandbox) Exec

Exec runs a command in the sandbox when a containment backend was supplied via WithExec; otherwise it returns sbx.ErrUnsupported (the git backend owns git state, not processes).

The request's Confinement (or a Permissive default over the workspace) is resolved against the placement: a confined placement probes the paddock fail-closed and refuses when it cannot be established; an advisory placement runs unconfined. Authority root selection is the caller's job (package confinement); Exec never invents roots.

func (*GitSandbox) ExecGrants

func (g *GitSandbox) ExecGrants(ctx context.Context, req sbx.ExecRequest, grants []confinement.BindingRootGrant, matchDigest string) (sbx.ExecResult, error)

ExecGrants is the per-binding-grant counterpart of ExecMay: it lowers grants to a deny-closed multi-root spec and runs the command through Exec.

func (*GitSandbox) ExecMay

func (g *GitSandbox) ExecMay(ctx context.Context, req sbx.ExecRequest, may, matchDigest string) (sbx.ExecResult, error)

ExecMay is the authority→exec wiring seam: it lowers a may= declaration to a confinement spec (the fail-closed lowerer) and runs the command through Exec. matchDigest is the authority-surface digest recorded in the run's evidence.

func (*GitSandbox) ReadFile

func (g *GitSandbox) ReadFile(_ context.Context, path string) ([]byte, error)

ReadFile reads a file inside the sandbox workspace. The path is resolved against the workspace root (a relative path joins it) and refused if it — or a symlink on the way — escapes the workspace.

func (*GitSandbox) WriteFile

func (g *GitSandbox) WriteFile(_ context.Context, path string, data []byte, perm fs.FileMode) error

WriteFile writes a file inside the sandbox workspace, creating parent directories as needed. Path containment is symlink-safe: a target whose parent (or the target itself) resolves outside the workspace is refused.

type GitSandboxConfig

type GitSandboxConfig struct {
	RepoPath     string
	WorktreePath string
}

GitSandboxConfig is the git backend's OpenSandbox configuration. RepoPath is required; an empty WorktreePath opens the in-place materializer over the repository, a non-empty one opens a detached-worktree sandbox that Create provisions at that path.

type Option

type Option func(*GitSandbox)

Option configures a GitSandbox at construction.

func WithCleanPolicy added in v0.22.0

func WithCleanPolicy(p CleanPolicy) Option

WithCleanPolicy sets the apply-time clean policy. Without this option the sandbox uses CleanAll, so existing hosts are unaffected.

func WithExec

WithExec enables Exec on the sandbox, using b as the containment backend and p as the default placement. Without it, Exec returns sbx.ErrUnsupported and Capabilities reports Exec=false. The caller supplies the backend (e.g. the landlock nested module) so this package depends on the contract, not on any particular isolation technology.

Jump to

Keyboard shortcuts

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