Merge pull request #403 from sinbad/endpoint_consistency

Consistently construct Endpoint from urls the same way
This commit is contained in:
risk danger olson 2015-06-17 14:34:50 -06:00
commit 29643c4b4a

@ -78,7 +78,7 @@ func (c *Configuration) GetenvBool(key string, def bool) bool {
func (c *Configuration) Endpoint() Endpoint { func (c *Configuration) Endpoint() Endpoint {
if url, ok := c.GitConfig("lfs.url"); ok { if url, ok := c.GitConfig("lfs.url"); ok {
return Endpoint{Url: url} return NewEndpoint(url)
} }
if len(c.CurrentRemote) > 0 && c.CurrentRemote != defaultRemote { if len(c.CurrentRemote) > 0 && c.CurrentRemote != defaultRemote {
@ -121,38 +121,47 @@ func (c *Configuration) RemoteEndpoint(remote string) Endpoint {
if len(remote) == 0 { if len(remote) == 0 {
remote = defaultRemote remote = defaultRemote
} }
if url, ok := c.GitConfig("remote." + remote + ".lfsurl"); ok { if url, ok := c.GitConfig("remote." + remote + ".lfsurl"); ok {
return Endpoint{Url: url} return NewEndpoint(url)
} }
if url, ok := c.GitConfig("remote." + remote + ".url"); ok { if url, ok := c.GitConfig("remote." + remote + ".url"); ok {
return remoteEndpointFromUrl(url) return NewEndpointFromCloneURL(url)
} }
return Endpoint{} return Endpoint{}
} }
func remoteEndpointFromUrl(url string) Endpoint { const ENDPOINT_URL_UNKNOWN = "<unknown>"
// Create a new endpoint from a URL associated with a git clone URL
// The difference to NewEndpoint is that it appends [.git]/info/lfs to the URL since it
// is the clone URL
func NewEndpointFromCloneURL(url string) Endpoint {
e := NewEndpoint(url)
if e.Url != ENDPOINT_URL_UNKNOWN {
// When using main remote URL for HTTP, append info/lfs
if path.Ext(url) == ".git" {
e.Url += "/info/lfs"
} else {
e.Url += ".git/info/lfs"
}
}
return e
}
// Create a new endpoint from a general URL
func NewEndpoint(url string) Endpoint {
e := Endpoint{Url: url} e := Endpoint{Url: url}
if !httpPrefixRe.MatchString(url) { if !httpPrefixRe.MatchString(url) {
pieces := strings.SplitN(url, ":", 2) pieces := strings.SplitN(url, ":", 2)
hostPieces := strings.SplitN(pieces[0], "@", 2) hostPieces := strings.SplitN(pieces[0], "@", 2)
if len(hostPieces) < 2 { if len(hostPieces) == 2 {
e.Url = "<unknown>"
return e
}
e.SshUserAndHost = pieces[0] e.SshUserAndHost = pieces[0]
e.SshPath = pieces[1] e.SshPath = pieces[1]
e.Url = fmt.Sprintf("https://%s/%s", hostPieces[1], pieces[1]) e.Url = fmt.Sprintf("https://%s/%s", hostPieces[1], pieces[1])
} }
if path.Ext(url) == ".git" {
e.Url += "/info/lfs"
} else {
e.Url += ".git/info/lfs"
} }
return e return e