Start to implement the commit detail page.

This commit is contained in:
takezoe
2013-04-21 03:45:30 +09:00
parent 82e65fc6b7
commit 1e80f03848
5 changed files with 124 additions and 22 deletions

View File

@@ -7,7 +7,12 @@ import java.io.File
import java.util.Date
import org.eclipse.jgit.api.Git
import org.eclipse.jgit.lib._
import org.eclipse.jgit.revwalk._
import org.apache.commons.io.FileUtils
import org.eclipse.jgit.revwalk.RevWalk
import org.eclipse.jgit.treewalk.CanonicalTreeParser
import org.eclipse.jgit.diff.DiffEntry.ChangeType
import org.eclipse.jgit.errors.MissingObjectException
case class RepositoryInfo(owner: String, name: String, url: String, branchList: List[String], tags: List[String])
@@ -15,6 +20,8 @@ case class FileInfo(isDirectory: Boolean, name: String, time: Date, message: Str
case class CommitInfo(id: String, time: Date, committer: String, message: String)
case class DiffInfo(changeType: ChangeType, oldPath: String, newPath: String, oldContent: Option[String], newContent: Option[String])
/**
* The repository viewer.
*/
@@ -64,7 +71,6 @@ class RepositoryViewerServlet extends ServletBase {
get("/:owner/:repository/commits/:branch"){
val owner = params("owner")
val repository = params("repository")
val branchName = params("branch")
val page = params.getOrElse("page", "1").toInt
val dir = getBranchDir(owner, repository, branchName)
@@ -93,10 +99,8 @@ class RepositoryViewerServlet extends ServletBase {
get("/:owner/:repository/blob/:branch/*"){
val owner = params("owner")
val repository = params("repository")
val branchName = params("branch")
val path = multiParams("splat").head.replaceFirst("^tree/.+?/", "")
val dir = getBranchDir(owner, repository, branchName)
val content = FileUtils.readFileToString(new File(dir, path), "UTF-8")
@@ -107,6 +111,54 @@ class RepositoryViewerServlet extends ServletBase {
CommitInfo(latestRev.getName, latestRev.getCommitterIdent.getWhen, latestRev.getCommitterIdent.getName, latestRev.getShortMessage))
}
/**
* Shows details of the specified commit.
*/
get("/:owner/:repository/commit/:id"){
val owner = params("owner")
val repository = params("repository")
val id = params("id")
val repositoryInfo = getRepositoryInfo(owner, repository)
// get branch by commit id
val branch = repositoryInfo.branchList.find { branch =>
val git = Git.open(getBranchDir(owner, repository, branch))
git.log.add(ObjectId.fromString(id)).call.iterator.hasNext
}.get
val dir = getBranchDir(owner, repository, branch)
val git = Git.open(dir)
val rev = git.log.add(ObjectId.fromString(id)).call.iterator.next
// get diff
val reader = git.getRepository.newObjectReader
val oldTreeIter = new CanonicalTreeParser
oldTreeIter.reset(reader, git.getRepository.resolve(id + "^{tree}"))
// TODO specify previous commit
val newTreeIter = new CanonicalTreeParser
newTreeIter.reset(reader, git.getRepository.resolve("HEAD^{tree}"))
import scala.collection.JavaConverters._
val diffs = git.diff.setNewTree(newTreeIter).setOldTree(oldTreeIter).call.asScala.map { diff =>
DiffInfo(diff.getChangeType, diff.getOldPath, diff.getNewPath,
getContent(git, diff.getOldId.toObjectId),
getContent(git, diff.getNewId.toObjectId))
}
html.commit(branch,
CommitInfo(rev.getName, rev.getCommitterIdent.getWhen, rev.getCommitterIdent.getName, rev.getFullMessage),
repositoryInfo, diffs)
}
def getContent(git: Git, id: ObjectId): Option[String] = try {
Some(new String(git.getRepository.getObjectDatabase.open(id).getBytes()))
} catch {
case e: MissingObjectException => None
}
/**
* Returns the repository information. It contains branch names and tag names.
*

View File

@@ -4,14 +4,23 @@ import java.text.SimpleDateFormat
object helpers {
def datetime(date: Date): String = {
new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(date)
}
/**
* Format java.util.Date to "yyyy/MM/dd HH:mm:ss".
*/
def datetime(date: Date): String = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(date)
def date(date: Date): String = {
new SimpleDateFormat("yyyy/MM/dd").format(date)
}
/**
* Format java.util.Date to "yyyy/MM/dd".
*/
def date(date: Date): String = new SimpleDateFormat("yyyy/MM/dd").format(date)
// TODO escape html tags using HtmlEscapeUtils (Commons Lang)
def text(value: String): twirl.api.Html = twirl.api.Html(
value.replaceAll(" ", "&nbsp;").replaceAll("\t", "&nbsp;&nbsp;&nbsp;&nbsp;").replaceAll("\n", "<br>"))
/**
* Cut the given string by specified length.
*/
def cut(message: String, length: Int): String = {
if(message.length > length){
message.substring(0, length) + "..."

View File

@@ -0,0 +1,41 @@
@(branch: String, commit: app.CommitInfo, repository: app.RepositoryInfo, diffs: Seq[app.DiffInfo])(implicit context: app.Context)
@import context._
@import view.helpers._
@main(cut(commit.message, 20)){
@header(branch, repository)
@navtab(branch, repository, "commits")
<table class="table table-bordered">
<tr>
<th>
<div>@text(commit.message)</div>
<div class="small" style="font-weight: normal;"><span class="description">@branch</span></div>
</th>
</tr>
<tr>
<td>
<a href="@path/@commit.committer">@commit.committer</a> <span class="description">@datetime(commit.time)</span>
<div class="pull-right align-right">
<span class="description">commit</span> @commit.id
</div>
</td>
</tr>
</table>
@diffs.map { diff =>
<table class="table table-bordered">
<tr>
<th style="font-weight: normal;">
@diff.newPath
<div class="pull-right align-right">
<a href="@path/@repository.owner/@repository.name/commit/@commit.id" class="btn btn-small">View file @@ @commit.id.substring(0, 10)</a>
</div>
</th>
</tr>
<tr>
<td>
<pre>@diff.newContent.getOrElse("")</pre>
</td>
</tr>
</table>
}
}

View File

@@ -23,8 +23,8 @@
</div>
</div>
<div class="pull-right align-right">
<a href="" class="btn btn-small monospace">@commit.id.substring(0, 10)@*<i class="icon-circle-arrow-right"></i>*@</a><br>
<a href="" class="small">Browse code@*<i class="icon-arrow-right"></i>*@</a>
<a href="@path/@repository.owner/@repository.name/commit/@commit.id" class="btn btn-small monospace">@commit.id.substring(0, 10)</a><br>
<a href="" class="small">Browse code</a>
</div>
</td>
</tr>

View File

@@ -17,7 +17,7 @@
@view.helpers.cut(latestCommit.message, 100)
<div class="pull-right align-right">
@view.helpers.datetime(latestCommit.time)
<a href="">@latestCommit.id.substring(0, 10)</a>
<a href="@path/@repository.owner/@repository.name/commit/@latestCommit.id">@latestCommit.id.substring(0, 10)</a>
</div>
</div>
</th>