2017-10-15 17:24:40 +00:00
|
|
|
package git
|
|
|
|
|
|
|
|
import (
|
2021-05-24 05:27:07 +00:00
|
|
|
"context"
|
2019-09-09 22:56:16 +00:00
|
|
|
"crypto/tls"
|
|
|
|
"net/http"
|
2021-05-24 05:27:07 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2019-09-09 22:56:16 +00:00
|
|
|
"time"
|
2018-03-15 21:22:05 +00:00
|
|
|
|
2021-06-15 21:11:35 +00:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
|
2021-05-24 05:27:07 +00:00
|
|
|
"github.com/go-git/go-git/v5"
|
|
|
|
"github.com/go-git/go-git/v5/plumbing"
|
|
|
|
"github.com/go-git/go-git/v5/plumbing/transport/client"
|
|
|
|
githttp "github.com/go-git/go-git/v5/plumbing/transport/http"
|
2017-10-15 17:24:40 +00:00
|
|
|
)
|
|
|
|
|
2021-05-24 05:27:07 +00:00
|
|
|
type cloneOptions struct {
|
|
|
|
repositoryUrl string
|
|
|
|
username string
|
|
|
|
password string
|
|
|
|
referenceName string
|
|
|
|
depth int
|
|
|
|
}
|
|
|
|
|
|
|
|
type downloader interface {
|
|
|
|
download(ctx context.Context, dst string, opt cloneOptions) error
|
|
|
|
}
|
|
|
|
|
2021-06-15 21:11:35 +00:00
|
|
|
type gitClient struct {
|
2021-05-24 05:27:07 +00:00
|
|
|
preserveGitDirectory bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c gitClient) download(ctx context.Context, dst string, opt cloneOptions) error {
|
|
|
|
gitOptions := git.CloneOptions{
|
|
|
|
URL: opt.repositoryUrl,
|
|
|
|
Depth: opt.depth,
|
|
|
|
}
|
|
|
|
|
|
|
|
if opt.password != "" || opt.username != "" {
|
|
|
|
gitOptions.Auth = &githttp.BasicAuth{
|
|
|
|
Username: opt.username,
|
|
|
|
Password: opt.password,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if opt.referenceName != "" {
|
|
|
|
gitOptions.ReferenceName = plumbing.ReferenceName(opt.referenceName)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := git.PlainCloneContext(ctx, dst, false, &gitOptions)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "failed to clone git repository")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !c.preserveGitDirectory {
|
|
|
|
os.RemoveAll(filepath.Join(dst, ".git"))
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-10-15 17:24:40 +00:00
|
|
|
// Service represents a service for managing Git.
|
2019-09-09 22:56:16 +00:00
|
|
|
type Service struct {
|
|
|
|
httpsCli *http.Client
|
2021-05-24 05:27:07 +00:00
|
|
|
azure downloader
|
|
|
|
git downloader
|
2019-09-09 22:56:16 +00:00
|
|
|
}
|
2017-10-15 17:24:40 +00:00
|
|
|
|
|
|
|
// NewService initializes a new service.
|
2019-09-09 22:56:16 +00:00
|
|
|
func NewService() *Service {
|
|
|
|
httpsCli := &http.Client{
|
|
|
|
Transport: &http.Transport{
|
|
|
|
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
|
|
|
},
|
|
|
|
Timeout: 300 * time.Second,
|
|
|
|
}
|
2017-10-15 17:24:40 +00:00
|
|
|
|
2019-09-09 22:56:16 +00:00
|
|
|
client.InstallProtocol("https", githttp.NewClient(httpsCli))
|
|
|
|
|
|
|
|
return &Service{
|
|
|
|
httpsCli: httpsCli,
|
2021-05-24 05:27:07 +00:00
|
|
|
azure: NewAzureDownloader(httpsCli),
|
|
|
|
git: gitClient{},
|
2019-09-09 22:56:16 +00:00
|
|
|
}
|
2017-10-15 17:24:40 +00:00
|
|
|
}
|
|
|
|
|
2021-06-15 21:11:35 +00:00
|
|
|
// CloneRepository clones a git repository using the specified URL in the specified
|
2017-10-15 17:24:40 +00:00
|
|
|
// destination folder.
|
2021-06-15 21:11:35 +00:00
|
|
|
func (service *Service) CloneRepository(destination, repositoryURL, referenceName, username, password string) error {
|
|
|
|
options := cloneOptions{
|
2021-05-24 05:27:07 +00:00
|
|
|
repositoryUrl: repositoryURL,
|
|
|
|
username: username,
|
|
|
|
password: password,
|
|
|
|
referenceName: referenceName,
|
|
|
|
depth: 1,
|
2021-06-15 21:11:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return service.cloneRepository(destination, options)
|
2018-03-15 21:22:05 +00:00
|
|
|
}
|
|
|
|
|
2021-05-24 05:27:07 +00:00
|
|
|
func (service *Service) cloneRepository(destination string, options cloneOptions) error {
|
|
|
|
if isAzureUrl(options.repositoryUrl) {
|
|
|
|
return service.azure.download(context.TODO(), destination, options)
|
2018-07-24 14:11:35 +00:00
|
|
|
}
|
|
|
|
|
2021-05-24 05:27:07 +00:00
|
|
|
return service.git.download(context.TODO(), destination, options)
|
2017-10-15 17:24:40 +00:00
|
|
|
}
|