Move prefix check to isGitTransport
- rename IsSSHTransport to IsImplicitSSHTransport - add testing for NewGitIdentifier Signed-off-by: Alex Couture-Beil <alex@earthly.dev>v0.9
parent
0dff0fdab4
commit
3a5bdf0710
|
@ -221,7 +221,7 @@ func getGitProtocol(remote string) (string, int) {
|
|||
}
|
||||
}
|
||||
|
||||
if protocolType == gitProtocolUnknown && sshutil.IsSSHTransport(remote) {
|
||||
if protocolType == gitProtocolUnknown && sshutil.IsImplicitSSHTransport(remote) {
|
||||
protocolType = gitProtocolSSH
|
||||
}
|
||||
|
||||
|
|
|
@ -27,8 +27,8 @@ func NewGitIdentifier(remoteURL string) (*GitIdentifier, error) {
|
|||
}
|
||||
|
||||
var fragment string
|
||||
if strings.HasPrefix(remoteURL, "git@") {
|
||||
// git@.. is not an URL, so cannot be parsed as URL
|
||||
if sshutil.IsImplicitSSHTransport(remoteURL) {
|
||||
// implicit ssh urls such as "git@.." are not actually a URL, so cannot be parsed as URL
|
||||
parts := strings.SplitN(remoteURL, "#", 2)
|
||||
|
||||
repo.Remote = parts[0]
|
||||
|
@ -59,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://") || sshutil.IsSSHTransport(str)
|
||||
return strings.HasPrefix(str, "http://") || strings.HasPrefix(str, "https://") || strings.HasPrefix(str, "git://") || strings.HasPrefix(str, "ssh://") || sshutil.IsImplicitSSHTransport(str)
|
||||
}
|
||||
|
||||
func getRefAndSubdir(fragment string) (ref string, subdir string) {
|
||||
|
|
|
@ -0,0 +1,34 @@
|
|||
package source
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestNewGitIdentifier(t *testing.T) {
|
||||
gi, err := NewGitIdentifier("ssh://root@subdomain.example.hostname:2222/root/my/really/weird/path/foo.git")
|
||||
require.Nil(t, err)
|
||||
require.Equal(t, "ssh://root@subdomain.example.hostname:2222/root/my/really/weird/path/foo.git", gi.Remote)
|
||||
require.Equal(t, "master", gi.Ref)
|
||||
require.Equal(t, "", gi.Subdir)
|
||||
|
||||
gi, err = NewGitIdentifier("ssh://root@subdomain.example.hostname:2222/root/my/really/weird/path/foo.git#main")
|
||||
require.Nil(t, err)
|
||||
require.Equal(t, "ssh://root@subdomain.example.hostname:2222/root/my/really/weird/path/foo.git", gi.Remote)
|
||||
require.Equal(t, "main", gi.Ref)
|
||||
require.Equal(t, "", gi.Subdir)
|
||||
|
||||
gi, err = NewGitIdentifier("git@github.com:moby/buildkit.git")
|
||||
require.Nil(t, err)
|
||||
require.Equal(t, "git@github.com:moby/buildkit.git", gi.Remote)
|
||||
require.Equal(t, "master", gi.Ref)
|
||||
require.Equal(t, "", gi.Subdir)
|
||||
|
||||
gi, err = NewGitIdentifier("github.com/moby/buildkit.git")
|
||||
require.Nil(t, err)
|
||||
require.Equal(t, "https://github.com/moby/buildkit.git", gi.Remote)
|
||||
require.Equal(t, "master", gi.Ref)
|
||||
require.Equal(t, "", gi.Subdir)
|
||||
|
||||
}
|
|
@ -2,17 +2,10 @@ package sshutil
|
|||
|
||||
import (
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var gitSSHRegex = regexp.MustCompile("^[a-zA-Z0-9-_]+@[a-zA-Z0-9-.]+:.*$")
|
||||
|
||||
func IsSSHTransport(s string) bool {
|
||||
// check for explicit ssh transport
|
||||
if strings.HasPrefix(s, "ssh://") {
|
||||
return true
|
||||
}
|
||||
|
||||
// check for implicit ssh transport
|
||||
func IsImplicitSSHTransport(s string) bool {
|
||||
return gitSSHRegex.MatchString(s)
|
||||
}
|
||||
|
|
|
@ -6,19 +6,21 @@ import (
|
|||
"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("git@helloworld.net"))
|
||||
require.False(t, IsSSHTransport("git@helloworld.net/foo/bar.git"))
|
||||
require.False(t, IsSSHTransport("bad:user@helloworld.net:foo/bar.git"))
|
||||
require.False(t, IsSSHTransport(""))
|
||||
require.True(t, IsSSHTransport("git@github.com:moby/buildkit.git"))
|
||||
require.True(t, IsSSHTransport("nonstandarduser@example.com:/srv/repos/weird/project.git"))
|
||||
require.True(t, IsSSHTransport("other_Funky-username52@example.com:path/to/repo.git/"))
|
||||
require.True(t, IsSSHTransport("other_Funky-username52@example.com:/to/really:odd:repo.git/"))
|
||||
require.True(t, IsSSHTransport("teddy@4houses-down.com:/~/catnip.git/"))
|
||||
require.True(t, IsSSHTransport("ssh://root@subdomain.example.hostname:2222/root/my/really/weird/path/foo.git"))
|
||||
func TestIsImplicitSSHTransport(t *testing.T) {
|
||||
require.False(t, IsImplicitSSHTransport("http://github.com/moby/buildkit"))
|
||||
require.False(t, IsImplicitSSHTransport("github.com/moby/buildkit"))
|
||||
require.False(t, IsImplicitSSHTransport("github.com:moby/buildkit.git"))
|
||||
require.False(t, IsImplicitSSHTransport("helloworld.net"))
|
||||
require.False(t, IsImplicitSSHTransport("git@helloworld.net"))
|
||||
require.False(t, IsImplicitSSHTransport("git@helloworld.net/foo/bar.git"))
|
||||
require.False(t, IsImplicitSSHTransport("bad:user@helloworld.net:foo/bar.git"))
|
||||
require.False(t, IsImplicitSSHTransport(""))
|
||||
require.True(t, IsImplicitSSHTransport("git@github.com:moby/buildkit.git"))
|
||||
require.True(t, IsImplicitSSHTransport("nonstandarduser@example.com:/srv/repos/weird/project.git"))
|
||||
require.True(t, IsImplicitSSHTransport("other_Funky-username52@example.com:path/to/repo.git/"))
|
||||
require.True(t, IsImplicitSSHTransport("other_Funky-username52@example.com:/to/really:odd:repo.git/"))
|
||||
require.True(t, IsImplicitSSHTransport("teddy@4houses-down.com:/~/catnip.git/"))
|
||||
|
||||
// explicit definitions are checked via isGitTransport, and are not implicit therefore this should fail:
|
||||
require.False(t, IsImplicitSSHTransport("ssh://root@subdomain.example.hostname:2222/root/my/really/weird/path/foo.git"))
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue