(refs #2)Comparing between the forked repository and the source repository.

This commit is contained in:
takezoe
2013-07-13 03:52:27 +09:00
parent f4cb0625bc
commit 2cc2902930
4 changed files with 153 additions and 0 deletions

View File

@@ -16,6 +16,7 @@ class ScalatraBootstrap extends LifeCycle {
context.mount(new LabelsController, "/*") context.mount(new LabelsController, "/*")
context.mount(new MilestonesController, "/*") context.mount(new MilestonesController, "/*")
context.mount(new IssuesController, "/*") context.mount(new IssuesController, "/*")
context.mount(new PullRequestsController, "/*")
context.mount(new RepositorySettingsController, "/*") context.mount(new RepositorySettingsController, "/*")
val dir = new java.io.File(_root_.util.Directory.GitBucketHome) val dir = new java.io.File(_root_.util.Directory.GitBucketHome)

View File

@@ -0,0 +1,69 @@
package app
import util.{FileUtil, JGitUtil, ReferrerAuthenticator}
import util.Directory._
import service._
import org.eclipse.jgit.treewalk.CanonicalTreeParser
import util.JGitUtil.{DiffInfo, CommitInfo}
import scala.collection.mutable.ArrayBuffer
class PullRequestsController extends PullRequestsControllerBase
with RepositoryService with AccountService with ReferrerAuthenticator
trait PullRequestsControllerBase extends ControllerBase {
self: ReferrerAuthenticator =>
get("/:owner/:repository/pulls")(referrersOnly { repository =>
pulls.html.list(repository)
})
// TODO Replace correct authenticator
get("/:owner/:repository/pulls/compare/*:*...*")(referrersOnly { repository =>
if(repository.repository.originUserName.isEmpty || repository.repository.originRepositoryName.isEmpty){
NotFound // TODO BadRequest?
} else {
val userName = params("owner")
val repositoryName = params("repository")
val Seq(origin, originId, forkedId) = multiParams("splat")
JGitUtil.withGit(getRepositoryDir(userName, repositoryName)){ newGit =>
JGitUtil.withGit(getRepositoryDir(origin, repository.repository.originRepositoryName.get)){ oldGit =>
val oldReader = oldGit.getRepository.newObjectReader
val oldTreeIter = new CanonicalTreeParser
oldTreeIter.reset(oldReader, oldGit.getRepository.resolve("master^{tree}"))
val newReader = newGit.getRepository.newObjectReader
val newTreeIter = new CanonicalTreeParser
newTreeIter.reset(newReader, newGit.getRepository.resolve("master^{tree}"))
import scala.collection.JavaConverters._
import util.Implicits._
val oldId = oldGit.getRepository.resolve(originId)
val newId = newGit.getRepository.resolve(forkedId)
val i = newGit.log.addRange(oldId, newId).call.iterator
val commits = new ArrayBuffer[CommitInfo]
while(i.hasNext){
val revCommit = i.next
commits += new CommitInfo(revCommit)
}
val diffs = newGit.diff.setOldTree(oldTreeIter).setNewTree(newTreeIter).call.asScala.map { diff =>
if(FileUtil.isImage(diff.getOldPath) || FileUtil.isImage(diff.getNewPath)){
DiffInfo(diff.getChangeType, diff.getOldPath, diff.getNewPath, None, None)
} else {
DiffInfo(diff.getChangeType, diff.getOldPath, diff.getNewPath,
JGitUtil.getContent(oldGit, diff.getOldId.toObjectId, false).filter(FileUtil.isText).map(new String(_, "UTF-8")),
JGitUtil.getContent(newGit, diff.getNewId.toObjectId, false).filter(FileUtil.isText).map(new String(_, "UTF-8")))
}
}
pulls.html.compare(commits.toList.splitWith{ (commit1, commit2) =>
view.helpers.date(commit1.time) == view.helpers.date(commit2.time)
}, diffs.toList, origin, originId, forkedId, newId.getName, repository)
}
}
}
})
}

View File

@@ -0,0 +1,75 @@
@(commits: Seq[Seq[util.JGitUtil.CommitInfo]], diffs: List[util.JGitUtil.DiffInfo],
origin: String, originId: String, forkedId: String, commitId: String,
repository: service.RepositoryService.RepositoryInfo)(implicit context: app.Context)
@import context._
@import view.helpers._
@import org.eclipse.jgit.diff.DiffEntry.ChangeType
@html.main("Pull Requests - " + repository.owner + "/" + repository.name){
@html.header("pulls", repository)
<div style="border: 1px solid #eee; background-color: #f8f8f8; margin-bottom: 10px; padding: 8px;">
<span class="label label-info monospace">@origin:@originId</span> ... <span class="label label-info monospace">@repository.owner:@forkedId</span>
</div>
<div style="margin-bottom: 10px;">
<a href="#" class="btn">Click to create a pull request for this comparison</a>
</div>
<div class="box">
<table class="table table-file-list" style="border: 1px solid silver;">
@commits.map { day =>
<tr>
<th colspan="3" class="box-header" style="font-weight: normal;">@date(day.head.time)</th>
</tr>
@day.map { commit =>
<tr>
<td style="width: 20%;">
@avatar(commit.committer, 20)
<a href="@url(commit.committer)" class="username">@commit.committer</a>
</td>
<td>@commit.shortMessage</td>
<td style="width: 10%; text-align: right;">
<a href="@url(repository)/commit/@commit.id" class="monospace">@commit.id.substring(0, 7)</a>
</td>
</tr>
}
}
</table>
</div>
<div>
<div class="pull-right" style="margin-bottom: 10px;">
<input id="toggle-file-list" type="button" class="btn" value="Show file list"/>
</div>
Showing @diffs.size changed @plural(diffs.size, "file")
</div>
<ul id="commit-file-list" style="display: none;">
@diffs.zipWithIndex.map { case (diff, i) =>
<li@if(i > 0){ class="border"}>
<a href="#diff-@i">
@if(diff.changeType == ChangeType.COPY || diff.changeType == ChangeType.RENAME){
<img src="@assets/common/images/diff_move.png"/> @diff.oldPath -> @diff.newPath
}
@if(diff.changeType == ChangeType.ADD){
<img src="@assets/common/images/diff_add.png"/> @diff.newPath
}
@if(diff.changeType == ChangeType.MODIFY){
<img src="@assets/common/images/diff_edit.png"/> @diff.newPath
}
@if(diff.changeType == ChangeType.DELETE){
<img src="@assets/common/images/diff_delete.png"/> @diff.oldPath
}
</a>
</li>
}
</ul>
@helper.html.diff(diffs, repository, Some(commitId))
}
<script>
$(function(){
$('#toggle-file-list').click(function(){
$('#commit-file-list').toggle();
if($(this).val() == 'Show file list'){
$(this).val('Hide file list');
} else {
$(this).val('Show file list');
}
});
});
</script>

View File

@@ -0,0 +1,8 @@
@(repository: service.RepositoryService.RepositoryInfo)(implicit context: app.Context)
@import context._
@import view.helpers._
@html.main("Pull Requests - " + repository.owner + "/" + repository.name){
@html.header("pulls", repository)
<a href="@url(repository)/pulls/compare" class="btn btn-success">New pull request</a>
}