@@ -10,6 +10,7 @@ import (
1010 "strings"
1111
1212 "code.gitea.io/gitea/modules/git"
13+ "code.gitea.io/gitea/modules/reqctx"
1314 "code.gitea.io/gitea/modules/setting"
1415 "code.gitea.io/gitea/modules/util"
1516)
@@ -38,63 +39,32 @@ func OpenWikiRepository(ctx context.Context, repo Repository) (*git.Repository,
3839
3940// contextKey is a value for use with context.WithValue.
4041type contextKey struct {
41- name string
42- }
43-
44- // RepositoryContextKey is a context key. It is used with context.Value() to get the current Repository for the context
45- var RepositoryContextKey = & contextKey {"repository" }
46-
47- // RepositoryFromContext attempts to get the repository from the context
48- func repositoryFromContext (ctx context.Context , repo Repository ) * git.Repository {
49- value := ctx .Value (RepositoryContextKey )
50- if value == nil {
51- return nil
52- }
53-
54- if gitRepo , ok := value .(* git.Repository ); ok && gitRepo != nil {
55- if gitRepo .Path == repoPath (repo ) {
56- return gitRepo
57- }
58- }
59-
60- return nil
42+ repoPath string
6143}
6244
6345// RepositoryFromContextOrOpen attempts to get the repository from the context or just opens it
6446func RepositoryFromContextOrOpen (ctx context.Context , repo Repository ) (* git.Repository , io.Closer , error ) {
65- gitRepo := repositoryFromContext (ctx , repo )
66- if gitRepo != nil {
67- return gitRepo , util.NopCloser {}, nil
47+ ds := reqctx .GetRequestDataStore (ctx )
48+ if ds != nil {
49+ gitRepo , err := RepositoryFromRequestContextOrOpen (ctx , ds , repo )
50+ return gitRepo , util.NopCloser {}, err
6851 }
69-
7052 gitRepo , err := OpenRepository (ctx , repo )
7153 return gitRepo , gitRepo , err
7254}
7355
74- // repositoryFromContextPath attempts to get the repository from the context
75- func repositoryFromContextPath (ctx context.Context , path string ) * git.Repository {
76- value := ctx .Value (RepositoryContextKey )
77- if value == nil {
78- return nil
56+ // RepositoryFromRequestContextOrOpen opens the repository at the given relative path in the provided request context
57+ // The repo will be automatically closed when the request context is done
58+ func RepositoryFromRequestContextOrOpen (ctx context.Context , ds reqctx.RequestDataStore , repo Repository ) (* git.Repository , error ) {
59+ ck := contextKey {repoPath : repoPath (repo )}
60+ if gitRepo , ok := ctx .Value (ck ).(* git.Repository ); ok {
61+ return gitRepo , nil
7962 }
80-
81- if repo , ok := value .(* git.Repository ); ok && repo != nil {
82- if repo .Path == path {
83- return repo
84- }
63+ gitRepo , err := git .OpenRepository (ctx , ck .repoPath )
64+ if err != nil {
65+ return nil , err
8566 }
86-
87- return nil
88- }
89-
90- // RepositoryFromContextOrOpenPath attempts to get the repository from the context or just opens it
91- // Deprecated: Use RepositoryFromContextOrOpen instead
92- func RepositoryFromContextOrOpenPath (ctx context.Context , path string ) (* git.Repository , io.Closer , error ) {
93- gitRepo := repositoryFromContextPath (ctx , path )
94- if gitRepo != nil {
95- return gitRepo , util.NopCloser {}, nil
96- }
97-
98- gitRepo , err := git .OpenRepository (ctx , path )
99- return gitRepo , gitRepo , err
67+ ds .AddCloser (gitRepo )
68+ ds .SetContextValue (ck , gitRepo )
69+ return gitRepo , nil
10070}
0 commit comments