worktree

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Feb 11, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package worktree provides git worktree management for moat runs.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BasePath

func BasePath() string

BasePath returns the root directory for worktrees. Checks MOAT_WORKTREE_BASE env var, defaults to ~/.moat/worktrees.

func Clean

func Clean(repoRoot, wtPath string) error

Clean removes a worktree directory and runs git worktree prune. The wtPath must be under BasePath() to prevent path traversal attacks.

func FindRepoRoot

func FindRepoRoot(dir string) (string, error)

FindRepoRoot returns the root of the git repository containing dir.

func ParseRemoteURL

func ParseRemoteURL(rawURL string) (string, error)

ParseRemoteURL normalizes a git remote URL to host/owner/repo format. Handles both HTTPS and SSH URLs, strips .git suffix.

func Path

func Path(repoID, branch string) string

Path returns the expected path for a worktree given a repo ID and branch.

func ResolveRepoID

func ResolveRepoID(repoRoot string) (string, error)

ResolveRepoID returns a normalized repository identifier. Uses the origin remote URL if available, otherwise falls back to _local/<dirname>.

func ValidateBranch

func ValidateBranch(branch string) error

ValidateBranch checks that a branch name is safe to use in filesystem paths.

Types

type Entry

type Entry struct {
	Branch string // branch name (directory name)
	Path   string // absolute path to worktree
}

Entry represents a managed worktree on disk.

func ListWorktrees

func ListWorktrees(repoID string) ([]Entry, error)

ListWorktrees returns all managed worktree entries for a given repo ID. It walks the directory tree to find worktrees, supporting branch names with slashes (e.g., feature/dark-mode) which create nested directories. A worktree is identified by the presence of a .git file (not directory) in its root, which git creates for worktrees.

type Result

type Result struct {
	WorkspacePath string // absolute path to worktree directory
	Branch        string // git branch name
	RunName       string // auto-generated run name ({agent}-{branch} or {branch})
	Reused        bool   // true if worktree already existed
	RepoID        string // normalized repo identifier
}

Result holds the outcome of a worktree resolution.

func Resolve

func Resolve(repoRoot, repoID, branch, agentName string) (*Result, error)

Resolve ensures a branch and worktree exist for the given branch name. It creates them if necessary, reuses them if they already exist.

Jump to

Keyboard shortcuts

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