Add reindex function

master
Vitaliy Filippov 2017-04-27 02:05:33 +03:00
parent 64250dbe44
commit 7d1f98a5b8
2 changed files with 56 additions and 11 deletions

View File

@ -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")

View File

@ -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
}