Documentation
¶
Overview ¶
Package gitlib provides a unified interface for git operations using libgit2. This replaces go-git with the faster libgit2 C library.
Index ¶
- Constants
- Variables
- func ConfigureMemoryLimits(mwindowLimit, cacheLimit int64, mallocArenaMax int) error
- func ParseTime(s string) (time.Time, error)
- func ReleaseNativeMemory() bool
- type BatchConfig
- type Blob
- type BlobBatchRequest
- type BlobBatchResponse
- type BlobResult
- type CGOBridge
- type CachedBlob
- type Change
- type ChangeAction
- type ChangeEntry
- type Changes
- type Commit
- func (c *Commit) Author() Signature
- func (c *Commit) Committer() Signature
- func (c *Commit) File(path string) (*File, error)
- func (c *Commit) Files() (*FileIter, error)
- func (c *Commit) FilesContext(_ context.Context) (*FileIter, error)
- func (c *Commit) Free()
- func (c *Commit) Hash() Hash
- func (c *Commit) Message() string
- func (c *Commit) Native() *git2go.Commit
- func (c *Commit) NumParents() int
- func (c *Commit) Parent(n int) (*Commit, error)
- func (c *Commit) ParentHash(n int) Hash
- func (c *Commit) Tree() (*Tree, error)
- func (c *Commit) TreeHash() Hash
- type CommitIter
- type CommitLoadOptions
- type ContextualRequest
- type Diff
- type DiffBatchRequest
- type DiffBatchResponse
- type DiffDelta
- type DiffFile
- type DiffOp
- type DiffOpType
- type DiffRequest
- type DiffResult
- type DiffStats
- type File
- type FileIter
- type Hash
- type LogOptions
- type Repository
- func (r *Repository) CommitCount(opts *LogOptions) (int, error)
- func (r *Repository) DiffTreeToTree(oldTree, newTree *Tree) (*Diff, error)
- func (r *Repository) Free()
- func (r *Repository) Head() (Hash, error)
- func (r *Repository) Log(opts *LogOptions) (*CommitIter, error)
- func (r *Repository) LookupBlob(_ context.Context, hash Hash) (*Blob, error)
- func (r *Repository) LookupCommit(_ context.Context, hash Hash) (*Commit, error)
- func (r *Repository) LookupTree(hash Hash) (*Tree, error)
- func (r *Repository) Native() *git2go.Repository
- func (r *Repository) Path() string
- func (r *Repository) ResolveTime(s string) (time.Time, error)
- func (r *Repository) Walk() (*RevWalk, error)
- type RevWalk
- type Signature
- type TestCommit
- func (m *TestCommit) Author() Signature
- func (m *TestCommit) Committer() Signature
- func (m *TestCommit) File(_ string) (*File, error)
- func (m *TestCommit) Files() (*FileIter, error)
- func (m *TestCommit) Free()
- func (m *TestCommit) Hash() Hash
- func (m *TestCommit) Message() string
- func (m *TestCommit) NumParents() int
- func (m *TestCommit) Parent(_ int) (*Commit, error)
- func (m *TestCommit) Tree() (*Tree, error)
- type Tree
- func (t *Tree) EntryByIndex(i uint64) *TreeEntry
- func (t *Tree) EntryByPath(path string) (*TreeEntry, error)
- func (t *Tree) EntryCount() uint64
- func (t *Tree) Files() *FileIter
- func (t *Tree) FilesContext(_ context.Context) *FileIter
- func (t *Tree) Free()
- func (t *Tree) Hash() Hash
- func (t *Tree) Native() *git2go.Tree
- type TreeDiffRequest
- type TreeDiffResponse
- type TreeEntry
- type Worker
- type WorkerRequest
Constants ¶
const ( // HashSize is the size of a SHA-1 hash in bytes. HashSize = 20 // HashHexSize is the size of a hex-encoded SHA-1 hash. HashHexSize = 40 )
Constants for hash operations.
Variables ¶
var ( ErrRepositoryPointer = cgoError("failed to get repository pointer") ErrBlobLookup = cgoError("blob lookup failed") ErrBlobMemory = cgoError("memory allocation failed for blob") ErrBlobBinary = cgoError("blob is binary") ErrDiffLookup = cgoError("diff blob lookup failed") ErrDiffMemory = cgoError("memory allocation failed for diff") ErrDiffBinary = cgoError("diff blob is binary") ErrDiffCompute = cgoError("diff computation failed") ErrArenaFull = cgoError("arena full") ErrConfigureMemory = cgoError("cf_configure_memory failed") )
CGO operation errors.
var ErrBinary = errors.New("binary")
ErrBinary is raised in CachedBlob.CountLines() if the file is binary.
var ErrInvalidTimeFormat = errors.New("cannot parse time")
ErrInvalidTimeFormat is returned when a time string cannot be parsed.
var ErrMockNotImplemented = errors.New("mock: operation not implemented")
ErrMockNotImplemented is returned by mock methods that are not implemented.
var ErrParentNotFound = errors.New("parent commit not found")
ErrParentNotFound is returned when the requested parent commit is not found.
var ErrRemoteNotSupported = errors.New("remote repositories not supported")
ErrRemoteNotSupported is returned when a remote repository URI is provided.
Functions ¶
func ConfigureMemoryLimits ¶
ConfigureMemoryLimits sets libgit2 global memory limits and glibc malloc arena count. mwindowLimit caps mmap'd pack data (default 8 GiB on 64-bit). cacheLimit caps the decompressed object cache (default 256 MiB). mallocArenaMax caps glibc malloc arenas (default 8*cores, causes RSS bloat). Pass 0 for any to leave unchanged. Must be called before opening repos.
func ParseTime ¶
ParseTime parses a time string in various formats: - Duration relative to now (e.g. "24h") - RFC3339 (e.g. "2024-01-01T00:00:00Z") - Date only (e.g. "2024-01-01").
func ReleaseNativeMemory ¶
func ReleaseNativeMemory() bool
ReleaseNativeMemory returns free native memory pages to the OS. On glibc systems, calls malloc_trim(0) to release freed pages from all malloc arenas. This is the C-side equivalent of debug.FreeOSMemory() for Go heap. Should be called between streaming chunks after bulk libgit2 operations that allocate and free large amounts of native memory. Returns true if memory was actually released.
Types ¶
type BatchConfig ¶
type BatchConfig struct {
// BlobBatchSize is the number of blobs to load per batch.
// Default: 100.
BlobBatchSize int
// DiffBatchSize is the number of diffs to compute per batch.
// Default: 50.
DiffBatchSize int
// Workers is the number of parallel workers for processing.
// Default: 1 (sequential processing within gitlib).
Workers int
}
BatchConfig configures batch processing parameters.
func DefaultBatchConfig ¶
func DefaultBatchConfig() BatchConfig
DefaultBatchConfig returns the default batch configuration.
type Blob ¶
type Blob struct {
// contains filtered or unexported fields
}
Blob wraps a libgit2 blob.
type BlobBatchRequest ¶
type BlobBatchRequest struct {
Hashes []Hash
Response chan<- BlobBatchResponse
Arena []byte
}
BlobBatchRequest asks to load a batch of blobs.
type BlobBatchResponse ¶
type BlobBatchResponse struct {
Blobs []*CachedBlob
Results []BlobResult
}
BlobBatchResponse is the response for a BlobBatchRequest.
type BlobResult ¶
type BlobResult struct {
Hash Hash
Data []byte
Size int64
IsBinary bool
LineCount int
Error error
KeepAlive any
}
BlobResult represents the result of loading a single blob.
type CGOBridge ¶
type CGOBridge struct {
// contains filtered or unexported fields
}
CGOBridge provides optimized batch operations using the C library. It minimizes CGO overhead by processing multiple items per call.
func NewCGOBridge ¶
func NewCGOBridge(repo *Repository) *CGOBridge
NewCGOBridge creates a new CGO bridge for the given repository.
func (*CGOBridge) BatchDiffBlobs ¶
func (b *CGOBridge) BatchDiffBlobs(requests []DiffRequest) []DiffResult
BatchDiffBlobs computes diffs for multiple blob pairs in a single CGO call. This minimizes CGO overhead by processing all requests together.
func (*CGOBridge) BatchLoadBlobs ¶
func (b *CGOBridge) BatchLoadBlobs(hashes []Hash) []BlobResult
BatchLoadBlobs loads multiple blobs in a single CGO call. This minimizes CGO overhead by processing all requests together.
func (*CGOBridge) BatchLoadBlobsArena ¶
func (b *CGOBridge) BatchLoadBlobsArena(hashes []Hash, arena []byte) []BlobResult
BatchLoadBlobsArena loads multiple blobs into a provided arena. It uses internal recycled buffers for C requests/results to avoid allocation.
type CachedBlob ¶
type CachedBlob struct {
// Data is the read contents of the blob object.
Data []byte
// contains filtered or unexported fields
}
CachedBlob caches blob data for efficient repeated access.
func NewCachedBlobForTest ¶
func NewCachedBlobForTest(data []byte) *CachedBlob
NewCachedBlobForTest creates a CachedBlob with the given data for testing purposes.
func NewCachedBlobFromRepo ¶
func NewCachedBlobFromRepo(ctx context.Context, repo *Repository, blobHash Hash) (*CachedBlob, error)
NewCachedBlobFromRepo loads and caches a blob from the repository.
func NewCachedBlobWithHashForTest ¶
func NewCachedBlobWithHashForTest(hash Hash, data []byte) *CachedBlob
NewCachedBlobWithHashForTest creates a CachedBlob with the given hash and data for testing.
func (*CachedBlob) Clone ¶
func (b *CachedBlob) Clone() *CachedBlob
Clone creates a deep copy of the CachedBlob, detaching the Data slice. This is useful when the original Data slice is part of a larger Arena.
func (*CachedBlob) CountLines ¶
func (b *CachedBlob) CountLines() (int, error)
CountLines returns the number of lines in the blob or (0, ErrBinary) if it is binary. The result is cached after the first call for efficiency.
func (*CachedBlob) IsBinary ¶
func (b *CachedBlob) IsBinary() bool
IsBinary returns true if the blob appears to be binary.
func (*CachedBlob) Reader ¶
func (b *CachedBlob) Reader() io.ReadCloser
Reader returns a reader for the blob data.
type Change ¶
type Change struct {
Action ChangeAction
From ChangeEntry
To ChangeEntry
}
Change represents a single file change between two trees.
type ChangeAction ¶
type ChangeAction int
ChangeAction represents the type of change in a diff.
const ( // Insert indicates a new file was added. Insert ChangeAction = iota // Delete indicates a file was removed. Delete // Modify indicates a file was modified. Modify )
type ChangeEntry ¶
ChangeEntry represents one side of a change (old or new file).
type Changes ¶
type Changes []*Change
Changes is a collection of Change objects.
func InitialTreeChanges ¶
InitialTreeChanges creates changes for an initial commit (all files are insertions).
type Commit ¶
type Commit struct {
// contains filtered or unexported fields
}
Commit wraps a libgit2 commit.
func LoadCommits ¶
func LoadCommits(ctx context.Context, repository *Repository, opts CommitLoadOptions) ([]*Commit, error)
LoadCommits loads commits from a repository with the given options.
func NewCommitForTest ¶
NewCommitForTest creates a Commit with the given hash for testing.
func (*Commit) Author ¶
Author returns the commit author. Zero value when commit is a test double (nil internal).
func (*Commit) Committer ¶
Committer returns the commit committer. Zero value when commit is a test double (nil internal).
func (*Commit) FilesContext ¶
FilesContext returns an iterator over all files in the commit's tree, accepting a context for tracing.
func (*Commit) Message ¶
Message returns the commit message. Empty when commit is a test double (nil internal).
func (*Commit) NumParents ¶
NumParents returns the number of parent commits. Zero when commit is a test double (nil internal).
func (*Commit) Parent ¶
Parent returns the nth parent commit. ErrParentNotFound when commit is a test double (nil internal).
func (*Commit) ParentHash ¶
ParentHash returns the hash of the nth parent. Zero hash when commit is a test double (nil internal).
type CommitIter ¶
type CommitIter struct {
// contains filtered or unexported fields
}
CommitIter iterates over commits.
func (*CommitIter) ForEach ¶
func (ci *CommitIter) ForEach(cb func(*Commit) error) error
ForEach calls the callback for each commit.
func (*CommitIter) Next ¶
func (ci *CommitIter) Next() (*Commit, error)
Next returns the next commit in the iteration.
func (*CommitIter) Skip ¶
func (ci *CommitIter) Skip(n int) error
Skip advances the iterator by n commits without materializing Commit objects. Used for checkpoint resume to efficiently skip already-processed commits.
type CommitLoadOptions ¶
CommitLoadOptions configures how commits are loaded from a repository.
type ContextualRequest ¶
type ContextualRequest struct {
WorkerRequest
// contains filtered or unexported fields
}
ContextualRequest wraps a WorkerRequest with an explicit context provider.
func WithContext ¶
func WithContext(ctx context.Context, req WorkerRequest) ContextualRequest
WithContext wraps a WorkerRequest with an explicit context.
type Diff ¶
type Diff struct {
// contains filtered or unexported fields
}
Diff wraps a libgit2 diff.
func (*Diff) ForEach ¶
func (d *Diff) ForEach( fileCallback func(delta DiffDelta, progress float64) (git2go.DiffForEachHunkCallback, error), detail git2go.DiffDetail, ) error
ForEach iterates over the diff with callbacks for files, hunks, and lines.
type DiffBatchRequest ¶
type DiffBatchRequest struct {
Requests []DiffRequest
Response chan<- DiffBatchResponse
}
DiffBatchRequest asks to compute diffs for a batch of pairs.
type DiffBatchResponse ¶
type DiffBatchResponse struct {
Results []DiffResult
}
DiffBatchResponse is the response for a DiffBatchRequest.
type DiffDelta ¶
type DiffDelta struct {
Status git2go.Delta
OldFile DiffFile
NewFile DiffFile
Flags git2go.DiffFlag
NumHunks int
}
DiffDelta represents a file change in a diff.
type DiffOp ¶
type DiffOp struct {
Type DiffOpType
LineCount int
}
DiffOp represents a single diff operation.
type DiffOpType ¶
type DiffOpType int
DiffOpType represents the type of diff operation.
const ( DiffOpEqual DiffOpType = 0 DiffOpInsert DiffOpType = 1 DiffOpDelete DiffOpType = 2 )
Diff operation types.
type DiffRequest ¶
type DiffRequest struct {
OldHash Hash
NewHash Hash
OldData []byte
NewData []byte
HasOld bool
HasNew bool
}
DiffRequest represents a request to diff two blobs.
type DiffResult ¶
DiffResult represents the result of diffing two blobs.
type DiffStats ¶
type DiffStats struct {
// contains filtered or unexported fields
}
DiffStats wraps libgit2 diff stats.
func (*DiffStats) FilesChanged ¶
FilesChanged returns the number of files changed.
func (*DiffStats) Insertions ¶
Insertions returns the number of insertions.
type File ¶
File represents a file in a tree with its content accessible.
func TreeFiles ¶
func TreeFiles(repo *Repository, tree *Tree) ([]*File, error)
TreeFiles returns all files in a tree.
func (*File) BlobContext ¶
BlobContext returns the blob object for this file, accepting a context for tracing.
func (*File) ContentsContext ¶
ContentsContext returns the file contents, accepting a context for tracing.
type FileIter ¶
type FileIter struct {
// contains filtered or unexported fields
}
FileIter iterates over files in a tree.
type Hash ¶
Hash represents a git object hash (SHA-1).
func HashFromOid ¶
HashFromOid converts a libgit2 Oid to Hash.
type LogOptions ¶
type LogOptions struct {
Since *time.Time // Only include commits after this time.
FirstParent bool // Follow only first parent (git log --first-parent).
Reverse bool // Yield oldest commits first (adds git2go.SortReverse).
}
LogOptions configures the commit log iteration.
type Repository ¶
type Repository struct {
// contains filtered or unexported fields
}
Repository wraps a libgit2 repository.
func LoadRepository ¶
func LoadRepository(uri string) (*Repository, error)
LoadRepository opens a local git repository. Returns an error for remote URIs.
func OpenRepository ¶
func OpenRepository(path string) (*Repository, error)
OpenRepository opens a git repository at the given path.
func (*Repository) CommitCount ¶
func (r *Repository) CommitCount(opts *LogOptions) (int, error)
CommitCount returns the number of commits matching the given log options. It walks the revision history counting OIDs without looking up full commit objects, making it O(N) in time but O(1) in memory. The Reverse option is ignored since ordering doesn't affect the count.
func (*Repository) DiffTreeToTree ¶
func (r *Repository) DiffTreeToTree(oldTree, newTree *Tree) (*Diff, error)
DiffTreeToTree computes the diff between two trees.
func (*Repository) Head ¶
func (r *Repository) Head() (Hash, error)
Head returns the HEAD reference target.
func (*Repository) Log ¶
func (r *Repository) Log(opts *LogOptions) (*CommitIter, error)
Log returns a commit iterator starting from HEAD.
func (*Repository) LookupBlob ¶
LookupBlob returns the blob with the given hash.
func (*Repository) LookupCommit ¶
LookupCommit returns the commit with the given hash.
func (*Repository) LookupTree ¶
func (r *Repository) LookupTree(hash Hash) (*Tree, error)
LookupTree returns the tree with the given hash.
func (*Repository) Native ¶
func (r *Repository) Native() *git2go.Repository
Native returns the underlying libgit2 repository for advanced operations.
func (*Repository) ResolveTime ¶
func (r *Repository) ResolveTime(s string) (time.Time, error)
ResolveTime resolves a time specification that may be a time string (duration, RFC3339, date-only) or a commit SHA/ref. When a SHA/ref is given, the commit's author timestamp is returned.
func (*Repository) Walk ¶
func (r *Repository) Walk() (*RevWalk, error)
Walk creates a new revision walker starting from HEAD.
type RevWalk ¶
type RevWalk struct {
// contains filtered or unexported fields
}
RevWalk wraps a libgit2 revision walker.
func (*RevWalk) Close ¶
func (w *RevWalk) Close()
Close releases the walker resources. Alias for Free to satisfy alg.Iterator.
type Signature ¶
Signature represents a git signature (author/committer).
func TestSignature ¶
TestSignature creates a signature for testing.
type TestCommit ¶
type TestCommit struct {
// contains filtered or unexported fields
}
TestCommit is a mock commit for testing purposes. This is used in unit tests where real git operations are not needed.
func NewTestCommit ¶
func NewTestCommit(hash Hash, author Signature, message string, parentHashes ...Hash) *TestCommit
NewTestCommit creates a new mock commit for testing.
func (*TestCommit) Author ¶
func (m *TestCommit) Author() Signature
Author returns the commit author.
func (*TestCommit) Committer ¶
func (m *TestCommit) Committer() Signature
Committer returns the commit committer.
func (*TestCommit) File ¶
func (m *TestCommit) File(_ string) (*File, error)
File returns an error for TestCommit (not implemented).
func (*TestCommit) Files ¶
func (m *TestCommit) Files() (*FileIter, error)
Files returns an error for TestCommit (not implemented).
func (*TestCommit) Message ¶
func (m *TestCommit) Message() string
Message returns the commit message.
func (*TestCommit) NumParents ¶
func (m *TestCommit) NumParents() int
NumParents returns the number of parent commits.
func (*TestCommit) Parent ¶
func (m *TestCommit) Parent(_ int) (*Commit, error)
Parent returns the nth parent (not implemented for TestCommit).
func (*TestCommit) Tree ¶
func (m *TestCommit) Tree() (*Tree, error)
Tree returns an error for TestCommit (not implemented).
type Tree ¶
type Tree struct {
// contains filtered or unexported fields
}
Tree wraps a libgit2 tree.
func (*Tree) EntryByIndex ¶
EntryByIndex returns the tree entry at the given index.
func (*Tree) EntryByPath ¶
EntryByPath returns the tree entry at the given path.
func (*Tree) EntryCount ¶
EntryCount returns the number of entries in the tree.
func (*Tree) FilesContext ¶
FilesContext returns an iterator over all files in the tree, accepting a context for tracing.
type TreeDiffRequest ¶
type TreeDiffRequest struct {
PreviousTree *Tree // Optimization: Use existing tree if on same worker/repo.
PreviousCommitHash Hash // Fallback: Lookup previous tree by hash (safe for pool workers).
CommitHash Hash // Hash of the commit to process.
Response chan<- TreeDiffResponse
}
TreeDiffRequest asks for a tree diff for a specific commit hash.
type TreeDiffResponse ¶
type TreeDiffResponse struct {
Changes Changes
CurrentTree *Tree // The tree of the processed commit. Caller must Free this or pass it back.
Error error
}
TreeDiffResponse is the response for a TreeDiffRequest.
type TreeEntry ¶
type TreeEntry struct {
// contains filtered or unexported fields
}
TreeEntry wraps a libgit2 tree entry.
type Worker ¶
type Worker struct {
// contains filtered or unexported fields
}
Worker manages exclusive, sequential access to the libgit2 Repository. It ensures all CGO calls happen on a single OS thread.
func NewWorker ¶
func NewWorker(repo *Repository, requests <-chan WorkerRequest) *Worker
NewWorker creates a new Gitlib Worker that consumes from the given channel.
type WorkerRequest ¶
type WorkerRequest interface {
// contains filtered or unexported methods
}
WorkerRequest is the interface for requests handled by the Gitlib Worker.