Skip to content

Commit 0202a09

Browse files
committed
refactor git repo usage
1 parent 586d304 commit 0202a09

File tree

16 files changed

+83
-63
lines changed

16 files changed

+83
-63
lines changed

modules/gitrepo/gitrepo.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,19 +43,20 @@ type contextKey struct {
4343
}
4444

4545
// RepositoryFromContextOrOpen attempts to get the repository from the context or just opens it
46+
// The caller must call "defer gitRepo.Close()"
4647
func RepositoryFromContextOrOpen(ctx context.Context, repo Repository) (*git.Repository, io.Closer, error) {
47-
ds := reqctx.GetRequestDataStore(ctx)
48-
if ds != nil {
49-
gitRepo, err := RepositoryFromRequestContextOrOpen(ctx, ds, repo)
48+
reqCtx := reqctx.FromContext(ctx)
49+
if reqCtx != nil {
50+
gitRepo, err := RepositoryFromRequestContextOrOpen(reqCtx, repo)
5051
return gitRepo, util.NopCloser{}, err
5152
}
5253
gitRepo, err := OpenRepository(ctx, repo)
5354
return gitRepo, gitRepo, err
5455
}
5556

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) {
57+
// RepositoryFromRequestContextOrOpen opens the repository at the given relative path in the provided request context.
58+
// Caller shouldn't close the git repo manually, the git repo will be automatically closed when the request context is done.
59+
func RepositoryFromRequestContextOrOpen(ctx reqctx.RequestContext, repo Repository) (*git.Repository, error) {
5960
ck := contextKey{repoPath: repoPath(repo)}
6061
if gitRepo, ok := ctx.Value(ck).(*git.Repository); ok {
6162
return gitRepo, nil
@@ -64,7 +65,7 @@ func RepositoryFromRequestContextOrOpen(ctx context.Context, ds reqctx.RequestDa
6465
if err != nil {
6566
return nil, err
6667
}
67-
ds.AddCloser(gitRepo)
68-
ds.SetContextValue(ck, gitRepo)
68+
ctx.AddCloser(gitRepo)
69+
ctx.SetContextValue(ck, gitRepo)
6970
return gitRepo, nil
7071
}

modules/reqctx/datastore.go

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,21 @@ func (r *requestDataStore) cleanUp() {
8888
}
8989
}
9090

91+
type RequestContext interface {
92+
context.Context
93+
RequestDataStore
94+
}
95+
96+
func FromContext(ctx context.Context) RequestContext {
97+
// here we must use the current ctx and the underlying store
98+
// the current ctx guarantees that the ctx deadline/cancellation/values are respected
99+
// the underlying store guarantees that the request-specific data is available
100+
if store := GetRequestDataStore(ctx); store != nil {
101+
return &requestContext{Context: ctx, RequestDataStore: store}
102+
}
103+
return nil
104+
}
105+
91106
func GetRequestDataStore(ctx context.Context) RequestDataStore {
92107
if req, ok := ctx.Value(RequestDataStoreKey).(*requestDataStore); ok {
93108
return req
@@ -97,27 +112,28 @@ func GetRequestDataStore(ctx context.Context) RequestDataStore {
97112

98113
type requestContext struct {
99114
context.Context
100-
dataStore *requestDataStore
115+
RequestDataStore
101116
}
102117

103118
func (c *requestContext) Value(key any) any {
104-
if v := c.dataStore.GetContextValue(key); v != nil {
119+
if v := c.GetContextValue(key); v != nil {
105120
return v
106121
}
107122
return c.Context.Value(key)
108123
}
109124

110125
func NewRequestContext(parentCtx context.Context, profDesc string) (_ context.Context, finished func()) {
111126
ctx, _, processFinished := process.GetManager().AddTypedContext(parentCtx, profDesc, process.RequestProcessType, true)
112-
reqCtx := &requestContext{Context: ctx, dataStore: &requestDataStore{values: make(map[any]any)}}
127+
store := &requestDataStore{values: make(map[any]any)}
128+
reqCtx := &requestContext{Context: ctx, RequestDataStore: store}
113129
return reqCtx, func() {
114-
reqCtx.dataStore.cleanUp()
130+
store.cleanUp()
115131
processFinished()
116132
}
117133
}
118134

119135
// NewRequestContextForTest creates a new RequestContext for testing purposes
120136
// It doesn't add the context to the process manager, nor do cleanup
121137
func NewRequestContextForTest(parentCtx context.Context) context.Context {
122-
return &requestContext{Context: parentCtx, dataStore: &requestDataStore{values: make(map[any]any)}}
138+
return &requestContext{Context: parentCtx, RequestDataStore: &requestDataStore{values: make(map[any]any)}}
123139
}

modules/templates/helper.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,6 @@ func QueryBuild(a ...any) template.URL {
286286
reqPath = s1 + "?"
287287
s = s2
288288
}
289-
290289
}
291290
for i := len(a) % 2; i < len(a); i += 2 {
292291
k, ok := a[i].(string)

routers/api/v1/repo/branch.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -729,7 +729,7 @@ func CreateBranchProtection(ctx *context.APIContext) {
729729
} else {
730730
if !isPlainRule {
731731
if ctx.Repo.GitRepo == nil {
732-
ctx.Repo.GitRepo, err = gitrepo.RepositoryFromRequestContextOrOpen(ctx, ctx, ctx.Repo.Repository)
732+
ctx.Repo.GitRepo, err = gitrepo.RepositoryFromRequestContextOrOpen(ctx, ctx.Repo.Repository)
733733
if err != nil {
734734
ctx.Error(http.StatusInternalServerError, "OpenRepository", err)
735735
return
@@ -1057,7 +1057,7 @@ func EditBranchProtection(ctx *context.APIContext) {
10571057
} else {
10581058
if !isPlainRule {
10591059
if ctx.Repo.GitRepo == nil {
1060-
ctx.Repo.GitRepo, err = gitrepo.RepositoryFromRequestContextOrOpen(ctx, ctx, ctx.Repo.Repository)
1060+
ctx.Repo.GitRepo, err = gitrepo.RepositoryFromRequestContextOrOpen(ctx, ctx.Repo.Repository)
10611061
if err != nil {
10621062
ctx.Error(http.StatusInternalServerError, "OpenRepository", err)
10631063
return

routers/api/v1/repo/compare.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func CompareDiff(ctx *context.APIContext) {
4545

4646
if ctx.Repo.GitRepo == nil {
4747
var err error
48-
ctx.Repo.GitRepo, err = gitrepo.RepositoryFromRequestContextOrOpen(ctx, ctx, ctx.Repo.Repository)
48+
ctx.Repo.GitRepo, err = gitrepo.RepositoryFromRequestContextOrOpen(ctx, ctx.Repo.Repository)
4949
if err != nil {
5050
ctx.Error(http.StatusInternalServerError, "OpenRepository", err)
5151
return

routers/api/v1/repo/download.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func DownloadArchive(ctx *context.APIContext) {
2929

3030
if ctx.Repo.GitRepo == nil {
3131
var err error
32-
ctx.Repo.GitRepo, err = gitrepo.RepositoryFromRequestContextOrOpen(ctx, ctx, ctx.Repo.Repository)
32+
ctx.Repo.GitRepo, err = gitrepo.RepositoryFromRequestContextOrOpen(ctx, ctx.Repo.Repository)
3333
if err != nil {
3434
ctx.Error(http.StatusInternalServerError, "OpenRepository", err)
3535
return

routers/api/v1/repo/file.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ func GetArchive(ctx *context.APIContext) {
282282

283283
if ctx.Repo.GitRepo == nil {
284284
var err error
285-
ctx.Repo.GitRepo, err = gitrepo.RepositoryFromRequestContextOrOpen(ctx, ctx, ctx.Repo.Repository)
285+
ctx.Repo.GitRepo, err = gitrepo.RepositoryFromRequestContextOrOpen(ctx, ctx.Repo.Repository)
286286
if err != nil {
287287
ctx.Error(http.StatusInternalServerError, "OpenRepository", err)
288288
return

routers/api/v1/repo/repo.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -726,7 +726,7 @@ func updateBasicProperties(ctx *context.APIContext, opts api.EditRepoOption) err
726726

727727
if ctx.Repo.GitRepo == nil && !repo.IsEmpty {
728728
var err error
729-
ctx.Repo.GitRepo, err = gitrepo.RepositoryFromRequestContextOrOpen(ctx, ctx, repo)
729+
ctx.Repo.GitRepo, err = gitrepo.RepositoryFromRequestContextOrOpen(ctx, repo)
730730
if err != nil {
731731
ctx.Error(http.StatusInternalServerError, "Unable to OpenRepository", err)
732732
return err

routers/private/internal_repo.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func RepoAssignment(ctx *gitea_context.PrivateContext) {
2727
return
2828
}
2929

30-
gitRepo, err := gitrepo.RepositoryFromRequestContextOrOpen(ctx, ctx, repo)
30+
gitRepo, err := gitrepo.RepositoryFromRequestContextOrOpen(ctx, repo)
3131
if err != nil {
3232
log.Error("Failed to open repository: %s/%s Error: %v", ownerName, repoName, err)
3333
ctx.JSON(http.StatusInternalServerError, private.Response{

routers/web/org/home.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -178,16 +178,15 @@ type prepareOrgProfileReadmeOptions struct {
178178

179179
func prepareOrgProfileReadme(ctx *context.Context, opts prepareOrgProfileReadmeOptions) bool {
180180
profileRepoName := util.Iif(opts.viewAsPrivate, shared_user.RepoNameProfilePrivate, shared_user.RepoNameProfile)
181-
profileDbRepo, profileGitRepo, profileReadme, profileClose := shared_user.FindOwnerProfileReadme(ctx, ctx.Doer, profileRepoName)
182-
defer profileClose()
181+
profileDbRepo, profileReadme := shared_user.FindOwnerProfileReadme(ctx, ctx.Doer, profileRepoName)
183182

184183
if opts.viewAsPrivate {
185184
ctx.Data["HasPrivateProfileReadme"] = profileReadme != nil
186185
} else {
187186
ctx.Data["HasPublicProfileReadme"] = profileReadme != nil
188187
}
189188

190-
if profileGitRepo == nil || profileReadme == nil || opts.viewRepositories {
189+
if profileReadme == nil || opts.viewRepositories {
191190
return false
192191
}
193192

0 commit comments

Comments
 (0)