Documentation
¶
Index ¶
- Constants
- func AddWorktree(ctx context.Context, path, branch string, copyOpts CopyOptions) error
- func AddWorktreeWithNewBranch(ctx context.Context, path, branch, startPoint string, copyOpts CopyOptions) error
- func BranchCommitMessages(ctx context.Context, patterns ...string) (map[string]string, error)
- func BranchExists(ctx context.Context, name string) (bool, error)
- func CopyFilesToWorktree(ctx context.Context, srcRoot, dstRoot string, opts CopyOptions, warn io.Writer) error
- func CreateBranch(ctx context.Context, name string) error
- func CurrentLocation(ctx context.Context) (string, error)
- func CurrentWorktree(ctx context.Context) (string, error)
- func DefaultBranch(ctx context.Context) (string, error)
- func DeleteBranch(ctx context.Context, name string, force bool) error
- func DeleteBranchInDir(ctx context.Context, name string, force bool, dir string) error
- func ExpandBaseDir(ctx context.Context, baseDir string) (string, error)
- func ExpandPath(ctx context.Context, path string) (string, error)
- func GitConfig(ctx context.Context, key string) ([]string, error)
- func HeadBranch(ctx context.Context) (string, error)
- func IsBareEntry(ctx context.Context, query string) (bool, error)
- func IsBareRepository(ctx context.Context) (bool, error)
- func IsBareRoot(ctx context.Context) (bool, error)
- func IsBaseDirConfigured(ctx context.Context) (bool, error)
- func IsBranchMerged(ctx context.Context, name string) (bool, error)
- func IsDefaultBranch(ctx context.Context, branch string) (bool, error)
- func ListBranches(ctx context.Context) ([]string, error)
- func ListModifiedFiles(ctx context.Context, root string) ([]string, error)
- func ListRemoteBranches(ctx context.Context) ([]string, error)
- func ListUntrackedFiles(ctx context.Context, root string) ([]string, error)
- func LocalBranchExists(ctx context.Context, name string) (bool, error)
- func MainRepoRoot(ctx context.Context) (string, error)
- func PruneWorktrees(ctx context.Context) error
- func RemoveWorktree(ctx context.Context, path string, force bool) error
- func RepoName(ctx context.Context) (string, error)
- func RunHooks(ctx context.Context, hooks []string, dir string, w io.Writer) error
- func RunRemover(ctx context.Context, remover string, wtPath string, dir string, w io.Writer) error
- func SetConfig(ctx context.Context, key, value string) error
- func ShowPrefix(ctx context.Context) (string, error)
- func WithRepoContext(ctx context.Context, rc RepoContext) context.Context
- func WorktreeDirName(ctx context.Context, wt *Worktree) (string, error)
- func WorktreePathFor(ctx context.Context, baseDir, branch string) (string, error)
- type Config
- type CopyOptions
- type RepoContext
- type Worktree
Constants ¶
const DetachedMarker = "[detached]"
DetachedMarker is used to indicate a detached HEAD state. This is an invalid branch name to avoid confusion with actual branch names.
Variables ¶
This section is empty.
Functions ¶
func AddWorktree ¶
func AddWorktree(ctx context.Context, path, branch string, copyOpts CopyOptions) error
AddWorktree creates a new worktree for the given branch.
func AddWorktreeWithNewBranch ¶
func AddWorktreeWithNewBranch(ctx context.Context, path, branch, startPoint string, copyOpts CopyOptions) error
AddWorktreeWithNewBranch creates a new worktree with a new branch. If startPoint is specified, the new branch will be created from that commit/branch.
func BranchCommitMessages ¶ added in v0.28.0
BranchCommitMessages returns the first line of the latest commit message for each ref matching the given for-each-ref patterns, keyed by short ref name (e.g. "main", "origin/main").
func BranchExists ¶
BranchExists checks if a branch exists (local or remote).
func CopyFilesToWorktree ¶ added in v0.2.0
func CopyFilesToWorktree(ctx context.Context, srcRoot, dstRoot string, opts CopyOptions, warn io.Writer) error
CopyFilesToWorktree copies files to the new worktree based on options. If w is non-nil, warnings about files that fail to copy are written to it.
func CreateBranch ¶
CreateBranch creates a new branch at the current HEAD.
func CurrentLocation ¶ added in v0.22.0
CurrentLocation returns the path that identifies the current position in the worktree list.
- bare root: returns MainRepoRoot() (bare repo directory path)
- worktree or normal repo: returns CurrentWorktree() (--show-toplevel)
func CurrentWorktree ¶ added in v0.3.0
CurrentWorktree returns the path of the current worktree.
func DefaultBranch ¶ added in v0.3.0
DefaultBranch returns the default branch name (e.g., main, master).
func DeleteBranch ¶
DeleteBranch deletes a branch. If force is true, it uses -D (force delete), otherwise -d (safe delete).
func DeleteBranchInDir ¶ added in v0.11.0
DeleteBranchInDir deletes a branch from a specific directory. If dir is empty, uses current directory.
func ExpandBaseDir ¶ added in v0.4.0
ExpandBaseDir expands template variables and path for the given base directory pattern.
func ExpandPath ¶
ExpandPath expands ~ to home directory and resolves relative paths. Relative paths are resolved from the main repository root, not the current worktree.
func HeadBranch ¶ added in v0.25.0
HeadBranch returns the branch name that HEAD points to. Returns an error if HEAD is detached or not a symbolic ref.
func IsBareEntry ¶ added in v0.25.0
IsBareEntry checks if the given query (branch name or path) corresponds to the bare repository entry. In bare repos the worktree-list "bare" entry carries no branch field, so we compare against HEAD's symbolic-ref for branch matching, and against the bare entry's path for path matching. Returns false (not an error) for non-bare repositories.
func IsBareRepository ¶ added in v0.21.0
IsBareRepository reports whether the main repository is bare. It is a convenience wrapper around DetectRepoContext.
func IsBareRoot ¶ added in v0.22.0
IsBareRoot reports whether the current directory is a bare repository root (no working tree).
func IsBaseDirConfigured ¶ added in v0.17.0
IsBaseDirConfigured checks if wt.basedir is explicitly configured in git config.
func IsBranchMerged ¶
IsBranchMerged checks if a branch is merged into the current branch.
func IsDefaultBranch ¶ added in v0.14.0
IsDefaultBranch checks if the given branch name is the default branch.
func ListBranches ¶
ListBranches returns a list of all local branch names.
func ListModifiedFiles ¶ added in v0.18.0
ListModifiedFiles returns tracked files with modifications.
func ListRemoteBranches ¶ added in v0.10.0
ListRemoteBranches returns a list of all remote branch names (e.g., origin/main).
func ListUntrackedFiles ¶ added in v0.18.0
ListUntrackedFiles returns untracked files (not ignored).
func LocalBranchExists ¶
LocalBranchExists checks if a local branch exists.
func MainRepoRoot ¶ added in v0.3.0
MainRepoRoot returns the root directory of the main git repository. Unlike RepoRoot, this returns the main repository root even when called from a worktree.
For normal repositories, git-common-dir is ".git" inside the repo root, so the parent directory is returned. For bare repositories, git-common-dir IS the repository directory itself, so it is returned directly.
func PruneWorktrees ¶ added in v0.23.0
PruneWorktrees runs 'git worktree prune' to clean up stale worktree entries.
func RemoveWorktree ¶
RemoveWorktree removes a worktree.
func RepoName ¶ added in v0.3.0
RepoName returns the name of the current git repository (directory name).
func RunHooks ¶ added in v0.6.0
RunHooks executes the configured hooks in the given directory. Hook stdout/stderr are written to the provided writer. If a hook fails, it stops immediately and returns the error.
func RunRemover ¶ added in v0.23.0
RunRemover executes a custom remover command to remove a worktree directory. The worktree path is passed safely as a positional argument via sh -c.
func ShowPrefix ¶ added in v0.19.0
ShowPrefix returns the path prefix of the current directory relative to the repository root. It runs "git rev-parse --show-prefix" and strips the trailing slash. Returns an empty string when at the repository root.
func WithRepoContext ¶ added in v0.21.0
func WithRepoContext(ctx context.Context, rc RepoContext) context.Context
WithRepoContext stores a RepoContext in the given context for later retrieval.
func WorktreeDirName ¶ added in v0.8.0
WorktreeDirName returns the directory name of a worktree (relative path from base dir).
Types ¶
type Config ¶ added in v0.3.0
type Config struct {
BaseDir string
CopyIgnored bool
CopyUntracked bool
CopyModified bool
NoCopy []string
Copy []string
Symlink []string
Hooks []string
DeleteHooks []string
Remover string
NoCd bool
Relative bool
}
Config holds all wt configuration values.
type CopyOptions ¶ added in v0.2.0
type CopyOptions struct {
CopyIgnored bool
CopyUntracked bool
CopyModified bool
NoCopy []string
Copy []string
Symlink []string // Patterns for directories to symlink instead of copy (gitignore syntax)
ExcludeDirs []string // Directories to exclude from copying (absolute paths)
}
CopyOptions holds the copy configuration.
type RepoContext ¶ added in v0.21.0
type RepoContext struct {
// contains filtered or unexported fields
}
RepoContext describes the type and location within a git repository.
The four possible states are:
{Bare: false, Worktree: false} — main working tree of a normal repository
{Bare: false, Worktree: true} — linked worktree of a normal repository
{Bare: true, Worktree: false} — bare repository root (no working tree)
{Bare: true, Worktree: true} — linked worktree created from a bare repository
func DetectRepoContext ¶ added in v0.21.0
func DetectRepoContext(ctx context.Context) (RepoContext, error)
DetectRepoContext detects whether the current repository is bare and whether the current working directory is inside a linked worktree.
Detection uses `git rev-parse --is-bare-repository --git-dir --git-common-dir` in a single process invocation:
- Bare: detected by combining two signals with OR: 1. --is-bare-repository flag (reliable at bare root, but returns false in bare-derived worktrees) 2. filepath.Base(gitCommonDir) != ".git" (catches bare repos named without .git suffix, but misses bare repos where git-common-dir is a .git subdirectory)
- Worktree: gitDir != gitCommonDir In the main working tree (or bare root), both are equal. In a linked worktree, gitDir points to a worktrees/X subdirectory.
func RepoContextFrom ¶ added in v0.21.0
func RepoContextFrom(ctx context.Context) *RepoContext
RepoContextFrom retrieves a cached RepoContext from ctx. It returns nil if no value is stored or if the current working directory differs from the directory recorded at detection time.
type Worktree ¶
Worktree represents a git worktree.
func FindWorktreeByBranch ¶
FindWorktreeByBranch finds a worktree by branch name.
func FindWorktreeByBranchOrDir ¶ added in v0.2.3
FindWorktreeByBranchOrDir finds a worktree by branch name or directory name. It first tries to match by branch name, then by directory name (relative path from base dir), and finally by filesystem path (relative or absolute).