worktree

package
v1.3.106 Latest Latest
Warning

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

Go to latest
Published: Oct 22, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Overview

Package worktree owns the git worktree mechanics used by the Runtime: name planning, materialization, listing, removal and reset. It never touches SQL, sessions or the Runtime; git is the only content authority it consults.

Index

Constants

View Source
const (
	CodeNotGit               = "not_git"
	CodeNameGenerationFailed = "name_generation_failed"
	CodeCreateFailed         = "create_failed"
	CodePopulateFailed       = "populate_failed"
	CodeListFailed           = "list_failed"
	CodeRemoveFailed         = "remove_failed"
	CodeResetFailed          = "reset_failed"
	CodeDefaultBranchFailed  = "default_branch_failed"
)

Error codes shared with the ACP/CLI projections.

Variables

This section is empty.

Functions

func Slugify

func Slugify(input string) string

Slugify lowercases input and reduces every run of non-alphanumeric runes to a single dash, trimming leading/trailing dashes.

Types

type Error

type Error struct {
	Code    string
	Message string
}

Error is a typed mechanism error carrying a stable machine code so adapters can map it to protocol errors without string matching.

func (*Error) Error

func (e *Error) Error() string

type Info

type Info struct {
	Name      string
	Branch    string
	Directory string
}

Info describes one git worktree derived from a repository. An empty Branch means the worktree is detached.

type Manager

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

Manager owns git worktree mechanics under Root. It is safe for concurrent use as long as callers serialize per repository root (the Runtime does).

func NewManager

func NewManager(root, branchPrefix string) *Manager

NewManager builds a Manager whose worktrees live under root. An empty branchPrefix falls back to "mothx".

func (*Manager) Add

func (m *Manager) Add(ctx context.Context, repoRoot string, info Info) error

Add materializes the worktree directory without checking out files. The directory exists after a successful Add, so it can be authorized before the (slower) Populate step runs.

func (*Manager) DefaultBranch

func (m *Manager) DefaultBranch(ctx context.Context, repoRoot string) (string, error)

DefaultBranch resolves the repository's default branch ref, preferring origin/HEAD and falling back to main/master.

func (*Manager) DirectoryFor

func (m *Manager) DirectoryFor(repoRoot, name string) string

DirectoryFor returns the canonical worktree directory for name under the repository's key directory.

func (*Manager) List

func (m *Manager) List(ctx context.Context, repoRoot string) ([]Info, error)

List returns the repository's worktrees excluding the primary worktree.

func (*Manager) LockDir

func (m *Manager) LockDir(ctx context.Context, directory string) (func(), error)

LockDir acquires a cross-process lock for one worktree directory under the manager root. The returned release function is always non-nil on success.

func (*Manager) Plan

func (m *Manager) Plan(ctx context.Context, repoRoot, requested string, detached bool) (Info, error)

Plan generates a unique name/directory/branch for a new worktree. requested is slugified; when empty, the repository basename is used. Conflicts on the directory or branch are retried with a random suffix.

func (*Manager) Populate

func (m *Manager) Populate(ctx context.Context, info Info) error

Populate checks out the worktree's committed tree.

func (*Manager) Remove

func (m *Manager) Remove(ctx context.Context, repoRoot, directory, branch string) error

Remove deletes a worktree directory and, when non-empty, its branch. It is the caller's responsibility to prove directory belongs to repoRoot's registered worktrees before calling Remove.

func (*Manager) RepoRoot

func (m *Manager) RepoRoot(ctx context.Context, dir string) (string, error)

RepoRoot resolves the primary worktree (repository root) for dir.

func (*Manager) Reset

func (m *Manager) Reset(ctx context.Context, repoRoot, directory string) error

Reset returns a worktree to the repository's default branch, discarding local changes. It fails rather than silently leaving the tree dirty.

func (*Manager) Root

func (m *Manager) Root() string

Root returns the directory all managed worktrees live under.

type Runner

type Runner func(ctx context.Context, cwd string, args ...string) (stdout, stderr string, code int, err error)

Runner executes git in cwd and returns stdout, stderr and the exit code. A non-nil error means the process could not be started at all; a non-zero code with a nil error is a normal git failure.

Jump to

Keyboard shortcuts

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