mirror of
https://github.com/go-gitea/gitea.git
synced 2026-08-01 15:14:16 +00:00
4d6e172a0d
Unfortunately, we can't completely remove the ctx from git.Repository, because the CatFileBatch still heavily depends on a parent context. If we remove the Repository ctx, the CatFileBatch will become a mess and create a lot of unnecessary git processes. http://localhost:3000/-/admin/monitor/perftrace * Before: open a repo home, dozens of git processes (duplicate cat-file) * After: only a few (no duplicate cat-file)
52 lines
1.6 KiB
Go
52 lines
1.6 KiB
Go
// Copyright 2024 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package git
|
|
|
|
import (
|
|
"context"
|
|
"io"
|
|
|
|
"gitea.dev/modules/git/gitcmd"
|
|
"gitea.dev/modules/reqctx"
|
|
"gitea.dev/modules/util"
|
|
)
|
|
|
|
// contextKey is a value for use with context.WithValue.
|
|
type contextKey struct {
|
|
key string
|
|
}
|
|
|
|
// RepositoryFromContextOrOpen attempts to get the repository from the context or just opens it
|
|
// The caller must call Closer.Close()
|
|
func RepositoryFromContextOrOpen(ctx context.Context, repo RepositoryFacade) (*Repository, io.Closer, error) {
|
|
reqCtx := reqctx.FromContext(ctx)
|
|
if reqCtx != nil {
|
|
gitRepo, err := RepositoryFromRequestContextOrOpen(reqCtx, repo)
|
|
return gitRepo, util.NopCloser{}, err
|
|
}
|
|
gitRepo, err := OpenRepository(ctx, repo)
|
|
return gitRepo, gitRepo, err
|
|
}
|
|
|
|
// RepositoryFromRequestContextOrOpen opens the repository at the given relative path in the provided request context.
|
|
// Caller shouldn't close the git repo manually, the git repo will be automatically closed when the request context is done.
|
|
func RepositoryFromRequestContextOrOpen(ctx reqctx.RequestContext, repo RepositoryFacade) (*Repository, error) {
|
|
ck := contextKey{key: repo.GitRepoLocation()}
|
|
if gitRepo, ok := ctx.Value(ck).(*Repository); ok {
|
|
return gitRepo, nil
|
|
}
|
|
gitRepo, err := OpenRepository(ctx, repo)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
ctx.AddCloser(gitRepo)
|
|
ctx.SetContextValue(ck, gitRepo)
|
|
return gitRepo, nil
|
|
}
|
|
|
|
func UpdateServerInfo(ctx context.Context, repo RepositoryFacade) error {
|
|
_, _, err := gitcmd.NewCommand("update-server-info").WithRepo(repo).RunStdBytes(ctx)
|
|
return err
|
|
}
|