git

package
v0.28.0 Latest Latest
Warning

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

Go to latest
Published: May 14, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

View Source
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

func BranchCommitMessages(ctx context.Context, patterns ...string) (map[string]string, error)

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

func BranchExists(ctx context.Context, name string) (bool, error)

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

func CreateBranch(ctx context.Context, name string) error

CreateBranch creates a new branch at the current HEAD.

func CurrentLocation added in v0.22.0

func CurrentLocation(ctx context.Context) (string, error)

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

func CurrentWorktree(ctx context.Context) (string, error)

CurrentWorktree returns the path of the current worktree.

func DefaultBranch added in v0.3.0

func DefaultBranch(ctx context.Context) (string, error)

DefaultBranch returns the default branch name (e.g., main, master).

func DeleteBranch

func DeleteBranch(ctx context.Context, name string, force bool) error

DeleteBranch deletes a branch. If force is true, it uses -D (force delete), otherwise -d (safe delete).

func DeleteBranchInDir added in v0.11.0

func DeleteBranchInDir(ctx context.Context, name string, force bool, dir string) error

DeleteBranchInDir deletes a branch from a specific directory. If dir is empty, uses current directory.

func ExpandBaseDir added in v0.4.0

func ExpandBaseDir(ctx context.Context, baseDir string) (string, error)

ExpandBaseDir expands template variables and path for the given base directory pattern.

func ExpandPath

func ExpandPath(ctx context.Context, path string) (string, error)

ExpandPath expands ~ to home directory and resolves relative paths. Relative paths are resolved from the main repository root, not the current worktree.

func GitConfig added in v0.4.0

func GitConfig(ctx context.Context, key string) ([]string, error)

GitConfig retrieves all git config values for a key.

func HeadBranch added in v0.25.0

func HeadBranch(ctx context.Context) (string, error)

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

func IsBareEntry(ctx context.Context, query string) (bool, error)

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

func IsBareRepository(ctx context.Context) (bool, error)

IsBareRepository reports whether the main repository is bare. It is a convenience wrapper around DetectRepoContext.

func IsBareRoot added in v0.22.0

func IsBareRoot(ctx context.Context) (bool, error)

IsBareRoot reports whether the current directory is a bare repository root (no working tree).

func IsBaseDirConfigured added in v0.17.0

func IsBaseDirConfigured(ctx context.Context) (bool, error)

IsBaseDirConfigured checks if wt.basedir is explicitly configured in git config.

func IsBranchMerged

func IsBranchMerged(ctx context.Context, name string) (bool, error)

IsBranchMerged checks if a branch is merged into the current branch.

func IsDefaultBranch added in v0.14.0

func IsDefaultBranch(ctx context.Context, branch string) (bool, error)

IsDefaultBranch checks if the given branch name is the default branch.

func ListBranches

func ListBranches(ctx context.Context) ([]string, error)

ListBranches returns a list of all local branch names.

func ListModifiedFiles added in v0.18.0

func ListModifiedFiles(ctx context.Context, root string) ([]string, error)

ListModifiedFiles returns tracked files with modifications.

func ListRemoteBranches added in v0.10.0

func ListRemoteBranches(ctx context.Context) ([]string, error)

ListRemoteBranches returns a list of all remote branch names (e.g., origin/main).

func ListUntrackedFiles added in v0.18.0

func ListUntrackedFiles(ctx context.Context, root string) ([]string, error)

ListUntrackedFiles returns untracked files (not ignored).

func LocalBranchExists

func LocalBranchExists(ctx context.Context, name string) (bool, error)

LocalBranchExists checks if a local branch exists.

func MainRepoRoot added in v0.3.0

func MainRepoRoot(ctx context.Context) (string, error)

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

func PruneWorktrees(ctx context.Context) error

PruneWorktrees runs 'git worktree prune' to clean up stale worktree entries.

func RemoveWorktree

func RemoveWorktree(ctx context.Context, path string, force bool) error

RemoveWorktree removes a worktree.

func RepoName added in v0.3.0

func RepoName(ctx context.Context) (string, error)

RepoName returns the name of the current git repository (directory name).

func RunHooks added in v0.6.0

func RunHooks(ctx context.Context, hooks []string, dir string, w io.Writer) error

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

func RunRemover(ctx context.Context, remover string, wtPath string, dir string, w io.Writer) error

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 SetConfig added in v0.17.0

func SetConfig(ctx context.Context, key, value string) error

SetConfig sets a git config value.

func ShowPrefix added in v0.19.0

func ShowPrefix(ctx context.Context) (string, error)

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

func WorktreeDirName(ctx context.Context, wt *Worktree) (string, error)

WorktreeDirName returns the directory name of a worktree (relative path from base dir).

func WorktreePathFor added in v0.4.0

func WorktreePathFor(ctx context.Context, baseDir, branch string) (string, error)

WorktreePathFor returns the full path for a worktree given a base directory pattern and branch name.

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.

func LoadConfig added in v0.4.0

func LoadConfig(ctx context.Context) (Config, error)

LoadConfig loads configuration from git config with default 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

type Worktree struct {
	Path   string
	Branch string
	Head   string
	Bare   bool
}

Worktree represents a git worktree.

func FindWorktreeByBranch

func FindWorktreeByBranch(ctx context.Context, branch string) (*Worktree, error)

FindWorktreeByBranch finds a worktree by branch name.

func FindWorktreeByBranchOrDir added in v0.2.3

func FindWorktreeByBranchOrDir(ctx context.Context, query string) (*Worktree, error)

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).

func ListWorktrees

func ListWorktrees(ctx context.Context) ([]Worktree, error)

ListWorktrees returns a list of all worktrees.

Jump to

Keyboard shortcuts

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