Add reindex function
parent
64250dbe44
commit
7d1f98a5b8
|
@ -10,6 +10,8 @@ import (
|
||||||
|
|
||||||
"github.com/go-xorm/xorm"
|
"github.com/go-xorm/xorm"
|
||||||
"github.com/go-xorm/builder"
|
"github.com/go-xorm/builder"
|
||||||
|
|
||||||
|
git "github.com/gogits/git-module"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Commit struct {
|
type Commit struct {
|
||||||
|
@ -26,6 +28,20 @@ type Commit struct {
|
||||||
Repo *Repository `xorm:"-"`
|
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) {
|
func FulltextSearchCommits(userid int64, q string, limit int, offset int) ([]*Commit, int64, error) {
|
||||||
sess := x.NewSession()
|
sess := x.NewSession()
|
||||||
sess.Join("INNER", "repository", "repository.id = commit.repo_id")
|
sess.Join("INNER", "repository", "repository.id = commit.repo_id")
|
||||||
|
|
|
@ -135,17 +135,7 @@ func PushUpdate(opts PushUpdateOptions) (err error) {
|
||||||
searchCommits := make([]*Commit, 0)
|
searchCommits := make([]*Commit, 0)
|
||||||
for e := l.Front(); e != nil; e = e.Next() {
|
for e := l.Front(); e != nil; e = e.Next() {
|
||||||
commit := e.Value.(*git.Commit)
|
commit := e.Value.(*git.Commit)
|
||||||
searchCommits = append(searchCommits, &Commit{
|
searchCommits = append(searchCommits, GitCommitToSearchCommit(repo.ID, 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,
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
x.Insert(searchCommits)
|
x.Insert(searchCommits)
|
||||||
|
|
||||||
|
@ -191,3 +181,42 @@ func PushUpdate(opts PushUpdateOptions) (err error) {
|
||||||
}
|
}
|
||||||
return nil
|
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
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue