lfsapi: avoid invoking Git on every endpoint lookup

Right now, we call `GitDir` to find the Git directory for our repository
every time we look up an endpoint.  However, we _might_ already have
this data in our Git configuration handler, so let's use that if
possible so we don't need to spawn a process every time.  If it's not,
then cache the data so that we spawn at most one process.
This commit is contained in:
brian m. carlson 2023-10-26 18:53:44 +00:00
parent b925d5309f
commit 86399f37eb
No known key found for this signature in database
GPG Key ID: 2D0C9BC12F82B3A1

@ -37,6 +37,7 @@ type endpointGitFinder struct {
gitConfig *git.Configuration
gitEnv config.Environment
gitProtocol string
gitDir string
aliasMu sync.Mutex
aliases map[string]string
@ -53,10 +54,19 @@ func NewEndpointFinder(ctx lfshttp.Context) EndpointFinder {
ctx = lfshttp.NewContext(nil, nil, nil)
}
var gitDir string
cfg := ctx.GitConfig()
if cfg != nil && cfg.GitDir != "" {
gitDir = cfg.GitDir
} else if dir, err := git.GitDir(); err == nil {
gitDir = dir
}
e := &endpointGitFinder{
gitConfig: ctx.GitConfig(),
gitEnv: ctx.GitEnv(),
gitProtocol: "https",
gitDir: gitDir,
aliases: make(map[string]string),
pushAliases: make(map[string]string),
urlAccess: make(map[string]creds.AccessMode),
@ -128,11 +138,10 @@ func (e *endpointGitFinder) RemoteEndpoint(operation, remote string) lfshttp.End
return e.NewEndpointFromCloneURL(operation, url)
}
gitDir, err := git.GitDir()
// Finally, fall back on .git/FETCH_HEAD but only if it exists and no specific remote was requested
// We can't know which remote FETCH_HEAD is pointing to
if err == nil && remote == defaultRemote {
url, err := parseFetchHead(strings.Join([]string{gitDir, "FETCH_HEAD"}, "/"))
if e.gitDir != "" && remote == defaultRemote {
url, err := parseFetchHead(strings.Join([]string{e.gitDir, "FETCH_HEAD"}, "/"))
if err == nil {
endpoint := e.NewEndpointFromCloneURL("download", url)
return endpoint