Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move git protocol detection into seperate util #2056

Merged
merged 1 commit into from
Apr 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 5 additions & 42 deletions client/llb/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/docker/distribution/reference"
"github.com/moby/buildkit/solver/pb"
"github.com/moby/buildkit/util/apicaps"
"github.com/moby/buildkit/util/gitutil"
"github.com/moby/buildkit/util/sshutil"
digest "github.com/opencontainers/go-digest"
"github.com/pkg/errors"
Expand Down Expand Up @@ -198,60 +199,22 @@ type ImageInfo struct {
RecordType string
}

const (
gitProtocolHTTP = iota + 1
gitProtocolHTTPS
gitProtocolSSH
gitProtocolGit
gitProtocolUnknown
)

func getGitProtocol(remote string) (string, int) {
prefixes := map[string]int{
"http://": gitProtocolHTTP,
"https://": gitProtocolHTTPS,
"git://": gitProtocolGit,
"ssh://": gitProtocolSSH,
}
protocolType := gitProtocolUnknown
for prefix, potentialType := range prefixes {
if strings.HasPrefix(remote, prefix) {
remote = strings.TrimPrefix(remote, prefix)
protocolType = potentialType
}
}

if protocolType == gitProtocolUnknown && sshutil.IsImplicitSSHTransport(remote) {
protocolType = gitProtocolSSH
}

// remove name from ssh
if protocolType == gitProtocolSSH {
parts := strings.SplitN(remote, "@", 2)
if len(parts) == 2 {
remote = parts[1]
}
}

return remote, protocolType
}

func Git(remote, ref string, opts ...GitOption) State {
url := strings.Split(remote, "#")[0]

var protocolType int
remote, protocolType = getGitProtocol(remote)
remote, protocolType = gitutil.ParseProtocol(remote)

var sshHost string
if protocolType == gitProtocolSSH {
if protocolType == gitutil.SSHProtocol {
parts := strings.SplitN(remote, ":", 2)
if len(parts) == 2 {
sshHost = parts[0]
// keep remote consistent with http(s) version
remote = parts[0] + "/" + parts[1]
}
}
if protocolType == gitProtocolUnknown {
if protocolType == gitutil.UnknownProtocol {
url = "https://" + url
}

Expand Down Expand Up @@ -289,7 +252,7 @@ func Git(remote, ref string, opts ...GitOption) State {
addCap(&gi.Constraints, pb.CapSourceGitHTTPAuth)
}
}
if protocolType == gitProtocolSSH {
if protocolType == gitutil.SSHProtocol {
if gi.KnownSSHHosts != "" {
attrs[pb.AttrKnownSSHHosts] = gi.KnownSSHHosts
} else if sshHost != "" {
Expand Down
46 changes: 46 additions & 0 deletions util/gitutil/git_protocol.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package gitutil

import (
"strings"

"github.com/moby/buildkit/util/sshutil"
)

const (
HTTPProtocol = iota + 1
HTTPSProtocol
SSHProtocol
GitProtocol
UnknownProtocol
)

// ParseProtocol parses a git URL and returns the remote url and protocol type
func ParseProtocol(remote string) (string, int) {
prefixes := map[string]int{
"http://": HTTPProtocol,
"https://": HTTPSProtocol,
"git://": GitProtocol,
"ssh://": SSHProtocol,
}
protocolType := UnknownProtocol
for prefix, potentialType := range prefixes {
if strings.HasPrefix(remote, prefix) {
remote = strings.TrimPrefix(remote, prefix)
protocolType = potentialType
}
}

if protocolType == UnknownProtocol && sshutil.IsImplicitSSHTransport(remote) {
protocolType = SSHProtocol
}

// remove name from ssh
if protocolType == SSHProtocol {
parts := strings.SplitN(remote, "@", 2)
if len(parts) == 2 {
remote = parts[1]
}
}

return remote, protocolType
}
51 changes: 51 additions & 0 deletions util/gitutil/git_protocol_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package gitutil

import (
"testing"

"github.com/stretchr/testify/require"
)

func TestParseProtocol(t *testing.T) {
tests := []struct {
url string
protocol int
remote string
}{
{
url: "http://github.com/moby/buildkit",
protocol: HTTPProtocol,
remote: "github.com/moby/buildkit",
},
{
url: "https://github.com/moby/buildkit",
protocol: HTTPSProtocol,
remote: "github.com/moby/buildkit",
},
{
url: "[email protected]:moby/buildkit.git",
protocol: SSHProtocol,
remote: "github.com:moby/buildkit.git",
},
{
url: "[email protected]:/srv/repos/weird/project.git",
protocol: SSHProtocol,
remote: "example.com:/srv/repos/weird/project.git",
},
{
url: "ssh://[email protected]:2222/root/my/really/weird/path/foo.git",
protocol: SSHProtocol,
remote: "subdomain.example.hostname:2222/root/my/really/weird/path/foo.git",
},
{
url: "git://host.xz:1234/path/to/repo.git",
protocol: GitProtocol,
remote: "host.xz:1234/path/to/repo.git",
},
}
for _, test := range tests {
remote, protocol := ParseProtocol(test.url)
require.Equal(t, remote, test.remote)
require.Equal(t, protocol, test.protocol)
}
}