-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
git: allow scp-style urls without username
According to the git docs: > Or you can use the shorter scp-like syntax for the SSH protocol: > > $ git clone [user@]server:project.git Previously, we were doing this potentially wrong - we were requiring the presence of a username for all scp-style URLs. This could result in disparity from the git CLI which successfully manages to parse `github.com:moby/buildkit.git` (though cloning will generally fail, with an ambiguous username). However, we aim keep the behavior of git ref as before - this is because making changes to this can effect the parsing of dockerfiles: where before "foo:bar" would be detected as a local source, after, it would be detected as a git repo (despite the fact it correctly parses as one). To avoid this, we simply keep this behavior as before. Signed-off-by: Justin Chadwell <[email protected]>
- Loading branch information
Showing
6 changed files
with
88 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
package gitutil | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
@@ -88,6 +89,10 @@ func TestParseGitRef(t *testing.T) { | |
ShortName: "buildkit", | ||
}, | ||
}, | ||
{ | ||
ref: "github.com:moby/buildkit", | ||
expected: nil, | ||
}, | ||
{ | ||
ref: "[email protected]:moby/buildkit", | ||
expected: &GitRef{ | ||
|
@@ -141,7 +146,7 @@ func TestParseGitRef(t *testing.T) { | |
} | ||
for _, tt := range cases { | ||
tt := tt | ||
t.Run(tt.ref, func(t *testing.T) { | ||
t.Run(strings.ReplaceAll(tt.ref, "/", "_"), func(t *testing.T) { | ||
got, err := ParseGitRef(tt.ref) | ||
if tt.expected == nil { | ||
require.Nil(t, got) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ package gitutil | |
|
||
import ( | ||
"net/url" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
@@ -75,6 +76,15 @@ func TestParseURL(t *testing.T) { | |
User: url.User("git"), | ||
}, | ||
}, | ||
{ | ||
url: "github.com:moby/buildkit.git", | ||
result: GitURL{ | ||
Scheme: SSHProtocol, | ||
Host: "github.com", | ||
Path: "moby/buildkit.git", | ||
User: nil, | ||
}, | ||
}, | ||
{ | ||
url: "[email protected]:moby/buildkit.git", | ||
result: GitURL{ | ||
|
@@ -153,7 +163,7 @@ func TestParseURL(t *testing.T) { | |
}, | ||
} | ||
for _, test := range tests { | ||
t.Run(test.url, func(t *testing.T) { | ||
t.Run(strings.ReplaceAll(test.url, "/", "_"), func(t *testing.T) { | ||
remote, err := ParseURL(test.url) | ||
if test.err { | ||
require.Error(t, err) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,31 @@ | ||
package sshutil | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
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("[email protected]")) | ||
require.False(t, IsImplicitSSHTransport("[email protected]/foo/bar.git")) | ||
require.False(t, IsImplicitSSHTransport("bad:[email protected]:foo/bar.git")) | ||
require.False(t, IsImplicitSSHTransport("")) | ||
require.True(t, IsImplicitSSHTransport("[email protected]:moby/buildkit.git")) | ||
require.True(t, IsImplicitSSHTransport("[email protected]:/srv/repos/weird/project.git")) | ||
require.True(t, IsImplicitSSHTransport("[email protected]:path/to/repo.git/")) | ||
require.True(t, IsImplicitSSHTransport("[email protected]:/to/really:odd:repo.git/")) | ||
require.True(t, IsImplicitSSHTransport("[email protected]:/~/catnip.git/")) | ||
assert.True(t, IsImplicitSSHTransport("github.com:moby/buildkit.git")) | ||
assert.True(t, IsImplicitSSHTransport("[email protected]:moby/buildkit.git")) | ||
assert.True(t, IsImplicitSSHTransport("[email protected]:/srv/repos/weird/project.git")) | ||
assert.True(t, IsImplicitSSHTransport("[email protected]:path/to/repo.git/")) | ||
assert.True(t, IsImplicitSSHTransport("[email protected]:/to/really:odd:repo.git/")) | ||
assert.True(t, IsImplicitSSHTransport("[email protected]:/~/catnip.git/")) | ||
assert.True(t, IsImplicitSSHTransport("weird:[email protected]:foo/bar.git")) | ||
|
||
assert.False(t, IsImplicitSSHTransport("http://github.com/moby/buildkit")) | ||
assert.False(t, IsImplicitSSHTransport("github.com/moby/buildkit")) | ||
assert.False(t, IsImplicitSSHTransport("helloworld.net")) | ||
assert.False(t, IsImplicitSSHTransport("[email protected]")) | ||
assert.False(t, IsImplicitSSHTransport("[email protected]/foo/bar.git")) | ||
assert.False(t, IsImplicitSSHTransport("")) | ||
|
||
// explicit definitions are checked via isGitTransport, and are not implicit therefore this should fail: | ||
require.False(t, IsImplicitSSHTransport("ssh://[email protected]:2222/root/my/really/weird/path/foo.git")) | ||
assert.False(t, IsImplicitSSHTransport("ssh://[email protected]:2222/root/my/really/weird/path/foo.git")) | ||
} | ||
|
||
func TestParseSCPStyleURL(t *testing.T) { | ||
|
@@ -42,6 +45,11 @@ func TestParseSCPStyleURL(t *testing.T) { | |
url: "ssh://[email protected]:22/moby/buildkit.git", | ||
err: true, | ||
}, | ||
{ | ||
url: "github.com:moby/buildkit.git", | ||
host: "github.com", | ||
path: "moby/buildkit.git", | ||
}, | ||
{ | ||
url: "[email protected]:moby/buildkit.git", | ||
host: "github.com", | ||
|
@@ -57,7 +65,7 @@ func TestParseSCPStyleURL(t *testing.T) { | |
}, | ||
} | ||
for _, test := range tests { | ||
t.Run(test.url, func(t *testing.T) { | ||
t.Run(strings.ReplaceAll(test.url, "/", "_"), func(t *testing.T) { | ||
remote, err := ParseSCPStyleURL(test.url) | ||
if test.err { | ||
require.Error(t, err) | ||
|