mirror of
				https://github.com/go-gitea/gitea.git
				synced 2025-10-29 10:57:44 +09:00 
			
		
		
		
	Decouple Batch from git.Repository to simplify usage without requiring the creation of a Repository struct. (#34001)
No logic change
This commit is contained in:
		| @@ -14,25 +14,26 @@ type Batch struct { | |||||||
| 	Writer WriteCloserError | 	Writer WriteCloserError | ||||||
| } | } | ||||||
|  |  | ||||||
| func (repo *Repository) NewBatch(ctx context.Context) (*Batch, error) { | // NewBatch creates a new batch for the given repository, the Close must be invoked before release the batch | ||||||
|  | func NewBatch(ctx context.Context, repoPath string) (*Batch, error) { | ||||||
| 	// 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! | 	// 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! | ||||||
| 	if err := ensureValidGitRepository(ctx, repo.Path); err != nil { | 	if err := ensureValidGitRepository(ctx, repoPath); err != nil { | ||||||
| 		return nil, err | 		return nil, err | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	var batch Batch | 	var batch Batch | ||||||
| 	batch.Writer, batch.Reader, batch.cancel = catFileBatch(ctx, repo.Path) | 	batch.Writer, batch.Reader, batch.cancel = catFileBatch(ctx, repoPath) | ||||||
| 	return &batch, nil | 	return &batch, nil | ||||||
| } | } | ||||||
|  |  | ||||||
| func (repo *Repository) NewBatchCheck(ctx context.Context) (*Batch, error) { | func NewBatchCheck(ctx context.Context, repoPath string) (*Batch, error) { | ||||||
| 	// 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! | 	// 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! | ||||||
| 	if err := ensureValidGitRepository(ctx, repo.Path); err != nil { | 	if err := ensureValidGitRepository(ctx, repoPath); err != nil { | ||||||
| 		return nil, err | 		return nil, err | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	var check Batch | 	var check Batch | ||||||
| 	check.Writer, check.Reader, check.cancel = catFileBatchCheck(ctx, repo.Path) | 	check.Writer, check.Reader, check.cancel = catFileBatchCheck(ctx, repoPath) | ||||||
| 	return &check, nil | 	return &check, nil | ||||||
| } | } | ||||||
|  |  | ||||||
|   | |||||||
| @@ -67,7 +67,7 @@ func OpenRepository(ctx context.Context, repoPath string) (*Repository, error) { | |||||||
| func (repo *Repository) CatFileBatch(ctx context.Context) (WriteCloserError, *bufio.Reader, func(), error) { | func (repo *Repository) CatFileBatch(ctx context.Context) (WriteCloserError, *bufio.Reader, func(), error) { | ||||||
| 	if repo.batch == nil { | 	if repo.batch == nil { | ||||||
| 		var err error | 		var err error | ||||||
| 		repo.batch, err = repo.NewBatch(ctx) | 		repo.batch, err = NewBatch(ctx, repo.Path) | ||||||
| 		if err != nil { | 		if err != nil { | ||||||
| 			return nil, nil, nil, err | 			return nil, nil, nil, err | ||||||
| 		} | 		} | ||||||
| @@ -81,7 +81,7 @@ func (repo *Repository) CatFileBatch(ctx context.Context) (WriteCloserError, *bu | |||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	log.Debug("Opening temporary cat file batch for: %s", repo.Path) | 	log.Debug("Opening temporary cat file batch for: %s", repo.Path) | ||||||
| 	tempBatch, err := repo.NewBatch(ctx) | 	tempBatch, err := NewBatch(ctx, repo.Path) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return nil, nil, nil, err | 		return nil, nil, nil, err | ||||||
| 	} | 	} | ||||||
| @@ -92,7 +92,7 @@ func (repo *Repository) CatFileBatch(ctx context.Context) (WriteCloserError, *bu | |||||||
| func (repo *Repository) CatFileBatchCheck(ctx context.Context) (WriteCloserError, *bufio.Reader, func(), error) { | func (repo *Repository) CatFileBatchCheck(ctx context.Context) (WriteCloserError, *bufio.Reader, func(), error) { | ||||||
| 	if repo.check == nil { | 	if repo.check == nil { | ||||||
| 		var err error | 		var err error | ||||||
| 		repo.check, err = repo.NewBatchCheck(ctx) | 		repo.check, err = NewBatchCheck(ctx, repo.Path) | ||||||
| 		if err != nil { | 		if err != nil { | ||||||
| 			return nil, nil, nil, err | 			return nil, nil, nil, err | ||||||
| 		} | 		} | ||||||
| @@ -106,7 +106,7 @@ func (repo *Repository) CatFileBatchCheck(ctx context.Context) (WriteCloserError | |||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	log.Debug("Opening temporary cat file batch-check for: %s", repo.Path) | 	log.Debug("Opening temporary cat file batch-check for: %s", repo.Path) | ||||||
| 	tempBatchCheck, err := repo.NewBatchCheck(ctx) | 	tempBatchCheck, err := NewBatchCheck(ctx, repo.Path) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return nil, nil, nil, err | 		return nil, nil, nil, err | ||||||
| 	} | 	} | ||||||
|   | |||||||
| @@ -16,7 +16,6 @@ import ( | |||||||
| 	"code.gitea.io/gitea/modules/analyze" | 	"code.gitea.io/gitea/modules/analyze" | ||||||
| 	"code.gitea.io/gitea/modules/charset" | 	"code.gitea.io/gitea/modules/charset" | ||||||
| 	"code.gitea.io/gitea/modules/git" | 	"code.gitea.io/gitea/modules/git" | ||||||
| 	"code.gitea.io/gitea/modules/gitrepo" |  | ||||||
| 	"code.gitea.io/gitea/modules/indexer" | 	"code.gitea.io/gitea/modules/indexer" | ||||||
| 	path_filter "code.gitea.io/gitea/modules/indexer/code/bleve/token/path" | 	path_filter "code.gitea.io/gitea/modules/indexer/code/bleve/token/path" | ||||||
| 	"code.gitea.io/gitea/modules/indexer/code/internal" | 	"code.gitea.io/gitea/modules/indexer/code/internal" | ||||||
| @@ -217,12 +216,7 @@ func (b *Indexer) addDelete(filename string, repo *repo_model.Repository, batch | |||||||
| func (b *Indexer) Index(ctx context.Context, repo *repo_model.Repository, sha string, changes *internal.RepoChanges) error { | func (b *Indexer) Index(ctx context.Context, repo *repo_model.Repository, sha string, changes *internal.RepoChanges) error { | ||||||
| 	batch := inner_bleve.NewFlushingBatch(b.inner.Indexer, maxBatchSize) | 	batch := inner_bleve.NewFlushingBatch(b.inner.Indexer, maxBatchSize) | ||||||
| 	if len(changes.Updates) > 0 { | 	if len(changes.Updates) > 0 { | ||||||
| 		r, err := gitrepo.OpenRepository(ctx, repo) | 		gitBatch, err := git.NewBatch(ctx, repo.RepoPath()) | ||||||
| 		if err != nil { |  | ||||||
| 			return err |  | ||||||
| 		} |  | ||||||
| 		defer r.Close() |  | ||||||
| 		gitBatch, err := r.NewBatch(ctx) |  | ||||||
| 		if err != nil { | 		if err != nil { | ||||||
| 			return err | 			return err | ||||||
| 		} | 		} | ||||||
|   | |||||||
| @@ -15,7 +15,6 @@ import ( | |||||||
| 	"code.gitea.io/gitea/modules/analyze" | 	"code.gitea.io/gitea/modules/analyze" | ||||||
| 	"code.gitea.io/gitea/modules/charset" | 	"code.gitea.io/gitea/modules/charset" | ||||||
| 	"code.gitea.io/gitea/modules/git" | 	"code.gitea.io/gitea/modules/git" | ||||||
| 	"code.gitea.io/gitea/modules/gitrepo" |  | ||||||
| 	"code.gitea.io/gitea/modules/indexer" | 	"code.gitea.io/gitea/modules/indexer" | ||||||
| 	"code.gitea.io/gitea/modules/indexer/code/internal" | 	"code.gitea.io/gitea/modules/indexer/code/internal" | ||||||
| 	indexer_internal "code.gitea.io/gitea/modules/indexer/internal" | 	indexer_internal "code.gitea.io/gitea/modules/indexer/internal" | ||||||
| @@ -209,12 +208,7 @@ func (b *Indexer) addDelete(filename string, repo *repo_model.Repository) elasti | |||||||
| func (b *Indexer) Index(ctx context.Context, repo *repo_model.Repository, sha string, changes *internal.RepoChanges) error { | func (b *Indexer) Index(ctx context.Context, repo *repo_model.Repository, sha string, changes *internal.RepoChanges) error { | ||||||
| 	reqs := make([]elastic.BulkableRequest, 0) | 	reqs := make([]elastic.BulkableRequest, 0) | ||||||
| 	if len(changes.Updates) > 0 { | 	if len(changes.Updates) > 0 { | ||||||
| 		r, err := gitrepo.OpenRepository(ctx, repo) | 		batch, err := git.NewBatch(ctx, repo.RepoPath()) | ||||||
| 		if err != nil { |  | ||||||
| 			return err |  | ||||||
| 		} |  | ||||||
| 		defer r.Close() |  | ||||||
| 		batch, err := r.NewBatch(ctx) |  | ||||||
| 		if err != nil { | 		if err != nil { | ||||||
| 			return err | 			return err | ||||||
| 		} | 		} | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user