mirror of
https://github.com/gitbucket/gitbucket.git
synced 2025-11-01 11:06:06 +01:00
(refs #3)Cleanup
This commit is contained in:
@@ -4,7 +4,6 @@ import util._
|
||||
import util.Directory._
|
||||
import service._
|
||||
import jp.sf.amateras.scalatra.forms._
|
||||
import org.apache.commons.io.FileUtils
|
||||
import org.eclipse.jgit.treewalk.TreeWalk
|
||||
import org.eclipse.jgit.revwalk.RevWalk
|
||||
import scala.collection.mutable.ListBuffer
|
||||
@@ -19,7 +18,7 @@ trait IndexControllerBase extends ControllerBase { self: RepositoryService
|
||||
with ReferrerAuthenticator =>
|
||||
|
||||
val searchForm = mapping(
|
||||
"query" -> trim(text(required)), // TODO optional?
|
||||
"query" -> trim(text(required)),
|
||||
"owner" -> trim(text(required)),
|
||||
"repository" -> trim(text(required))
|
||||
)(SearchForm.apply)
|
||||
@@ -40,33 +39,25 @@ trait IndexControllerBase extends ControllerBase { self: RepositoryService
|
||||
redirect(s"${form.owner}/${form.repository}/search?q=${StringUtil.urlEncode(form.query)}")
|
||||
}
|
||||
|
||||
// TODO readable only
|
||||
get("/:owner/:repository/search")(referrersOnly { repository =>
|
||||
val query = params("q")
|
||||
val target = params.getOrElse("type", "Code")
|
||||
|
||||
target.toLowerCase match {
|
||||
case "issue" => {
|
||||
// TODO search issue
|
||||
val lowerQueries = query.toLowerCase.split("[ \\t ]+")
|
||||
|
||||
search.html.issues(queryIssues(repository.owner, repository.name, query).map { case (issue, commentCount, content) =>
|
||||
val lowerText = content.toLowerCase
|
||||
val indices = lowerQueries.map { lowerQuery =>
|
||||
lowerText.indexOf(lowerQuery)
|
||||
}
|
||||
val highlightText = if(!indices.exists(_ < 0)){
|
||||
val lineNumber = content.substring(0, indices.min).split("\n").size - 1
|
||||
StringUtil.escapeHtml(content.split("\n").drop(lineNumber).take(5).mkString("\n"))
|
||||
.replaceAll("(?i)(" + lowerQueries.map("\\Q" + _ + "\\E").mkString("|") + ")",
|
||||
"<span style=\"background-color: yellow;\">$1</span>")
|
||||
} else content.split("\n").take(5).mkString("\n")
|
||||
|
||||
IssueSearchResult(issue.issueId, issue.title, issue.openedUserName, issue.registeredDate, commentCount, highlightText)
|
||||
IssueSearchResult(
|
||||
issue.issueId,
|
||||
issue.title,
|
||||
issue.openedUserName,
|
||||
issue.registeredDate,
|
||||
commentCount,
|
||||
getHighlightText(content, query))
|
||||
}, query, repository)
|
||||
|
||||
}
|
||||
case _ => {
|
||||
// TODO move to JGitUtil?
|
||||
JGitUtil.withGit(getRepositoryDir(repository.owner, repository.name)){ git =>
|
||||
val revWalk = new RevWalk(git.getRepository)
|
||||
val objectId = git.getRepository.resolve("HEAD")
|
||||
@@ -75,23 +66,17 @@ trait IndexControllerBase extends ControllerBase { self: RepositoryService
|
||||
treeWalk.setRecursive(true)
|
||||
treeWalk.addTree(revCommit.getTree)
|
||||
|
||||
val lowerQueries = query.toLowerCase.split("[ \\t ]+")
|
||||
val lowerQueries = 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 text = new String(bytes, "UTF-8")
|
||||
val lowerText = text.toLowerCase
|
||||
val indices = lowerQueries.map { lowerQuery =>
|
||||
lowerText.indexOf(lowerQuery)
|
||||
}
|
||||
val indices = lowerQueries.map(lowerText.indexOf _)
|
||||
if(!indices.exists(_ < 0)){
|
||||
val lineNumber = text.substring(0, indices.min).split("\n").size - 1
|
||||
val highlightText = StringUtil.escapeHtml(text.split("\n").drop(lineNumber).take(5).mkString("\n"))
|
||||
.replaceAll("(?i)(" + lowerQueries.map("\\Q" + _ + "\\E").mkString("|") + ")",
|
||||
"<span style=\"background-color: yellow;\">$1</span>")
|
||||
list.append((treeWalk.getPathString, highlightText))
|
||||
list.append((treeWalk.getPathString, getHighlightText(text, query)))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -110,11 +95,18 @@ trait IndexControllerBase extends ControllerBase { self: RepositoryService
|
||||
}
|
||||
})
|
||||
|
||||
private def searchDirectory(query: String, dir: java.io.File, matched: List[java.io.File] = Nil): List[java.io.File] = {
|
||||
dir.listFiles.toList.flatMap {
|
||||
case file if(file.isDirectory && file.getName != ".git") => searchDirectory(query, file, matched)
|
||||
case file if(file.isFile && FileUtils.readFileToString(file).contains(query)) => matched :+ file
|
||||
case _ => matched
|
||||
private def getHighlightText(content: String, query: String): String = {
|
||||
val lowerQueries = StringUtil.splitWords(query.toLowerCase)
|
||||
val lowerText = content.toLowerCase
|
||||
val indices = lowerQueries.map(lowerText.indexOf _)
|
||||
|
||||
if(!indices.exists(_ < 0)){
|
||||
val lineNumber = content.substring(0, indices.min).split("\n").size - 1
|
||||
StringUtil.escapeHtml(content.split("\n").drop(lineNumber).take(5).mkString("\n"))
|
||||
.replaceAll("(?i)(" + lowerQueries.map("\\Q" + _ + "\\E").mkString("|") + ")",
|
||||
"<span style=\"background-color: yellow;\">$1</span>")
|
||||
} else {
|
||||
content.split("\n").take(5).mkString("\n")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user