2017-10-15 17:24:40 +00:00
|
|
|
package git
|
|
|
|
|
|
|
|
import (
|
2021-05-24 05:27:07 +00:00
|
|
|
"context"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2021-08-17 01:12:07 +00:00
|
|
|
"strings"
|
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"
|
2021-08-17 01:12:07 +00:00
|
|
|
"github.com/go-git/go-git/v5/config"
|
2021-05-24 05:27:07 +00:00
|
|
|
"github.com/go-git/go-git/v5/plumbing"
|
2022-09-21 05:47:02 +00:00
|
|
|
"github.com/go-git/go-git/v5/plumbing/object"
|
2021-05-24 05:27:07 +00:00
|
|
|
githttp "github.com/go-git/go-git/v5/plumbing/transport/http"
|
2021-08-17 01:12:07 +00:00
|
|
|
"github.com/go-git/go-git/v5/storage/memory"
|
2017-10-15 17:24:40 +00:00
|
|
|
)
|
|
|
|
|
2021-06-15 21:11:35 +00:00
|
|
|
type gitClient struct {
|
2021-05-24 05:27:07 +00:00
|
|
|
preserveGitDirectory bool
|
|
|
|
}
|
|
|
|
|
2022-09-21 05:47:02 +00:00
|
|
|
func NewGitClient(preserveGitDir bool) *gitClient {
|
|
|
|
return &gitClient{
|
|
|
|
preserveGitDirectory: preserveGitDir,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *gitClient) download(ctx context.Context, dst string, opt cloneOption) error {
|
2021-05-24 05:27:07 +00:00
|
|
|
gitOptions := git.CloneOptions{
|
|
|
|
URL: opt.repositoryUrl,
|
|
|
|
Depth: opt.depth,
|
2021-08-17 01:12:07 +00:00
|
|
|
Auth: getAuth(opt.username, opt.password),
|
2021-05-24 05:27:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if opt.referenceName != "" {
|
|
|
|
gitOptions.ReferenceName = plumbing.ReferenceName(opt.referenceName)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := git.PlainCloneContext(ctx, dst, false, &gitOptions)
|
|
|
|
|
|
|
|
if err != nil {
|
2022-09-15 04:32:05 +00:00
|
|
|
if err.Error() == "authentication required" {
|
|
|
|
return ErrAuthenticationFailure
|
|
|
|
}
|
2021-05-24 05:27:07 +00:00
|
|
|
return errors.Wrap(err, "failed to clone git repository")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !c.preserveGitDirectory {
|
|
|
|
os.RemoveAll(filepath.Join(dst, ".git"))
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-09-21 05:47:02 +00:00
|
|
|
func (c *gitClient) latestCommitID(ctx context.Context, opt fetchOption) (string, error) {
|
2021-08-17 01:12:07 +00:00
|
|
|
remote := git.NewRemote(memory.NewStorage(), &config.RemoteConfig{
|
|
|
|
Name: "origin",
|
|
|
|
URLs: []string{opt.repositoryUrl},
|
|
|
|
})
|
|
|
|
|
|
|
|
listOptions := &git.ListOptions{
|
|
|
|
Auth: getAuth(opt.username, opt.password),
|
|
|
|
}
|
|
|
|
|
|
|
|
refs, err := remote.List(listOptions)
|
|
|
|
if err != nil {
|
2022-09-15 04:32:05 +00:00
|
|
|
if err.Error() == "authentication required" {
|
|
|
|
return "", ErrAuthenticationFailure
|
|
|
|
}
|
2021-08-17 01:12:07 +00:00
|
|
|
return "", errors.Wrap(err, "failed to list repository refs")
|
|
|
|
}
|
|
|
|
|
2022-05-15 21:35:11 +00:00
|
|
|
referenceName := opt.referenceName
|
|
|
|
if referenceName == "" {
|
|
|
|
for _, ref := range refs {
|
|
|
|
if strings.EqualFold(ref.Name().String(), "HEAD") {
|
|
|
|
referenceName = ref.Target().String()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-17 01:12:07 +00:00
|
|
|
for _, ref := range refs {
|
2022-05-15 21:35:11 +00:00
|
|
|
if strings.EqualFold(ref.Name().String(), referenceName) {
|
2021-08-17 01:12:07 +00:00
|
|
|
return ref.Hash().String(), nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return "", errors.Errorf("could not find ref %q in the repository", opt.referenceName)
|
|
|
|
}
|
|
|
|
|
|
|
|
func getAuth(username, password string) *githttp.BasicAuth {
|
|
|
|
if password != "" {
|
|
|
|
if username == "" {
|
|
|
|
username = "token"
|
|
|
|
}
|
|
|
|
|
|
|
|
return &githttp.BasicAuth{
|
|
|
|
Username: username,
|
|
|
|
Password: password,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-09-21 05:47:02 +00:00
|
|
|
func (c *gitClient) listRefs(ctx context.Context, opt baseOption) ([]string, error) {
|
|
|
|
rem := git.NewRemote(memory.NewStorage(), &config.RemoteConfig{
|
|
|
|
Name: "origin",
|
|
|
|
URLs: []string{opt.repositoryUrl},
|
|
|
|
})
|
2017-10-15 17:24:40 +00:00
|
|
|
|
2022-09-21 05:47:02 +00:00
|
|
|
listOptions := &git.ListOptions{
|
|
|
|
Auth: getAuth(opt.username, opt.password),
|
2019-09-09 22:56:16 +00:00
|
|
|
}
|
2017-10-15 17:24:40 +00:00
|
|
|
|
2022-09-21 05:47:02 +00:00
|
|
|
refs, err := rem.List(listOptions)
|
|
|
|
if err != nil {
|
|
|
|
return nil, checkGitError(err)
|
2019-09-09 22:56:16 +00:00
|
|
|
}
|
2017-10-15 17:24:40 +00:00
|
|
|
|
2022-09-21 05:47:02 +00:00
|
|
|
var ret []string
|
|
|
|
for _, ref := range refs {
|
|
|
|
if ref.Name().String() == "HEAD" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
ret = append(ret, ref.Name().String())
|
2021-06-15 21:11:35 +00:00
|
|
|
}
|
|
|
|
|
2022-09-21 05:47:02 +00:00
|
|
|
return ret, nil
|
2018-03-15 21:22:05 +00:00
|
|
|
}
|
|
|
|
|
2022-09-21 05:47:02 +00:00
|
|
|
// listFiles list all filenames under the specific repository
|
|
|
|
func (c *gitClient) listFiles(ctx context.Context, opt fetchOption) ([]string, error) {
|
|
|
|
cloneOption := &git.CloneOptions{
|
|
|
|
URL: opt.repositoryUrl,
|
|
|
|
NoCheckout: true,
|
|
|
|
Depth: 1,
|
|
|
|
SingleBranch: true,
|
|
|
|
ReferenceName: plumbing.ReferenceName(opt.referenceName),
|
|
|
|
Auth: getAuth(opt.username, opt.password),
|
2018-07-24 14:11:35 +00:00
|
|
|
}
|
|
|
|
|
2022-09-21 05:47:02 +00:00
|
|
|
repo, err := git.Clone(memory.NewStorage(), nil, cloneOption)
|
|
|
|
if err != nil {
|
|
|
|
return nil, checkGitError(err)
|
|
|
|
}
|
2021-08-17 01:12:07 +00:00
|
|
|
|
2022-09-21 05:47:02 +00:00
|
|
|
head, err := repo.Head()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2021-08-17 01:12:07 +00:00
|
|
|
}
|
|
|
|
|
2022-09-21 05:47:02 +00:00
|
|
|
commit, err := repo.CommitObject(head.Hash())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2021-08-17 01:12:07 +00:00
|
|
|
}
|
|
|
|
|
2022-09-21 05:47:02 +00:00
|
|
|
tree, err := commit.Tree()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var allPaths []string
|
|
|
|
tree.Files().ForEach(func(f *object.File) error {
|
|
|
|
allPaths = append(allPaths, f.Name)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
return allPaths, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkGitError(err error) error {
|
|
|
|
errMsg := err.Error()
|
|
|
|
if errMsg == "repository not found" {
|
|
|
|
return ErrIncorrectRepositoryURL
|
|
|
|
} else if errMsg == "authentication required" {
|
|
|
|
return ErrAuthenticationFailure
|
|
|
|
}
|
|
|
|
return err
|
2021-08-17 01:12:07 +00:00
|
|
|
}
|