Skip to content

Commit 20e37ea

Browse files
committed
improve url parsing function in gitcloner
1 parent b971e6a commit 20e37ea

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

pkg/loader/gitcloner.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func isRepoUrl(arg string) bool {
4444
(strings.HasPrefix(arg, "git::") ||
4545
strings.HasPrefix(arg, "gh:") ||
4646
strings.HasPrefix(arg, "github.com") ||
47-
strings.HasPrefix(arg, "git@github.com:") ||
47+
strings.HasPrefix(arg, "git@") ||
4848
strings.Index(arg, "github.com/") > -1 ||
4949
isAzureHost(arg) || isAWSHost(arg))
5050
}
@@ -106,7 +106,10 @@ func parseUrl(n string) (
106106
return
107107
}
108108

109-
const refQuery = "?ref="
109+
const (
110+
refQuery = "?ref="
111+
gitSuffix = ".git"
112+
)
110113

111114
// From strings like [email protected]:someOrg/someRepo.git or
112115
// https://github.com/someOrg/someRepo?ref=someHash, extract
@@ -116,8 +119,16 @@ func parseGithubUrl(n string) (
116119
host, n = parseHostSpec(n)
117120
host = normalizeGitHostSpec(host)
118121

119-
if strings.HasSuffix(n, ".git") {
120-
n = n[0 : len(n)-len(".git")]
122+
if strings.HasSuffix(n, gitSuffix) {
123+
repo = n[0 : len(n)-len(gitSuffix)]
124+
return
125+
}
126+
if strings.Contains(n, gitSuffix) {
127+
index := strings.Index(n, gitSuffix)
128+
repo = n[0:index]
129+
n = n[index+len(gitSuffix):]
130+
path, gitRef = peelQuery(n)
131+
return
121132
}
122133
i := strings.Index(n, "/")
123134
if i < 1 {

pkg/loader/gitcloner_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,14 @@ func TestIsRepoURL(t *testing.T) {
5252
input: "git::https://gitlab.com/org/repo",
5353
expected: true,
5454
},
55+
{
56+
input: "[email protected]:10022/infra/kubernetes/thanos-base.git?ref=v0.1.0",
57+
expected: true,
58+
},
59+
{
60+
input: "[email protected]:org/repo.git",
61+
expected: true,
62+
},
5563
{
5664
input: "/github.com/org/repo",
5765
expected: false,
@@ -283,6 +291,12 @@ func TestParseUrl(t *testing.T) {
283291
path: "somedir",
284292
ref: "",
285293
},
294+
{
295+
input: "[email protected]:10022/infra/kubernetes/thanos-base.git?ref=v0.1.0",
296+
repo: "[email protected]:10022/infra/kubernetes/thanos-base.git",
297+
path: "",
298+
ref: "v0.1.0",
299+
},
286300
}
287301
for _, testcase := range testcases {
288302
repo, path, ref, err := parseUrl(testcase.input)

0 commit comments

Comments
 (0)