git

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Jan 22, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package git provides a wrapper for git operations via subprocess.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNotARepo       = errors.New("not a git repository")
	ErrMergeConflict  = errors.New("merge conflict")
	ErrAuthFailure    = errors.New("authentication failed")
	ErrRebaseConflict = errors.New("rebase conflict")
)

Common errors

View Source
var ExcludedContextFiles = []string{
	".cursor",
	".mcp.json",
}

ExcludedContextFiles lists all context files that should be excluded by sparse checkout.

Functions

func CheckExcludedFilesExist

func CheckExcludedFilesExist(repoPath string) []string

CheckExcludedFilesExist checks if any context files still exist in the repo after sparse checkout was configured. These files should have been removed by git read-tree, but may remain if they were untracked or modified. Returns a list of files that still exist and should be manually removed.

func ConfigureSparseCheckout

func ConfigureSparseCheckout(repoPath string) error

ConfigureSparseCheckout sets up sparse checkout for a clone or worktree to exclude .cursor/. This ensures source repo settings don't override Gas Town agent settings. Exported for use by doctor checks.

func IsSparseCheckoutConfigured

func IsSparseCheckoutConfigured(repoPath string) bool

IsSparseCheckoutConfigured checks if sparse checkout is enabled and configured to exclude Cursor context files for a given repo/worktree. Returns true only if both core.sparseCheckout is true AND the sparse-checkout file contains all required exclusion patterns.

Types

type Git

type Git struct {
	// contains filtered or unexported fields
}

Git wraps git operations for a working directory.

func NewGit

func NewGit(workDir string) *Git

NewGit creates a new Git wrapper for the given directory.

func NewGitWithDir

func NewGitWithDir(gitDir, workDir string) *Git

NewGitWithDir creates a Git wrapper with an explicit git directory. This is used for bare repos where gitDir points to the .git directory and workDir may be empty or point to a worktree.

func (*Git) AbortMerge

func (g *Git) AbortMerge() error

AbortMerge aborts a merge in progress.

func (*Git) AbortRebase

func (g *Git) AbortRebase() error

AbortRebase aborts a rebase in progress.

func (*Git) Add

func (g *Git) Add(paths ...string) error

Add stages files for commit.

func (*Git) BranchCreatedDate

func (g *Git) BranchCreatedDate(branch string) (string, error)

BranchCreatedDate returns the date when a branch was created. This uses the committer date of the first commit on the branch. Returns date in YYYY-MM-DD format.

func (*Git) BranchExists

func (g *Git) BranchExists(name string) (bool, error)

BranchExists checks if a branch exists locally.

func (*Git) BranchPushedToRemote

func (g *Git) BranchPushedToRemote(localBranch, remote string) (bool, int, error)

BranchPushedToRemote checks if a branch has been pushed to the remote. Returns (pushed bool, unpushedCount int, err). This handles polecat branches that don't have upstream tracking configured.

func (*Git) CheckConflicts

func (g *Git) CheckConflicts(source, target string) ([]string, error)

CheckConflicts performs a test merge to check if source can be merged into target without conflicts. Returns a list of conflicting files, or empty slice if clean. The merge is always aborted after checking - no actual changes are made.

The caller must ensure the working directory is clean before calling this. After return, the working directory is restored to the target branch.

func (*Git) CheckUncommittedWork

func (g *Git) CheckUncommittedWork() (*UncommittedWorkStatus, error)

CheckUncommittedWork performs a comprehensive check for uncommitted work.

func (*Git) Checkout

func (g *Git) Checkout(ref string) error

Checkout checks out the given ref.

func (*Git) Clone

func (g *Git) Clone(url, dest string) error

Clone clones a repository to the destination.

func (*Git) CloneBare

func (g *Git) CloneBare(url, dest string) error

CloneBare clones a repository as a bare repo (no working directory). This is used for the shared repo architecture where all worktrees share a single git database.

func (*Git) CloneBareWithReference

func (g *Git) CloneBareWithReference(url, dest, reference string) error

