@@ -10,6 +10,7 @@ import (
10
10
"strings"
11
11
12
12
"code.gitea.io/gitea/modules/git"
13
+ "code.gitea.io/gitea/modules/reqctx"
13
14
"code.gitea.io/gitea/modules/setting"
14
15
"code.gitea.io/gitea/modules/util"
15
16
)
@@ -38,63 +39,32 @@ func OpenWikiRepository(ctx context.Context, repo Repository) (*git.Repository,
38
39
39
40
// contextKey is a value for use with context.WithValue.
40
41
type 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
61
43
}
62
44
63
45
// RepositoryFromContextOrOpen attempts to get the repository from the context or just opens it
64
46
func 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
68
51
}
69
-
70
52
gitRepo , err := OpenRepository (ctx , repo )
71
53
return gitRepo , gitRepo , err
72
54
}
73
55
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
79
62
}
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
85
66
}
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
100
70
}
0 commit comments