git

package
v1.1.3 Latest Latest
Warning

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

Go to latest
Published: Mar 6, 2026 License: Apache-2.0 Imports: 19 Imported by: 0

Documentation

Overview

Package git provides Git repository infrastructure implementations.

Index

Constants

This section is empty.

Variables

View Source
var ErrBranchNotFound = errors.New("branch not found")

ErrBranchNotFound indicates the requested branch was not found.

View Source
var ErrFileNotFound = errors.New("file not found")

ErrFileNotFound indicates the requested file does not exist in the repository at the given commit.

Functions

This section is empty.

Types

type Adapter

type Adapter interface {
	// CloneRepository clones a repository to local path.
	CloneRepository(ctx context.Context, remoteURI string, localPath string) error

	// CheckoutCommit checks out a specific commit.
	CheckoutCommit(ctx context.Context, localPath string, commitSHA string) error

	// CheckoutBranch checks out a specific branch.
	CheckoutBranch(ctx context.Context, localPath string, branchName string) error

	// FetchRepository fetches latest changes for existing repository.
	FetchRepository(ctx context.Context, localPath string) error

	// PullRepository pulls latest changes for existing repository.
	PullRepository(ctx context.Context, localPath string) error

	// AllBranches returns all branches in repository.
	AllBranches(ctx context.Context, localPath string) ([]BranchInfo, error)

	// BranchCommits returns commit history for a specific branch.
	BranchCommits(ctx context.Context, localPath string, branchName string) ([]CommitInfo, error)

	// AllCommitsBulk returns all commits from all branches in bulk.
	AllCommitsBulk(ctx context.Context, localPath string, since *time.Time) (map[string]CommitInfo, error)

	// BranchCommitSHAs returns only commit SHAs for a branch.
	BranchCommitSHAs(ctx context.Context, localPath string, branchName string) ([]string, error)

	// AllBranchHeadSHAs returns head commit SHAs for all branches in one operation.
	AllBranchHeadSHAs(ctx context.Context, localPath string, branchNames []string) (map[string]string, error)

	// CommitFiles returns all files in a specific commit from the git tree.
	CommitFiles(ctx context.Context, localPath string, commitSHA string) ([]FileInfo, error)

	// RepositoryExists checks if repository exists at local path.
	RepositoryExists(ctx context.Context, localPath string) (bool, error)

	// CommitDetails returns detailed information about a specific commit.
	CommitDetails(ctx context.Context, localPath string, commitSHA string) (CommitInfo, error)

	// EnsureRepository clones if doesn't exist, otherwise fetches latest changes.
	EnsureRepository(ctx context.Context, remoteURI string, localPath string) error

	// FileContent returns file content at specific commit.
	FileContent(ctx context.Context, localPath string, commitSHA string, filePath string) ([]byte, error)

	// DefaultBranch returns the default branch name with fallback strategies.
	DefaultBranch(ctx context.Context, localPath string) (string, error)

	// LatestCommitSHA returns the latest commit SHA for a branch.
	LatestCommitSHA(ctx context.Context, localPath string, branchName string) (string, error)

	// AllTags returns all tags in repository.
	AllTags(ctx context.Context, localPath string) ([]TagInfo, error)

	// CommitDiff returns the diff for a specific commit.
	CommitDiff(ctx context.Context, localPath string, commitSHA string) (string, error)

	// Grep searches for a pattern in tracked files at a specific commit using git grep.
	Grep(ctx context.Context, localPath string, commitSHA string, pattern string, pathspec string, maxMatches int) ([]GrepMatch, error)
}