CloneBareWithReference clones a bare repository using a local repo as an object reference.

func (*Git) CloneWithReference

func (g *Git) CloneWithReference(url, dest, reference string) error

CloneWithReference clones a repository using a local repo as an object reference. This saves disk by sharing objects without changing remotes.

func (*Git) Commit

func (g *Git) Commit(message string) error

Commit creates a commit with the given message.

func (*Git) CommitAll

func (g *Git) CommitAll(message string) error

CommitAll stages all changes and commits.

func (*Git) CommitsAhead

func (g *Git) CommitsAhead(base, branch string) (int, error)

CommitsAhead returns the number of commits that branch has ahead of base. For example, CommitsAhead("main", "feature") returns how many commits are on feature that are not on main.

func (*Git) CountCommitsBehind

func (g *Git) CountCommitsBehind(ref string) (int, error)

CountCommitsBehind returns the number of commits that HEAD is behind the given ref. For example, CountCommitsBehind("origin/main") returns how many commits are on origin/main that are not on the current HEAD.

func (*Git) CreateBranch

func (g *Git) CreateBranch(name string) error

CreateBranch creates a new branch.

func (*Git) CreateBranchFrom

func (g *Git) CreateBranchFrom(name, ref string) error

CreateBranchFrom creates a new branch from a specific ref.

func (*Git) CurrentBranch

func (g *Git) CurrentBranch() (string, error)

CurrentBranch returns the current branch name.

func (*Git) DefaultBranch

func (g *Git) DefaultBranch() string

DefaultBranch returns the default branch name (what HEAD points to). This works for both regular and bare repositories. Returns "main" as fallback if detection fails.

func (*Git) DeleteBranch

func (g *Git) DeleteBranch(name string, force bool) error

DeleteBranch deletes a local branch.

func (*Git) DeleteRemoteBranch

func (g *Git) DeleteRemoteBranch(remote, branch string) error

DeleteRemoteBranch deletes a branch on the remote.

func (*Git) Fetch

func (g *Git) Fetch(remote string) error

Fetch fetches from the remote.

func (*Git) FetchBranch

func (g *Git) FetchBranch(remote, branch string) error

FetchBranch fetches a specific branch from the remote.

func (*Git) HasUncommittedChanges

func (g *Git) HasUncommittedChanges() (bool, error)

HasUncommittedChanges returns true if there are uncommitted changes.

func (*Git) IsAncestor

func (g *Git) IsAncestor(ancestor, descendant string) (bool, error)

IsAncestor checks if ancestor is an ancestor of descendant.

func (*Git) IsRepo

func (g *Git) IsRepo() bool

IsRepo returns true if the workDir is a git repository.

func (*Git) ListBranches

func (g *Git) ListBranches(pattern string) ([]string, error)

ListBranches returns all local branches matching a pattern. Pattern uses git's pattern matching (e.g., "polecat/*" matches all polecat branches). Returns branch names without the refs/heads/ prefix.

func (*Git) Merge

func (g *Git) Merge(branch string) error

Merge merges the given branch into the current branch.

func (*Git) MergeNoFF

func (g *Git) MergeNoFF(branch, message string) error

MergeNoFF merges the given branch with --no-ff flag and a custom message.

func (*Git) Pull

func (g *Git) Pull(remote, branch string) error

Pull pulls from the remote branch.

func (*Git) Push

func (g *Git) Push(remote, branch string, force bool) error

Push pushes to the remote branch.

func (*Git) Rebase

func (g *Git) Rebase(onto string) error

Rebase rebases the current branch onto the given ref.

func (*Git) RemoteBranchExists

func (g *Git) RemoteBranchExists(remote, branch string) (bool, error)

RemoteBranchExists checks if a branch exists on the remote.

func (*Git) RemoteDefaultBranch

func (g *Git) RemoteDefaultBranch() string

RemoteDefaultBranch returns the default branch from the remote (origin). This is useful in worktrees where HEAD may not reflect the repo's actual default. Checks origin/HEAD first, then falls back to checking if master/main exists. Returns "main" as final fallback.

