(refs #3)Search git repository without cloning to the file system.

This commit is contained in:
takezoe
2013-07-17 06:36:21 +09:00
parent 79ec96343f
commit 4796d7f450
2 changed files with 39 additions and 23 deletions

View File

@@ -6,6 +6,11 @@ import service._
import jp.sf.amateras.scalatra.forms._ import jp.sf.amateras.scalatra.forms._
import org.eclipse.jgit.api.Git import org.eclipse.jgit.api.Git
import org.apache.commons.io.FileUtils import org.apache.commons.io.FileUtils
import org.eclipse.jgit.treewalk.TreeWalk
import org.eclipse.jgit.revwalk.RevWalk
import scala.collection.mutable.ListBuffer
import org.eclipse.jgit.lib.FileMode
import java.util.regex.Pattern
class IndexController extends IndexControllerBase class IndexController extends IndexControllerBase
with RepositoryService with AccountService with SystemSettingsService with ActivityService with RepositoryService with AccountService with SystemSettingsService with ActivityService
@@ -50,29 +55,39 @@ trait IndexControllerBase extends ControllerBase { self: RepositoryService
} }
case _ => { case _ => {
JGitUtil.withGit(getRepositoryDir(owner, name)){ git => JGitUtil.withGit(getRepositoryDir(owner, name)){ git =>
// TODO search code val revWalk = new RevWalk(git.getRepository)
val dir = new java.io.File(getTemporaryDir(owner, name), "search") val objectId = git.getRepository.resolve("HEAD")
if(!dir.exists){ val revCommit = revWalk.parseCommit(objectId)
val git = Git val treeWalk = new TreeWalk(git.getRepository)
.cloneRepository.setDirectory(dir) treeWalk.setRecursive(true)
.setURI(getRepositoryDir(owner, name).toURI.toString) treeWalk.addTree(revCommit.getTree)
.setBranch(repository.repository.defaultBranch)
.call
git.getRepository.close
} else {
val git = Git.open(dir)
git.pull.call
if(git.getRepository.getBranch != repository.repository.defaultBranch){
git.checkout.setName(repository.repository.defaultBranch).call
}
git.getRepository.close
}
search.html.code(searchDirectory(query, dir).map { file => val lowerQuery = query.toLowerCase
FileSearchResult( val list = new ListBuffer[(String, String)]
file.getAbsolutePath.substring(dir.getAbsolutePath.length + 1).replace('\\', '/'), while (treeWalk.next()) {
new java.util.Date(file.lastModified) 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 index = text.toLowerCase.indexOf(lowerQuery)
if(index >= 0){
val lineNumber = text.substring(0, index).split("\n").size - 1
val highlightText = text.split("\n").drop(lineNumber).take(5).mkString("\n")
.replace("&", "&amp;").replace("<", "&gt;").replace(">", "&gt;").replace("\"", "&quot;")
.replaceAll("(?i)(\\Q" + query + "\\E)", "<span style=\"background-color: yellow;\">$1</span>")
list.append((treeWalk.getPathString, highlightText))
}
}
}
}
}
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)
}, query, repository) }, query, repository)
} }
} }
@@ -89,4 +104,4 @@ trait IndexControllerBase extends ControllerBase { self: RepositoryService
} }
case class FileSearchResult(path: String, lastModified: java.util.Date) case class FileSearchResult(path: String, lastModified: java.util.Date, highlightText: String)

View File

@@ -11,6 +11,7 @@
<div> <div>
<div><a href="@url(repository)/blob/@repository.repository.defaultBranch/@file.path">@file.path</a></div> <div><a href="@url(repository)/blob/@repository.repository.defaultBranch/@file.path">@file.path</a></div>
<div class="muted">@datetime(file.lastModified)</div> <div class="muted">@datetime(file.lastModified)</div>
<pre>@Html(file.highlightText)</pre>
</div> </div>
} }
} }