Documentation
¶
Overview ¶
Package git provides git operations via shell commands.
All operations use os/exec.Command to call the git CLI directly rather than using Go git libraries. This approach is simpler, more reliable, and ensures compatibility with user configurations (SSH keys, credential helpers, aliases).
Worktree Operations ¶
Core worktree management:
- AddWorktree: Create worktrees for new or existing branches
- RemoveWorktree: Remove worktrees with optional force flag
- MoveWorktree: Relocate worktrees to new paths
- ListWorktrees: Scan directory for worktrees with batched git calls
- GetWorktreeInfo: Get detailed info for a single worktree
Repository Operations ¶
Repository and branch queries:
- GetOriginURL, GetRepoName: Extract repository information
- GetCurrentBranch, BranchExists: Branch operations
- GetCommitCount: Commits ahead of default branch
- GetDefaultBranch: Detect main/master branch
Repository Discovery ¶
Finding repositories by name or label:
- FindRepoByName: Search for repo by name in a directory
- FindAllRepos: List all git repos in a directory
- FindSimilarRepos: Suggest similar names when repo not found
Link Validation and Repair ¶
Git worktrees have bidirectional links between the worktree's .git file and the main repo's .git/worktrees/ directory. These can break when repos are moved. Functions for diagnosis and repair:
- IsWorktreeLinkValid: Check if links are intact
- CanRepairWorktree: Check if repair is possible
- RepairWorktree: Fix broken links via "git worktree repair"
- ListPrunableWorktrees: Find stale git references
Batching for Performance ¶
ListWorktrees uses batched git calls to minimize subprocess overhead: one "git worktree list --porcelain" per repo instead of individual calls per worktree. For 10 worktrees across 2 repos, this reduces calls from ~40 to ~8 (or ~18 with dirty checks enabled).
Index ¶
- Variables
- func AddLabel(ctx context.Context, repoPath, label string) error
- func BranchExists(ctx context.Context, branch string) (bool, error)
- func CanRepairWorktree(worktreePath string) bool
- func CheckGit() error
- func ClearBranchNote(ctx context.Context, repoPath, branch string) error
- func ClearLabels(ctx context.Context, repoPath string) error
- func CloneBareWithWorktreeSupport(ctx context.Context, url, destPath string) error
- func CreateWorktree(ctx context.Context, gitDir, wtPath, branch string) error
- func CreateWorktreeNewBranch(ctx context.Context, gitDir, wtPath, branch, baseRef string) error
- func DeleteLocalBranch(ctx context.Context, repoPath, branch string, force bool) error
- func ExtractRepoNameFromURL(url string) string
- func FetchBranch(ctx context.Context, repoPath, branch string) error
- func FetchDefaultBranch(ctx context.Context, repoPath string) error
- func FindAllRepos(basePath string) ([]string, error)
- func FindRepoByName(basePath, name string) (string, error)
- func FindRepoInDirs(repoName string, searchDirs ...string) string
- func FindReposByLabel(ctx context.Context, repoDir, label string) ([]string, error)
- func FindSimilarRepos(basePath, search string) []string
- func GetAllBranchConfig(ctx context.Context, repoPath string) (notes map[string]string, upstreams map[string]bool)
- func GetBranchCreatedTime(ctx context.Context, repoPath, branch string) (time.Time, error)
- func GetBranchNote(ctx context.Context, repoPath, branch string) (string, error)
- func GetBranchWorktree(ctx context.Context, branch string) (string, error)
- func GetCommitCount(ctx context.Context, repoPath, branch string) (int, error)
- func GetCommitCountWithBase(ctx context.Context, repoPath, branch, baseBranch string) (int, error)
- func GetCommitsBehind(ctx context.Context, repoPath, branch string) (int, error)
- func GetCommitsBehindWithBase(ctx context.Context, repoPath, branch, baseBranch string) (int, error)
- func GetCurrentBranch(ctx context.Context, path string) (string, error)
- func GetCurrentRepoMainPath(ctx context.Context) string
- func GetCurrentRepoMainPathFrom(ctx context.Context, path string) string
- func GetDefaultBranch(ctx context.Context, repoPath string) string
- func GetGitDir(repoPath string, repoType RepoType) string
- func GetGitDirForWorktree(worktreePath string) (string, error)
- func GetGitDirForWorktreeWithContext(ctx context.Context, worktreePath string) (string, error)
- func GetLabels(ctx context.Context, repoPath string) ([]string, error)
- func GetLastCommitRelative(ctx context.Context, path string) (string, error)
- func GetLastCommitTime(ctx context.Context, path string) (time.Time, error)
- func GetMainRepoPath(worktreePath string) (string, error)
- func GetMainRepoPathWithContext(ctx context.Context, worktreePath string) (string, error)
- func GetOriginURL(ctx context.Context, repoPath string) (string, error)
- func GetRepoDisplayName(repoPath string) string
- func GetRepoFolderName(ctx context.Context) (string, error)
- func GetRepoName(ctx context.Context) (string, error)
- func GetRepoNameFrom(ctx context.Context, repoPath string) (string, error)
- func GetRepoNameFromWorktree(worktreePath string) string
- func GetShortCommitHash(ctx context.Context, path string) (string, error)
- func GetUpstreamBranch(ctx context.Context, repoPath, branch string) string
- func GetWorktreeBranches(ctx context.Context, repoPath string) map[string]bool
- func GroupWorktreesByRepo(worktrees []Worktree) map[string][]Worktree
- func HasLabel(ctx context.Context, repoPath, label string) (bool, error)
- func HasRemote(ctx context.Context, repoPath, remoteName string) bool
- func IsDirty(ctx context.Context, path string) bool
- func IsInsideRepo(ctx context.Context) bool
- func IsInsideRepoPath(ctx context.Context, path string) bool
- func IsMainRepo(path string) bool
- func IsWorktree(path string) bool
- func IsWorktreeLinkValid(worktreePath string) bool
- func ListLocalBranches(ctx context.Context, repoPath string) ([]string, error)
- func ListPrunableWorktrees(ctx context.Context, repoPath string) ([]string, error)
- func ListRemoteBranches(ctx context.Context, repoPath string) ([]string, error)
- func MoveWorktree(ctx context.Context, worktree Worktree, newPath string, force bool) error
- func ParseRepoArg(repo string) (org, name string)
- func PruneWorktrees(ctx context.Context, repoPath string) error
- func PushBranch(ctx context.Context, repoPath, branch string) error
- func RemoteBranchExists(ctx context.Context, repoPath, branch string) bool
- func RemoveLabel(ctx context.Context, repoPath, label string) error
- func RemoveWorktree(ctx context.Context, worktree Worktree, force bool) error
- func RepairWorktree(ctx context.Context, repoPath, worktreePath string) error
- func RepairWorktreesFromRepo(ctx context.Context, repoPath string) error
- func RunGitCommand(ctx context.Context, dir string, args ...string) error
- func SetBranchNote(ctx context.Context, repoPath, branch, note string) error
- func SetLabels(ctx context.Context, repoPath string, labels []string) error
- func SetUpstreamBranch(ctx context.Context, repoPath, localBranch, upstream string) error
- func Stash(ctx context.Context, path string) error
- func StashPop(ctx context.Context, path string) error
- func StripRepoPrefix(repoName, wtName string) string
- type CreateWorktreeResult
- func AddWorktree(ctx context.Context, basePath, branch, worktreeFmt string, createNew bool, ...) (*CreateWorktreeResult, error)
- func CreateWorktreeFrom(ctx context.Context, repoPath, basePath, branch, worktreeFmt, baseRef string) (*CreateWorktreeResult, error)
- func OpenWorktreeFrom(ctx context.Context, absRepoPath, basePath, branch, worktreeFmt string) (*CreateWorktreeResult, error)
- type DiffStats
- type LastCommitInfo
- type MigrateToBareResult
- type MigrationOptions
- type MigrationPlan
- type RepoType
- type Worktree
- type WorktreeInfo
- type WorktreeMigration
Constants ¶
This section is empty.
Variables ¶
var ErrGitNotFound = fmt.Errorf("git not found: please install git (https://git-scm.com)")
ErrGitNotFound indicates git is not installed or not in PATH
Functions ¶
func BranchExists ¶
BranchExists checks if a local branch exists
func CanRepairWorktree ¶ added in v0.4.0
CanRepairWorktree checks if a worktree can potentially be repaired. Returns true if .git file exists but links are broken.
func ClearBranchNote ¶
ClearBranchNote removes the note (description) from a branch
func ClearLabels ¶
ClearLabels removes all labels from a repository
func CloneBareWithWorktreeSupport ¶ added in v0.13.0
CloneBareWithWorktreeSupport clones a repo as a bare repo inside the .git directory. This allows worktrees to be created as siblings while git commands work normally.
The directory structure will be:
destPath/ └── .git/ # bare git repo contents (HEAD, objects/, refs/, etc.)
func CreateWorktree ¶ added in v0.13.0
CreateWorktree creates a worktree for an existing branch. gitDir is the .git directory (for regular repos) or the bare repo path. wtPath is the target worktree path. branch is the existing branch to checkout.
func CreateWorktreeNewBranch ¶ added in v0.13.0
CreateWorktreeNewBranch creates a worktree with a new branch. gitDir is the .git directory (for regular repos) or the bare repo path. wtPath is the target worktree path. branch is the new branch name. baseRef is the starting point (e.g., "origin/main").
func DeleteLocalBranch ¶
DeleteLocalBranch deletes a local branch
func ExtractRepoNameFromURL ¶
ExtractRepoNameFromURL extracts the repository name from a git URL
func FetchBranch ¶
FetchBranch fetches a specific branch from origin
func FetchDefaultBranch ¶
FetchDefaultBranch fetches the default branch (main/master) from origin
func FindAllRepos ¶
FindAllRepos returns paths to all main git repositories in basePath (direct children only). Excludes worktrees (only includes repos where .git is a directory).
func FindRepoByName ¶
FindRepoByName searches direct children of basePath for a git repo with the given folder name. Matches by folder name only (case-insensitive), excludes worktrees. Returns the full path if found, or an error if not found.
func FindRepoInDirs ¶ added in v0.6.0
FindRepoInDirs searches for a repo with the given folder name across multiple directories. Returns the absolute path to the repo if found, empty string otherwise. Similar to FindRepoByName but checks multiple directories (stops at first match).
func FindReposByLabel ¶
FindReposByLabel scans a directory for repos with the given label Returns paths to matching repositories
func FindSimilarRepos ¶
FindSimilarRepos returns repo names in basePath that contain the search string. Useful for providing suggestions when a repo is not found.
func GetAllBranchConfig ¶
func GetAllBranchConfig(ctx context.Context, repoPath string) (notes map[string]string, upstreams map[string]bool)
GetAllBranchConfig returns branch notes and upstreams for a repository in one call. Uses: `git config --get-regexp 'branch\.'` Returns: notes map (branch -> note), upstreams map (branch -> upstream ref)
func GetBranchCreatedTime ¶
GetBranchCreatedTime returns when the branch was created (first commit on branch) Falls back to first commit time if reflog is unavailable
func GetBranchNote ¶
GetBranchNote returns the note (description) for a branch Returns empty string if no note is set
func GetBranchWorktree ¶
GetBranchWorktree returns the worktree path if branch is checked out, empty string if not
func GetCommitCount ¶
GetCommitCount returns number of commits ahead of the default branch
func GetCommitCountWithBase ¶ added in v0.12.0
GetCommitCountWithBase returns number of commits ahead of the given base branch. Use this when you already have the default branch to avoid redundant git calls.
func GetCommitsBehind ¶
GetCommitsBehind returns number of commits behind the default branch
func GetCommitsBehindWithBase ¶ added in v0.12.0
func GetCommitsBehindWithBase(ctx context.Context, repoPath, branch, baseBranch string) (int, error)
GetCommitsBehindWithBase returns number of commits behind the given base branch. Use this when you already have the default branch to avoid redundant git calls.
func GetCurrentBranch ¶
GetCurrentBranch returns the current branch name Returns "(detached)" for detached HEAD state
func GetCurrentRepoMainPath ¶
GetCurrentRepoMainPath returns the main repository path from cwd Works whether you're in the main repo or a worktree Returns empty string if not in a git repo
func GetCurrentRepoMainPathFrom ¶ added in v0.8.0
GetCurrentRepoMainPathFrom returns the main repository path from the given path Works whether you're in the main repo or a worktree Returns empty string if not in a git repo
func GetDefaultBranch ¶
GetDefaultBranch returns the default branch name for the remote (e.g., "main" or "master")
func GetGitDirForWorktree ¶ added in v0.13.0
GetGitDirForWorktree returns the shared git directory for a worktree or repo. Uses git rev-parse --git-common-dir instead of reading .git files directly.
func GetGitDirForWorktreeWithContext ¶ added in v0.13.0
GetGitDirForWorktreeWithContext returns the shared git directory for a worktree or repo.
func GetLabels ¶
GetLabels returns the labels for a repository Returns empty slice if no labels are set
func GetLastCommitRelative ¶
GetLastCommitRelative returns relative time of last commit
func GetLastCommitTime ¶ added in v0.2.0
GetLastCommitTime returns the unix timestamp of the last commit
func GetMainRepoPath ¶
GetMainRepoPath returns the main repository path from a worktree path. Uses git commands rather than reading .git files directly.
func GetMainRepoPathWithContext ¶ added in v0.13.0
GetMainRepoPathWithContext returns the main repository path from a worktree path. Uses git rev-parse --git-common-dir to find the shared git directory.
func GetOriginURL ¶
GetOriginURL gets the origin URL for a repository
func GetRepoDisplayName ¶ added in v0.4.0
GetRepoDisplayName returns the folder name of the repository.
func GetRepoFolderName ¶
GetRepoFolderName returns the actual folder name of the git repo on disk Uses git rev-parse --show-toplevel to get the root directory. If inside a worktree, resolves to the main repo folder name.
func GetRepoName ¶
GetRepoName extracts the repository name from the origin URL
func GetRepoNameFrom ¶
GetRepoNameFrom extracts the repository name from the origin URL of a repo at the given path
func GetRepoNameFromWorktree ¶ added in v0.6.0
GetRepoNameFromWorktree extracts the expected repo name from a worktree. Returns the folder name of the main repository.
func GetShortCommitHash ¶
GetShortCommitHash returns the short (7 char) commit hash for HEAD in a worktree
func GetUpstreamBranch ¶
GetUpstreamBranch returns the remote branch name for a local branch. Returns empty string if no upstream is configured.
func GetWorktreeBranches ¶ added in v0.8.0
GetWorktreeBranches returns a set of branch names that are currently checked out in worktrees. Useful for filtering out branches that can't be checked out again.
func GroupWorktreesByRepo ¶
GroupWorktreesByRepo groups worktrees by their main repository
func IsInsideRepo ¶
IsInsideRepo returns true if the current working directory is inside a git repository
func IsInsideRepoPath ¶ added in v0.8.0
IsInsideRepoPath returns true if the given path is inside a git repository
func IsMainRepo ¶ added in v0.10.0
IsMainRepo checks if path is a main git repository (not a worktree). Main repos have .git as a directory; worktrees have .git as a file.
func IsWorktree ¶
IsWorktree returns true if path is a git worktree (not main repo) Worktrees have .git as a file pointing to the main repo, while main repos have .git as a directory.
func IsWorktreeLinkValid ¶ added in v0.4.0
IsWorktreeLinkValid checks if a worktree's bidirectional link is valid. Returns true if both the .git file in worktree and gitdir in main repo exist and match.
func ListLocalBranches ¶ added in v0.8.0
ListLocalBranches returns all local branch names for a repository.
func ListPrunableWorktrees ¶ added in v0.4.0
ListPrunableWorktrees returns worktree paths that git considers stale. Uses `git worktree prune --dry-run` and parses the output.
func ListRemoteBranches ¶ added in v0.8.0
ListRemoteBranches returns all remote branch names (without origin/ prefix) for a repository.
func MoveWorktree ¶
MoveWorktree moves a git worktree to a new path
func ParseRepoArg ¶
ParseRepoArg splits a repo argument into org and name components. "org/repo" returns ("org", "repo") "repo" returns ("", "repo")
func PruneWorktrees ¶
PruneWorktrees prunes stale worktree references
func PushBranch ¶ added in v0.14.0
PushBranch pushes a branch to origin.
func RemoteBranchExists ¶ added in v0.14.0
RemoteBranchExists checks if a remote tracking branch exists.
func RemoveLabel ¶
RemoveLabel removes a label from a repository
func RemoveWorktree ¶
RemoveWorktree removes a git worktree
func RepairWorktree ¶ added in v0.4.0
RepairWorktree attempts to repair broken links for a single worktree. Uses `git worktree repair <path>` from the main repo.
func RepairWorktreesFromRepo ¶ added in v0.4.0
RepairWorktreesFromRepo repairs all worktrees for a repository. Uses `git worktree repair` without arguments to repair all.
func RunGitCommand ¶ added in v0.13.0
RunGitCommand executes a git command with context support and verbose logging. This is the exported version of runGit for use by commands.
func SetBranchNote ¶
SetBranchNote sets a note (description) on a branch
func SetUpstreamBranch ¶ added in v0.14.0
SetUpstreamBranch sets the upstream tracking branch for a local branch. upstream should be "origin/<branch>" or just "<branch>" (will prepend origin/).
func Stash ¶ added in v0.10.0
Stash creates a stash entry with a specific message. Includes untracked files (-u) to capture all uncommitted changes. Returns nil if successful.
func StashPop ¶ added in v0.10.0
StashPop applies and removes the most recent stash entry. Returns nil if successful.
func StripRepoPrefix ¶ added in v0.13.0
StripRepoPrefix removes the "repo-" prefix from worktree names if present. For example, if repoName is "myapp" and wtName is "myapp-feature", returns "feature".
Types ¶
type CreateWorktreeResult ¶
CreateWorktreeResult contains the result of creating a worktree
func AddWorktree ¶
func AddWorktree(ctx context.Context, basePath, branch, worktreeFmt string, createNew bool, baseRef string) (*CreateWorktreeResult, error)
AddWorktree creates a git worktree at basePath/<formatted-name> If createNew is true, creates a new branch (-b flag); otherwise checks out existing branch baseRef is the starting point for new branches (e.g., "origin/main")
func CreateWorktreeFrom ¶
func CreateWorktreeFrom(ctx context.Context, repoPath, basePath, branch, worktreeFmt, baseRef string) (*CreateWorktreeResult, error)
CreateWorktreeFrom creates a worktree from a specified repository path Used when working with a repo that isn't the current working directory baseRef is the starting point for the new branch (e.g., "origin/main", or empty for HEAD)
func OpenWorktreeFrom ¶
func OpenWorktreeFrom(ctx context.Context, absRepoPath, basePath, branch, worktreeFmt string) (*CreateWorktreeResult, error)
OpenWorktreeFrom creates a worktree for an existing branch in a specified repo
type DiffStats ¶
DiffStats contains diff statistics
func GetDiffStats ¶
GetDiffStats returns additions, deletions, and files changed vs default branch
func GetDiffStatsWithBase ¶ added in v0.12.0
func GetDiffStatsWithBase(ctx context.Context, repoPath, branch, baseBranch string) (DiffStats, error)
GetDiffStatsWithBase returns additions, deletions, and files changed vs the given base branch. Use this when you already have the default branch to avoid redundant git calls.
type LastCommitInfo ¶ added in v0.12.0
type LastCommitInfo struct {
Relative string // Human-readable relative time (e.g., "2 days ago")
Time time.Time // Absolute timestamp
}
LastCommitInfo contains both relative and absolute time of the last commit
func GetLastCommitInfo ¶ added in v0.12.0
func GetLastCommitInfo(ctx context.Context, path string) (LastCommitInfo, error)
GetLastCommitInfo returns both relative time and absolute timestamp in a single git call. Use this instead of calling GetLastCommitRelative and GetLastCommitTime separately.
type MigrateToBareResult ¶ added in v0.13.0
type MigrateToBareResult struct {
MainWorktreePath string // Path to the new main worktree (e.g., repo/main)
GitDir string // Path to the .git directory
}
MigrateToBareResult contains the result of a successful migration
func MigrateToBare ¶ added in v0.13.0
func MigrateToBare(ctx context.Context, plan *MigrationPlan) (*MigrateToBareResult, error)
MigrateToBare converts a regular repo to bare-in-.git format. This preserves all working tree files including uncommitted changes.
type MigrationOptions ¶ added in v0.14.0
type MigrationOptions struct {
WorktreeFormat string // Format string for worktree paths (e.g., "{branch}", "../{repo}-{branch}")
RepoName string // Repository name for path resolution
}
MigrationOptions configures how the migration computes worktree paths
type MigrationPlan ¶ added in v0.13.0
type MigrationPlan struct {
RepoPath string // Original repo path
GitDir string // Current .git directory path
CurrentBranch string // Branch to use for main worktree
MainBranchUpstream string // Upstream for the main branch (e.g., "main" for origin/main)
MainWorktreePath string // Computed path for the main worktree
WorktreesToFix []WorktreeMigration
HasSubmodules bool
}
MigrationPlan describes what will be done during migration
func ValidateMigration ¶ added in v0.13.0
func ValidateMigration(ctx context.Context, repoPath string, opts MigrationOptions) (*MigrationPlan, error)
ValidateMigration checks if a repo can be migrated and returns the migration plan. The opts parameter configures how worktree paths are computed.
type RepoType ¶ added in v0.13.0
type RepoType int
RepoType indicates whether a repo is bare or regular
func DetectRepoType ¶ added in v0.13.0
DetectRepoType determines if a path is a bare or regular git repository
type Worktree ¶
type Worktree struct {
Path string `json:"path"`
Branch string `json:"branch"`
MainRepo string `json:"main_repo"`
RepoName string `json:"repo_name"`
OriginURL string `json:"origin_url"`
IsMerged bool `json:"is_merged"`
CommitCount int `json:"commit_count"`
IsDirty bool `json:"is_dirty"` // only populated when includeDirty=true
HasUpstream bool `json:"has_upstream"`
LastCommit string `json:"last_commit"`
LastCommitTime time.Time `json:"last_commit_time"` // for sorting by commit date
Note string `json:"note,omitempty"`
}
Worktree represents a git worktree with its status
func FilterWorktreesByRepo ¶ added in v0.10.0
FilterWorktreesByRepo returns worktrees that belong to the given main repo path.
func GetWorktreeInfo ¶
GetWorktreeInfo returns info for a single worktree at the given path
func ListWorktrees ¶
ListWorktrees scans a directory for git worktrees with batched git calls per repo. If includeDirty is true, checks each worktree for dirty status (adds subprocess calls). For 10 worktrees across 2 repos: ~8 calls (list) or ~18 calls with dirty checks (prune).
type WorktreeInfo ¶
type WorktreeInfo struct {
Path string
Branch string
CommitHash string // Full hash from git, caller can truncate
}
WorktreeInfo contains basic worktree information from git worktree list.
func ListWorktreesFromRepo ¶
func ListWorktreesFromRepo(ctx context.Context, repoPath string) ([]WorktreeInfo, error)
ListWorktreesFromRepo returns all worktrees for a repository using git worktree list --porcelain -z. Uses NUL-separated output for robust parsing of paths with special characters. Skips bare worktrees (e.g., .bare directory in bare repo layouts).
type WorktreeMigration ¶ added in v0.13.0
type WorktreeMigration struct {
OldPath string // Current worktree path
NewPath string // New path after migration (may be same)
Branch string
Upstream string // Upstream branch (e.g., "feature" for origin/feature)
OldName string // Name in .git/worktrees/
NewName string // Name after migration (may be same)
NeedsMove bool // Whether the worktree folder needs to be moved
IsOutside bool // Whether worktree is outside the repo directory
}
WorktreeMigration describes a worktree that needs to be updated