-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
Copy pathurl.go
66 lines (52 loc) · 1.24 KB
/
url.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package git
import (
"net/url"
"regexp"
"strings"
)
var (
cachedSSHConfig SSHConfig
protocolRe = regexp.MustCompile("^[a-zA-Z_+-]+://")
)
type URLParser struct {
SSHConfig SSHConfig
}
func (p *URLParser) Parse(rawURL string) (u *url.URL, err error) {
if !protocolRe.MatchString(rawURL) &&
strings.Contains(rawURL, ":") &&
// not a Windows path
!strings.Contains(rawURL, "\\") {
rawURL = "ssh://" + strings.Replace(rawURL, ":", "/", 1)
}
u, err = url.Parse(rawURL)
if err != nil {
return
}
if u.Scheme == "git+ssh" {
u.Scheme = "ssh"
}
if u.Scheme != "ssh" {
return
}
if strings.HasPrefix(u.Path, "//") {
u.Path = strings.TrimPrefix(u.Path, "/")
}
if idx := strings.Index(u.Host, ":"); idx >= 0 {
u.Host = u.Host[0:idx]
}
sshHost := p.SSHConfig[u.Host]
// ignore replacing host that fixes for limited network
// https://help.github.com/articles/using-ssh-over-the-https-port
ignoredHost := u.Host == "github.com" && sshHost == "ssh.github.com"
if !ignoredHost && sshHost != "" {
u.Host = sshHost
}
return
}
func ParseURL(rawURL string) (u *url.URL, err error) {
if cachedSSHConfig == nil {
cachedSSHConfig = newSSHConfigReader().Read()
}
p := &URLParser{cachedSSHConfig}
return p.Parse(rawURL)
}