git

package
v0.8.6 Latest Latest
Warning

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

Go to latest
Published: Jan 15, 2026 License: EUPL-1.2 Imports: 14 Imported by: 0

Documentation

Overview

Package git implements Git repository operations.

Index

Constants

View Source
const (
	// MaxGitRefLength is the maximum length for a Git reference name.
	// Git itself limits ref names to 255 bytes.
	MaxGitRefLength = 255

	// InitialCommitMapCapacity is the initial capacity for commit reachability maps.
	// This avoids frequent map resizing for typical repository sizes.
	InitialCommitMapCapacity = 200

	// DefaultTimeout is the default timeout for git operations.
	DefaultTimeout = 30 * time.Second
)

Git-related constants.

View Source
const (
	MaxCommitTraversalDepth = 10000  // Prevent stack overflow from deep histories
	MaxCommitCount          = 100000 // Prevent memory exhaustion
)

Security constants for git operations.

Variables

View Source
var (
	ErrGitRefEmpty            = errors.New("git reference cannot be empty")
	ErrGitRefTooLong          = errors.New("git reference too long")
	ErrGitRefNullByte         = errors.New("git reference contains null byte")
	ErrGitRefInvalidChars     = errors.New("git reference contains command injection characters")
	ErrReferenceNotFound      = errors.New("reference not found")
	ErrTraversalDepthExceeded = errors.New("git traversal depth exceeded maximum")
	ErrCommitCountExceeded    = errors.New("git commit count exceeded maximum")
	ErrFoundReference         = errors.New("found reference")
)

Repository errors.

View Source
var DefaultFactory = &threadSafeFactory{}

DefaultFactory is the global factory used by CLI commands. It can be overridden for testing.

View Source
var ErrRepositoryNotFound = errors.New("in-memory repository not found")

ErrRepositoryNotFound is returned when an in-memory repository is not found.

Functions

func IsVirtualPath

func IsVirtualPath(path string) bool

IsVirtualPath checks if a path refers to an in-memory repository.

func SetDefaultFactory

func SetDefaultFactory(factory RepositoryFactory)

SetDefaultFactory sets the default factory (primarily for testing).

func WithFactory

func WithFactory(factory RepositoryFactory, fn func())

WithFactory temporarily sets a factory for a function execution. This is useful for tests to use isolated factories without races.

Types

type FileSystemFactory

type FileSystemFactory struct{}

FileSystemFactory creates repositories from filesystem paths (production use).

func (*FileSystemFactory) CreateRepository

func (f *FileSystemFactory) CreateRepository(path string) (*Repository, error)

CreateRepository creates a repository from a filesystem path.

type InMemoryFactory

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

InMemoryFactory creates in-memory repositories for testing. Repositories are identified by virtual paths like "mem://repo-name".

func NewInMemoryFactory

func NewInMemoryFactory() *InMemoryFactory

NewInMemoryFactory creates a new in-memory repository factory.

func (*InMemoryFactory) CreateInMemoryRepo

func (f *InMemoryFactory) CreateInMemoryRepo(name string) (*gogit.Repository, error)

CreateInMemoryRepo creates a new in-memory repository and registers it.

func (*InMemoryFactory) CreateRepository

func (f *InMemoryFactory) CreateRepository(identifier string) (*Repository, error)

CreateRepository creates or retrieves an in-memory repository.

func (*InMemoryFactory) GetVirtualPath

func (f *InMemoryFactory) GetVirtualPath(name string) string

GetVirtualPath returns the virtual path for an in-memory repository.

func (*InMemoryFactory) RegisterRepository

func (f *InMemoryFactory) RegisterRepository(name string, repo *gogit.Repository)

RegisterRepository adds a new in-memory repository to the factory.

type Repository

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

Repository implements the CommitRepository port.

func NewRepository

func NewRepository(storer storage.Storer, worktree billy.Filesystem, opts ...RepositoryOption) (*Repository, error)

NewRepository opens a git repository with flexible storage backend. For filesystem repositories, pass filesystem storage and worktree. For in-memory repositories (tests), pass memory storage and memfs.

func NewRepositoryFromPath

func NewRepositoryFromPath(path string, opts ...RepositoryOption) (*Repository, error)

NewRepositoryFromPath opens a git repository from a filesystem path. NewRepositoryFromPath creates a repository from a filesystem path.

func (Repository) BranchExists

func (r Repository) BranchExists(_ context.Context, branchName string) bool

BranchExists checks if a branch reference exists (local or remote).

func (Repository) GetCommit

func (r Repository) GetCommit(_ context.Context, ref string) (domain.Commit, error)

GetCommit retrieves a single commit by hash or reference.

func (Repository) GetCommitRange

func (r Repository) GetCommitRange(ctx context.Context, fromRef, toRef string) ([]domain.Commit, error)

GetCommitRange retrieves commits in a range (from..to) using Git's three-dot syntax. Implements Git's reachability logic: commits reachable from 'to' but not reachable from 'from'.

func (Repository) GetCommitsAheadCount

func (r Repository) GetCommitsAheadCount(ctx context.Context, referenceBranch string) (int, error)

GetCommitsAheadCount calculates how many commits the current branch is ahead of the reference.

func (Repository) GetCurrentBranch

func (r Repository) GetCurrentBranch(ctx context.Context) (string, error)

GetCurrentBranch returns the name of the current branch.

func (Repository) GetHeadCommits

func (r Repository) GetHeadCommits(ctx context.Context, count int) ([]domain.Commit, error)

GetHeadCommits retrieves the latest N commits from HEAD.

type RepositoryFactory

type RepositoryFactory interface {
	CreateRepository(identifier string) (*Repository, error)
}

RepositoryFactory creates Repository instances from identifiers.

func GetDefaultFactory

func GetDefaultFactory() RepositoryFactory

GetDefaultFactory returns the current default factory.

type RepositoryOption

type RepositoryOption func(*Repository)

RepositoryOption defines a configuration option for Repository.

func WithTimeout

func WithTimeout(timeout time.Duration) RepositoryOption

WithTimeout sets the operation timeout for git operations.

Jump to

Keyboard shortcuts

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