repo

package
v1.9.2 Latest Latest
Warning

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

Go to latest
Published: Mar 31, 2026 License: MIT Imports: 20 Imported by: 0

Documentation

Overview

Package repo provides repository URL parsing and metadata extraction, converting host/owner/repo paths into a RepoLike abstraction that supports both GitHub (org/repo) and GitLab (group/subgroup/repo) path formats.

Index

Constants

View Source
const (
	SourceUnknown = iota
	SourceMemory
	SourceLocal
)

Variables

View Source
var (
	// ErrNoRepository is returned by WithRepo when no *git.Repository has been set.
	ErrNoRepository = errors.New("repository not initialised; call Open, Clone, or SetRepo first")

	// ErrNoWorktree is returned by WithTree when no *git.Worktree has been set.
	ErrNoWorktree = errors.New("worktree not initialised; call Open, Clone, or SetTree first")
)

Functions

func GetSSHKey

func GetSSHKey(filePath string, localfs afero.Fs) (*ssh.PublicKeys, error)

Types

type CloneOption

type CloneOption func(*git.CloneOptions)

CloneOption represents a function that configures clone options.

func WithNoTags

func WithNoTags() CloneOption

WithNoTags configures the clone to skip fetching tags.

func WithRecurseSubmodules

func WithRecurseSubmodules() CloneOption

WithRecurseSubmodules configures recursive submodule cloning.

func WithShallowClone

func WithShallowClone(depth int) CloneOption

WithShallowClone configures a shallow clone with the specified depth.

func WithSingleBranch

func WithSingleBranch(branch string) CloneOption

WithSingleBranch configures the clone to only fetch a single branch.

type Repo

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

func NewRepo

func NewRepo(props *props.Props, ops ...RepoOpt) (*Repo, error)

func (*Repo) AddToFS

func (r *Repo) AddToFS(fs afero.Fs, gitFile *object.File, fullPath string) error

AddToFS ensures that a git file is available in the afero filesystem.

func (*Repo) Checkout

func (r *Repo) Checkout(branch plumbing.ReferenceName) error

func (*Repo) CheckoutCommit

func (r *Repo) CheckoutCommit(hash plumbing.Hash) error

CheckoutCommit checks out a specific commit by hash.

func (*Repo) Clone

func (r *Repo) Clone(uri string, targetPath string, opts ...CloneOption) (*git.Repository, *git.Worktree, error)

Clone clones a git repository to a target path on the filesystem Supports both remote URLs and local git repository paths with clone options.

func (*Repo) Commit

func (r *Repo) Commit(commitMsg string, opts *git.CommitOptions) (plumbing.Hash, error)

func (*Repo) CreateBranch

func (r *Repo) CreateBranch(branchName string) error

CreateBranch creates a branch in the git tree.

func (*Repo) CreateRemote

func (r *Repo) CreateRemote(name string, urls []string) (*git.Remote, error)

func (*Repo) DirectoryExists

func (r *Repo) DirectoryExists(relPath string) (bool, error)

DirectoryExists checks if a directory exists in the git repository at the given relative path In git, directories don't exist as separate entities - we check if any files exist under the path.

func (*Repo) FileExists

func (r *Repo) FileExists(relPath string) (bool, error)

FileExists checks if a file exists in the git repository at the given relative path.

func (*Repo) GetAuth

func (r *Repo) GetAuth() transport.AuthMethod

func (*Repo) GetFile

func (r *Repo) GetFile(relPath string) (*object.File, error)

GetFile retrieves a file from the git repository at the given relative path.

func (*Repo) Open

func (r *Repo) Open(repoType RepoType, location string, branch string, opts ...CloneOption) (*git.Repository, *git.Worktree, error)

Open opens a local git repository. if no repo exists will init a repo.

func (*Repo) OpenInMemory

func (r *Repo) OpenInMemory(location string, branch string, opts ...CloneOption) (*git.Repository, *git.Worktree, error)

func (*Repo) OpenLocal

func (r *Repo) OpenLocal(location string, branch string) (*git.Repository, *git.Worktree, error)

Open opens a local git repository. if no repo exists will init a repo.

func (*Repo) Push

func (r *Repo) Push(opts *git.PushOptions) error

func (*Repo) Remote

func (r *Repo) Remote(name string) (*git.Remote, error)

func (*Repo) SetBasicAuth

func (r *Repo) SetBasicAuth(username, password string)

func (*Repo) SetKey

func (r *Repo) SetKey(key *ssh.PublicKeys)

func (*Repo) SetRepo

func (r *Repo) SetRepo(repo *git.Repository)

func (*Repo) SetSource

func (r *Repo) SetSource(source int)

func (*Repo) SetTree

func (r *Repo) SetTree(tree *git.Worktree)

func (*Repo) SourceIs

func (r *Repo) SourceIs(source int) bool

func (*Repo) WalkTree

func (r *Repo) WalkTree(fn func(*object.File) error) error

WalkTree walks the git tree and calls the provided function for each file.

func (*Repo) WithRepo added in v1.8.0

func (r *Repo) WithRepo(fn func(*git.Repository) error) error

WithRepo calls fn with the underlying *git.Repository. Repo is not safe for concurrent use; callers are responsible for external synchronisation if sharing a *Repo across goroutines.

Returns ErrNoRepository if the repository has not been initialised.

func (*Repo) WithTree added in v1.8.0

func (r *Repo) WithTree(fn func(*git.Worktree) error) error

WithTree calls fn with the underlying *git.Worktree. Repo is not safe for concurrent use; callers are responsible for external synchronisation if sharing a *Repo across goroutines.

Returns ErrNoWorktree if the worktree has not been initialised.

type RepoLike

type RepoLike interface {
	SourceIs(int) bool
	SetSource(int)
	SetRepo(*git.Repository)
	SetKey(*ssh.PublicKeys)
	SetBasicAuth(string, string)
	GetAuth() transport.AuthMethod
	SetTree(*git.Worktree)
	Checkout(plumbing.ReferenceName) error
	CheckoutCommit(plumbing.Hash) error
	CreateBranch(string) error
	Push(*git.PushOptions) error
	Commit(string, *git.CommitOptions) (plumbing.Hash, error)

	OpenInMemory(string, string, ...CloneOption) (*git.Repository, *git.Worktree, error)
	OpenLocal(string, string) (*git.Repository, *git.Worktree, error)
	Open(RepoType, string, string, ...CloneOption) (*git.Repository, *git.Worktree, error)
	Clone(string, string, ...CloneOption) (*git.Repository, *git.Worktree, error)

	// Git tree operations for in-memory repositories
	WalkTree(func(*object.File) error) error
	FileExists(string) (bool, error)
	DirectoryExists(string) (bool, error)
	GetFile(string) (*object.File, error)
	AddToFS(fs afero.Fs, gitFile *object.File, fullPath string) error

	// WithRepo calls fn with the underlying *git.Repository.
	// Returns ErrNoRepository if the repository has not been initialised.
	WithRepo(func(*git.Repository) error) error

	// WithTree calls fn with the underlying *git.Worktree.
	// Returns ErrNoWorktree if the worktree has not been initialised.
	WithTree(func(*git.Worktree) error) error
}

type RepoOpt

type RepoOpt func(*Repo) error

func WithConfig

func WithConfig(cfg *config.Config) RepoOpt

type RepoType

type RepoType = string
var (
	LocalRepo    RepoType = "local"
	InMemoryRepo RepoType = "inmemory"
)

type ThreadSafeRepo added in v1.8.0

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

ThreadSafeRepo wraps a *Repo with a mutex so that all RepoLike methods are safe to call concurrently from multiple goroutines.

Thread-safety guarantee

Every method acquires the internal mutex for its full duration. Concurrent callers are serialised; no two calls to any method execute simultaneously.

WithRepo and WithTree

These are the only way to interact with the underlying go-git objects safely. The callback executes while the mutex is held. Callers must not:

  • Retain the pointer after the callback returns.
  • Call any ThreadSafeRepo method from inside the callback (deadlock).
  • Spawn goroutines inside the callback that access the pointer after it returns.

go-git concurrency model

go-git mutates internal caches during read operations. ThreadSafeRepo uses sync.Mutex (exclusive) rather than sync.RWMutex; concurrent reads are not permitted.

func NewThreadSafeRepo added in v1.8.0

func NewThreadSafeRepo(p *props.Props, opts ...RepoOpt) (*ThreadSafeRepo, error)

