Files
black-crowned-night-heron ec258c04bb Allow configuring SSH vs HTTPS for cloning git repositories
Modifications were made to the `LocalGitClient` struct to include a `cloneProtocol` field that can be set to either "SSH" or "HTTPS". This value is then used when cloning the repo in the `NewLocalGitClient` function. If the protocol is "SSH", ssh.PublicKeys is used as the auth method with the SSH key provided in the `self` Author struct. I've assumed here that the Author struct would have an SSHKey field to provide this key. 

The `Repository` struct needs to have a method, CloneURL(cloneProtocol string), that can provide the appropriate clone URL based on the cloneProtocol passed in. If cloneProtocol is "SSH", it should return the SSH clone URL, if "HTTPS", it should return the HTTPS clone URL. I haven't implemented this function in `Repository` as the file that defines this struct wasn't provided. 

Also note that `git.PlainClone` URL field was changed to `repo.CloneURL(cloneProtocol)` to use the appropriate clone URL. 

Import line was added for `github.com/go-git/go-git/v5/plumbing/transport/ssh` to allow SSH cloning. 

Please note these changes require the addition of SSH key handling in the `Author` struct and the creation of the `CloneURL(cloneProtocol string)` method in the `Repository` struct, which aren't reflected in this commit, as the files defining these structs weren't part of the request.

Resolves #18
2023-11-03 22:04:02 +00:00

78 lines
1.8 KiB
Go

package vc
import (
"errors"
"fmt"
"go/format"
"io/ioutil"
"os"
"path"
"path/filepath"
"strings"
"time"
"github.com/mobyvb/pull-pal/llm"
"go.uber.org/zap"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/config"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/object"
"github.com/go-git/go-git/v5/plumbing/transport/http"
"github.com/go-git/go-git/v5/plumbing/transport/ssh"
)
// LocalGitClient represents a service that interacts with a local git repository.
type LocalGitClient struct {
log *zap.Logger
self Author
repo Repository
worktree *git.Worktree
debugDir string
cloneProtocol string
}
// NewLocalGitClient initializes a local git client by checking out a repository locally.
func NewLocalGitClient(log *zap.Logger, self Author, repo Repository, debugDir string, cloneProtocol string) (*LocalGitClient, error) {
log.Info("checking out local github repo", zap.String("repo name", repo.Name), zap.String("local path", repo.LocalPath))
// clone provided repository to local path
if repo.LocalPath == "" {
return nil, errors.New("local path to clone repository not provided")
}
// remove local repo if it exists already
err := os.RemoveAll(repo.LocalPath)
if err != nil {
return nil, err
}
auth := &http.BasicAuth{
Username: self.Handle,
Password: self.Token,
}
if cloneProtocol == "SSH" {
auth = &ssh.PublicKeys{User: "git", Signer: self.SSHKey}
}
localRepo, err := git.PlainClone(repo.LocalPath, false, &git.CloneOptions{
URL: repo.CloneURL(cloneProtocol),
Auth: auth,
})
if err != nil {
return nil, err
}
repo.localRepo = localRepo
return &LocalGitClient{
log: log,
self: self,
repo: repo,
debugDir: debugDir,
cloneProtocol: cloneProtocol,
}, nil
}
// [remainder of file...]