Skip to content

Commit 176b3c6

Browse files
author
York Chen
committed
fix: downloading github url with / in branch name
1 parent c12e42f commit 176b3c6

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

detect_github.go

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,17 @@ func (d *GitHubDetector) Detect(src, _ string) (string, bool, error) {
2323
}
2424

2525
func (d *GitHubDetector) detectHTTP(src string) (string, bool, error) {
26-
parts := strings.Split(src, "/")
27-
if len(parts) < 3 {
26+
parts := strings.Split(src, "?")
27+
if len(parts) > 2 {
28+
return "", false, fmt.Errorf("there is more than 1 '?' in the URL")
29+
}
30+
hostAndPath := parts[0]
31+
hostAndPathParts := strings.Split(hostAndPath, "/")
32+
if len(hostAndPathParts) < 3 {
2833
return "", false, fmt.Errorf(
2934
"GitHub URLs should be github.com/username/repo")
3035
}
31-
32-
urlStr := fmt.Sprintf("https://%s", strings.Join(parts[:3], "/"))
36+
urlStr := fmt.Sprintf("https://%s", strings.Join(hostAndPathParts[:3], "/"))
3337
url, err := url.Parse(urlStr)
3438
if err != nil {
3539
return "", true, fmt.Errorf("error parsing GitHub URL: %s", err)
@@ -39,8 +43,12 @@ func (d *GitHubDetector) detectHTTP(src string) (string, bool, error) {
3943
url.Path += ".git"
4044
}
4145

42-
if len(parts) > 3 {
43-
url.Path += "//" + strings.Join(parts[3:], "/")
46+
if len(hostAndPathParts) > 3 {
47+
url.Path += "//" + strings.Join(hostAndPathParts[3:], "/")
48+
}
49+
50+
if len(parts) == 2 {
51+
url.RawQuery = parts[1]
4452
}
4553

4654
return "git::" + url.String(), true, nil

detect_github_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ func TestGitHubDetector(t *testing.T) {
2424
"github.com/hashicorp/foo.git?foo=bar",
2525
"git::https://github.com/hashicorp/foo.git?foo=bar",
2626
},
27+
{
28+
"github.com/hashicorp/foo.git?foo=bar/foobar,barfoo=bar",
29+
"git::https://github.com/hashicorp/foo.git?foo=bar/foobar,barfoo=bar",
30+
},
2731
}
2832

2933
pwd := "/pwd"

0 commit comments

Comments
 (0)