Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug 1884270: bypass golang url parsing with scp styled ssh git URLs; refactor URL for older git clients #1054

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions pkg/scm/git/url.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ func splitOnByte(s string, c byte) (string, string) {
// Parse parses a "Git URL"
func Parse(rawurl string) (*URL, error) {
if urlSchemeRegexp.MatchString(rawurl) &&
// at least through golang 1.14.9, url.Parse cannot handle ssh://[email protected]:sclorg/nodejs-ex
!strings.HasPrefix(rawurl, "ssh://") &&
(runtime.GOOS != "windows" || !dosDriveRegexp.MatchString(rawurl)) {
u, err := url.Parse(rawurl)
if err != nil {
Expand All @@ -106,6 +108,12 @@ func Parse(rawurl string) (*URL, error) {
}, nil
}

// if ssh://[email protected]:sclorg/nodejs-ex then strip ssh:// a la what we
// see in other upstream git parsing handling of scp styled git URLs;
if strings.HasPrefix(rawurl, "ssh://") {
rawurl = strings.Trim(rawurl, "ssh://")
}

s, fragment := splitOnByte(rawurl, '#')

if m := scpRegexp.FindStringSubmatch(s); m != nil &&
Expand Down
35 changes: 30 additions & 5 deletions pkg/scm/git/url_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

type parseTest struct {
rawurl string
ammendedRawURL string
expectedGitURL *URL
expectedError bool
}
Expand Down Expand Up @@ -184,6 +185,19 @@ func TestParse(t *testing.T) {
Type: URLTypeSCP,
},
},
parseTest{
rawurl: "ssh://[email protected]:sclorg/nodejs-ex",
ammendedRawURL: "[email protected]:sclorg/nodejs-ex",
expectedGitURL: &URL{
URL: url.URL{
User: url.User("git"),
Host: "github.com",
Path: "sclorg/nodejs-ex",
},
Type: URLTypeSCP,
},
expectedError: false,
},

// path ...
parseTest{
Expand Down Expand Up @@ -221,12 +235,23 @@ func TestParse(t *testing.T) {
t.Errorf("%s: Parse() returned\n\t%#v\nWanted\n\t%#v", test.rawurl, parsedURL, test.expectedGitURL)
}

if parsedURL.String() != test.rawurl {
t.Errorf("%s: String() returned %s", test.rawurl, parsedURL.String())
}
if len(test.ammendedRawURL) > 0 {
if parsedURL.String() != test.ammendedRawURL {
t.Errorf("%s: String() returned %s", test.ammendedRawURL, parsedURL.String())
}

if parsedURL.StringNoFragment() != strings.SplitN(test.ammendedRawURL, "#", 2)[0] {
t.Errorf("%s: StringNoFragment() returned %s", test.ammendedRawURL, parsedURL.StringNoFragment())
}

} else {
if parsedURL.String() != test.rawurl {
t.Errorf("%s: String() returned %s", test.rawurl, parsedURL.String())
}

if parsedURL.StringNoFragment() != strings.SplitN(test.rawurl, "#", 2)[0] {
t.Errorf("%s: StringNoFragment() returned %s", test.rawurl, parsedURL.StringNoFragment())
if parsedURL.StringNoFragment() != strings.SplitN(test.rawurl, "#", 2)[0] {
t.Errorf("%s: StringNoFragment() returned %s", test.rawurl, parsedURL.StringNoFragment())
}
}
}
}
Expand Down