func (*Git) RemoteURL

func (g *Git) RemoteURL(remote string) (string, error)

RemoteURL returns the URL for the given remote.

func (*Git) Remotes

func (g *Git) Remotes() ([]string, error)

Remotes returns the list of configured remote names.

func (*Git) ResetBranch

func (g *Git) ResetBranch(name, ref string) error

ResetBranch force-updates a branch to point to a ref. This is useful for resetting stale polecat branches to main.

func (*Git) Rev

func (g *Git) Rev(ref string) (string, error)

Rev returns the commit hash for the given ref.

func (*Git) StashCount

func (g *Git) StashCount() (int, error)

StashCount returns the number of stashes in the repository.

func (*Git) Status

func (g *Git) Status() (*GitStatus, error)

Status returns the current git status.

func (*Git) UnpushedCommits

func (g *Git) UnpushedCommits() (int, error)

UnpushedCommits returns the number of commits that are not pushed to the remote. It checks if the current branch has an upstream and counts commits ahead. Returns 0 if there is no upstream configured.

func (*Git) WorkDir

func (g *Git) WorkDir() string

WorkDir returns the working directory for this Git instance.

func (*Git) WorktreeAdd

func (g *Git) WorktreeAdd(path, branch string) error

WorktreeAdd creates a new worktree at the given path with a new branch. The new branch is created from the current HEAD. Sparse checkout is enabled to exclude .cursor/ from source repos.

func (*Git) WorktreeAddDetached

func (g *Git) WorktreeAddDetached(path, ref string) error

WorktreeAddDetached creates a new worktree at the given path with a detached HEAD. Sparse checkout is enabled to exclude .cursor/ from source repos.

func (*Git) WorktreeAddExisting

func (g *Git) WorktreeAddExisting(path, branch string) error

WorktreeAddExisting creates a new worktree at the given path for an existing branch. Sparse checkout is enabled to exclude .cursor/ from source repos.

func (*Git) WorktreeAddExistingForce

func (g *Git) WorktreeAddExistingForce(path, branch string) error

WorktreeAddExistingForce creates a new worktree even if the branch is already checked out elsewhere. This is useful for cross-rig worktrees where multiple clones need to be on main. Sparse checkout is enabled to exclude .cursor/ from source repos.

func (*Git) WorktreeAddFromRef

func (g *Git) WorktreeAddFromRef(path, branch, startPoint string) error

WorktreeAddFromRef creates a new worktree at the given path with a new branch starting from the specified ref (e.g., "origin/main"). Sparse checkout is enabled to exclude .cursor/ from source repos.

func (*Git) WorktreeList

func (g *Git) WorktreeList() ([]Worktree, error)

WorktreeList returns all worktrees for this repository.

func (*Git) WorktreePrune

func (g *Git) WorktreePrune() error

WorktreePrune removes worktree entries for deleted paths.

func (*Git) WorktreeRemove

func (g *Git) WorktreeRemove(path string, force bool) error

WorktreeRemove removes a worktree.

type GitStatus

type GitStatus struct {
	Clean     bool
	Modified  []string
	Added     []string
	Deleted   []string
	Untracked []string
}

GitStatus represents the status of the working directory.

type UncommittedWorkStatus

type UncommittedWorkStatus struct {
	HasUncommittedChanges bool
	StashCount            int
	UnpushedCommits       int
	// Details for error messages
	ModifiedFiles  []string
	UntrackedFiles []string
}

UncommittedWorkStatus contains information about uncommitted work in a repo.

func (*UncommittedWorkStatus) Clean

func (s *UncommittedWorkStatus) Clean() bool

Clean returns true if there is no uncommitted work.

func (*UncommittedWorkStatus) String

func (s *UncommittedWorkStatus) String() string

String returns a human-readable summary of uncommitted work.

type Worktree

type Worktree struct {
	Path   string
	Branch string
	Commit string
}

Worktree represents a git worktree.

Jump to

Keyboard shortcuts

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