lfsapi: cache the remote list for each endpoint lookup

When we're locking or unlocking multiple times, we may end up looking up
the endpoint a few times.  This is very cheap if we don't need to invoke
Git.  However, as written right now, we call Git every time we validate
a remote, which is wasteful.

To make this more efficient, let's compute the remote list once and
cache it to avoid spawning a process.  Then, we can use the cached value
every time we need to validate a remote.
This commit is contained in:
brian m. carlson 2023-10-26 18:50:14 +00:00
parent d2cdff0d68
commit b925d5309f
No known key found for this signature in database
GPG Key ID: 2D0C9BC12F82B3A1

@ -41,6 +41,7 @@ type endpointGitFinder struct {
aliasMu sync.Mutex
aliases map[string]string
pushAliases map[string]string
remoteList []string
accessMu sync.Mutex
urlAccess map[string]creds.AccessMode
@ -61,6 +62,9 @@ func NewEndpointFinder(ctx lfshttp.Context) EndpointFinder {
urlAccess: make(map[string]creds.AccessMode),
}
remotes, _ := git.RemoteList()
e.remoteList = remotes
e.urlConfig = config.NewURLConfig(e.gitEnv)
if v, ok := e.gitEnv.Get("lfs.gitprotocol"); ok {
e.gitProtocol = v
@ -187,7 +191,7 @@ func (e *endpointGitFinder) GitRemoteURL(remote string, forpush bool) string {
}
}
if err := git.ValidateRemote(remote); err == nil {
if err := git.ValidateRemoteFromList(e.remoteList, remote); err == nil {
return remote
}