1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

Merge pull request #40178 from thaJeztah/remote_context_refactor

builder/remotecontext: small refactor
This commit is contained in:
Brian Goff 2019-12-05 09:39:03 -08:00 committed by GitHub
commit 3ce9258447
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 94 additions and 27 deletions

View file

@ -10,7 +10,6 @@ import (
"strings"
"github.com/docker/docker/pkg/symlink"
"github.com/docker/docker/pkg/urlutil"
"github.com/pkg/errors"
)
@ -135,7 +134,7 @@ func fetchArgs(remoteURL string, ref string) []string {
// Check if a given git URL supports a shallow git clone,
// i.e. it is a non-HTTP server or a smart HTTP server.
func supportsShallowClone(remoteURL string) bool {
if urlutil.IsURL(remoteURL) {
if scheme := getScheme(remoteURL); scheme == "http" || scheme == "https" {
// Check if the HTTP server is smart
// Smart servers must correctly respond to a query for the git-upload-pack service
@ -205,5 +204,24 @@ func git(args ...string) ([]byte, error) {
// 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 urlutil.IsURL(str) || strings.HasPrefix(str, "git://") || strings.HasPrefix(str, "git@")
if strings.HasPrefix(str, "git@") {
return true
}
switch getScheme(str) {
case "git", "http", "https":
return true
}
return false
}
// getScheme returns addresses' scheme in lowercase, or an empty
// string in case address is an invalid URL.
func getScheme(address string) string {
u, err := url.Parse(address)
if err != nil {
return ""
}
return u.Scheme
}

View file

@ -19,32 +19,81 @@ import (
)
func TestParseRemoteURL(t *testing.T) {
dir, err := parseRemoteURL("git://github.com/user/repo.git")
assert.NilError(t, err)
assert.Check(t, is.DeepEqual(gitRepo{"git://github.com/user/repo.git", "master", ""}, dir, cmpGitRepoOpt))
dir, err = parseRemoteURL("git://github.com/user/repo.git#mybranch:mydir/mysubdir/")
assert.NilError(t, err)
assert.Check(t, is.DeepEqual(gitRepo{"git://github.com/user/repo.git", "mybranch", "mydir/mysubdir/"}, dir, cmpGitRepoOpt))
dir, err = parseRemoteURL("https://github.com/user/repo.git")
assert.NilError(t, err)
assert.Check(t, is.DeepEqual(gitRepo{"https://github.com/user/repo.git", "master", ""}, dir, cmpGitRepoOpt))
dir, err = parseRemoteURL("https://github.com/user/repo.git#mybranch:mydir/mysubdir/")
assert.NilError(t, err)
assert.Check(t, is.DeepEqual(gitRepo{"https://github.com/user/repo.git", "mybranch", "mydir/mysubdir/"}, dir, cmpGitRepoOpt))
dir, err = parseRemoteURL("git@github.com:user/repo.git")
assert.NilError(t, err)
assert.Check(t, is.DeepEqual(gitRepo{"git@github.com:user/repo.git", "master", ""}, dir, cmpGitRepoOpt))
dir, err = parseRemoteURL("git@github.com:user/repo.git#mybranch:mydir/mysubdir/")
assert.NilError(t, err)
assert.Check(t, is.DeepEqual(gitRepo{"git@github.com:user/repo.git", "mybranch", "mydir/mysubdir/"}, dir, cmpGitRepoOpt))
tests := []struct {
doc string
url string
expected gitRepo
}{
{
doc: "git scheme uppercase, no url-fragment",
url: "GIT://github.com/user/repo.git",
expected: gitRepo{
remote: "git://github.com/user/repo.git",
ref: "master",
},
},
{
doc: "git scheme, no url-fragment",
url: "git://github.com/user/repo.git",
expected: gitRepo{
remote: "git://github.com/user/repo.git",
ref: "master",
},
},
{
doc: "git scheme, with url-fragment",
url: "git://github.com/user/repo.git#mybranch:mydir/mysubdir/",
expected: gitRepo{
remote: "git://github.com/user/repo.git",
ref: "mybranch",
subdir: "mydir/mysubdir/",
},
},
{
doc: "https scheme, no url-fragment",
url: "https://github.com/user/repo.git",
expected: gitRepo{
remote: "https://github.com/user/repo.git",
ref: "master",
},
},
{
doc: "https scheme, with url-fragment",
url: "https://github.com/user/repo.git#mybranch:mydir/mysubdir/",
expected: gitRepo{
remote: "https://github.com/user/repo.git",
ref: "mybranch",
subdir: "mydir/mysubdir/",
},
},
{
doc: "git@, no url-fragment",
url: "git@github.com:user/repo.git",
expected: gitRepo{
remote: "git@github.com:user/repo.git",
ref: "master",
},
},
{
doc: "git@, with url-fragment",
url: "git@github.com:user/repo.git#mybranch:mydir/mysubdir/",
expected: gitRepo{
remote: "git@github.com:user/repo.git",
ref: "mybranch",
subdir: "mydir/mysubdir/",
},
},
}
var cmpGitRepoOpt = cmp.AllowUnexported(gitRepo{})
for _, tc := range tests {
tc := tc
t.Run(tc.doc, func(t *testing.T) {
repo, err := parseRemoteURL(tc.url)
assert.NilError(t, err)
assert.Check(t, is.DeepEqual(tc.expected, repo, cmp.AllowUnexported(gitRepo{})))
})
}
}
func TestCloneArgsSmartHttp(t *testing.T) {
mux := http.NewServeMux()