History

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. ---------

Lunny Xiao committed 2 years ago

Commit 5f82ead1 parent 60e4a9884 files · +426 −273Browse files

Showing the changes to modules/git/repo_base.go (1 of 84 files). Show all

Unified · Split

modules/git/repo_base.go+0−44
@@ -3,48 +3,4 @@ · 2 unmodified lines
33
44 package git
55
6−import (
7− "context"
8− "io"
9−)
10−
116 var isGogit bool
12−
13−// contextKey is a value for use with context.WithValue.
14−type contextKey struct {
15− name string
16−}
17−
18−// RepositoryContextKey is a context key. It is used with context.Value() to get the current Repository for the context
19−var RepositoryContextKey = &contextKey{"repository"}
20−
21−// RepositoryFromContext attempts to get the repository from the context
22−func RepositoryFromContext(ctx context.Context, path string) *Repository {
23− value := ctx.Value(RepositoryContextKey)
24− if value == nil {
25− return nil
26− }
27−
28− if repo, ok := value.(*Repository); ok && repo != nil {
29− if repo.Path == path {
30− return repo
31− }
32− }
33−
34− return nil
35−}
36−
37−type nopCloser func()
38−
39−func (nopCloser) Close() error { return nil }
40−
41−// RepositoryFromContextOrOpen attempts to get the repository from the context or just opens it
42−func RepositoryFromContextOrOpen(ctx context.Context, path string) (*Repository, io.Closer, error) {
43− gitRepo := RepositoryFromContext(ctx, path)
44− if gitRepo != nil {
45− return gitRepo, nopCloser(nil), nil
46− }
47−
48− gitRepo, err := OpenRepository(ctx, path)
49− return gitRepo, gitRepo, err
50−}

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t