mirror of
https://github.com/gitbucket/gitbucket.git
synced 2025-11-06 21:45:50 +01:00
(refs #3)Add result count to the menu.
This commit is contained in:
@@ -43,11 +43,12 @@ trait IndexControllerBase extends ControllerBase { self: RepositoryService
|
||||
val query = params("q").trim
|
||||
val target = params.getOrElse("type", "code")
|
||||
|
||||
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)
|
||||
|
||||
target.toLowerCase match {
|
||||
case "issue" => if(query.isEmpty){
|
||||
search.html.issues(Nil, "", repository)
|
||||
} else {
|
||||
search.html.issues(searchIssuesByKeyword(repository.owner, repository.name, query).map { case (issue, commentCount, content) =>
|
||||
case "issue" =>
|
||||
search.html.issues(issues.map { case (issue, commentCount, content) =>
|
||||
IssueSearchResult(
|
||||
issue.issueId,
|
||||
issue.title,
|
||||
@@ -55,48 +56,51 @@ trait IndexControllerBase extends ControllerBase { self: RepositoryService
|
||||
issue.registeredDate,
|
||||
commentCount,
|
||||
getHighlightText(content, query)._1)
|
||||
}, query, repository)
|
||||
}
|
||||
case _ => if(query.isEmpty){
|
||||
// TODO move to JGitUtil?
|
||||
search.html.code(Nil, "", repository)
|
||||
} else {
|
||||
}, files.size, query, repository)
|
||||
case _ =>
|
||||
JGitUtil.withGit(getRepositoryDir(repository.owner, repository.name)){ git =>
|
||||
val revWalk = new RevWalk(git.getRepository)
|
||||
val objectId = git.getRepository.resolve("HEAD")
|
||||
val revCommit = revWalk.parseCommit(objectId)
|
||||
val treeWalk = new TreeWalk(git.getRepository)
|
||||
treeWalk.setRecursive(true)
|
||||
treeWalk.addTree(revCommit.getTree)
|
||||
val commits = JGitUtil.getLatestCommitFromPaths(git, files.toList.map(_._1), "HEAD")
|
||||
|
||||
val keywords = StringUtil.splitWords(query.toLowerCase)
|
||||
val list = new ListBuffer[(String, (String, Int))]
|
||||
while (treeWalk.next()) {
|
||||
if(treeWalk.getFileMode(0) != FileMode.TREE){
|
||||
JGitUtil.getContent(git, treeWalk.getObjectId(0), false).foreach { bytes =>
|
||||
if(FileUtil.isText(bytes)){
|
||||
val text = new String(bytes, "UTF-8")
|
||||
val lowerText = text.toLowerCase
|
||||
val indices = keywords.map(lowerText.indexOf _)
|
||||
if(!indices.exists(_ < 0)){
|
||||
list.append((treeWalk.getPathString, getHighlightText(text, query)))
|
||||
}
|
||||
}
|
||||
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)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
private def searchRepositoryFiles(owner: String, repository: String, query: String): List[(String, String)] = {
|
||||
JGitUtil.withGit(getRepositoryDir(owner, repository)){ git =>
|
||||
val revWalk = new RevWalk(git.getRepository)
|
||||
val objectId = git.getRepository.resolve("HEAD")
|
||||
val revCommit = revWalk.parseCommit(objectId)
|
||||
val treeWalk = new TreeWalk(git.getRepository)
|
||||
treeWalk.setRecursive(true)
|
||||
treeWalk.addTree(revCommit.getTree)
|
||||
|
||||
val keywords = StringUtil.splitWords(query.toLowerCase)
|
||||
val list = new ListBuffer[(String, String)]
|
||||
|
||||
while (treeWalk.next()) {
|
||||
if(treeWalk.getFileMode(0) != FileMode.TREE){
|
||||
JGitUtil.getContent(git, treeWalk.getObjectId(0), false).foreach { bytes =>
|
||||
if(FileUtil.isText(bytes)){
|
||||
val text = new String(bytes, "UTF-8")
|
||||
val lowerText = text.toLowerCase
|
||||
val indices = keywords.map(lowerText.indexOf _)
|
||||
if(!indices.exists(_ < 0)){
|
||||
list.append((treeWalk.getPathString, text))
|
||||
}
|
||||
}
|
||||
}
|
||||
treeWalk.release
|
||||
revWalk.release
|
||||
|
||||
val commits = JGitUtil.getLatestCommitFromPaths(git, list.toList.map(_._1), "HEAD")
|
||||
|
||||
search.html.code(list.toList.map { case (path, highlightText) =>
|
||||
FileSearchResult(path, commits(path).getCommitterIdent.getWhen, highlightText._1, highlightText._2)
|
||||
}, query, repository)
|
||||
}
|
||||
}
|
||||
treeWalk.release
|
||||
revWalk.release
|
||||
|
||||
list.toList
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
private def getHighlightText(content: String, query: String): (String, Int) = {
|
||||
val keywords = StringUtil.splitWords(query.toLowerCase)
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
@(files: List[app.FileSearchResult], query: String, repository: service.RepositoryService.RepositoryInfo)(implicit context: app.Context)
|
||||
@(files: List[app.FileSearchResult], issueCount: Int, query: String, repository: service.RepositoryService.RepositoryInfo)(implicit context: app.Context)
|
||||
@import context._
|
||||
@import view.helpers._
|
||||
@html.main("Search Results", Some(repository)){
|
||||
@menu("code", query, repository){
|
||||
@menu("code", files.size, issueCount, query, repository){
|
||||
@if(files.isEmpty){
|
||||
<h4>We couldn't find any code matching '@query'</h4>
|
||||
} else {
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
@(issues: List[app.IssueSearchResult], query: String, repository: service.RepositoryService.RepositoryInfo)(implicit context: app.Context)
|
||||
@(issues: List[app.IssueSearchResult], fileCount: Int, query: String, repository: service.RepositoryService.RepositoryInfo)(implicit context: app.Context)
|
||||
@import context._
|
||||
@import view.helpers._
|
||||
@html.main("Search Results", Some(repository)){
|
||||
@menu("issue", query, repository){
|
||||
@menu("issue", fileCount, issues.size, query, repository){
|
||||
@if(issues.isEmpty){
|
||||
<h4>We couldn't find any code matching '@query'</h4>
|
||||
} else {
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
@(active: String, query: String, repository: service.RepositoryService.RepositoryInfo)(body: Html)(implicit context: app.Context)
|
||||
@(active: String, fileCount: Int, issueCount: Int, query: String,
|
||||
repository: service.RepositoryService.RepositoryInfo)(body: Html)(implicit context: app.Context)
|
||||
@import context._
|
||||
@import view.helpers._
|
||||
@html.header("", repository)
|
||||
@@ -7,10 +8,16 @@
|
||||
<div class="box">
|
||||
<ul class="nav nav-tabs nav-stacked side-menu">
|
||||
<li@if(active=="code"){ class="active"}>
|
||||
<a href="@url(repository)/search?q=@urlEncode(query)&type=code">Code</a>
|
||||
<a href="@url(repository)/search?q=@urlEncode(query)&type=code">
|
||||
<span class="badge pull-right">@fileCount</span>
|
||||
Code
|
||||
</a>
|
||||
</li>
|
||||
<li@if(active=="issue"){ class="active"}>
|
||||
<a href="@url(repository)/search?q=@urlEncode(query)&type=issue">Issue</a>
|
||||
<a href="@url(repository)/search?q=@urlEncode(query)&type=issue">
|
||||
<span class="badge pull-right">@issueCount</span>
|
||||
Issue
|
||||
</a>
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user