10 const UrlUnknown = "<unknown>"
12 // An Endpoint describes how to access a Git LFS server.
13 type Endpoint struct {
21 func endpointOperation(e Endpoint, method string) string {
22 if len(e.Operation) > 0 {
34 // endpointFromBareSshUrl constructs a new endpoint from a bare SSH URL:
36 // user@host.com:path/to/repo.git
38 func endpointFromBareSshUrl(rawurl string) Endpoint {
39 parts := strings.Split(rawurl, ":")
40 partsLen := len(parts)
42 return Endpoint{Url: rawurl}
45 // Treat presence of ':' as a bare URL
47 if len(parts) > 2 { // port included; really should only ever be 3 parts
48 newPath = fmt.Sprintf("%v:%v", parts[0], strings.Join(parts[1:], "/"))
50 newPath = strings.Join(parts, "/")
52 newrawurl := fmt.Sprintf("ssh://%v", newPath)
53 newu, err := url.Parse(newrawurl)
55 return Endpoint{Url: UrlUnknown}
58 return endpointFromSshUrl(newu)
61 // endpointFromSshUrl constructs a new endpoint from an ssh:// URL
62 func endpointFromSshUrl(u *url.URL) Endpoint {
64 // Pull out port now, we need it separately for SSH
65 regex := regexp.MustCompile(`^([^\:]+)(?:\:(\d+))?$`)
66 match := regex.FindStringSubmatch(u.Host)
67 if match == nil || len(match) < 2 {
68 endpoint.Url = UrlUnknown
73 if u.User != nil && u.User.Username() != "" {
74 endpoint.SshUserAndHost = fmt.Sprintf("%s@%s", u.User.Username(), host)
76 endpoint.SshUserAndHost = host
80 endpoint.SshPort = match[2]
83 // u.Path includes a preceding '/', strip off manually
84 // rooted paths in the URL will be '//path/to/blah'
85 // this is just how Go's URL parsing works
86 if strings.HasPrefix(u.Path, "/") {
87 endpoint.SshPath = u.Path[1:]
89 endpoint.SshPath = u.Path
92 // Fallback URL for using HTTPS while still using SSH for git
93 // u.Host includes host & port so can't use SSH port
94 endpoint.Url = fmt.Sprintf("https://%s%s", host, u.Path)
99 // Construct a new endpoint from a HTTP URL
100 func endpointFromHttpUrl(u *url.URL) Endpoint {
101 // just pass this straight through
102 return Endpoint{Url: u.String()}
105 func endpointFromGitUrl(u *url.URL, e *endpointGitFinder) Endpoint {
106 u.Scheme = e.gitProtocol
107 return Endpoint{Url: u.String()}