mirror of
https://github.com/gitbucket/gitbucket.git
synced 2025-11-01 11:06:06 +01:00
Merge branch 'repository-search-cache' into #3_repository-search
This commit is contained in:
@@ -8,6 +8,7 @@ import org.eclipse.jgit.treewalk.TreeWalk
|
||||
import org.eclipse.jgit.revwalk.RevWalk
|
||||
import scala.collection.mutable.ListBuffer
|
||||
import org.eclipse.jgit.lib.FileMode
|
||||
import model.Issue
|
||||
|
||||
class IndexController extends IndexControllerBase
|
||||
with RepositoryService with AccountService with SystemSettingsService with ActivityService with IssuesService
|
||||
@@ -40,11 +41,18 @@ trait IndexControllerBase extends ControllerBase { self: RepositoryService
|
||||
}
|
||||
|
||||
get("/:owner/:repository/search")(referrersOnly { repository =>
|
||||
import RepositorySearch._
|
||||
val query = params("q").trim
|
||||
val target = params.getOrElse("type", "code")
|
||||
val page = try {
|
||||
val i = params.getOrElse("page", "1").toInt
|
||||
if(i <= 0) 1 else i
|
||||
} catch {
|
||||
case e: NumberFormatException => 1
|
||||
}
|
||||
|
||||
val issues = if(query.isEmpty) Nil else searchIssuesByKeyword(repository.owner, repository.name, query)
|
||||
val files = if(query.isEmpty) Nil else searchRepositoryFiles(repository.owner, repository.name, query)
|
||||
|
||||
val SearchResult(files, issues) = searchRepository(repository.owner, repository.name, query)
|
||||
|
||||
target.toLowerCase match {
|
||||
case "issue" =>
|
||||
@@ -56,19 +64,34 @@ trait IndexControllerBase extends ControllerBase { self: RepositoryService
|
||||
issue.registeredDate,
|
||||
commentCount,
|
||||
getHighlightText(content, query)._1)
|
||||
}, files.size, query, repository)
|
||||
}, files.size, query, page, repository)
|
||||
case _ =>
|
||||
JGitUtil.withGit(getRepositoryDir(repository.owner, repository.name)){ git =>
|
||||
val commits = JGitUtil.getLatestCommitFromPaths(git, files.toList.map(_._1), "HEAD")
|
||||
|
||||
search.html.code(files.toList.map { case (path, text) =>
|
||||
val (highlightText, lineNumber) = getHighlightText(text, query)
|
||||
FileSearchResult(path, commits(path).getCommitterIdent.getWhen, highlightText, lineNumber)
|
||||
}, issues.size, query, repository)
|
||||
FileSearchResult(
|
||||
path,
|
||||
commits(path).getCommitterIdent.getWhen,
|
||||
highlightText,
|
||||
lineNumber)
|
||||
}, issues.size, query, page, repository)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
case class SearchResult(
|
||||
files: List[(String, String)],
|
||||
issues: List[(Issue, Int, String)]
|
||||
)
|
||||
|
||||
def searchRepository(owner: String, repository: String, query: String): SearchResult = {
|
||||
val issues = if(query.isEmpty) Nil else searchIssuesByKeyword(owner, repository, query)
|
||||
val files = if(query.isEmpty) Nil else searchRepositoryFiles(owner, repository, query)
|
||||
SearchResult(files, issues)
|
||||
}
|
||||
|
||||
private def searchRepositoryFiles(owner: String, repository: String, query: String): List[(String, String)] = {
|
||||
JGitUtil.withGit(getRepositoryDir(owner, repository)){ git =>
|
||||
val revWalk = new RevWalk(git.getRepository)
|
||||
@@ -102,6 +125,7 @@ trait IndexControllerBase extends ControllerBase { self: RepositoryService
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private def getHighlightText(content: String, query: String): (String, Int) = {
|
||||
val keywords = StringUtil.splitWords(query.toLowerCase)
|
||||
val lowerText = content.toLowerCase
|
||||
@@ -120,7 +144,6 @@ trait IndexControllerBase extends ControllerBase { self: RepositoryService
|
||||
|
||||
}
|
||||
|
||||
|
||||
case class IssueSearchResult(
|
||||
issueId: Int,
|
||||
title: String,
|
||||
@@ -133,4 +156,9 @@ case class FileSearchResult(
|
||||
path: String,
|
||||
lastModified: java.util.Date,
|
||||
highlightText: String,
|
||||
highlightLineNumber: Int)
|
||||
highlightLineNumber: Int)
|
||||
|
||||
object RepositorySearch extends IssuesService {
|
||||
val CodeLimit = 10
|
||||
val IssueLimit = 10
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user