Adapter defines the interface for Git repository operations. Implementations wrap specific git libraries (e.g., Gitea's git module).

type BranchInfo

type BranchInfo struct {
	Name      string
	HeadSHA   string
	IsDefault bool
}

BranchInfo holds branch metadata returned from the adapter.

type CommitInfo

type CommitInfo struct {
	SHA            string
	Message        string
	AuthorName     string
	AuthorEmail    string
	CommitterName  string
	CommitterEmail string
	AuthoredAt     time.Time
	CommittedAt    time.Time
	ParentSHA      string
}

CommitInfo holds commit metadata returned from the adapter.

type FileInfo

type FileInfo struct {
	Path     string
	BlobSHA  string
	Size     int64
	MimeType string
}

FileInfo holds file metadata returned from the adapter.

type GiteaAdapter

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

GiteaAdapter implements Adapter using Gitea's git module (native git binary).

func NewGiteaAdapter

func NewGiteaAdapter(logger zerolog.Logger) (*GiteaAdapter, error)

NewGiteaAdapter creates a new GiteaAdapter. It initializes the Gitea git module once (verifying the git binary is available).

func (*GiteaAdapter) AllBranchHeadSHAs

func (g *GiteaAdapter) AllBranchHeadSHAs(ctx context.Context, localPath string, branchNames []string) (map[string]string, error)

AllBranchHeadSHAs returns head commit SHAs for all branches in one operation.

func (*GiteaAdapter) AllBranches

func (g *GiteaAdapter) AllBranches(ctx context.Context, localPath string) ([]BranchInfo, error)

AllBranches returns all branches in repository.

func (*GiteaAdapter) AllCommitsBulk

func (g *GiteaAdapter) AllCommitsBulk(ctx context.Context, localPath string, since *time.Time) (map[string]CommitInfo, error)

AllCommitsBulk returns all commits from all branches in bulk.

func (*GiteaAdapter) AllTags

func (g *GiteaAdapter) AllTags(ctx context.Context, localPath string) ([]TagInfo, error)

AllTags returns all tags in repository.

func (*GiteaAdapter) BranchCommitSHAs

func (g *GiteaAdapter) BranchCommitSHAs(ctx context.Context, localPath string, branchName string) ([]string, error)

BranchCommitSHAs returns only commit SHAs for a branch.

func (*GiteaAdapter) BranchCommits

func (g *GiteaAdapter) BranchCommits(ctx context.Context, localPath string, branchName string) ([]CommitInfo, error)

BranchCommits returns commit history for a specific branch.

func (*GiteaAdapter) CheckoutBranch

func (g *GiteaAdapter) CheckoutBranch(ctx context.Context, localPath string, branchName string) error

CheckoutBranch checks out a specific branch.

func (*GiteaAdapter) CheckoutCommit

func (g *GiteaAdapter) CheckoutCommit(ctx context.Context, localPath string, commitSHA string) error

CheckoutCommit checks out a specific commit.

func (*GiteaAdapter) CloneRepository

func (g *GiteaAdapter) CloneRepository(ctx context.Context, remoteURI string, localPath string) error

CloneRepository clones a repository to local path.

func (*GiteaAdapter) CommitDetails

func (g *GiteaAdapter) CommitDetails(ctx context.Context, localPath string, commitSHA string) (CommitInfo, error)

CommitDetails returns detailed information about a specific commit.

func (*GiteaAdapter) CommitDiff

func (g *GiteaAdapter) CommitDiff(ctx context.Context, localPath string, commitSHA string) (string, error)

CommitDiff returns the diff for a specific commit.

func (*GiteaAdapter) CommitFiles

func (g *GiteaAdapter) CommitFiles(ctx context.Context, localPath string, commitSHA string) ([]FileInfo, error)

CommitFiles returns all files in a specific commit from the git tree.

func (*GiteaAdapter) DefaultBranch

func (g *GiteaAdapter) DefaultBranch(ctx context.Context, localPath string) (string, error)

DefaultBranch returns the default branch name with fallback strategies.

func (*GiteaAdapter) EnsureRepository

func (g *GiteaAdapter) EnsureRepository(ctx context.Context, remoteURI string, localPath string) error

EnsureRepository clones if doesn't exist, otherwise fetches latest changes.

func (*GiteaAdapter) FetchRepository

func (g *GiteaAdapter) FetchRepository(ctx context.Context, localPath string) error

FetchRepository fetches latest changes for existing repository.

func (*GiteaAdapter) FileContent

func (g *GiteaAdapter) FileContent(ctx context.Context, localPath string, commitSHA string, filePath string) ([]byte, error)

FileContent returns file content at specific commit.

func (*GiteaAdapter) Grep

func (g *GiteaAdapter) Grep(ctx context.Context, localPath string, commitSHA string, pattern string, pathspec string, maxMatches int) ([]GrepMatch, error)

Grep searches for a pattern in tracked files at a specific commit using Gitea's native GrepSearch API (which wraps git grep internally).

func (*GiteaAdapter) LatestCommitSHA

func (g *GiteaAdapter) LatestCommitSHA(ctx context.Context, localPath string, branchName string) (string, error)

LatestCommitSHA returns the latest commit SHA for a branch.

func (*GiteaAdapter) PullRepository

func (g *GiteaAdapter) PullRepository(ctx context.Context, localPath string) error

PullRepository pulls latest changes for existing repository.

func (*GiteaAdapter) RepositoryExists

func (g *GiteaAdapter) RepositoryExists(ctx context.Context, localPath string) (bool, error)

RepositoryExists checks if repository exists at local path.

type GrepMatch

type GrepMatch struct {
	Path    string
	Line    int
	Content string
}

GrepMatch holds a single line match from git grep.

type IgnorePattern

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

IgnorePattern provides file ignore pattern matching for git repositories. It combines gitignore rules from the repository with custom .noindex patterns.

func NewIgnorePattern

func NewIgnorePattern(base string) (IgnorePattern, error)

NewIgnorePattern creates an IgnorePattern for the given base directory. Returns an error if the base directory does not exist or is not a directory.

func (IgnorePattern) ShouldIgnore

func (p IgnorePattern) ShouldIgnore(path string) bool

ShouldIgnore checks if a path should be ignored. Directories are never ignored. Files are ignored if they match gitignore rules or .noindex patterns.

type NotDirectoryError

type NotDirectoryError struct {
	Path string
}

NotDirectoryError indicates the path is not a directory.

func (*NotDirectoryError) Error

func (e *NotDirectoryError) Error() string

type RepositoryCloner

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

RepositoryCloner handles repository cloning and updating operations. Implements domain/service.Cloner interface.

func NewRepositoryCloner

func NewRepositoryCloner(adapter Adapter, cloneDir string, logger zerolog.Logger) *RepositoryCloner

NewRepositoryCloner creates a new RepositoryCloner with the specified adapter and clone directory.

func (*RepositoryCloner) Clone

func (c *RepositoryCloner) Clone(ctx context.Context, remoteURI string) (string, error)

Clone clones a repository and returns the local path.

func (*RepositoryCloner) ClonePathFromURI

func (c *RepositoryCloner) ClonePathFromURI(uri string) string

ClonePathFromURI returns the local clone path for a given repository URI.

func (*RepositoryCloner) CloneToPath

func (c *RepositoryCloner) CloneToPath(ctx context.Context, remoteURI string, clonePath string) error

CloneToPath clones a repository to a specific path.

func (*RepositoryCloner) Ensure

func (c *RepositoryCloner) Ensure(ctx context.Context, remoteURI string) (string, error)

Ensure clones the repository if it doesn't exist, otherwise pulls latest changes.

func (*RepositoryCloner) Update

Update updates a repository based on its tracking configuration. Returns the actual clone path used, which may differ from the stored path if the repository was relocated (e.g. after migration).

type RepositoryScanner

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

RepositoryScanner extracts data from Git repositories without mutation. Implements domain/service.Scanner interface.

func NewRepositoryScanner

func NewRepositoryScanner(adapter Adapter, logger zerolog.Logger) *RepositoryScanner

NewRepositoryScanner creates a new RepositoryScanner with the specified adapter.

func (*RepositoryScanner) FilesForCommitsBatch

func (s *RepositoryScanner) FilesForCommitsBatch(ctx context.Context, clonedPath string, commitSHAs []string) ([]repository.File, error)

FilesForCommitsBatch processes files for a batch of commits. Reuses adapter resources efficiently for large batches.

func (*RepositoryScanner) ScanAllBranches

func (s *RepositoryScanner) ScanAllBranches(ctx context.Context, clonedPath string, repoID int64) ([]repository.Branch, error)

ScanAllBranches scans metadata for all branches.

func (*RepositoryScanner) ScanAllTags

func (s *RepositoryScanner) ScanAllTags(ctx context.Context, clonedPath string, repoID int64) ([]repository.Tag, error)

ScanAllTags scans metadata for all tags.

func (*RepositoryScanner) ScanBranch

func (s *RepositoryScanner) ScanBranch(ctx context.Context, clonedPath string, branchName string, repoID int64) ([]repository.Commit, error)

ScanBranch scans all commits on a branch.

func (*RepositoryScanner) ScanCommit

func (s *RepositoryScanner) ScanCommit(ctx context.Context, clonedPath string, commitSHA string, repoID int64) (service.ScanCommitResult, error)

ScanCommit scans a specific commit and returns commit with its files.

type TagInfo

type TagInfo struct {
	Name            string
	TargetCommitSHA string
	Message         string
	TaggerName      string
	TaggerEmail     string
	TaggedAt        time.Time
}

TagInfo holds tag metadata returned from the adapter.

Jump to

Keyboard shortcuts

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