diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2021-12-10 09:27:50 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-12-10 09:27:50 +0800 |
commit | 719bddcd76610a63dadc8555760072957a11cf30 (patch) | |
tree | 0df26092fba7e3e21444fe493e6b349473b6b0cb /modules/indexer | |
parent | fb8166c6c6b652a0e6fa98681780a6a71090faf3 (diff) | |
download | gitea-719bddcd76610a63dadc8555760072957a11cf30.tar.gz gitea-719bddcd76610a63dadc8555760072957a11cf30.zip |
Move repository model into models/repo (#17933)
* Some refactors related repository model
* Move more methods out of repository
* Move repository into models/repo
* Fix test
* Fix test
* some improvements
* Remove unnecessary function
Diffstat (limited to 'modules/indexer')
-rw-r--r-- | modules/indexer/code/bleve.go | 8 | ||||
-rw-r--r-- | modules/indexer/code/elastic_search.go | 8 | ||||
-rw-r--r-- | modules/indexer/code/git.go | 12 | ||||
-rw-r--r-- | modules/indexer/code/indexer.go | 14 | ||||
-rw-r--r-- | modules/indexer/code/indexer_test.go | 1 | ||||
-rw-r--r-- | modules/indexer/code/wrapped.go | 4 | ||||
-rw-r--r-- | modules/indexer/issues/indexer.go | 5 | ||||
-rw-r--r-- | modules/indexer/issues/indexer_test.go | 1 | ||||
-rw-r--r-- | modules/indexer/stats/db.go | 8 | ||||
-rw-r--r-- | modules/indexer/stats/indexer.go | 4 | ||||
-rw-r--r-- | modules/indexer/stats/indexer_test.go | 9 | ||||
-rw-r--r-- | modules/indexer/stats/queue.go | 4 |
12 files changed, 41 insertions, 37 deletions
diff --git a/modules/indexer/code/bleve.go b/modules/indexer/code/bleve.go index 97d5fb082c..1affdf73b0 100644 --- a/modules/indexer/code/bleve.go +++ b/modules/indexer/code/bleve.go @@ -13,7 +13,7 @@ import ( "strings" "time" - "code.gitea.io/gitea/models" + repo_model "code.gitea.io/gitea/models/repo" "code.gitea.io/gitea/modules/analyze" "code.gitea.io/gitea/modules/charset" "code.gitea.io/gitea/modules/git" @@ -181,7 +181,7 @@ func NewBleveIndexer(indexDir string) (*BleveIndexer, bool, error) { } func (b *BleveIndexer) addUpdate(batchWriter git.WriteCloserError, batchReader *bufio.Reader, commitSha string, - update fileUpdate, repo *models.Repository, batch *gitea_bleve.FlushingBatch) error { + update fileUpdate, repo *repo_model.Repository, batch *gitea_bleve.FlushingBatch) error { // Ignore vendored files in code search if setting.Indexer.ExcludeVendored && analyze.IsVendor(update.Filename) { return nil @@ -234,7 +234,7 @@ func (b *BleveIndexer) addUpdate(batchWriter git.WriteCloserError, batchReader * }) } -func (b *BleveIndexer) addDelete(filename string, repo *models.Repository, batch *gitea_bleve.FlushingBatch) error { +func (b *BleveIndexer) addDelete(filename string, repo *repo_model.Repository, batch *gitea_bleve.FlushingBatch) error { id := filenameIndexerID(repo.ID, filename) return batch.Delete(id) } @@ -271,7 +271,7 @@ func (b *BleveIndexer) Close() { } // Index indexes the data -func (b *BleveIndexer) Index(repo *models.Repository, sha string, changes *repoChanges) error { +func (b *BleveIndexer) Index(repo *repo_model.Repository, sha string, changes *repoChanges) error { batch := gitea_bleve.NewFlushingBatch(b.indexer, maxBatchSize) if len(changes.Updates) > 0 { diff --git a/modules/indexer/code/elastic_search.go b/modules/indexer/code/elastic_search.go index 6e0813dc15..bd5faf3b04 100644 --- a/modules/indexer/code/elastic_search.go +++ b/modules/indexer/code/elastic_search.go @@ -13,7 +13,7 @@ import ( "strings" "time" - "code.gitea.io/gitea/models" + repo_model "code.gitea.io/gitea/models/repo" "code.gitea.io/gitea/modules/analyze" "code.gitea.io/gitea/modules/charset" "code.gitea.io/gitea/modules/git" @@ -177,7 +177,7 @@ func (b *ElasticSearchIndexer) init() (bool, error) { return exists, nil } -func (b *ElasticSearchIndexer) addUpdate(batchWriter git.WriteCloserError, batchReader *bufio.Reader, sha string, update fileUpdate, repo *models.Repository) ([]elastic.BulkableRequest, error) { +func (b *ElasticSearchIndexer) addUpdate(batchWriter git.WriteCloserError, batchReader *bufio.Reader, sha string, update fileUpdate, repo *repo_model.Repository) ([]elastic.BulkableRequest, error) { // Ignore vendored files in code search if setting.Indexer.ExcludeVendored && analyze.IsVendor(update.Filename) { return nil, nil @@ -236,7 +236,7 @@ func (b *ElasticSearchIndexer) addUpdate(batchWriter git.WriteCloserError, batch }, nil } -func (b *ElasticSearchIndexer) addDelete(filename string, repo *models.Repository) elastic.BulkableRequest { +func (b *ElasticSearchIndexer) addDelete(filename string, repo *repo_model.Repository) elastic.BulkableRequest { id := filenameIndexerID(repo.ID, filename) return elastic.NewBulkDeleteRequest(). Index(b.indexerAliasName). @@ -244,7 +244,7 @@ func (b *ElasticSearchIndexer) addDelete(filename string, repo *models.Repositor } // Index will save the index data -func (b *ElasticSearchIndexer) Index(repo *models.Repository, sha string, changes *repoChanges) error { +func (b *ElasticSearchIndexer) Index(repo *repo_model.Repository, sha string, changes *repoChanges) error { reqs := make([]elastic.BulkableRequest, 0) if len(changes.Updates) > 0 { diff --git a/modules/indexer/code/git.go b/modules/indexer/code/git.go index 919d785406..b76f6d6778 100644 --- a/modules/indexer/code/git.go +++ b/modules/indexer/code/git.go @@ -8,7 +8,7 @@ import ( "strconv" "strings" - "code.gitea.io/gitea/models" + repo_model "code.gitea.io/gitea/models/repo" "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/setting" @@ -27,7 +27,7 @@ type repoChanges struct { RemovedFilenames []string } -func getDefaultBranchSha(repo *models.Repository) (string, error) { +func getDefaultBranchSha(repo *repo_model.Repository) (string, error) { stdout, err := git.NewCommand("show-ref", "-s", git.BranchPrefix+repo.DefaultBranch).RunInDir(repo.RepoPath()) if err != nil { return "", err @@ -36,8 +36,8 @@ func getDefaultBranchSha(repo *models.Repository) (string, error) { } // getRepoChanges returns changes to repo since last indexer update -func getRepoChanges(repo *models.Repository, revision string) (*repoChanges, error) { - status, err := repo.GetIndexerStatus(models.RepoIndexerTypeCode) +func getRepoChanges(repo *repo_model.Repository, revision string) (*repoChanges, error) { + status, err := repo_model.GetIndexerStatus(repo, repo_model.RepoIndexerTypeCode) if err != nil { return nil, err } @@ -89,7 +89,7 @@ func parseGitLsTreeOutput(stdout []byte) ([]fileUpdate, error) { } // genesisChanges get changes to add repo to the indexer for the first time -func genesisChanges(repo *models.Repository, revision string) (*repoChanges, error) { +func genesisChanges(repo *repo_model.Repository, revision string) (*repoChanges, error) { var changes repoChanges stdout, err := git.NewCommand("ls-tree", "--full-tree", "-l", "-r", revision). RunInDirBytes(repo.RepoPath()) @@ -101,7 +101,7 @@ func genesisChanges(repo *models.Repository, revision string) (*repoChanges, err } // nonGenesisChanges get changes since the previous indexer update -func nonGenesisChanges(repo *models.Repository, revision string) (*repoChanges, error) { +func nonGenesisChanges(repo *repo_model.Repository, revision string) (*repoChanges, error) { diffCmd := git.NewCommand("diff", "--name-status", repo.CodeIndexerStatus.CommitSha, revision) stdout, err := diffCmd.RunInDir(repo.RepoPath()) diff --git a/modules/indexer/code/indexer.go b/modules/indexer/code/indexer.go index c56b1b2bb0..a616d0e662 100644 --- a/modules/indexer/code/indexer.go +++ b/modules/indexer/code/indexer.go @@ -11,8 +11,8 @@ import ( "strings" "time" - "code.gitea.io/gitea/models" "code.gitea.io/gitea/models/db" + repo_model "code.gitea.io/gitea/models/repo" "code.gitea.io/gitea/modules/graceful" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/queue" @@ -42,7 +42,7 @@ type SearchResultLanguages struct { // Indexer defines an interface to index and search code contents type Indexer interface { - Index(repo *models.Repository, sha string, changes *repoChanges) error + Index(repo *repo_model.Repository, sha string, changes *repoChanges) error Delete(repoID int64) error Search(repoIDs []int64, language, keyword string, page, pageSize int, isMatch bool) (int64, []*SearchResult, []*SearchResultLanguages, error) Close() @@ -83,8 +83,8 @@ var ( ) func index(indexer Indexer, repoID int64) error { - repo, err := models.GetRepositoryByID(repoID) - if models.IsErrRepoNotExist(err) { + repo, err := repo_model.GetRepositoryByID(repoID) + if repo_model.IsErrRepoNotExist(err) { return indexer.Delete(repoID) } if err != nil { @@ -106,7 +106,7 @@ func index(indexer Indexer, repoID int64) error { return err } - return repo.UpdateIndexerStatus(models.RepoIndexerTypeCode, sha) + return repo_model.UpdateIndexerStatus(repo, repo_model.RepoIndexerTypeCode, sha) } // Init initialize the repo indexer @@ -256,7 +256,7 @@ func Init() { } // UpdateRepoIndexer update a repository's entries in the indexer -func UpdateRepoIndexer(repo *models.Repository) { +func UpdateRepoIndexer(repo *repo_model.Repository) { indexData := &IndexerData{RepoID: repo.ID} if err := indexerQueue.Push(indexData); err != nil { log.Error("Update repo index data %v failed: %v", indexData, err) @@ -297,7 +297,7 @@ func populateRepoIndexer(ctx context.Context) { return default: } - ids, err := models.GetUnindexedRepos(models.RepoIndexerTypeCode, maxRepoID, 0, 50) + ids, err := repo_model.GetUnindexedRepos(repo_model.RepoIndexerTypeCode, maxRepoID, 0, 50) if err != nil { log.Error("populateRepoIndexer: %v", err) return diff --git a/modules/indexer/code/indexer_test.go b/modules/indexer/code/indexer_test.go index b8fedcb323..98494afceb 100644 --- a/modules/indexer/code/indexer_test.go +++ b/modules/indexer/code/indexer_test.go @@ -8,6 +8,7 @@ import ( "path/filepath" "testing" + _ "code.gitea.io/gitea/models" "code.gitea.io/gitea/models/unittest" "github.com/stretchr/testify/assert" diff --git a/modules/indexer/code/wrapped.go b/modules/indexer/code/wrapped.go index 5b19f9c625..7e97a6a324 100644 --- a/modules/indexer/code/wrapped.go +++ b/modules/indexer/code/wrapped.go @@ -8,7 +8,7 @@ import ( "fmt" "sync" - "code.gitea.io/gitea/models" + repo_model "code.gitea.io/gitea/models/repo" ) var ( @@ -57,7 +57,7 @@ func (w *wrappedIndexer) get() (Indexer, error) { return w.internal, nil } -func (w *wrappedIndexer) Index(repo *models.Repository, sha string, changes *repoChanges) error { +func (w *wrappedIndexer) Index(repo *repo_model.Repository, sha string, changes *repoChanges) error { indexer, err := w.get() if err != nil { return err diff --git a/modules/indexer/issues/indexer.go b/modules/indexer/issues/indexer.go index 0b855460bd..4db5091762 100644 --- a/modules/indexer/issues/indexer.go +++ b/modules/indexer/issues/indexer.go @@ -13,6 +13,7 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/models/db" + repo_model "code.gitea.io/gitea/models/repo" "code.gitea.io/gitea/modules/graceful" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/queue" @@ -269,7 +270,7 @@ func populateIssueIndexer(ctx context.Context) { } // UpdateRepoIndexer add/update all issues of the repositories -func UpdateRepoIndexer(repo *models.Repository) { +func UpdateRepoIndexer(repo *repo_model.Repository) { is, err := models.Issues(&models.IssuesOptions{ RepoIDs: []int64{repo.ID}, IsClosed: util.OptionalBoolNone, @@ -310,7 +311,7 @@ func UpdateIssueIndexer(issue *models.Issue) { } // DeleteRepoIssueIndexer deletes repo's all issues indexes -func DeleteRepoIssueIndexer(repo *models.Repository) { +func DeleteRepoIssueIndexer(repo *repo_model.Repository) { var ids []int64 ids, err := models.GetIssueIDsByRepoID(repo.ID) if err != nil { diff --git a/modules/indexer/issues/indexer_test.go b/modules/indexer/issues/indexer_test.go index 8353891c7c..0855165556 100644 --- a/modules/indexer/issues/indexer_test.go +++ b/modules/indexer/issues/indexer_test.go @@ -11,6 +11,7 @@ import ( "testing" "time" + _ "code.gitea.io/gitea/models" "code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/util" diff --git a/modules/indexer/stats/db.go b/modules/indexer/stats/db.go index 9e251d0f69..9d2942a266 100644 --- a/modules/indexer/stats/db.go +++ b/modules/indexer/stats/db.go @@ -7,7 +7,7 @@ package stats import ( "fmt" - "code.gitea.io/gitea/models" + repo_model "code.gitea.io/gitea/models/repo" "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/graceful" "code.gitea.io/gitea/modules/log" @@ -23,7 +23,7 @@ func (db *DBIndexer) Index(id int64) error { ctx, _, finished := process.GetManager().AddContext(graceful.GetManager().ShutdownContext(), fmt.Sprintf("Stats.DB Index Repo[%d]", id)) defer finished() - repo, err := models.GetRepositoryByID(id) + repo, err := repo_model.GetRepositoryByID(id) if err != nil { return err } @@ -31,7 +31,7 @@ func (db *DBIndexer) Index(id int64) error { return nil } - status, err := repo.GetIndexerStatus(models.RepoIndexerTypeStats) + status, err := repo_model.GetIndexerStatus(repo, repo_model.RepoIndexerTypeStats) if err != nil { return err } @@ -64,7 +64,7 @@ func (db *DBIndexer) Index(id int64) error { log.Error("Unable to get language stats for ID %s for default branch %s in %s. Error: %v", commitID, repo.DefaultBranch, repo.RepoPath(), err) return err } - err = repo.UpdateLanguageStats(commitID, stats) + err = repo_model.UpdateLanguageStats(repo, commitID, stats) if err != nil { log.Error("Unable to update language stats for ID %s for default branch %s in %s. Error: %v", commitID, repo.DefaultBranch, repo.RepoPath(), err) return err diff --git a/modules/indexer/stats/indexer.go b/modules/indexer/stats/indexer.go index fe87a2268b..f4fe54b8cb 100644 --- a/modules/indexer/stats/indexer.go +++ b/modules/indexer/stats/indexer.go @@ -5,8 +5,8 @@ package stats import ( - "code.gitea.io/gitea/models" "code.gitea.io/gitea/models/db" + repo_model "code.gitea.io/gitea/models/repo" "code.gitea.io/gitea/modules/graceful" "code.gitea.io/gitea/modules/log" ) @@ -62,7 +62,7 @@ func populateRepoIndexer() { return default: } - ids, err := models.GetUnindexedRepos(models.RepoIndexerTypeStats, maxRepoID, 0, 50) + ids, err := repo_model.GetUnindexedRepos(repo_model.RepoIndexerTypeStats, maxRepoID, 0, 50) if err != nil { log.Error("populateRepoIndexer: %v", err) return diff --git a/modules/indexer/stats/indexer_test.go b/modules/indexer/stats/indexer_test.go index f52d73a32e..b32100b458 100644 --- a/modules/indexer/stats/indexer_test.go +++ b/modules/indexer/stats/indexer_test.go @@ -9,7 +9,8 @@ import ( "testing" "time" - "code.gitea.io/gitea/models" + _ "code.gitea.io/gitea/models" + repo_model "code.gitea.io/gitea/models/repo" "code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/modules/setting" @@ -32,12 +33,12 @@ func TestRepoStatsIndex(t *testing.T) { time.Sleep(5 * time.Second) - repo, err := models.GetRepositoryByID(1) + repo, err := repo_model.GetRepositoryByID(1) assert.NoError(t, err) - status, err := repo.GetIndexerStatus(models.RepoIndexerTypeStats) + status, err := repo_model.GetIndexerStatus(repo, repo_model.RepoIndexerTypeStats) assert.NoError(t, err) assert.Equal(t, "65f1bf27bc3bf70f64657658635e66094edbcb4d", status.CommitSha) - langs, err := repo.GetTopLanguageStats(5) + langs, err := repo_model.GetTopLanguageStats(repo, 5) assert.NoError(t, err) assert.Empty(t, langs) } diff --git a/modules/indexer/stats/queue.go b/modules/indexer/stats/queue.go index fde3f2ff01..b458444697 100644 --- a/modules/indexer/stats/queue.go +++ b/modules/indexer/stats/queue.go @@ -7,7 +7,7 @@ package stats import ( "fmt" - "code.gitea.io/gitea/models" + repo_model "code.gitea.io/gitea/models/repo" "code.gitea.io/gitea/modules/graceful" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/queue" @@ -38,7 +38,7 @@ func initStatsQueue() error { } // UpdateRepoIndexer update a repository's entries in the indexer -func UpdateRepoIndexer(repo *models.Repository) error { +func UpdateRepoIndexer(repo *repo_model.Repository) error { if err := statsQueue.Push(repo.ID); err != nil { if err != queue.ErrAlreadyInQueue { return err |