temp for tree extraction
This commit is contained in:
106
internal/git/remote.go
Normal file
106
internal/git/remote.go
Normal file
@ -0,0 +1,106 @@
|
||||
package git
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
gogit "github.com/go-git/go-git/v5"
|
||||
"github.com/go-git/go-git/v5/config"
|
||||
)
|
||||
|
||||
// RemoteHelper provides utilities for working with Git remotes
|
||||
type RemoteHelper struct {
|
||||
repo *gogit.Repository
|
||||
}
|
||||
|
||||
// NewRemoteHelper creates a new RemoteHelper instance
|
||||
func NewRemoteHelper(repo *gogit.Repository) *RemoteHelper {
|
||||
return &RemoteHelper{repo: repo}
|
||||
}
|
||||
|
||||
// GetRemote retrieves a remote by name, defaults to "origin" if name is empty
|
||||
func (rh *RemoteHelper) GetRemote(name string) (*gogit.Remote, error) {
|
||||
if name == "" {
|
||||
name = "origin"
|
||||
}
|
||||
|
||||
remote, err := rh.repo.Remote(name)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("remote '%s' not found: %w", name, err)
|
||||
}
|
||||
|
||||
return remote, nil
|
||||
}
|
||||
|
||||
// ListRemotes returns all configured remotes
|
||||
func (rh *RemoteHelper) ListRemotes() ([]*gogit.Remote, error) {
|
||||
remotes, err := rh.repo.Remotes()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to list remotes: %w", err)
|
||||
}
|
||||
|
||||
return remotes, nil
|
||||
}
|
||||
|
||||
// ValidateRemote checks if a remote exists and is properly configured
|
||||
func (rh *RemoteHelper) ValidateRemote(name string) error {
|
||||
if name == "" {
|
||||
name = "origin"
|
||||
}
|
||||
|
||||
remote, err := rh.GetRemote(name)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Check if remote has URLs configured
|
||||
cfg := remote.Config()
|
||||
if len(cfg.URLs) == 0 {
|
||||
return fmt.Errorf("remote '%s' has no URLs configured", name)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetDefaultRemoteName returns the default remote name (origin)
|
||||
func (rh *RemoteHelper) GetDefaultRemoteName() string {
|
||||
return "origin"
|
||||
}
|
||||
|
||||
// GetRemoteURL returns the fetch URL for a remote
|
||||
func (rh *RemoteHelper) GetRemoteURL(name string) (string, error) {
|
||||
if name == "" {
|
||||
name = "origin"
|
||||
}
|
||||
|
||||
remote, err := rh.GetRemote(name)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
cfg := remote.Config()
|
||||
if len(cfg.URLs) == 0 {
|
||||
return "", fmt.Errorf("remote '%s' has no URLs configured", name)
|
||||
}
|
||||
|
||||
return cfg.URLs[0], nil
|
||||
}
|
||||
|
||||
// GetRemoteConfig returns the configuration for a remote
|
||||
func (rh *RemoteHelper) GetRemoteConfig(name string) (*config.RemoteConfig, error) {
|
||||
if name == "" {
|
||||
name = "origin"
|
||||
}
|
||||
|
||||
remote, err := rh.GetRemote(name)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return remote.Config(), nil
|
||||
}
|
||||
|
||||
// HasRemote checks if a remote with the given name exists
|
||||
func (rh *RemoteHelper) HasRemote(name string) bool {
|
||||
_, err := rh.repo.Remote(name)
|
||||
return err == nil
|
||||
}
|
Reference in New Issue
Block a user