-
Notifications
You must be signed in to change notification settings - Fork 115
Git Source Handler for MCPRegistry #1925
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
0488965
Update registry from toolhive-registry release v2025.09.11
dmartinol 8d3c49e
git source handler with no auth (for demos)
dmartinol 7e04622
increase test coverage
dmartinol 5491060
lint issues
dmartinol 6579ec4
rebase issue
dmartinol fc90778
removed unused fields
dmartinol 7a01fbe
chart version bump
dmartinol 7cb8125
- Set clone depth to 1 for non-commit-based clones to optimize perfor…
dmartinol 2c0b4b4
Merge branch 'stacklok:main' into registry_githandler
dmartinol 7178bf0
lint issue
dmartinol File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
package git | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
|
||
"github.com/go-git/go-git/v5" | ||
"github.com/go-git/go-git/v5/plumbing" | ||
) | ||
|
||
// Client defines the interface for Git operations | ||
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(repoInfo *RepositoryInfo) error | ||
} | ||
|
||
// DefaultGitClient implements GitClient using go-git | ||
type DefaultGitClient struct{} | ||
|
||
// NewDefaultGitClient creates a new DefaultGitClient | ||
func NewDefaultGitClient() *DefaultGitClient { | ||
return &DefaultGitClient{} | ||
} | ||
|
||
// Clone clones a repository with the given configuration | ||
func (c *DefaultGitClient) Clone(ctx context.Context, config *CloneConfig) (*RepositoryInfo, error) { | ||
// Prepare clone options (no authentication for initial version) | ||
cloneOptions := &git.CloneOptions{ | ||
URL: config.URL, | ||
} | ||
|
||
// Set reference if specified (but not for commit-based clones) | ||
if config.Commit == "" { | ||
cloneOptions.Depth = 1 | ||
if config.Branch != "" { | ||
cloneOptions.ReferenceName = plumbing.NewBranchReferenceName(config.Branch) | ||
cloneOptions.SingleBranch = true | ||
} else if config.Tag != "" { | ||
cloneOptions.ReferenceName = plumbing.NewTagReferenceName(config.Tag) | ||
cloneOptions.SingleBranch = true | ||
} | ||
} | ||
// For commit-based clones, we need the full repository to ensure the commit is available | ||
|
||
// Clone the repository | ||
repo, err := git.PlainCloneContext(ctx, config.Directory, false, cloneOptions) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to clone repository: %w", err) | ||
} | ||
|
||
// Get repository information | ||
repoInfo := &RepositoryInfo{ | ||
Repository: repo, | ||
RemoteURL: config.URL, | ||
} | ||
|
||
// If specific commit is requested, checkout that commit | ||
if config.Commit != "" { | ||
workTree, err := repo.Worktree() | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to get worktree: %w", err) | ||
} | ||
|
||
hash := plumbing.NewHash(config.Commit) | ||
err = workTree.Checkout(&git.CheckoutOptions{ | ||
Hash: hash, | ||
}) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to checkout commit %s: %w", config.Commit, err) | ||
} | ||
} | ||
|
||
// Update repository info with current state | ||
if err := c.updateRepositoryInfo(repoInfo); err != nil { | ||
return nil, fmt.Errorf("failed to update repository info: %w", err) | ||
} | ||
|
||
return repoInfo, nil | ||
} | ||
|
||
// GetFileContent retrieves the content of a file from the repository | ||
func (*DefaultGitClient) GetFileContent(repoInfo *RepositoryInfo, path string) ([]byte, error) { | ||
if repoInfo == nil || repoInfo.Repository == nil { | ||
return nil, fmt.Errorf("repository is nil") | ||
} | ||
|
||
// Get the HEAD reference | ||
ref, err := repoInfo.Repository.Head() | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to get HEAD reference: %w", err) | ||
} | ||
|
||
// Get the commit object | ||
commit, err := repoInfo.Repository.CommitObject(ref.Hash()) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to get commit object: %w", err) | ||
} | ||
|
||
// Get the tree | ||
tree, err := commit.Tree() | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to get tree: %w", err) | ||
} | ||
|
||
// Get the file | ||
file, err := tree.File(path) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to get file %s: %w", path, err) | ||
} | ||
|
||
// Read file contents | ||
content, err := file.Contents() | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to read file contents: %w", err) | ||
} | ||
|
||
return []byte(content), nil | ||
} | ||
|
||
// Cleanup removes local repository directory | ||
func (*DefaultGitClient) Cleanup(repoInfo *RepositoryInfo) error { | ||
if repoInfo == nil || repoInfo.Repository == nil { | ||
return nil | ||
} | ||
|
||
// Get the repository directory from the worktree | ||
workTree, err := repoInfo.Repository.Worktree() | ||
if err != nil { | ||
return fmt.Errorf("failed to get worktree: %w", err) | ||
} | ||
|
||
// Remove the directory | ||
return os.RemoveAll(workTree.Filesystem.Root()) | ||
} | ||
|
||
// updateRepositoryInfo updates the repository info with current state | ||
func (*DefaultGitClient) updateRepositoryInfo(repoInfo *RepositoryInfo) error { | ||
if repoInfo == nil || repoInfo.Repository == nil { | ||
return fmt.Errorf("repository is nil") | ||
} | ||
|
||
// Get current branch name | ||
ref, err := repoInfo.Repository.Head() | ||
if err != nil { | ||
return fmt.Errorf("failed to get HEAD reference: %w", err) | ||
} | ||
|
||
if ref.Name().IsBranch() { | ||
repoInfo.Branch = ref.Name().Short() | ||
} | ||
|
||
return nil | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.