Skip to content

Commit c1b9ecc

Browse files
authored
Decouple Batch from git.Repository to simplify usage without requiring the creation of a Repository struct. (#34001)
No logic change
1 parent 053592d commit c1b9ecc

File tree

4 files changed

+13
-24
lines changed

4 files changed

+13
-24
lines changed

modules/git/batch.go

+7-6
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

+4-4
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func OpenRepository(ctx context.Context, repoPath string) (*Repository, error) {
6767
func (repo *Repository) CatFileBatch(ctx context.Context) (WriteCloserError, *bufio.Reader, func(), error) {
6868
if repo.batch == nil {
6969
var err error
70-
repo.batch, err = repo.NewBatch(ctx)
70+
repo.batch, err = NewBatch(ctx, repo.Path)
7171
if err != nil {
7272
return nil, nil, nil, err
7373
}
@@ -81,7 +81,7 @@ func (repo *Repository) CatFileBatch(ctx context.Context) (WriteCloserError, *bu
8181
}
8282

8383
log.Debug("Opening temporary cat file batch for: %s", repo.Path)
84-
tempBatch, err := repo.NewBatch(ctx)
84+
tempBatch, err := NewBatch(ctx, repo.Path)
8585
if err != nil {
8686
return nil, nil, nil, err
8787
}
@@ -92,7 +92,7 @@ func (repo *Repository) CatFileBatch(ctx context.Context) (WriteCloserError, *bu
9292
func (repo *Repository) CatFileBatchCheck(ctx context.Context) (WriteCloserError, *bufio.Reader, func(), error) {
9393
if repo.check == nil {
9494
var err error
95-
repo.check, err = repo.NewBatchCheck(ctx)
95+
repo.check, err = NewBatchCheck(ctx, repo.Path)
9696
if err != nil {
9797
return nil, nil, nil, err
9898
}
@@ -106,7 +106,7 @@ func (repo *Repository) CatFileBatchCheck(ctx context.Context) (WriteCloserError
106106
}
107107

108108
log.Debug("Opening temporary cat file batch-check for: %s", repo.Path)
109-
tempBatchCheck, err := repo.NewBatchCheck(ctx)
109+
tempBatchCheck, err := NewBatchCheck(ctx, repo.Path)
110110
if err != nil {
111111
return nil, nil, nil, err
112112
}

modules/indexer/code/bleve/bleve.go

+1-7
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,12 +216,7 @@ 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)
221-
if err != nil {
222-
return err
223-
}
224-
defer r.Close()
225-
gitBatch, err := r.NewBatch(ctx)
219+
gitBatch, err := git.NewBatch(ctx, repo.RepoPath())
226220
if err != nil {
227221
return err
228222
}

modules/indexer/code/elasticsearch/elasticsearch.go

+1-7
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,12 +208,7 @@ 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)
213-
if err != nil {
214-
return err
215-
}
216-
defer r.Close()
217-
batch, err := r.NewBatch(ctx)
211+
batch, err := git.NewBatch(ctx, repo.RepoPath())
218212
if err != nil {
219213
return err
220214
}

0 commit comments

Comments
 (0)