2017-10-15 17:24:40 +00:00
|
|
|
package git
|
|
|
|
|
|
|
|
import (
|
2018-03-15 21:22:05 +00:00
|
|
|
"net/url"
|
|
|
|
"strings"
|
|
|
|
|
2017-10-15 17:24:40 +00:00
|
|
|
"gopkg.in/src-d/go-git.v4"
|
2018-07-24 14:11:35 +00:00
|
|
|
"gopkg.in/src-d/go-git.v4/plumbing"
|
2017-10-15 17:24:40 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Service represents a service for managing Git.
|
|
|
|
type Service struct{}
|
|
|
|
|
|
|
|
// NewService initializes a new service.
|
|
|
|
func NewService(dataStorePath string) (*Service, error) {
|
|
|
|
service := &Service{}
|
|
|
|
|
|
|
|
return service, nil
|
|
|
|
}
|
|
|
|
|
2018-03-15 21:22:05 +00:00
|
|
|
// ClonePublicRepository clones a public git repository using the specified URL in the specified
|
2017-10-15 17:24:40 +00:00
|
|
|
// destination folder.
|
2018-07-24 14:11:35 +00:00
|
|
|
func (service *Service) ClonePublicRepository(repositoryURL, referenceName string, destination string) error {
|
|
|
|
return cloneRepository(repositoryURL, referenceName, destination)
|
2018-03-15 21:22:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ClonePrivateRepositoryWithBasicAuth clones a private git repository using the specified URL in the specified
|
|
|
|
// destination folder. It will use the specified username and password for basic HTTP authentication.
|
2018-07-24 14:11:35 +00:00
|
|
|
func (service *Service) ClonePrivateRepositoryWithBasicAuth(repositoryURL, referenceName string, destination, username, password string) error {
|
2018-03-15 21:22:05 +00:00
|
|
|
credentials := username + ":" + url.PathEscape(password)
|
|
|
|
repositoryURL = strings.Replace(repositoryURL, "://", "://"+credentials+"@", 1)
|
2018-07-24 14:11:35 +00:00
|
|
|
return cloneRepository(repositoryURL, referenceName, destination)
|
2018-03-15 21:22:05 +00:00
|
|
|
}
|
|
|
|
|
2018-07-24 14:11:35 +00:00
|
|
|
func cloneRepository(repositoryURL, referenceName string, destination string) error {
|
|
|
|
options := &git.CloneOptions{
|
2018-03-15 21:22:05 +00:00
|
|
|
URL: repositoryURL,
|
2018-07-24 14:11:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if referenceName != "" {
|
|
|
|
options.ReferenceName = plumbing.ReferenceName(referenceName)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := git.PlainClone(destination, false, options)
|
2017-10-15 17:24:40 +00:00
|
|
|
return err
|
|
|
|
}
|