cli/git/url.go
Kynan Ware 72a8fd28c1 Add godoc comments to exported symbols in git/
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-03-04 15:58:15 -07:00

62 lines
1.3 KiB
Go

package git
import (
"net/url"
"strings"
)
// IsURL reports whether the string looks like a git remote URL.
func IsURL(u string) bool {
return strings.HasPrefix(u, "git@") || isSupportedProtocol(u)
}
func isSupportedProtocol(u string) bool {
return strings.HasPrefix(u, "ssh:") ||
strings.HasPrefix(u, "git+ssh:") ||
strings.HasPrefix(u, "git:") ||
strings.HasPrefix(u, "http:") ||
strings.HasPrefix(u, "git+https:") ||
strings.HasPrefix(u, "https:")
}
func isPossibleProtocol(u string) bool {
return isSupportedProtocol(u) ||
strings.HasPrefix(u, "ftp:") ||
strings.HasPrefix(u, "ftps:") ||
strings.HasPrefix(u, "file:")
}
// ParseURL normalizes git remote urls
func ParseURL(rawURL string) (*url.URL, error) {
if !isPossibleProtocol(rawURL) &&
strings.ContainsRune(rawURL, ':') &&
// not a Windows path
!strings.ContainsRune(rawURL, '\\') {
// support scp-like syntax for ssh protocol
rawURL = "ssh://" + strings.Replace(rawURL, ":", "/", 1)
}
u, err := url.Parse(rawURL)
if err != nil {
return nil, err
}
switch u.Scheme {
case "git+https":
u.Scheme = "https"
case "git+ssh":
u.Scheme = "ssh"
}
if u.Scheme != "ssh" {
return u, nil
}
if strings.HasPrefix(u.Path, "//") {
u.Path = strings.TrimPrefix(u.Path, "/")
}
u.Host = strings.TrimSuffix(u.Host, ":"+u.Port())
return u, nil
}