Skip to content

Commit ca988f3

Browse files
committed
For temprory batch catfile, it should not be assigned to the repository's member fields
1 parent 7674eaf commit ca988f3

File tree

3 files changed

+86
-36
lines changed

3 files changed

+86
-36
lines changed

modules/git/batch.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Copyright 2024 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package git
5+
6+
import (
7+
"bufio"
8+
"context"
9+
)
10+
11+
type Batch struct {
12+
cancel context.CancelFunc
13+
Reader *bufio.Reader
14+
Writer WriteCloserError
15+
repo *Repository
16+
}
17+
18+
func (repo *Repository) NewBatch(ctx context.Context) *Batch {
19+
batch := Batch{
20+
repo: repo,
21+
}
22+
batch.Writer, batch.Reader, batch.cancel = CatFileBatch(ctx, repo.Path)
23+
return &batch
24+
}
25+
26+
func (repo *Repository) NewBatchCheck(ctx context.Context) *Batch {
27+
check := Batch{
28+
repo: repo,
29+
}
30+
check.Writer, check.Reader, check.cancel = CatFileBatchCheck(ctx, repo.Path)
31+
return &check
32+
}
33+
34+
func (b *Batch) Close() {
35+
if b.cancel != nil {
36+
b.cancel()
37+
b.Reader = nil
38+
b.Writer = nil
39+
b.cancel = nil
40+
}
41+
}

modules/git/repo_base_nogogit.go

Lines changed: 35 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,11 @@ type Repository struct {
2525

2626
gpgSettings *GPGSettings
2727

28-
batchInUse bool
29-
batchCancel context.CancelFunc
30-
batchReader *bufio.Reader
31-
batchWriter WriteCloserError
28+
batchInUse bool
29+
batch *Batch
3230

33-
checkInUse bool
34-
checkCancel context.CancelFunc
35-
checkReader *bufio.Reader
36-
checkWriter WriteCloserError
31+
checkInUse bool
32+
check *Batch
3733

3834
Ctx context.Context
3935
LastCommitCache *LastCommitCache
@@ -66,52 +62,59 @@ func OpenRepository(ctx context.Context, repoPath string) (*Repository, error) {
6662
Ctx: ctx,
6763
}
6864

69-
repo.batchWriter, repo.batchReader, repo.batchCancel = CatFileBatch(ctx, repoPath)
70-
repo.checkWriter, repo.checkReader, repo.checkCancel = CatFileBatchCheck(ctx, repoPath)
65+
repo.batch = repo.NewBatch(ctx)
66+
repo.check = repo.NewBatchCheck(ctx)
7167

7268
return repo, nil
7369
}
7470

7571
// CatFileBatch obtains a CatFileBatch for this repository
7672
func (repo *Repository) CatFileBatch(ctx context.Context) (WriteCloserError, *bufio.Reader, func()) {
77-
if repo.batchCancel == nil || repo.batchInUse {
78-
log.Debug("Opening temporary cat file batch for: %s", repo.Path)
79-
return CatFileBatch(ctx, repo.Path)
73+
if repo.batch == nil {
74+
repo.batch = repo.NewBatch(ctx)
8075
}
81-
repo.batchInUse = true
82-
return repo.batchWriter, repo.batchReader, func() {
83-
repo.batchInUse = false
76+
77+
if !repo.batchInUse {
78+
repo.batchInUse = true
79+
return repo.batch.Writer, repo.batch.Reader, func() {
80+
repo.batchInUse = false
81+
}
8482
}
83+
84+
log.Debug("Opening temporary cat file batch for: %s", repo.Path)
85+
tempBatch := repo.NewBatch(ctx)
86+
return tempBatch.Writer, tempBatch.Reader, tempBatch.Close
8587
}
8688

8789
// CatFileBatchCheck obtains a CatFileBatchCheck for this repository
8890
func (repo *Repository) CatFileBatchCheck(ctx context.Context) (WriteCloserError, *bufio.Reader, func()) {
89-
if repo.checkCancel == nil || repo.checkInUse {
90-
log.Debug("Opening temporary cat file batch-check for: %s", repo.Path)
91-
return CatFileBatchCheck(ctx, repo.Path)
91+
if repo.check == nil {
92+
repo.check = repo.NewBatchCheck(ctx)
9293
}
93-
repo.checkInUse = true
94-
return repo.checkWriter, repo.checkReader, func() {
95-
repo.checkInUse = false
94+
95+
if !repo.checkInUse {
96+
return repo.check.Writer, repo.check.Reader, func() {
97+
repo.checkInUse = false
98+
}
9699
}
100+
101+
log.Debug("Opening temporary cat file batch-check for: %s", repo.Path)
102+
tempBatchCheck := repo.NewBatchCheck(ctx)
103+
return tempBatchCheck.Writer, tempBatchCheck.Reader, tempBatchCheck.Close
97104
}
98105

99106
func (repo *Repository) Close() error {
100107
if repo == nil {
101108
return nil
102109
}
103-
if repo.batchCancel != nil {
104-
repo.batchCancel()
105-
repo.batchReader = nil
106-
repo.batchWriter = nil
107-
repo.batchCancel = nil
110+
if repo.batch != nil {
111+
repo.batch.Close()
112+
repo.batch = nil
108113
repo.batchInUse = false
109114
}
110-
if repo.checkCancel != nil {
111-
repo.checkCancel()
112-
repo.checkCancel = nil
113-
repo.checkReader = nil
114-
repo.checkWriter = nil
115+
if repo.check != nil {
116+
repo.check.Close()
117+
repo.check = nil
115118
repo.checkInUse = false
116119
}
117120
repo.LastCommitCache = nil

modules/indexer/code/elasticsearch/elasticsearch.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"code.gitea.io/gitea/modules/analyze"
1616
"code.gitea.io/gitea/modules/charset"
1717
"code.gitea.io/gitea/modules/git"
18+
"code.gitea.io/gitea/modules/gitrepo"
1819
"code.gitea.io/gitea/modules/indexer/code/internal"
1920
indexer_internal "code.gitea.io/gitea/modules/indexer/internal"
2021
inner_elasticsearch "code.gitea.io/gitea/modules/indexer/internal/elasticsearch"
@@ -160,19 +161,24 @@ func (b *Indexer) Index(ctx context.Context, repo *repo_model.Repository, sha st
160161
return err
161162
}
162163

163-
batchWriter, batchReader, cancel := git.CatFileBatch(ctx, repo.RepoPath())
164-
defer cancel()
164+
r, err := gitrepo.OpenRepository(ctx, repo)
165+
if err != nil {
166+
return err
167+
}
168+
defer r.Close()
169+
batch := r.NewBatch(ctx)
170+
defer batch.Close()
165171

166172
for _, update := range changes.Updates {
167-
updateReqs, err := b.addUpdate(ctx, batchWriter, batchReader, sha, update, repo)
173+
updateReqs, err := b.addUpdate(ctx, batch.Writer, batch.Reader, sha, update, repo)
168174
if err != nil {
169175
return err
170176
}
171177
if len(updateReqs) > 0 {
172178
reqs = append(reqs, updateReqs...)
173179
}
174180
}
175-
cancel()
181+
batch.Close()
176182
}
177183

178184
for _, filename := range changes.RemovedFilenames {

0 commit comments

Comments
 (0)