Documentation
¶
Index ¶
- Constants
- func CurrentBranch(ctx context.Context, dir string) (string, error)
- func CurrentCommit(ctx context.Context, dir string) (string, error)
- func CurrentWorktreeName(ctx context.Context, dir string) (string, error)
- func DetectBranch() string
- func DetectWorktreeName() string
- func IsAncestor(ctx context.Context, dir, ancestor, descendant string) (bool, error)
- func IsWorktree() bool
- func MainRepoRoot(dir string) (string, error)
- func Prune(ctx context.Context, repoRoot string, name string) error
- func Remove(ctx context.Context, repoRoot string, name string) error
- func RemoveWithPrefix(ctx context.Context, repoRoot string, name string, branchPrefix string) error
- func RemoveWithPrefixLease(ctx context.Context, repoRoot string, name string, branchPrefix string, ...) error
- func RepoRoot(dir string) (string, error)
- func ResolveCommit(ctx context.Context, dir, ref string) (string, error)
- func ResolveGitDir(path string) string
- func SanitizeName(input string) (string, error)
- type InvalidNameError
- type NotGitRepoError
- type WorktreeExistsError
- type WorktreeInfo
- func Create(ctx context.Context, repoRoot string, name string, baseRef string) (*WorktreeInfo, error)
- func CreateWithPrefix(ctx context.Context, repoRoot string, name string, baseRef string, ...) (*WorktreeInfo, error)
- func CreateWithPrefixLease(ctx context.Context, repoRoot string, name string, baseRef string, ...) (*WorktreeInfo, error)
- func List(ctx context.Context, repoRoot string) ([]WorktreeInfo, error)
- func Resolve(ctx context.Context, repoRoot string, name string) (*WorktreeInfo, error)
- type WorktreeNotFoundError
Constants ¶
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 ¶
CurrentBranch returns the currently checked-out branch name for a worktree.
func CurrentCommit ¶
CurrentCommit returns the commit at HEAD in dir.
func CurrentWorktreeName ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ResolveCommit ¶
ResolveCommit resolves ref to an exact commit.
func ResolveGitDir ¶
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 ¶
Types ¶
type InvalidNameError ¶
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.
type WorktreeNotFoundError ¶
type WorktreeNotFoundError struct {
Name string
}
WorktreeNotFoundError indicates no worktree with the given name exists.
func (WorktreeNotFoundError) Error ¶
func (e WorktreeNotFoundError) Error() string