NewThreadSafeRepo creates a ThreadSafeRepo backed by a freshly constructed *Repo. The props and opts arguments have the same semantics as NewRepo.

func (*ThreadSafeRepo) AddToFS added in v1.8.0

func (r *ThreadSafeRepo) AddToFS(fs afero.Fs, gitFile *object.File, fullPath string) error

func (*ThreadSafeRepo) Checkout added in v1.8.0

func (r *ThreadSafeRepo) Checkout(branch plumbing.ReferenceName) error

func (*ThreadSafeRepo) CheckoutCommit added in v1.8.0

func (r *ThreadSafeRepo) CheckoutCommit(hash plumbing.Hash) error

func (*ThreadSafeRepo) Clone added in v1.8.0

func (r *ThreadSafeRepo) Clone(uri string, targetPath string, opts ...CloneOption) (*git.Repository, *git.Worktree, error)

func (*ThreadSafeRepo) Commit added in v1.8.0

func (r *ThreadSafeRepo) Commit(commitMsg string, opts *git.CommitOptions) (plumbing.Hash, error)

func (*ThreadSafeRepo) CreateBranch added in v1.8.0

func (r *ThreadSafeRepo) CreateBranch(branchName string) error

func (*ThreadSafeRepo) DirectoryExists added in v1.8.0

func (r *ThreadSafeRepo) DirectoryExists(relPath string) (bool, error)

func (*ThreadSafeRepo) FileExists added in v1.8.0

func (r *ThreadSafeRepo) FileExists(relPath string) (bool, error)

func (*ThreadSafeRepo) GetAuth added in v1.8.0

func (r *ThreadSafeRepo) GetAuth() transport.AuthMethod

func (*ThreadSafeRepo) GetFile added in v1.8.0

func (r *ThreadSafeRepo) GetFile(relPath string) (*object.File, error)

func (*ThreadSafeRepo) Open added in v1.8.0

func (r *ThreadSafeRepo) Open(repoType RepoType, location string, branch string, opts ...CloneOption) (*git.Repository, *git.Worktree, error)

func (*ThreadSafeRepo) OpenInMemory added in v1.8.0

func (r *ThreadSafeRepo) OpenInMemory(location string, branch string, opts ...CloneOption) (*git.Repository, *git.Worktree, error)

func (*ThreadSafeRepo) OpenLocal added in v1.8.0

func (r *ThreadSafeRepo) OpenLocal(location string, branch string) (*git.Repository, *git.Worktree, error)

func (*ThreadSafeRepo) Push added in v1.8.0

func (r *ThreadSafeRepo) Push(opts *git.PushOptions) error

func (*ThreadSafeRepo) SetBasicAuth added in v1.8.0

func (r *ThreadSafeRepo) SetBasicAuth(username, password string)

func (*ThreadSafeRepo) SetKey added in v1.8.0

func (r *ThreadSafeRepo) SetKey(key *ssh.PublicKeys)

func (*ThreadSafeRepo) SetRepo added in v1.8.0

func (r *ThreadSafeRepo) SetRepo(repo *git.Repository)

func (*ThreadSafeRepo) SetSource added in v1.8.0

func (r *ThreadSafeRepo) SetSource(source int)

func (*ThreadSafeRepo) SetTree added in v1.8.0

func (r *ThreadSafeRepo) SetTree(tree *git.Worktree)

func (*ThreadSafeRepo) SourceIs added in v1.8.0

func (r *ThreadSafeRepo) SourceIs(source int) bool

func (*ThreadSafeRepo) WalkTree added in v1.8.0

func (r *ThreadSafeRepo) WalkTree(fn func(*object.File) error) error

func (*ThreadSafeRepo) WithRepo added in v1.8.0

func (r *ThreadSafeRepo) WithRepo(fn func(*git.Repository) error) error

WithRepo acquires the mutex and calls fn with the underlying *git.Repository. The callback executes while the lock is held.

Returns ErrNoRepository if the repository has not been initialised.

func (*ThreadSafeRepo) WithTree added in v1.8.0

func (r *ThreadSafeRepo) WithTree(fn func(*git.Worktree) error) error

WithTree acquires the mutex and calls fn with the underlying *git.Worktree. The callback executes while the lock is held.

Returns ErrNoWorktree if the worktree has not been initialised.

Jump to

Keyboard shortcuts

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