git-lfs/lfsapi/endpoint_finder.go

351 lines
8.7 KiB
Go
Raw Normal View History

2016-12-19 18:45:22 +00:00
package lfsapi
import (
"fmt"
"net/url"
"os"
"path"
"strings"
"sync"
"github.com/git-lfs/git-lfs/v3/config"
"github.com/git-lfs/git-lfs/v3/creds"
"github.com/git-lfs/git-lfs/v3/git"
"github.com/git-lfs/git-lfs/v3/lfshttp"
2021-12-14 16:05:42 +00:00
"github.com/git-lfs/git-lfs/v3/tr"
"github.com/rubyist/tracerx"
2016-12-19 18:45:22 +00:00
)
2016-12-19 18:55:47 +00:00
const (
defaultRemote = "origin"
2016-12-19 18:55:47 +00:00
)
2016-12-19 18:45:22 +00:00
type EndpointFinder interface {
NewEndpointFromCloneURL(operation, rawurl string) lfshttp.Endpoint
NewEndpoint(operation, rawurl string) lfshttp.Endpoint
Endpoint(operation, remote string) lfshttp.Endpoint
RemoteEndpoint(operation, remote string) lfshttp.Endpoint
2016-12-19 18:45:22 +00:00
GitRemoteURL(remote string, forpush bool) string
AccessFor(rawurl string) creds.Access
SetAccess(access creds.Access)
2016-12-19 18:45:22 +00:00
GitProtocol() string
}
type endpointGitFinder struct {
gitConfig *git.Configuration
gitEnv config.Environment
2016-12-19 18:45:22 +00:00
gitProtocol string
aliasMu sync.Mutex
aliases map[string]string
pushAliases map[string]string
accessMu sync.Mutex
urlAccess map[string]creds.AccessMode
urlConfig *config.URLConfig
2016-12-19 18:45:22 +00:00
}
func NewEndpointFinder(ctx lfshttp.Context) EndpointFinder {
if ctx == nil {
ctx = lfshttp.NewContext(nil, nil, nil)
}
2016-12-19 18:45:22 +00:00
e := &endpointGitFinder{
gitConfig: ctx.GitConfig(),
gitEnv: ctx.GitEnv(),
2016-12-19 18:45:22 +00:00
gitProtocol: "https",
aliases: make(map[string]string),
pushAliases: make(map[string]string),
urlAccess: make(map[string]creds.AccessMode),
2016-12-19 18:45:22 +00:00
}
e.urlConfig = config.NewURLConfig(e.gitEnv)
if v, ok := e.gitEnv.Get("lfs.gitprotocol"); ok {
e.gitProtocol = v
2016-12-19 18:45:22 +00:00
}
initAliases(e, e.gitEnv)
2016-12-19 18:45:22 +00:00
return e
}
func (e *endpointGitFinder) Endpoint(operation, remote string) lfshttp.Endpoint {
ep := e.getEndpoint(operation, remote)
ep.Operation = operation
return ep
}
func (e *endpointGitFinder) getEndpoint(operation, remote string) lfshttp.Endpoint {
if e.gitEnv == nil {
return lfshttp.Endpoint{}
2016-12-19 18:45:22 +00:00
}
if operation == "upload" {
if url, ok := e.gitEnv.Get("lfs.pushurl"); ok {
return e.NewEndpoint(operation, url)
2016-12-19 18:45:22 +00:00
}
}
if url, ok := e.gitEnv.Get("lfs.url"); ok {
return e.NewEndpoint(operation, url)
2016-12-19 18:45:22 +00:00
}
if len(remote) > 0 && remote != defaultRemote {
if e := e.RemoteEndpoint(operation, remote); len(e.Url) > 0 {
return e
}
}
return e.RemoteEndpoint(operation, defaultRemote)
}
func (e *endpointGitFinder) RemoteEndpoint(operation, remote string) lfshttp.Endpoint {
if e.gitEnv == nil {
return lfshttp.Endpoint{}
2016-12-19 18:45:22 +00:00
}
if len(remote) == 0 {
remote = defaultRemote
}
// Support separate push URL if specified and pushing
if operation == "upload" {
if url, ok := e.gitEnv.Get("remote." + remote + ".lfspushurl"); ok {
return e.NewEndpoint(operation, url)
2016-12-19 18:45:22 +00:00
}
}
if url, ok := e.gitEnv.Get("remote." + remote + ".lfsurl"); ok {
return e.NewEndpoint(operation, url)
2016-12-19 18:45:22 +00:00
}
// finally fall back on git remote url (also supports pushurl)
if url := e.GitRemoteURL(remote, operation == "upload"); url != "" {
return e.NewEndpointFromCloneURL(operation, url)
2016-12-19 18:45:22 +00:00
}
return lfshttp.Endpoint{}
2016-12-19 18:45:22 +00:00
}
func (e *endpointGitFinder) GitRemoteURL(remote string, forpush bool) string {
if e.gitEnv != nil {
2016-12-19 18:45:22 +00:00
if forpush {
if u, ok := e.gitEnv.Get("remote." + remote + ".pushurl"); ok {
2016-12-19 18:45:22 +00:00
return u
}
}
if u, ok := e.gitEnv.Get("remote." + remote + ".url"); ok {
2016-12-19 18:45:22 +00:00
return u
}
}
if err := git.ValidateRemote(remote); err == nil {
return remote
}
return ""
}
func (e *endpointGitFinder) NewEndpointFromCloneURL(operation, rawurl string) lfshttp.Endpoint {
ep := e.NewEndpoint(operation, rawurl)
if ep.Url == lfshttp.UrlUnknown {
2016-12-19 18:45:22 +00:00
return ep
}
if strings.HasSuffix(rawurl, "/") {
ep.Url = rawurl[0 : len(rawurl)-1]
}
if strings.HasPrefix(ep.Url, "file://") {
return ep
}
2016-12-19 18:45:22 +00:00
// When using main remote URL for HTTP, append info/lfs
if path.Ext(ep.Url) == ".git" {
ep.Url += "/info/lfs"
} else {
ep.Url += ".git/info/lfs"
}
return ep
}
func (e *endpointGitFinder) NewEndpoint(operation, rawurl string) lfshttp.Endpoint {
rawurl = e.ReplaceUrlAlias(operation, rawurl)
if strings.HasPrefix(rawurl, "/") {
return lfshttp.EndpointFromLocalPath(rawurl)
}
2016-12-19 18:45:22 +00:00
u, err := url.Parse(rawurl)
if err != nil {
return lfshttp.EndpointFromBareSshUrl(rawurl)
2016-12-19 18:45:22 +00:00
}
switch u.Scheme {
case "ssh", "git+ssh", "ssh+git":
return lfshttp.EndpointFromSshUrl(u)
2016-12-19 18:45:22 +00:00
case "http", "https":
return lfshttp.EndpointFromHttpUrl(u)
2016-12-19 18:45:22 +00:00
case "git":
return endpointFromGitUrl(u, e)
case "file":
return lfshttp.EndpointFromFileUrl(u)
2016-12-19 18:45:22 +00:00
case "":
// If it looks like a local path, it probably is.
if _, err := os.Stat(rawurl); err == nil {
return lfshttp.EndpointFromLocalPath(rawurl)
}
return lfshttp.EndpointFromBareSshUrl(u.String())
2016-12-19 18:45:22 +00:00
default:
if strings.HasPrefix(rawurl, u.Scheme+"::") {
// Looks like a remote helper; just pass it through.
return lfshttp.Endpoint{Url: rawurl}
}
// If it looks like a local path, it probably is.
if _, err := os.Stat(rawurl); err == nil {
return lfshttp.EndpointFromLocalPath(rawurl)
}
// We probably got here because the "scheme" that was parsed is
// a hostname (whether FQDN or single word) and the URL parser
// didn't know what to do with it. Do what Git does and treat
// it as an SSH URL. This ensures we handle SSH config aliases
// properly.
return lfshttp.EndpointFromBareSshUrl(u.String())
2016-12-19 18:45:22 +00:00
}
}
func (e *endpointGitFinder) AccessFor(rawurl string) creds.Access {
accessurl := urlWithoutAuth(rawurl)
if e.gitEnv == nil {
return creds.NewAccess(creds.NoneAccess, accessurl)
2016-12-19 18:55:47 +00:00
}
e.accessMu.Lock()
defer e.accessMu.Unlock()
if cached, ok := e.urlAccess[accessurl]; ok {
return creds.NewAccess(cached, accessurl)
}
e.urlAccess[accessurl] = e.fetchGitAccess(accessurl)
return creds.NewAccess(e.urlAccess[accessurl], accessurl)
}
func (e *endpointGitFinder) SetAccess(access creds.Access) {
key := fmt.Sprintf("lfs.%s.access", access.URL())
tracerx.Printf("setting repository access to %s", access.Mode())
e.accessMu.Lock()
defer e.accessMu.Unlock()
switch access.Mode() {
case creds.EmptyAccess, creds.NoneAccess:
2017-10-26 01:20:35 +00:00
e.gitConfig.UnsetLocalKey(key)
e.urlAccess[access.URL()] = creds.NoneAccess
default:
e.gitConfig.SetLocal(key, string(access.Mode()))
e.urlAccess[access.URL()] = access.Mode()
}
}
func urlWithoutAuth(rawurl string) string {
if !strings.Contains(rawurl, "@") {
return rawurl
}
u, err := url.Parse(rawurl)
if err != nil {
fmt.Fprintf(os.Stderr, "Error parsing URL %q: %s", rawurl, err)
return rawurl
}
u.User = nil
return u.String()
}
func (e *endpointGitFinder) fetchGitAccess(rawurl string) creds.AccessMode {
if v, _ := e.urlConfig.Get("lfs", rawurl, "access"); len(v) > 0 {
access := creds.AccessMode(strings.ToLower(v))
if access == creds.PrivateAccess {
return creds.BasicAccess
2016-12-19 18:55:47 +00:00
}
return access
2016-12-19 18:55:47 +00:00
}
return creds.NoneAccess
2016-12-19 18:55:47 +00:00
}
2016-12-19 18:45:22 +00:00
func (e *endpointGitFinder) GitProtocol() string {
return e.gitProtocol
}
// ReplaceUrlAlias returns a url with a prefix from a `url.*.insteadof` git
// config setting. If multiple aliases match, use the longest one.
// See https://git-scm.com/docs/git-config for Git's docs.
func (e *endpointGitFinder) ReplaceUrlAlias(operation, rawurl string) string {
2016-12-19 18:45:22 +00:00
e.aliasMu.Lock()
defer e.aliasMu.Unlock()
if operation == "upload" {
if rawurl, replaced := e.replaceUrlAlias(e.pushAliases, rawurl); replaced {
return rawurl
}
}
rawurl, _ = e.replaceUrlAlias(e.aliases, rawurl)
return rawurl
}
// replaceUrlAlias is a helper function for ReplaceUrlAlias. It must only be
// called while the e.aliasMu mutex is held.
func (e *endpointGitFinder) replaceUrlAlias(aliases map[string]string, rawurl string) (string, bool) {
2016-12-19 18:45:22 +00:00
var longestalias string
for alias, _ := range aliases {
2016-12-19 18:45:22 +00:00
if !strings.HasPrefix(rawurl, alias) {
continue
}
if longestalias < alias {
longestalias = alias
}
}
if len(longestalias) > 0 {
return aliases[longestalias] + rawurl[len(longestalias):], true
2016-12-19 18:45:22 +00:00
}
return rawurl, false
2016-12-19 18:45:22 +00:00
}
const (
aliasPrefix = "url."
)
func initAliases(e *endpointGitFinder, git config.Environment) {
2016-12-19 18:45:22 +00:00
suffix := ".insteadof"
pushSuffix := ".pushinsteadof"
2016-12-19 18:45:22 +00:00
for gitkey, gitval := range git.All() {
if len(gitval) == 0 || !strings.HasPrefix(gitkey, aliasPrefix) {
2016-12-19 18:45:22 +00:00
continue
}
if strings.HasSuffix(gitkey, suffix) {
storeAlias(e.aliases, gitkey, gitval, suffix)
} else if strings.HasSuffix(gitkey, pushSuffix) {
storeAlias(e.pushAliases, gitkey, gitval, pushSuffix)
}
}
}
func storeAlias(aliases map[string]string, key string, values []string, suffix string) {
for _, value := range values {
url := key[len(aliasPrefix) : len(key)-len(suffix)]
if v, ok := aliases[value]; ok && v != url {
2021-12-14 16:05:42 +00:00
fmt.Fprintf(os.Stderr, tr.Tr.Get("warning: Multiple 'url.*.%s' keys with the same alias: %q\n", suffix, value))
}
aliases[value] = url
2016-12-19 18:45:22 +00:00
}
}
func endpointFromGitUrl(u *url.URL, e *endpointGitFinder) lfshttp.Endpoint {
u.Scheme = e.gitProtocol
return lfshttp.Endpoint{Url: u.String()}
}