Documentation
¶
Overview ¶
Package git provides Git repository operations for registry sources.
This package implements a thin wrapper around the go-git library to enable registry resources to fetch registry data directly from Git repositories. It supports cloning repositories, checking out specific branches/tags/commits, and retrieving file contents from the repository.
Key Components:
Client Interface ¶
The Client interface defines the core Git operations:
- Clone: Clone public repositories to in-memory filesystem
- GetFileContent: Retrieve specific files from repositories
- Cleanup: Clean up in-memory repository resources
Repository information including commit hashes is tracked via the RepositoryInfo struct which is populated during clone operations.
Example Usage ¶
client := git.NewDefaultGitClient()
config := &git.CloneConfig{
URL: "https://github.com/example/registry.git",
Branch: "main",
Directory: "/tmp/repo",
}
repoInfo, err := client.Clone(ctx, config)
if err != nil {
return err
}
defer client.Cleanup(repoInfo)
content, err := client.GetFileContent(repoInfo, "registry.json")
if err != nil {
return err
}
Security Considerations ¶
This package is designed to be used within a Kubernetes operator environment where Git repositories contain MCP server registry data. Security features include:
- In-memory filesystem operations (no disk access)
- Size limits on cloned repositories (max files and total size)
- Shallow clones by default for efficiency
Future security hardening may include:
- Repository URL validation to prevent SSRF attacks
- Additional resource limits and timeouts
- Secure credential management via Kubernetes secrets for private repos
Implementation Details ¶
Current implementation uses:
- In-memory filesystems (go-billy memfs) for all Git operations
- LimitedFs wrapper to enforce size constraints (10k files, 100MB total)
- Shallow clones (depth=1) for branch/tag checkouts
- Full clones only when specific commits are requested
- Explicit memory cleanup via Cleanup() method with GC hints
Supported features:
- Public repository access via HTTPS
- Branch, tag, and commit checkout
- File content retrieval from any path in the repository
Planned features:
- Authentication for private repositories
- Webhook support for immediate sync triggers
- Git LFS support for large files
Index ¶
- Variables
- func CreateTestRepo(t *testing.T, config TestRepoConfig) (string, func())
- func CreateTestRepoWithBranches(t *testing.T, mainCommit TestRepoConfig, branches map[string]TestRepoConfig) (string, []string, func())
- func CreateTestRepoWithCommits(t *testing.T, commits []TestRepoConfig) (string, []plumbing.Hash, func())
- type Client
- type CloneConfig
- type DefaultGitClient
- type LimitedFs
- func (*LimitedFs) Chroot(_ string) (billy.Filesystem, error)
- func (f *LimitedFs) Create(filename string) (billy.File, error)
- func (f *LimitedFs) Join(elem ...string) string
- func (f *LimitedFs) Lstat(filename string) (fs.FileInfo, error)
- func (f *LimitedFs) MkdirAll(filename string, perm fs.FileMode) error
- func (f *LimitedFs) Open(filename string) (billy.File, error)
- func (f *LimitedFs) OpenFile(filename string, flag int, perm fs.FileMode) (billy.File, error)
- func (f *LimitedFs) ReadDir(path string) ([]fs.FileInfo, error)
- func (f *LimitedFs) Readlink(link string) (string, error)
- func (f *LimitedFs) Remove(filename string) error
- func (f *LimitedFs) Rename(oldpath string, newpath string) error
- func (f *LimitedFs) Root() string
- func (f *LimitedFs) Stat(filename string) (fs.FileInfo, error)
- func (f *LimitedFs) Symlink(target string, link string) error
- func (f *LimitedFs) TempFile(dir string, prefix string) (billy.File, error)
- type RepositoryInfo
- type TestRepoConfig
Constants ¶
This section is empty.
Variables ¶
var ErrNotImplemented = fmt.Errorf("not implemented")
ErrNotImplemented is returned when a method is not implemented.
var ErrTooBig = fmt.Errorf("file too big")
ErrTooBig is returned when a file is too big.
var ErrTooManyFiles = fmt.Errorf("too many files")
ErrTooManyFiles is returned when there are too many files.
Functions ¶
func CreateTestRepo ¶
func CreateTestRepo(t *testing.T, config TestRepoConfig) (string, func())
CreateTestRepo creates a temporary Git repository with the specified files and commits Returns the repository path and a cleanup function
func CreateTestRepoWithBranches ¶
func CreateTestRepoWithBranches( t *testing.T, mainCommit TestRepoConfig, branches map[string]TestRepoConfig, ) (string, []string, func())
CreateTestRepoWithBranches creates a test repository with multiple branches The first commit creates the main branch, subsequent commits create additional branches Returns the repository path, branch names, and cleanup function
func CreateTestRepoWithCommits ¶
func CreateTestRepoWithCommits(t *testing.T, commits []TestRepoConfig) (string, []plumbing.Hash, func())
CreateTestRepoWithCommits creates a test repository with multiple commits Returns the repository path, commit hashes, and cleanup function
Types ¶
type Client ¶
type Client interface {
// Clone clones a repository with the given configuration
Clone(ctx context.Context, config *CloneConfig) (*RepositoryInfo, error)
// GetFileContent retrieves the content of a file from the repository
GetFileContent(repoInfo *RepositoryInfo, path string) ([]byte, error)
// Cleanup removes local repository directory
Cleanup(ctx context.Context, repoInfo *RepositoryInfo) error
}
Client defines the interface for Git operations
type CloneConfig ¶
type CloneConfig struct {
// URL is the repository URL to clone
URL string
// Branch is the specific branch to clone (optional)
Branch string
// Tag is the specific tag to clone (optional)
Tag string
// Commit is the specific commit to clone (optional)
Commit string
}
CloneConfig contains configuration for cloning a repository
type DefaultGitClient ¶
type DefaultGitClient struct{}
DefaultGitClient implements GitClient using go-git
func NewDefaultGitClient ¶
func NewDefaultGitClient() *DefaultGitClient
NewDefaultGitClient creates a new DefaultGitClient
func (*DefaultGitClient) Cleanup ¶
func (*DefaultGitClient) Cleanup(ctx context.Context, repoInfo *RepositoryInfo) error
Cleanup removes local repository directory
func (*DefaultGitClient) Clone ¶
func (c *DefaultGitClient) Clone(ctx context.Context, config *CloneConfig) (*RepositoryInfo, error)
Clone clones a repository with the given configuration
func (*DefaultGitClient) GetFileContent ¶
func (*DefaultGitClient) GetFileContent(repoInfo *RepositoryInfo, path string) ([]byte, error)
GetFileContent retrieves the content of a file from the repository
type LimitedFs ¶
type LimitedFs struct {
Fs billy.Filesystem
MaxFiles int64
TotalFileSize int64
// contains filtered or unexported fields
}
LimitedFs provides a size-limited billy.Filesystem. This is a struct, there's no constructor here. Note that LimitedFs is not thread-safe.
func (*LimitedFs) Chroot ¶
func (*LimitedFs) Chroot(_ string) (billy.Filesystem, error)
Chroot implements billy.Filesystem.
type RepositoryInfo ¶
type RepositoryInfo struct {
// Repository is the go-git repository instance
Repository *git.Repository
// Branch is the current branch name
Branch string
// RemoteURL is the remote repository URL
RemoteURL string
// contains filtered or unexported fields
}
RepositoryInfo contains information about a Git repository