git

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jan 30, 2026 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package git provides a safe wrapper around git command execution.

This package delegates all git operations to the system git binary, leveraging the user's existing git configuration for authentication. It does not implement any platform-specific code (GitHub, GitLab, etc.) and does not store or manage credentials.

Key features:

  • Command execution with proper stderr capture for error diagnostics
  • Git version detection and validation (minimum: 2.5)
  • Ref existence checking for local and remote repositories
  • Repository state detection (shallow clone, git root)
  • Structured error types with actionable messages

Example usage:

// Check if git is available
if !git.Available() {
    return git.ErrGitNotFound
}

// Check git version
if err := git.CheckVersion(2, 5); err != nil {
    return err
}

// Check if a ref exists locally
exists, err := git.RefExists("/path/to/repo", "main")

// Check if a ref exists in a remote repository
exists, err := git.RemoteRefExists("https://github.com/org/repo", "v1.0.0")

Index

Constants

View Source
const (
	MinVersionMajor = 2
	MinVersionMinor = 5
)

MinVersion is the minimum git version required for tfbreak.

Variables

View Source
var ErrGitNotFound = errors.New("git is not installed or not in PATH")

ErrGitNotFound is returned when git is not installed or not in PATH.

Functions

func Available

func Available() bool

Available returns true if git is installed and in PATH.

func CheckMinVersion

func CheckMinVersion() error

CheckMinVersion verifies git meets the minimum version for tfbreak.

func CheckVersion

func CheckVersion(minMajor, minMinor int) error

CheckVersion verifies git is installed and meets the minimum version requirement. Returns an error if git is not installed or version is below minMajor.minMinor.

func CleanupOrphanedWorktrees

func CleanupOrphanedWorktrees(repoDir string)

CleanupOrphanedWorktrees removes any tfbreak worktrees that weren't properly cleaned up. This is a best-effort operation and errors are ignored.

func FindGitRoot

func FindGitRoot(dir string) (string, error)

FindGitRoot finds the root directory of the git repository containing dir. Returns the path to the repository root, or an error if dir is not in a git repository.

func GetCurrentBranch

func GetCurrentBranch(dir string) (string, error)

GetCurrentBranch returns the current branch name, or empty string if in detached HEAD state.

func GetHEAD

func GetHEAD(dir string) (string, error)

GetHEAD returns the current HEAD commit SHA.

func IsAuthError

func IsAuthError(err error) bool

IsAuthError returns true if the error indicates an authentication failure.

func IsGitRepository

func IsGitRepository(dir string) bool

IsGitRepository returns true if dir is inside a git repository.

func IsNotFound

func IsNotFound(err error) bool

IsNotFound returns true if the error indicates a ref was not found.

func IsShallowClone

func IsShallowClone(dir string) (bool, error)

IsShallowClone returns true if the repository at dir is a shallow clone. A shallow clone has a .git/shallow file.

func ListRemoteRefs

func ListRemoteRefs(url string, patterns ...string) (map[string]string, error)

ListRemoteRefs lists all refs in a remote repository. If pattern is provided, only matching refs are returned.

func RefExists

func RefExists(dir, ref string) (bool, error)

RefExists checks if a ref exists in a local repository. Returns true if the ref exists, false if it doesn't, or an error if the check fails.

func RemoteRefExists

func RemoteRefExists(url, ref string) (bool, error)

RemoteRefExists checks if a ref exists in a remote repository without cloning. This uses git ls-remote which only fetches ref information, not content.

func ResolveRef

func ResolveRef(dir, ref string) (string, error)

ResolveRef resolves a ref to its commit SHA in a local repository.

func ResolveRemoteRef

func ResolveRemoteRef(url, ref string) (sha string, fullRef string, err error)

ResolveRemoteRef resolves a ref to its commit SHA in a remote repository. Returns the SHA and the full ref name (e.g., "refs/tags/v1.0.0").

func Run

func Run(args []string, opts *RunOptions) (string, error)

