Skip to content

Commit 396f6db

Browse files
committed
fix git repo
1 parent 98a0b71 commit 396f6db

File tree

13 files changed

+137
-147
lines changed

13 files changed

+137
-147
lines changed

modules/gitrepo/gitrepo.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ func RepositoryFromContextOrOpen(ctx context.Context, repo Repository) (*git.Rep
5555

5656
// RepositoryFromRequestContextOrOpen opens the repository at the given relative path in the provided request context
5757
// The repo will be automatically closed when the request context is done
58-
func RepositoryFromRequestContextOrOpen(ctx *reqctx.RequestContext, repo Repository) (*git.Repository, error) {
58+
func RepositoryFromRequestContextOrOpen(ctx reqctx.RequestContext, repo Repository) (*git.Repository, error) {
5959
ck := contextKey{repoPath: repoPath(repo)}
6060
if gitRepo, ok := ctx.Value(ck).(*git.Repository); ok {
6161
return gitRepo, nil

modules/reqctx/reqctx.go

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package reqctx
55

66
import (
77
"context"
8+
"io"
89
"sync"
910
"time"
1011

@@ -15,8 +16,16 @@ type RequestContextKeyType struct{}
1516

1617
var RequestContextKey RequestContextKeyType
1718

19+
type RequestContext interface {
20+
context.Context
21+
SetContextValue(k, v any)
22+
GetData() ContextData
23+
AddCleanUp(f func())
24+
AddCloser(c io.Closer)
25+
}
26+
1827
// RequestContext is a short-lived context that is used to store request-specific data.
19-
type RequestContext struct {
28+
type requestContext struct {
2029
ctx context.Context
2130
data ContextData
2231

@@ -26,19 +35,19 @@ type RequestContext struct {
2635
cleanUpFuncs []func()
2736
}
2837

29-
func (r *RequestContext) Deadline() (deadline time.Time, ok bool) {
38+
func (r *requestContext) Deadline() (deadline time.Time, ok bool) {
3039
return r.ctx.Deadline()
3140
}
3241

33-
func (r *RequestContext) Done() <-chan struct{} {
42+
func (r *requestContext) Done() <-chan struct{} {
3443
return r.ctx.Done()
3544
}
3645

37-
func (r *RequestContext) Err() error {
46+
func (r *requestContext) Err() error {
3847
return r.ctx.Err()
3948
}
4049

41-
func (r *RequestContext) Value(key any) any {
50+
func (r *requestContext) Value(key any) any {
4251
if key == RequestContextKey {
4352
return r
4453
}
@@ -51,42 +60,46 @@ func (r *RequestContext) Value(key any) any {
5160
return r.ctx.Value(key)
5261
}
5362

54-
func (r *RequestContext) SetContextValue(k, v any) {
63+
func (r *requestContext) SetContextValue(k, v any) {
5564
r.mu.Lock()
5665
r.values[k] = v
5766
r.mu.Unlock()
5867
}
5968

6069
// GetData and the underlying ContextData are not thread-safe, callers should ensure thread-safety.
61-
func (r *RequestContext) GetData() ContextData {
70+
func (r *requestContext) GetData() ContextData {
6271
if r.data == nil {
6372
r.data = make(ContextData)
6473
}
6574
return r.data
6675
}
6776

68-
func (r *RequestContext) AddCleanUp(f func()) {
77+
func (r *requestContext) AddCleanUp(f func()) {
6978
r.mu.Lock()
7079
r.cleanUpFuncs = append(r.cleanUpFuncs, f)
7180
r.mu.Unlock()
7281
}
7382

74-
func (r *RequestContext) cleanUp() {
83+
func (r *requestContext) AddCloser(c io.Closer) {
84+
r.AddCleanUp(func() { _ = c.Close() })
85+
}
86+
87+
func (r *requestContext) cleanUp() {
7588
for _, f := range r.cleanUpFuncs {
7689
f()
7790
}
7891
}
7992

80-
func GetRequestContext(ctx context.Context) *RequestContext {
81-
if req, ok := ctx.Value(RequestContextKey).(*RequestContext); ok {
93+
func GetRequestContext(ctx context.Context) RequestContext {
94+
if req, ok := ctx.Value(RequestContextKey).(*requestContext); ok {
8295
return req
8396
}
8497
return nil
8598
}
8699

87-
func NewRequestContext(parentCtx context.Context, profDesc string) (_ *RequestContext, finished func()) {
100+
func NewRequestContext(parentCtx context.Context, profDesc string) (_ RequestContext, finished func()) {
88101
ctx, _, processFinished := process.GetManager().AddTypedContext(parentCtx, profDesc, process.RequestProcessType, true)
89-
reqCtx := &RequestContext{ctx: ctx, values: make(map[any]any)}
102+
reqCtx := &requestContext{ctx: ctx, values: make(map[any]any)}
90103
return reqCtx, func() {
91104
reqCtx.cleanUp()
92105
processFinished()
@@ -95,7 +108,7 @@ func NewRequestContext(parentCtx context.Context, profDesc string) (_ *RequestCo
95108

96109
// NewRequestContextForTest creates a new RequestContext for testing purposes
97110
// It doesn't add the context to the process manager, nor do cleanup
98-
func NewRequestContextForTest(parentCtx context.Context) *RequestContext {
99-
reqCtx := &RequestContext{ctx: parentCtx, values: make(map[any]any)}
111+
func NewRequestContextForTest(parentCtx context.Context) RequestContext {
112+
reqCtx := &requestContext{ctx: parentCtx, values: make(map[any]any)}
100113
return reqCtx
101114
}

routers/api/v1/repo/branch.go

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -729,15 +729,11 @@ func CreateBranchProtection(ctx *context.APIContext) {
729729
} else {
730730
if !isPlainRule {
731731
if ctx.Repo.GitRepo == nil {
732-
ctx.Repo.GitRepo, err = gitrepo.OpenRepository(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
736736
}
737-
defer func() {
738-
ctx.Repo.GitRepo.Close()
739-
ctx.Repo.GitRepo = nil
740-
}()
741737
}
742738
// FIXME: since we only need to recheck files protected rules, we could improve this
743739
matchedBranches, err := git_model.FindAllMatchedBranches(ctx, ctx.Repo.Repository.ID, ruleName)
@@ -1066,10 +1062,6 @@ func EditBranchProtection(ctx *context.APIContext) {
10661062
ctx.Error(http.StatusInternalServerError, "OpenRepository", err)
10671063
return
10681064
}
1069-
defer func() {
1070-
ctx.Repo.GitRepo.Close()
1071-
ctx.Repo.GitRepo = nil
1072-
}()
10731065
}
10741066

10751067
// FIXME: since we only need to recheck files protected rules, we could improve this

routers/api/v1/repo/compare.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,12 @@ func CompareDiff(ctx *context.APIContext) {
4444
// "$ref": "#/responses/notFound"
4545

4646
if ctx.Repo.GitRepo == nil {
47-
gitRepo, err := gitrepo.OpenRepository(ctx, ctx.Repo.Repository)
47+
var err error
48+
ctx.Repo.GitRepo, err = gitrepo.RepositoryFromRequestContextOrOpen(ctx, ctx.Repo.Repository)
4849
if err != nil {
4950
ctx.Error(http.StatusInternalServerError, "OpenRepository", err)
5051
return
5152
}
52-
ctx.Repo.GitRepo = gitRepo
53-
defer gitRepo.Close()
5453
}
5554

5655
infoPath := ctx.PathParam("*")

routers/api/v1/repo/download.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,12 @@ func DownloadArchive(ctx *context.APIContext) {
2828
}
2929

3030
if ctx.Repo.GitRepo == nil {
31-
gitRepo, err := gitrepo.OpenRepository(ctx, ctx.Repo.Repository)
31+
var err error
32+
ctx.Repo.GitRepo, err = gitrepo.RepositoryFromRequestContextOrOpen(ctx, ctx.Repo.Repository)
3233
if err != nil {
3334
ctx.Error(http.StatusInternalServerError, "OpenRepository", err)
3435
return
3536
}
36-
ctx.Repo.GitRepo = gitRepo
37-
defer gitRepo.Close()
3837
}
3938

4039
r, err := archiver_service.NewRequest(ctx.Repo.Repository.ID, ctx.Repo.GitRepo, ctx.PathParam("*"), tp)

routers/api/v1/repo/file.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -287,13 +287,12 @@ func GetArchive(ctx *context.APIContext) {
287287
// "$ref": "#/responses/notFound"
288288

289289
if ctx.Repo.GitRepo == nil {
290-
gitRepo, err := gitrepo.OpenRepository(ctx, ctx.Repo.Repository)
290+
var err error
291+
ctx.Repo.GitRepo, err = gitrepo.RepositoryFromRequestContextOrOpen(ctx, ctx.Repo.Repository)
291292
if err != nil {
292293
ctx.Error(http.StatusInternalServerError, "OpenRepository", err)
293294
return
294295
}
295-
ctx.Repo.GitRepo = gitRepo
296-
defer gitRepo.Close()
297296
}
298297

299298
archiveDownload(ctx)

routers/api/v1/repo/repo.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -726,12 +726,11 @@ 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.OpenRepository(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
733733
}
734-
defer ctx.Repo.GitRepo.Close()
735734
}
736735

737736
// Default branch only updated if changed and exist or the repository is empty

routers/api/v1/repo/transfer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ func Transfer(ctx *context.APIContext) {
100100
}
101101

102102
if ctx.Repo.GitRepo != nil {
103-
ctx.Repo.GitRepo.Close()
103+
_ = ctx.Repo.GitRepo.Close()
104104
ctx.Repo.GitRepo = nil
105105
}
106106

routers/private/internal_repo.go

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
package private
55

66
import (
7-
"context"
87
"fmt"
98
"net/http"
109

@@ -18,14 +17,14 @@ import (
1817
// This file contains common functions relating to setting the Repository for the internal routes
1918

2019
// RepoAssignment assigns the repository and gitrepository to the private context
21-
func RepoAssignment(ctx *gitea_context.PrivateContext) context.CancelFunc {
20+
func RepoAssignment(ctx *gitea_context.PrivateContext) {
2221
ownerName := ctx.PathParam(":owner")
2322
repoName := ctx.PathParam(":repo")
2423

2524
repo := loadRepository(ctx, ownerName, repoName)
2625
if ctx.Written() {
2726
// Error handled in loadRepository
28-
return nil
27+
return
2928
}
3029

3130
gitRepo, err := gitrepo.OpenRepository(ctx, repo)
@@ -34,23 +33,14 @@ func RepoAssignment(ctx *gitea_context.PrivateContext) context.CancelFunc {
3433
ctx.JSON(http.StatusInternalServerError, private.Response{
3534
Err: fmt.Sprintf("Failed to open repository: %s/%s Error: %v", ownerName, repoName, err),
3635
})
37-
return nil
36+
return
3837
}
3938

4039
ctx.Repo = &gitea_context.Repository{
4140
Repository: repo,
4241
GitRepo: gitRepo,
4342
}
44-
45-
// We opened it, we should close it
46-
cancel := func() {
47-
// If it's been set to nil then assume someone else has closed it.
48-
if ctx.Repo.GitRepo != nil {
49-
ctx.Repo.GitRepo.Close()
50-
}
51-
}
52-
53-
return cancel
43+
ctx.AddCloser(gitRepo) // We opened it, we should close it
5444
}
5545

5646
func loadRepository(ctx *gitea_context.PrivateContext, ownerName, repoName string) *repo_model.Repository {

routers/web/web.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
package web
55

66
import (
7-
gocontext "context"
87
"net/http"
98
"strings"
109

@@ -1521,24 +1520,24 @@ func registerRoutes(m *web.Router) {
15211520

15221521
m.Group("/blob_excerpt", func() {
15231522
m.Get("/{sha}", repo.SetEditorconfigIfExists, repo.SetDiffViewStyle, repo.ExcerptBlob)
1524-
}, func(ctx *context.Context) gocontext.CancelFunc {
1523+
}, func(ctx *context.Context) {
15251524
// FIXME: refactor this function, use separate routes for wiki/code
15261525
if ctx.FormBool("wiki") {
15271526
ctx.Data["PageIsWiki"] = true
15281527
repo.MustEnableWiki(ctx)
1529-
return nil
1528+
return
15301529
}
15311530

15321531
if ctx.Written() {
1533-
return nil
1532+
return
15341533
}
1535-
cancel := context.RepoRef()(ctx)
1534+
context.RepoRef()(ctx)
15361535
if ctx.Written() {
1537-
return cancel
1536+
return
15381537
}
15391538

15401539
repo.MustBeNotEmpty(ctx)
1541-
return cancel
1540+
return
15421541
})
15431542

15441543
m.Group("/media", func() {

0 commit comments

Comments
 (0)