ParseURL normalizes git remote urls
(rawURL string)
| 27 | |
| 28 | // ParseURL normalizes git remote urls |
| 29 | func ParseURL(rawURL string) (*url.URL, error) { |
| 30 | if !isPossibleProtocol(rawURL) && |
| 31 | strings.ContainsRune(rawURL, ':') && |
| 32 | // not a Windows path |
| 33 | !strings.ContainsRune(rawURL, '\\') { |
| 34 | // support scp-like syntax for ssh protocol |
| 35 | rawURL = "ssh://" + strings.Replace(rawURL, ":", "/", 1) |
| 36 | } |
| 37 | |
| 38 | u, err := url.Parse(rawURL) |
| 39 | if err != nil { |
| 40 | return nil, err |
| 41 | } |
| 42 | |
| 43 | switch u.Scheme { |
| 44 | case "git+https": |
| 45 | u.Scheme = "https" |
| 46 | case "git+ssh": |
| 47 | u.Scheme = "ssh" |
| 48 | } |
| 49 | |
| 50 | if u.Scheme != "ssh" { |
| 51 | return u, nil |
| 52 | } |
| 53 | |
| 54 | if strings.HasPrefix(u.Path, "//") { |
| 55 | u.Path = strings.TrimPrefix(u.Path, "/") |
| 56 | } |
| 57 | |
| 58 | u.Host = strings.TrimSuffix(u.Host, ":"+u.Port()) |
| 59 | |
| 60 | return u, nil |
| 61 | } |