Skip to content

Commit 0e8e20a

Browse files
committed
Small refactor for catfile
1 parent 82bc8b8 commit 0e8e20a

File tree

4 files changed

+15
-26
lines changed

4 files changed

+15
-26
lines changed

modules/git/batch.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,26 @@ type Batch struct {
1414
Writer WriteCloserError
1515
}
1616

17-
func (repo *Repository) NewBatch(ctx context.Context) (*Batch, error) {
17+
// NewBatch creates a new batch for the given repository, the Close must be invoked before release the batch
18+
func NewBatch(ctx context.Context, repoPath string) (*Batch, error) {
1819
// Now because of some insanity with git cat-file not immediately failing if not run in a valid git directory we need to run git rev-parse first!
19-
if err := ensureValidGitRepository(ctx, repo.Path); err != nil {
20+
if err := ensureValidGitRepository(ctx, repoPath); err != nil {
2021
return nil, err
2122
}
2223

2324
var batch Batch
24-
batch.Writer, batch.Reader, batch.cancel = catFileBatch(ctx, repo.Path)
25+
batch.Writer, batch.Reader, batch.cancel = catFileBatch(ctx, repoPath)
2526
return &batch, nil
2627
}
2728

28-
func (repo *Repository) NewBatchCheck(ctx context.Context) (*Batch, error) {
29+
func NewBatchCheck(ctx context.Context, repoPath string) (*Batch, error) {
2930
// Now because of some insanity with git cat-file not immediately failing if not run in a valid git directory we need to run git rev-parse first!
30-
if err := ensureValidGitRepository(ctx, repo.Path); err != nil {
31+
if err := ensureValidGitRepository(ctx, repoPath); err != nil {
3132
return nil, err
3233
}
3334

3435
var check Batch
35-
check.Writer, check.Reader, check.cancel = catFileBatchCheck(ctx, repo.Path)
36+
check.Writer, check.Reader, check.cancel = catFileBatchCheck(ctx, repoPath)
3637
return &check, nil
3738
}
3839

modules/git/repo_base_nogogit.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func OpenRepository(ctx context.Context, repoPath string) (*Repository, error) {
6262
func (repo *Repository) CatFileBatch(ctx context.Context) (WriteCloserError, *bufio.Reader, func(), error) {
6363
if repo.batch == nil {
6464
var err error
65-
repo.batch, err = repo.NewBatch(ctx)
65+
repo.batch, err = NewBatch(ctx, repo.Path)
6666
if err != nil {
6767
return nil, nil, nil, err
6868
}
@@ -76,7 +76,7 @@ func (repo *Repository) CatFileBatch(ctx context.Context) (WriteCloserError, *bu
7676
}
7777

7878
log.Debug("Opening temporary cat file batch for: %s", repo.Path)
79-
tempBatch, err := repo.NewBatch(ctx)
79+
tempBatch, err := NewBatch(ctx, repo.Path)
8080
if err != nil {
8181
return nil, nil, nil, err
8282
}
@@ -87,7 +87,7 @@ func (repo *Repository) CatFileBatch(ctx context.Context) (WriteCloserError, *bu
8787
func (repo *Repository) CatFileBatchCheck(ctx context.Context) (WriteCloserError, *bufio.Reader, func(), error) {
8888
if repo.check == nil {
8989
var err error
90-
repo.check, err = repo.NewBatchCheck(ctx)
90+
repo.check, err = NewBatchCheck(ctx, repo.Path)
9191
if err != nil {
9292
return nil, nil, nil, err
9393
}
@@ -101,7 +101,7 @@ func (repo *Repository) CatFileBatchCheck(ctx context.Context) (WriteCloserError
101101
}
102102

103103
log.Debug("Opening temporary cat file batch-check for: %s", repo.Path)
104-
tempBatchCheck, err := repo.NewBatchCheck(ctx)
104+
tempBatchCheck, err := NewBatchCheck(ctx, repo.Path)
105105
if err != nil {
106106
return nil, nil, nil, err
107107
}

modules/indexer/code/bleve/bleve.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import (
1616
"code.gitea.io/gitea/modules/analyze"
1717
"code.gitea.io/gitea/modules/charset"
1818
"code.gitea.io/gitea/modules/git"
19-
"code.gitea.io/gitea/modules/gitrepo"
2019
"code.gitea.io/gitea/modules/indexer"
2120
path_filter "code.gitea.io/gitea/modules/indexer/code/bleve/token/path"
2221
"code.gitea.io/gitea/modules/indexer/code/internal"
@@ -217,19 +216,14 @@ func (b *Indexer) addDelete(filename string, repo *repo_model.Repository, batch
217216
func (b *Indexer) Index(ctx context.Context, repo *repo_model.Repository, sha string, changes *internal.RepoChanges) error {
218217
batch := inner_bleve.NewFlushingBatch(b.inner.Indexer, maxBatchSize)
219218
if len(changes.Updates) > 0 {
220-
r, err := gitrepo.OpenRepository(ctx, repo)
219+
gitBatch, err := git.NewBatch(ctx, repo.RepoPath())
221220
if err != nil {
222221
return err
223222
}
224-
defer r.Close()
225-
gitBatch, err := r.NewBatch(ctx)
226-
if err != nil {
227-
return err
228-
}
229-
defer gitBatch.Close()
230223

231224
for _, update := range changes.Updates {
232225
if err := b.addUpdate(ctx, gitBatch.Writer, gitBatch.Reader, sha, update, repo, batch); err != nil {
226+
gitBatch.Close()
233227
return err
234228
}
235229
}

modules/indexer/code/elasticsearch/elasticsearch.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ 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"
1918
"code.gitea.io/gitea/modules/indexer"
2019
"code.gitea.io/gitea/modules/indexer/code/internal"
2120
indexer_internal "code.gitea.io/gitea/modules/indexer/internal"
@@ -209,20 +208,15 @@ func (b *Indexer) addDelete(filename string, repo *repo_model.Repository) elasti
209208
func (b *Indexer) Index(ctx context.Context, repo *repo_model.Repository, sha string, changes *internal.RepoChanges) error {
210209
reqs := make([]elastic.BulkableRequest, 0)
211210
if len(changes.Updates) > 0 {
212-
r, err := gitrepo.OpenRepository(ctx, repo)
211+
batch, err := git.NewBatch(ctx, repo.RepoPath())
213212
if err != nil {
214213
return err
215214
}
216-
defer r.Close()
217-
batch, err := r.NewBatch(ctx)
218-
if err != nil {
219-
return err
220-
}
221-
defer batch.Close()
222215

223216
for _, update := range changes.Updates {
224217
updateReqs, err := b.addUpdate(ctx, batch.Writer, batch.Reader, sha, update, repo)
225218
if err != nil {
219+
batch.Close()
226220
return err
227221
}
228222
if len(updateReqs) > 0 {

0 commit comments

Comments
 (0)