mirror of
https://github.com/go-gitea/gitea
synced 2025-07-22 18:28:37 +00:00
Simplify how git repositories are opened (#28937)
## Purpose This is a refactor toward building an abstraction over managing git repositories. Afterwards, it does not matter anymore if they are stored on the local disk or somewhere remote. ## What this PR changes We used `git.OpenRepository` everywhere previously. Now, we should split them into two distinct functions: Firstly, there are temporary repositories which do not change: ```go git.OpenRepository(ctx, diskPath) ``` Gitea managed repositories having a record in the database in the `repository` table are moved into the new package `gitrepo`: ```go gitrepo.OpenRepository(ctx, repo_model.Repo) ``` Why is `repo_model.Repository` the second parameter instead of file path? Because then we can easily adapt our repository storage strategy. The repositories can be stored locally, however, they could just as well be stored on a remote server. ## Further changes in other PRs - A Git Command wrapper on package `gitrepo` could be created. i.e. `NewCommand(ctx, repo_model.Repository, commands...)`. `git.RunOpts{Dir: repo.RepoPath()}`, the directory should be empty before invoking this method and it can be filled in the function only. #28940 - Remove the `RepoPath()`/`WikiPath()` functions to reduce the possibility of mistakes. --------- Co-authored-by: delvh <dev.lh@web.de>
This commit is contained in:
@@ -3,48 +3,4 @@
|
||||
|
||||
package git
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
)
|
||||
|
||||
var isGogit bool
|
||||
|
||||
// contextKey is a value for use with context.WithValue.
|
||||
type contextKey struct {
|
||||
name string
|
||||
}
|
||||
|
||||
// RepositoryContextKey is a context key. It is used with context.Value() to get the current Repository for the context
|
||||
var RepositoryContextKey = &contextKey{"repository"}
|
||||
|
||||
// RepositoryFromContext attempts to get the repository from the context
|
||||
func RepositoryFromContext(ctx context.Context, path string) *Repository {
|
||||
value := ctx.Value(RepositoryContextKey)
|
||||
if value == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
if repo, ok := value.(*Repository); ok && repo != nil {
|
||||
if repo.Path == path {
|
||||
return repo
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
type nopCloser func()
|
||||
|
||||
func (nopCloser) Close() error { return nil }
|
||||
|
||||
// RepositoryFromContextOrOpen attempts to get the repository from the context or just opens it
|
||||
func RepositoryFromContextOrOpen(ctx context.Context, path string) (*Repository, io.Closer, error) {
|
||||
gitRepo := RepositoryFromContext(ctx, path)
|
||||
if gitRepo != nil {
|
||||
return gitRepo, nopCloser(nil), nil
|
||||
}
|
||||
|
||||
gitRepo, err := OpenRepository(ctx, path)
|
||||
return gitRepo, gitRepo, err
|
||||
}
|
||||
|
Reference in New Issue
Block a user