From 7d1f98a5b81e65164b1c59ddb9ca75ececdeafca Mon Sep 17 00:00:00 2001 From: Vitaliy Filippov Date: Thu, 27 Apr 2017 02:05:33 +0300 Subject: [PATCH] Add reindex function --- models/commit.go | 16 +++++++++++++++ models/update.go | 51 +++++++++++++++++++++++++++++++++++++----------- 2 files changed, 56 insertions(+), 11 deletions(-) diff --git a/models/commit.go b/models/commit.go index 50ca7ebc..6f078529 100644 --- a/models/commit.go +++ b/models/commit.go @@ -10,6 +10,8 @@ import ( "github.com/go-xorm/xorm" "github.com/go-xorm/builder" + + git "github.com/gogits/git-module" ) type Commit struct { @@ -26,6 +28,20 @@ type Commit struct { Repo *Repository `xorm:"-"` } +func GitCommitToSearchCommit(repoID int64, commit *git.Commit) *Commit { + return &Commit{ + RepoID: repoID, + Sha: commit.ID.String(), + Message: commit.Message(), + AuthorEmail: commit.Author.Email, + AuthorName: commit.Author.Name, + AuthorTime: commit.Author.When, + CommitterEmail: commit.Committer.Email, + CommitterName: commit.Committer.Name, + CommitterTime: commit.Committer.When, + } +} + func FulltextSearchCommits(userid int64, q string, limit int, offset int) ([]*Commit, int64, error) { sess := x.NewSession() sess.Join("INNER", "repository", "repository.id = commit.repo_id") diff --git a/models/update.go b/models/update.go index bd87141e..3e6be338 100644 --- a/models/update.go +++ b/models/update.go @@ -135,17 +135,7 @@ func PushUpdate(opts PushUpdateOptions) (err error) { searchCommits := make([]*Commit, 0) for e := l.Front(); e != nil; e = e.Next() { commit := e.Value.(*git.Commit) - searchCommits = append(searchCommits, &Commit{ - RepoID: repo.ID, - Sha: commit.ID.String(), - Message: commit.Message(), - AuthorEmail: commit.Author.Email, - AuthorName: commit.Author.Name, - AuthorTime: commit.Author.When, - CommitterEmail: commit.Committer.Email, - CommitterName: commit.Committer.Name, - CommitterTime: commit.Committer.When, - }) + searchCommits = append(searchCommits, GitCommitToSearchCommit(repo.ID, commit)) } x.Insert(searchCommits) @@ -191,3 +181,42 @@ func PushUpdate(opts PushUpdateOptions) (err error) { } return nil } + +func ReindexCommits(RepoUserName string, RepoName string) (err error) { + repoPath := RepoPath(RepoUserName, RepoName) + + gitRepo, err := git.OpenRepository(repoPath) + if err != nil { + return fmt.Errorf("OpenRepository: %v", err) + } + + owner, err := GetUserByName(RepoUserName) + if err != nil { + return fmt.Errorf("GetUserByName: %v", err) + } + + repo, err := GetRepositoryByName(owner.ID, RepoName) + if err != nil { + return fmt.Errorf("GetRepositoryByName: %v", err) + } + + x.Delete(&Commit{RepoID: repo.ID}) + + for page := 0; ; page++ { + commits, err := gitRepo.CommitsByRangeSize("--all", page, 1000) + if err != nil { + return fmt.Errorf("CommitsByRange: %v", err) + } + searchCommits := make([]*Commit, 0) + for e := commits.Front(); e != nil; e = e.Next() { + commit := e.Value.(*git.Commit) + searchCommits = append(searchCommits, GitCommitToSearchCommit(repo.ID, commit)) + } + x.Insert(searchCommits) + if len(searchCommits) < 1000 { + break + } + } + + return nil +}