Skip to content

Commit

Permalink
Merge pull request #1886 from tonistiigi/git-proto-fix
Browse files Browse the repository at this point in the history
fix building from git url without a protocol
  • Loading branch information
tonistiigi authored Dec 11, 2020
2 parents 275dd40 + fbf8ed1 commit bf5e780
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 5 deletions.
8 changes: 4 additions & 4 deletions client/llb/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
_ "crypto/sha256" // for opencontainers/go-digest
"encoding/json"
"os"
"regexp"
"strconv"
"strings"

Expand Down Expand Up @@ -207,8 +206,6 @@ const (
gitProtocolUnknown
)

var gitSSHRegex = regexp.MustCompile("^([a-z0-9]+@)?[^:]+:.*$")

func getGitProtocol(remote string) (string, int) {
prefixes := map[string]int{
"http://": gitProtocolHTTP,
Expand All @@ -224,7 +221,7 @@ func getGitProtocol(remote string) (string, int) {
}
}

if protocolType == gitProtocolUnknown && gitSSHRegex.MatchString(remote) {
if protocolType == gitProtocolUnknown && sshutil.IsSSHTransport(remote) {
protocolType = gitProtocolSSH
}

Expand Down Expand Up @@ -254,6 +251,9 @@ func Git(remote, ref string, opts ...GitOption) State {
remote = parts[0] + "/" + parts[1]
}
}
if protocolType == gitProtocolUnknown {
url = "https://" + url
}

id := remote

Expand Down
3 changes: 2 additions & 1 deletion source/gitidentifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"net/url"
"strings"

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

Expand Down Expand Up @@ -58,7 +59,7 @@ func (i *GitIdentifier) ID() string {
// 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 {
return strings.HasPrefix(str, "http://") || strings.HasPrefix(str, "https://") || strings.HasPrefix(str, "git://") || strings.HasPrefix(str, "git@")
return strings.HasPrefix(str, "http://") || strings.HasPrefix(str, "https://") || strings.HasPrefix(str, "git://") || sshutil.IsSSHTransport(str)
}

func getRefAndSubdir(fragment string) (ref string, subdir string) {
Expand Down
3 changes: 3 additions & 0 deletions source/identifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ func FromLLB(op *pb.Op_Source, platform *pb.Platform) (Identifier, error) {
id.KeepGitDir = true
}
case pb.AttrFullRemoteURL:
if !isGitTransport(v) {
v = "https://" + v
}
id.Remote = v
case pb.AttrAuthHeaderSecret:
id.AuthHeaderSecret = v
Expand Down
11 changes: 11 additions & 0 deletions util/sshutil/transport_validation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package sshutil

import (
"regexp"
)

var gitSSHRegex = regexp.MustCompile("^[a-zA-Z0-9-_]+@[a-zA-Z0-9-.]+:.*$")

func IsSSHTransport(s string) bool {
return gitSSHRegex.MatchString(s)
}
23 changes: 23 additions & 0 deletions util/sshutil/transport_validation_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package sshutil

import (
"testing"

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

func TestIsSSHTransport(t *testing.T) {
require.False(t, IsSSHTransport("http://github.com/moby/buildkit"))
require.False(t, IsSSHTransport("github.com/moby/buildkit"))
require.False(t, IsSSHTransport("github.com:moby/buildkit.git"))
require.False(t, IsSSHTransport("helloworld.net"))
require.False(t, IsSSHTransport("[email protected]"))
require.False(t, IsSSHTransport("[email protected]/foo/bar.git"))
require.False(t, IsSSHTransport("bad:[email protected]:foo/bar.git"))
require.False(t, IsSSHTransport(""))
require.True(t, IsSSHTransport("[email protected]:moby/buildkit.git"))
require.True(t, IsSSHTransport("[email protected]:/srv/repos/weird/project.git"))
require.True(t, IsSSHTransport("[email protected]:path/to/repo.git/"))
require.True(t, IsSSHTransport("[email protected]:/to/really:odd:repo.git/"))
require.True(t, IsSSHTransport("[email protected]:/~/catnip.git/"))
}

0 comments on commit bf5e780

Please sign in to comment.