vcs

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Aug 28, 2026 License: AGPL-3.0 Imports: 11 Imported by: 0

Documentation

Index

Constants

View Source
const MaxWorktreeNameLen = 64

MaxWorktreeNameLen is the maximum length of a sanitised worktree name. Git imposes no hard limit, but filesystems and ergonomics do.

Variables

This section is empty.

Functions

func CurrentBranch

func CurrentBranch(ctx context.Context, dir string) (string, error)

CurrentBranch returns the currently checked-out branch name for a worktree.

func CurrentCommit

func CurrentCommit(ctx context.Context, dir string) (string, error)

CurrentCommit returns the commit at HEAD in dir.

func CurrentWorktreeName

func CurrentWorktreeName(ctx context.Context, dir string) (string, error)

CurrentWorktreeName returns the mivia worktree name if dir is inside a mivia-managed worktree under workspace.WorktreesDir(main root). Returns empty string if dir is the main tree or not inside any worktree. The worktree root is the main repo root: RepoRoot alone returns a linked worktree's own toplevel, which has no .mivia/worktrees directory of its own. A subdirectory of a worktree still belongs to that worktree, so the search ascends until it reaches the directory directly under worktrees/.

func DetectBranch

func DetectBranch() string

DetectBranch returns the current HEAD branch name, or empty if not a repo.

func DetectWorktreeName

func DetectWorktreeName() string

DetectWorktreeName returns the worktree name if the cwd is inside a git worktree. For mivia-managed worktrees it returns the directory name under .mivia/worktrees/ (resolving subdirectories to the worktree root). For other linked worktrees it returns the base directory name. Returns empty for the main working tree.

func IsAncestor

func IsAncestor(ctx context.Context, dir, ancestor, descendant string) (bool, error)

IsAncestor reports whether ancestor is an ancestor of descendant.

func IsWorktree

func IsWorktree() bool

IsWorktree returns true if the current directory is inside a git worktree (as opposed to the main working tree).

func MainRepoRoot

func MainRepoRoot(dir string) (string, error)

MainRepoRoot finds the main repository root (the one with .git/ as a real directory) from any directory inside the repo, including linked worktrees. Unlike RepoRoot (which returns the worktree's own toplevel), this always returns the primary working tree path.

func Prune

func Prune(ctx context.Context, repoRoot string, name string) error

Prune drops the Git worktree registration for name, but only when the registered working-tree directory no longer exists. An intact working tree is never dropped, even when its on-disk .git gitfile is broken or missing: discoverability of a live worktree outranks clearing a stale entry. The orphan removal path calls it after RemoveWithPrefixLease reports a missing directory, so the stale entry disappears from the worktree list.

func Remove

func Remove(ctx context.Context, repoRoot string, name string) error

Remove deletes a worktree by name and clears a stale registration for the same name when its recorded working-tree directory is gone. It never drops a registration whose working tree is intact. It preserves all branches.

func RemoveWithPrefix

func RemoveWithPrefix(ctx context.Context, repoRoot string, name string, branchPrefix string) error

RemoveWithPrefix deletes a worktree by name and clears a stale registration for the same name when its recorded working-tree directory is gone; a registration whose working tree is intact is never dropped. It preserves all branches. This avoids a race when another process changes the worktree branch during removal.

func RemoveWithPrefixLease

func RemoveWithPrefixLease(ctx context.Context, repoRoot string, name string, branchPrefix string, lease *os.File) error

RemoveWithPrefixLease keeps lease open in the Git mutation process.

func RepoRoot

func RepoRoot(dir string) (string, error)

RepoRoot finds the git repository root from any directory inside it.

func ResolveCommit

func ResolveCommit(ctx context.Context, dir, ref string) (string, error)

ResolveCommit resolves ref to an exact commit.

func ResolveGitDir

func ResolveGitDir(path string) string

ResolveGitDir follows a .git file's gitdir: pointer to the actual git directory. Returns the path unchanged if it is a directory, a plain file that is not a valid gitdir pointer, or cannot be read. It performs no process execution, so callers outside this package (for example a no-exec workspace/branch detector) can reuse the same resolution logic this package already uses for worktree detection instead of reimplementing gitdir-pointer parsing.

func SanitizeName

func SanitizeName(input string) (string, error)

Types

type InvalidNameError

type InvalidNameError struct {
	Input  string
	Reason string
}

SanitizeName converts a user-provided worktree name to a safe directory name. Rules:

  • Trim whitespace
  • Lowercase
  • Replace runs of non-alphanumeric characters with a single hyphen
  • Strip leading/trailing hyphens
  • Reject names that require truncation
  • Reject if empty or reserved after sanitisation

func (InvalidNameError) Error

func (e InvalidNameError) Error() string

type NotGitRepoError

type NotGitRepoError struct {
	Dir string
}

NotGitRepoError indicates the directory is not inside a git repository or git is not available.

func (NotGitRepoError) Error

func (e NotGitRepoError) Error() string

type WorktreeExistsError

type WorktreeExistsError struct {
	Name string
}

WorktreeExistsError indicates a worktree with the given name already exists.

func (WorktreeExistsError) Error

func (e WorktreeExistsError) Error() string

type WorktreeInfo

type WorktreeInfo struct {
	Name   string // human name (sanitised)
	Path   string // absolute path on disk (under workspace.WorktreesDir())
	Branch string // checked-out branch/commit
}

WorktreeInfo describes a single mivia-managed worktree.

func Create

func Create(ctx context.Context, repoRoot string, name string, baseRef string) (*WorktreeInfo, error)

Create adds a new worktree under workspace.WorktreesDir(repoRoot). baseRef is the branch, tag, or SHA to check out. Returns the WorktreeInfo for the new worktree.

func CreateWithPrefix

func CreateWithPrefix(ctx context.Context, repoRoot string, name string, baseRef string, branchPrefix string) (*WorktreeInfo, error)

CreateWithPrefix adds a new worktree under workspace.WorktreesDir(repoRoot). It creates the branch branchPrefix plus the sanitised worktree name. baseRef is the branch, tag, or SHA to check out.

func CreateWithPrefixLease

func CreateWithPrefixLease(ctx context.Context, repoRoot string, name string, baseRef string, branchPrefix string, lease *os.File) (*WorktreeInfo, error)

CreateWithPrefixLease keeps lease open in the Git mutation process.

func List

func List(ctx context.Context, repoRoot string) ([]WorktreeInfo, error)

List returns all mivia-managed worktrees for the repo at repoRoot. The main worktree is filtered out.

func Resolve

func Resolve(ctx context.Context, repoRoot string, name string) (*WorktreeInfo, error)

Resolve finds a worktree by name. Returns nil, nil if not found.

type WorktreeNotFoundError

type WorktreeNotFoundError struct {
	Name string
}

WorktreeNotFoundError indicates no worktree with the given name exists.

func (WorktreeNotFoundError) Error

func (e WorktreeNotFoundError) Error() string

Jump to

Keyboard shortcuts

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