Run executes a git command and returns the stdout output. If the command fails, a *GitError is returned with stderr context.

func RunSilent

func RunSilent(args []string, opts *RunOptions) error

RunSilent executes a git command without capturing output. It returns an error if the command fails.

func WorktreeList

func WorktreeList(repoDir string) ([]string, error)

WorktreeList returns a list of all worktrees for the repository.

Types

type Clone

type Clone struct {
	// Path is the filesystem path to the clone.
	Path string

	// URL is the repository URL that was cloned.
	URL string

	// Ref is the ref that was checked out.
	Ref string

	// SHA is the resolved commit SHA.
	SHA string
}

Clone represents a shallow clone that will be cleaned up.

func CloneForComparison

func CloneForComparison(url, baseRef, headRef string) (baseClone, headClone *Clone, err error)

CloneForComparison creates two shallow clones for comparing two refs. Both clones are returned, and the caller is responsible for calling Remove() on both. If creating either clone fails, any created clone is cleaned up before returning.

func ShallowClone

func ShallowClone(url, ref string) (*Clone, error)

ShallowClone creates a shallow clone of a remote repository at a specific ref. The clone is created in a temporary directory and should be cleaned up by calling Remove() when done.

This uses --depth 1 --single-branch for efficiency, downloading only the necessary data for the specified ref.

func (*Clone) Remove

func (c *Clone) Remove() error

Remove cleans up the clone by removing the directory.

type ErrNotARepository

type ErrNotARepository struct {
	Dir string
}

ErrNotARepository is returned when the directory is not inside a git repository.

func (*ErrNotARepository) Error

func (e *ErrNotARepository) Error() string

type ErrRefNotFound

type ErrRefNotFound struct {
	Ref       string
	Remote    string // Empty for local refs
	IsShallow bool
}

ErrRefNotFound is returned when a ref doesn't exist.

func (*ErrRefNotFound) Error

func (e *ErrRefNotFound) Error() string

type ErrVersionTooOld

type ErrVersionTooOld struct {
	Current  string
	Required string
}

ErrVersionTooOld is returned when git version is below the minimum required.

func (*ErrVersionTooOld) Error

func (e *ErrVersionTooOld) Error() string

type GitError

type GitError struct {
	Command  []string
	ExitCode int
	Stderr   string
}

GitError wraps errors from git command execution with full context.

func (*GitError) Error

func (e *GitError) Error() string

type RunOptions

type RunOptions struct {
	// Dir is the working directory for the command.
	// If empty, the current working directory is used.
	Dir string

	// Env contains additional environment variables.
	// These are appended to the current environment.
	Env []string
}

RunOptions configures how a git command is executed.

type Version

type Version struct {
	Major int
	Minor int
	Patch int
	Raw   string
}

Version represents a parsed git version.

func GetVersion

func GetVersion() (*Version, error)

GetVersion returns the installed git version.

func ParseVersion

func ParseVersion(s string) (*Version, error)

ParseVersion parses a git version string.

func (*Version) AtLeast

func (v *Version) AtLeast(major, minor int) bool

AtLeast returns true if this version is at least major.minor.

func (*Version) String

func (v *Version) String() string

String returns the version as "major.minor.patch".

type Worktree

type Worktree struct {
	// Path is the filesystem path to the worktree.
	Path string

	// RepoDir is the path to the main repository.
	RepoDir string

	// Ref is the ref that was checked out.
	Ref string

	// SHA is the resolved commit SHA.
	SHA string
}

Worktree represents a git worktree that will be cleaned up.

func CreateWorktree

func CreateWorktree(repoDir, ref string) (*Worktree, error)

CreateWorktree creates a detached worktree at the specified ref. The worktree is created in a temporary directory and should be cleaned up by calling Remove() when done.

func (*Worktree) Remove

func (w *Worktree) Remove() error

Remove cleans up the worktree. It removes the worktree from git's tracking and deletes the directory. Errors during removal are logged but not returned to ensure cleanup completes.

Jump to

Keyboard shortcuts

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