improve url parsing function in gitcloner

This commit is contained in:
Jingfang Liu
2018-12-13 10:35:03 -08:00
parent b971e6a1da
commit 20e37eaf65
2 changed files with 29 additions and 4 deletions

View File

@@ -44,7 +44,7 @@ func isRepoUrl(arg string) bool {
(strings.HasPrefix(arg, "git::") ||
strings.HasPrefix(arg, "gh:") ||
strings.HasPrefix(arg, "github.com") ||
strings.HasPrefix(arg, "git@github.com:") ||
strings.HasPrefix(arg, "git@") ||
strings.Index(arg, "github.com/") > -1 ||
isAzureHost(arg) || isAWSHost(arg))
}
@@ -106,7 +106,10 @@ func parseUrl(n string) (
return
}
const refQuery = "?ref="
const (
refQuery = "?ref="
gitSuffix = ".git"
)
// From strings like git@github.com:someOrg/someRepo.git or
// https://github.com/someOrg/someRepo?ref=someHash, extract
@@ -116,8 +119,16 @@ func parseGithubUrl(n string) (
host, n = parseHostSpec(n)
host = normalizeGitHostSpec(host)
if strings.HasSuffix(n, ".git") {
n = n[0 : len(n)-len(".git")]
if strings.HasSuffix(n, gitSuffix) {
repo = n[0 : len(n)-len(gitSuffix)]
return
}
if strings.Contains(n, gitSuffix) {
index := strings.Index(n, gitSuffix)
repo = n[0:index]
n = n[index+len(gitSuffix):]
path, gitRef = peelQuery(n)
return
}
i := strings.Index(n, "/")
if i < 1 {

View File

@@ -52,6 +52,14 @@ func TestIsRepoURL(t *testing.T) {
input: "git::https://gitlab.com/org/repo",
expected: true,
},
{
input: "git@gitlab2.sqtools.ru:10022/infra/kubernetes/thanos-base.git?ref=v0.1.0",
expected: true,
},
{
input: "git@bitbucket.org:org/repo.git",
expected: true,
},
{
input: "/github.com/org/repo",
expected: false,
@@ -283,6 +291,12 @@ func TestParseUrl(t *testing.T) {
path: "somedir",
ref: "",
},
{
input: "git@gitlab2.sqtools.ru:10022/infra/kubernetes/thanos-base.git?ref=v0.1.0",
repo: "git@gitlab2.sqtools.ru:10022/infra/kubernetes/thanos-base.git",
path: "",
ref: "v0.1.0",
},
}
for _, testcase := range testcases {
repo, path, ref, err := parseUrl(testcase.input)