Merge pull request #8929 from cli/wm/parse-url-no-nakaed-return

Remove naked returns from git ParseURL
This commit is contained in:
William Martin 2024-04-05 16:33:09 +02:00 committed by GitHub
commit 3aa3d61108
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 29 additions and 28 deletions

View file

@ -26,7 +26,7 @@ func isPossibleProtocol(u string) bool {
}
// ParseURL normalizes git remote urls
func ParseURL(rawURL string) (u *url.URL, err error) {
func ParseURL(rawURL string) (*url.URL, error) {
if !isPossibleProtocol(rawURL) &&
strings.ContainsRune(rawURL, ':') &&
// not a Windows path
@ -35,21 +35,20 @@ func ParseURL(rawURL string) (u *url.URL, err error) {
rawURL = "ssh://" + strings.Replace(rawURL, ":", "/", 1)
}
u, err = url.Parse(rawURL)
u, err := url.Parse(rawURL)
if err != nil {
return
return nil, err
}
if u.Scheme == "git+ssh" {
switch u.Scheme {
case "git+https":
u.Scheme = "https"
case "git+ssh":
u.Scheme = "ssh"
}
if u.Scheme == "git+https" {
u.Scheme = "https"
}
if u.Scheme != "ssh" {
return
return u, nil
}
if strings.HasPrefix(u.Path, "//") {
@ -58,5 +57,5 @@ func ParseURL(rawURL string) (u *url.URL, err error) {
u.Host = strings.TrimSuffix(u.Host, ":"+u.Port())
return
return u, nil
}

View file

@ -1,6 +1,11 @@
package git
import "testing"
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestIsURL(t *testing.T) {
tests := []struct {
@ -56,9 +61,7 @@ func TestIsURL(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := IsURL(tt.url); got != tt.want {
t.Errorf("IsURL() = %v, want %v", got, tt.want)
}
assert.Equal(t, tt.want, IsURL(tt.url))
})
}
}
@ -216,25 +219,24 @@ func TestParseURL(t *testing.T) {
Path: "",
},
},
{
name: "fails to parse",
url: "ssh://git@[/tmp/git-repo",
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
u, err := ParseURL(tt.url)
if (err != nil) != tt.wantErr {
t.Fatalf("got error: %v", err)
}
if u.Scheme != tt.want.Scheme {
t.Errorf("expected scheme %q, got %q", tt.want.Scheme, u.Scheme)
}
if u.User.Username() != tt.want.User {
t.Errorf("expected user %q, got %q", tt.want.User, u.User.Username())
}
if u.Host != tt.want.Host {
t.Errorf("expected host %q, got %q", tt.want.Host, u.Host)
}
if u.Path != tt.want.Path {
t.Errorf("expected path %q, got %q", tt.want.Path, u.Path)
if tt.wantErr {
require.Error(t, err)
return
}
assert.Equal(t, u.Scheme, tt.want.Scheme)
assert.Equal(t, u.User.Username(), tt.want.User)
assert.Equal(t, u.Host, tt.want.Host)
assert.Equal(t, u.Path, tt.want.Path)
})
}
}