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>
2024-01-28 04:09:51 +08:00
|
|
|
// Copyright 2024 The Gitea Authors. All rights reserved.
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
|
|
|
package gitrepo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"io"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"code.gitea.io/gitea/modules/git"
|
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2024-11-04 18:59:50 +08:00
|
|
|
"code.gitea.io/gitea/modules/util"
|
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>
2024-01-28 04:09:51 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type Repository interface {
|
|
|
|
GetName() string
|
|
|
|
GetOwnerName() string
|
|
|
|
}
|
|
|
|
|
|
|
|
func repoPath(repo Repository) string {
|
|
|
|
return filepath.Join(setting.RepoRootPath, strings.ToLower(repo.GetOwnerName()), strings.ToLower(repo.GetName())+".git")
|
|
|
|
}
|
|
|
|
|
|
|
|
func wikiPath(repo Repository) string {
|
|
|
|
return filepath.Join(setting.RepoRootPath, strings.ToLower(repo.GetOwnerName()), strings.ToLower(repo.GetName())+".wiki.git")
|
|
|
|
}
|
|
|
|
|
|
|
|
// OpenRepository opens the repository at the given relative path with the provided context.
|
|
|
|
func OpenRepository(ctx context.Context, repo Repository) (*git.Repository, error) {
|
|
|
|
return git.OpenRepository(ctx, repoPath(repo))
|
|
|
|
}
|
|
|
|
|
|
|
|
func OpenWikiRepository(ctx context.Context, repo Repository) (*git.Repository, error) {
|
|
|
|
return git.OpenRepository(ctx, wikiPath(repo))
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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, repo Repository) *git.Repository {
|
|
|
|
value := ctx.Value(RepositoryContextKey)
|
|
|
|
if value == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if gitRepo, ok := value.(*git.Repository); ok && gitRepo != nil {
|
|
|
|
if gitRepo.Path == repoPath(repo) {
|
|
|
|
return gitRepo
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// RepositoryFromContextOrOpen attempts to get the repository from the context or just opens it
|
|
|
|
func RepositoryFromContextOrOpen(ctx context.Context, repo Repository) (*git.Repository, io.Closer, error) {
|
|
|
|
gitRepo := repositoryFromContext(ctx, repo)
|
|
|
|
if gitRepo != nil {
|
2024-11-04 18:59:50 +08:00
|
|
|
return gitRepo, util.NopCloser{}, nil
|
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>
2024-01-28 04:09:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
gitRepo, err := OpenRepository(ctx, repo)
|
|
|
|
return gitRepo, gitRepo, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// repositoryFromContextPath attempts to get the repository from the context
|
|
|
|
func repositoryFromContextPath(ctx context.Context, path string) *git.Repository {
|
|
|
|
value := ctx.Value(RepositoryContextKey)
|
|
|
|
if value == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if repo, ok := value.(*git.Repository); ok && repo != nil {
|
|
|
|
if repo.Path == path {
|
|
|
|
return repo
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// RepositoryFromContextOrOpenPath attempts to get the repository from the context or just opens it
|
|
|
|
// Deprecated: Use RepositoryFromContextOrOpen instead
|
|
|
|
func RepositoryFromContextOrOpenPath(ctx context.Context, path string) (*git.Repository, io.Closer, error) {
|
|
|
|
gitRepo := repositoryFromContextPath(ctx, path)
|
|
|
|
if gitRepo != nil {
|
2024-11-04 18:59:50 +08:00
|
|
|
return gitRepo, util.NopCloser{}, nil
|
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>
2024-01-28 04:09:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
gitRepo, err := git.OpenRepository(ctx, path)
|
|
|
|
return gitRepo, gitRepo, err
|
|
|
|
}
|