mirror of
https://github.com/gitbucket/gitbucket.git
synced 2025-11-02 03:26:06 +01:00
Start to implement the commit detail page.
This commit is contained in:
@@ -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.
|
||||
*/
|
||||
@@ -32,7 +39,7 @@ class RepositoryViewerServlet extends ServletBase {
|
||||
* Shows the file list of the repository root and the default branch.
|
||||
*/
|
||||
get("/:owner/:repository") {
|
||||
val owner = params("owner")
|
||||
val owner = params("owner")
|
||||
val repository = params("repository")
|
||||
|
||||
fileList(owner, repository)
|
||||
@@ -42,7 +49,7 @@ class RepositoryViewerServlet extends ServletBase {
|
||||
* Shows the file list of the repository root and the specified branch.
|
||||
*/
|
||||
get("/:owner/:repository/tree/:branch") {
|
||||
val owner = params("owner")
|
||||
val owner = params("owner")
|
||||
val repository = params("repository")
|
||||
|
||||
fileList(owner, repository, params("branch"))
|
||||
@@ -52,7 +59,7 @@ class RepositoryViewerServlet extends ServletBase {
|
||||
* Shows the file list of the specified path and branch.
|
||||
*/
|
||||
get("/:owner/:repository/tree/:branch/*") {
|
||||
val owner = params("owner")
|
||||
val owner = params("owner")
|
||||
val repository = params("repository")
|
||||
|
||||
fileList(owner, repository, params("branch"), multiParams("splat").head)
|
||||
@@ -62,12 +69,11 @@ class RepositoryViewerServlet extends ServletBase {
|
||||
* Shows the commit list of the specified branch.
|
||||
*/
|
||||
get("/:owner/:repository/commits/:branch"){
|
||||
val owner = params("owner")
|
||||
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)
|
||||
val page = params.getOrElse("page", "1").toInt
|
||||
val dir = getBranchDir(owner, repository, branchName)
|
||||
|
||||
// TODO Do recursive without var.
|
||||
val i = Git.open(dir).log.call.iterator
|
||||
@@ -91,14 +97,12 @@ class RepositoryViewerServlet extends ServletBase {
|
||||
* Shows the file content of the specified branch.
|
||||
*/
|
||||
get("/:owner/:repository/blob/:branch/*"){
|
||||
val owner = params("owner")
|
||||
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")
|
||||
val path = multiParams("splat").head.replaceFirst("^tree/.+?/", "")
|
||||
val dir = getBranchDir(owner, repository, branchName)
|
||||
val content = FileUtils.readFileToString(new File(dir, path), "UTF-8")
|
||||
|
||||
val git = Git.open(dir)
|
||||
val latestRev = git.log.addPath(path).call.iterator.next
|
||||
@@ -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.
|
||||
*
|
||||
|
||||
@@ -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(" ", " ").replaceAll("\t", " ").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) + "..."
|
||||
|
||||
41
src/main/twirl/commit.scala.html
Normal file
41
src/main/twirl/commit.scala.html
Normal 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>
|
||||
}
|
||||
}
|
||||
@@ -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>
|
||||
|
||||
@@ -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>
|
||||
|
||||
Reference in New Issue
Block a user