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/gitrepo (4 of 84 files). Show all

Unified · Split

4 files
  1. modules/gitrepo/branch.go +32 −0
  2. modules/gitrepo/gitrepo.go +103 −0
  3. modules/gitrepo/walk_gogit.go +40 −0
  4. modules/gitrepo/walk_nogogit.go +17 −0
modules/gitrepo/branch.go+32−0
@@ -0,0 +1,32 @@
1+// Copyright 2024 The Gitea Authors. All rights reserved.
2+// SPDX-License-Identifier: MIT
3+
4+package gitrepo
5+
6+import (
7+ "context"
8+
9+ "code.gitea.io/gitea/modules/git"
10+)
11+
12+// GetBranchesByPath returns a branch by its path
13+// if limit = 0 it will not limit
14+func GetBranchesByPath(ctx context.Context, repo Repository, skip, limit int) ([]*git.Branch, int, error) {
15+ gitRepo, err := OpenRepository(ctx, repo)
16+ if err != nil {
17+ return nil, 0, err
18+ }
19+ defer gitRepo.Close()
20+
21+ return gitRepo.GetBranches(skip, limit)
22+}
23+
24+func GetBranchCommitID(ctx context.Context, repo Repository, branch string) (string, error) {
25+ gitRepo, err := OpenRepository(ctx, repo)
26+ if err != nil {
27+ return "", err
28+ }
29+ defer gitRepo.Close()
30+
31+ return gitRepo.GetBranchCommitID(branch)
32+}
modules/gitrepo/gitrepo.go+103−0
@@ -0,0 +1,103 @@
1+// Copyright 2024 The Gitea Authors. All rights reserved.
2+// SPDX-License-Identifier: MIT
3+
4+package gitrepo
5+
6+import (
7+ "context"
8+ "io"
9+ "path/filepath"
10+ "strings"
11+
12+ "code.gitea.io/gitea/modules/git"
13+ "code.gitea.io/gitea/modules/setting"
14+)
15+
16+type Repository interface {
17+ GetName() string
18+ GetOwnerName() string
19+}
20+
21+func repoPath(repo Repository) string {
22+ return filepath.Join(setting.RepoRootPath, strings.ToLower(repo.GetOwnerName()), strings.ToLower(repo.GetName())+".git")
23+}
24+
25+func wikiPath(repo Repository) string {
26+ return filepath.Join(setting.RepoRootPath, strings.ToLower(repo.GetOwnerName()), strings.ToLower(repo.GetName())+".wiki.git")
27+}
28+
29+// OpenRepository opens the repository at the given relative path with the provided context.
30+func OpenRepository(ctx context.Context, repo Repository) (*git.Repository, error) {
31+ return git.OpenRepository(ctx, repoPath(repo))
32+}
33+
34+func OpenWikiRepository(ctx context.Context, repo Repository) (*git.Repository, error) {
35+ return git.OpenRepository(ctx, wikiPath(repo))
36+}
37+
38+// contextKey is a value for use with context.WithValue.
39+type contextKey struct {
40+ name string
41+}
42+
43+// RepositoryContextKey is a context key. It is used with context.Value() to get the current Repository for the context
44+var RepositoryContextKey = &contextKey{"repository"}
45+
46+// RepositoryFromContext attempts to get the repository from the context
47+func repositoryFromContext(ctx context.Context, repo Repository) *git.Repository {
48+ value := ctx.Value(RepositoryContextKey)
49+ if value == nil {
50+ return nil
51+ }
52+
53+ if gitRepo, ok := value.(*git.Repository); ok && gitRepo != nil {
54+ if gitRepo.Path == repoPath(repo) {
55+ return gitRepo
56+ }
57+ }
58+
59+ return nil
60+}
61+
62+type nopCloser func()
63+
64+func (nopCloser) Close() error { return nil }
65+
66+// RepositoryFromContextOrOpen attempts to get the repository from the context or just opens it
67+func RepositoryFromContextOrOpen(ctx context.Context, repo Repository) (*git.Repository, io.Closer, error) {
68+ gitRepo := repositoryFromContext(ctx, repo)
69+ if gitRepo != nil {
70+ return gitRepo, nopCloser(nil), nil
71+ }
72+
73+ gitRepo, err := OpenRepository(ctx, repo)
74+ return gitRepo, gitRepo, err
75+}
76+
77+// repositoryFromContextPath attempts to get the repository from the context
78+func repositoryFromContextPath(ctx context.Context, path string) *git.Repository {
79+ value := ctx.Value(RepositoryContextKey)
80+ if value == nil {
81+ return nil
82+ }
83+
84+ if repo, ok := value.(*git.Repository); ok && repo != nil {
85+ if repo.Path == path {
86+ return repo
87+ }
88+ }
89+
90+ return nil
91+}
92+
93+// RepositoryFromContextOrOpenPath attempts to get the repository from the context or just opens it
94+// Deprecated: Use RepositoryFromContextOrOpen instead
95+func RepositoryFromContextOrOpenPath(ctx context.Context, path string) (*git.Repository, io.Closer, error) {
96+ gitRepo := repositoryFromContextPath(ctx, path)
97+ if gitRepo != nil {
98+ return gitRepo, nopCloser(nil), nil
99+ }
100+
101+ gitRepo, err := git.OpenRepository(ctx, path)
102+ return gitRepo, gitRepo, err
103+}
modules/gitrepo/walk_gogit.go+40−0
@@ -0,0 +1,40 @@
1+// Copyright 2024 The Gitea Authors. All rights reserved.
2+// SPDX-License-Identifier: MIT
3+
4+//go:build gogit
5+
6+package gitrepo
7+
8+import (
9+ "context"
10+
11+ "github.com/go-git/go-git/v5/plumbing"
12+)
13+
14+// WalkReferences walks all the references from the repository
15+// refname is empty, ObjectTag or ObjectBranch. All other values should be treated as equivalent to empty.
16+func WalkReferences(ctx context.Context, repo Repository, walkfn func(sha1, refname string) error) (int, error) {
17+ gitRepo := repositoryFromContext(ctx, repo)
18+ if gitRepo == nil {
19+ var err error
20+ gitRepo, err = OpenRepository(ctx, repo)
21+ if err != nil {
22+ return 0, err
23+ }
24+ defer gitRepo.Close()
25+ }
26+
27+ i := 0
28+ iter, err := gitRepo.GoGitRepo().References()
29+ if err != nil {
30+ return i, err
31+ }
32+ defer iter.Close()
33+
34+ err = iter.ForEach(func(ref *plumbing.Reference) error {
35+ err := walkfn(ref.Hash().String(), string(ref.Name()))
36+ i++
37+ return err
38+ })
39+ return i, err
40+}
modules/gitrepo/walk_nogogit.go+17−0
@@ -0,0 +1,17 @@
1+// Copyright 2024 The Gitea Authors. All rights reserved.
2+// SPDX-License-Identifier: MIT
3+
4+//go:build !gogit
5+
6+package gitrepo
7+
8+import (
9+ "context"
10+
11+ "code.gitea.io/gitea/modules/git"
12+)
13+
14+// WalkReferences walks all the references from the repository
15+func WalkReferences(ctx context.Context, repo Repository, walkfn func(sha1, refname string) error) (int, error) {
16+ return git.WalkShowRef(ctx, repoPath(repo), nil, 0, 0, walkfn)
17+}