2017-07-07 21:35:10 +00:00
|
|
|
package source
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/url"
|
2021-05-20 04:10:58 +00:00
|
|
|
"path"
|
2017-07-07 21:35:10 +00:00
|
|
|
"strings"
|
|
|
|
|
2021-09-17 23:39:19 +00:00
|
|
|
srctypes "github.com/moby/buildkit/source/types"
|
2020-12-10 22:36:58 +00:00
|
|
|
"github.com/moby/buildkit/util/sshutil"
|
2017-07-07 21:35:10 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type GitIdentifier struct {
|
2020-06-16 23:45:17 +00:00
|
|
|
Remote string
|
|
|
|
Ref string
|
|
|
|
Subdir string
|
|
|
|
KeepGitDir bool
|
|
|
|
AuthTokenSecret string
|
|
|
|
AuthHeaderSecret string
|
2020-11-13 01:29:49 +00:00
|
|
|
MountSSHSock string
|
2020-11-10 20:42:33 +00:00
|
|
|
KnownSSHHosts string
|
2017-07-07 21:35:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewGitIdentifier(remoteURL string) (*GitIdentifier, error) {
|
|
|
|
repo := GitIdentifier{}
|
|
|
|
|
2020-11-13 01:29:49 +00:00
|
|
|
if !isGitTransport(remoteURL) {
|
|
|
|
remoteURL = "https://" + remoteURL
|
|
|
|
}
|
|
|
|
|
2017-07-07 21:35:10 +00:00
|
|
|
var fragment string
|
2021-01-04 19:07:28 +00:00
|
|
|
if sshutil.IsImplicitSSHTransport(remoteURL) {
|
|
|
|
// implicit ssh urls such as "git@.." are not actually a URL, so cannot be parsed as URL
|
2017-07-07 21:35:10 +00:00
|
|
|
parts := strings.SplitN(remoteURL, "#", 2)
|
|
|
|
|
|
|
|
repo.Remote = parts[0]
|
|
|
|
if len(parts) == 2 {
|
|
|
|
fragment = parts[1]
|
|
|
|
}
|
|
|
|
repo.Ref, repo.Subdir = getRefAndSubdir(fragment)
|
|
|
|
} else {
|
|
|
|
u, err := url.Parse(remoteURL)
|
|
|
|
if err != nil {
|
2017-12-16 07:17:06 +00:00
|
|
|
return nil, err
|
2017-07-07 21:35:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
repo.Ref, repo.Subdir = getRefAndSubdir(u.Fragment)
|
|
|
|
u.Fragment = ""
|
|
|
|
repo.Remote = u.String()
|
|
|
|
}
|
2021-05-20 04:10:58 +00:00
|
|
|
if sd := path.Clean(repo.Subdir); sd == "/" || sd == "." {
|
|
|
|
repo.Subdir = ""
|
2017-07-07 21:35:10 +00:00
|
|
|
}
|
|
|
|
return &repo, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *GitIdentifier) ID() string {
|
2021-09-17 23:39:19 +00:00
|
|
|
return srctypes.GitScheme
|
2017-07-07 21:35:10 +00:00
|
|
|
}
|
|
|
|
|
2020-11-13 01:29:49 +00:00
|
|
|
// isGitTransport returns true if the provided str is a git transport by inspecting
|
|
|
|
// the prefix of the string for known protocols used in git.
|
|
|
|
func isGitTransport(str string) bool {
|
2021-01-04 19:07:28 +00:00
|
|
|
return strings.HasPrefix(str, "http://") || strings.HasPrefix(str, "https://") || strings.HasPrefix(str, "git://") || strings.HasPrefix(str, "ssh://") || sshutil.IsImplicitSSHTransport(str)
|
2020-11-13 01:29:49 +00:00
|
|
|
}
|
|
|
|
|
2017-07-07 21:35:10 +00:00
|
|
|
func getRefAndSubdir(fragment string) (ref string, subdir string) {
|
|
|
|
refAndDir := strings.SplitN(fragment, ":", 2)
|
2021-03-07 21:45:24 +00:00
|
|
|
ref = ""
|
2017-07-07 21:35:10 +00:00
|
|
|
if len(refAndDir[0]) != 0 {
|
|
|
|
ref = refAndDir[0]
|
|
|
|
}
|
|
|
|
if len(refAndDir) > 1 && len(refAndDir[1]) != 0 {
|
|
|
|
subdir = refAndDir[1]
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|