6
0
mirror of https://github.com/grdl/git-get.git synced 2026-02-04 17:24:49 +00:00

Make the URL scheme default to ssh instead of https and make it configurable

This commit is contained in:
Grzegorz Dlugoszewski
2020-09-02 03:14:01 +02:00
parent ccbee82f0b
commit 0d797d625a
5 changed files with 84 additions and 57 deletions

View File

@@ -17,8 +17,9 @@ var errEmptyURLPath = errors.New("parsed URL path is empty")
var scpSyntax = regexp.MustCompile(`^([a-zA-Z0-9_]+)@([a-zA-Z0-9._-]+):(.*)$`)
// ParseURL parses given rawURL string into a URL.
// The defaultHost argument defines the host to use (eg, github.com) in case parsed URL has an empty host.
func ParseURL(rawURL string, defaultHost string) (url *urlpkg.URL, err error) {
// When the parsed URL has an empty host, use the defaultHost.
// When the parsed URL has an empty scheme, use the defaultScheme.
func ParseURL(rawURL string, defaultHost string, defaultScheme string) (url *urlpkg.URL, err error) {
// If rawURL matches the SCP-like syntax, convert it into a standard ssh Path.
// eg, git@github.com:user/repo => ssh://git@github.com/user/repo
if m := scpSyntax.FindStringSubmatch(rawURL); m != nil {
@@ -26,7 +27,7 @@ func ParseURL(rawURL string, defaultHost string) (url *urlpkg.URL, err error) {
Scheme: "ssh",
User: urlpkg.User(m[1]),
Host: m[2],
Path: m[3],
Path: path.Join("/", m[3]),
}
} else {
url, err = urlpkg.Parse(rawURL)
@@ -43,14 +44,21 @@ func ParseURL(rawURL string, defaultHost string) (url *urlpkg.URL, err error) {
url.Scheme = "ssh"
}
// Default to "git" user when using ssh and no user is provided
if url.Scheme == "ssh" && url.User == nil {
url.User = urlpkg.User("git")
}
// Default to configured defaultHost when host is empty
if url.Host == "" {
url.Host = defaultHost
// Add a leading slash to path when host is missing. It's needed to correctly compare urlpkg.URL structs.
url.Path = path.Join("/", url.Path)
}
// Default to configured defaultScheme when scheme is empty
if url.Scheme == "" {
url.Scheme = defaultScheme
}
// Default to "git" user when using ssh and no user is provided
if url.Scheme == "ssh" && url.User == nil {
url.User = urlpkg.User("git")
}
// Don't use host when scheme is file://. The fragment detected as url.Host should be a first directory of url.Path
@@ -59,11 +67,6 @@ func ParseURL(rawURL string, defaultHost string) (url *urlpkg.URL, err error) {
url.Host = ""
}
// Default to https when scheme is empty
if url.Scheme == "" {
url.Scheme = "https"
}
return url, nil
}