Add a Patch button to the diff view

This commit is contained in:
Naoki Takezoe
2017-12-05 13:05:55 +09:00
parent 9a9be12324
commit 9cc466a727
3 changed files with 25 additions and 6 deletions

View File

@@ -466,10 +466,10 @@ trait RepositoryViewerControllerBase extends ControllerBase {
} }
}) })
get("/:owner/:repository/commit/:id/patch")(referrersOnly { repository => get("/:owner/:repository/patch/:id")(referrersOnly { repository =>
try { try {
using(Git.open(getRepositoryDir(repository.owner, repository.name))) { git => using(Git.open(getRepositoryDir(repository.owner, repository.name))) { git =>
val diff = JGitUtil.getPatch(git, params("id")) val diff = JGitUtil.getPatch(git, None, params("id"))
contentType = formats("txt") contentType = formats("txt")
diff diff
} }
@@ -478,6 +478,19 @@ trait RepositoryViewerControllerBase extends ControllerBase {
} }
}) })
get("/:owner/:repository/patch/*...*")(referrersOnly { repository =>
try {
val Seq(fromId, toId) = multiParams("splat")
using(Git.open(getRepositoryDir(repository.owner, repository.name))) { git =>
val diff = JGitUtil.getPatch(git, Some(fromId), toId)
contentType = formats("txt")
diff
}
} catch {
case e: MissingObjectException => NotFound()
}
})
post("/:owner/:repository/commit/:id/comment/new", commentForm)(readableUsersOnly { (form, repository) => post("/:owner/:repository/commit/:id/comment/new", commentForm)(readableUsersOnly { (form, repository) =>
val id = params("id") val id = params("id")
createCommitComment(repository.owner, repository.name, id, context.loginAccount.get.userName, form.content, createCommitComment(repository.owner, repository.name, id, context.loginAccount.get.userName, form.content,

View File

@@ -519,13 +519,13 @@ object JGitUtil {
}.toMap }.toMap
} }
def getPatch(git: Git, id: String): String = { def getPatch(git: Git, from: Option[String], to: String): String = {
val out = new ByteArrayOutputStream() val out = new ByteArrayOutputStream()
val df = new DiffFormatter(out) val df = new DiffFormatter(out)
df.setRepository(git.getRepository) df.setRepository(git.getRepository)
df.setDiffComparator(RawTextComparator.DEFAULT) df.setDiffComparator(RawTextComparator.DEFAULT)
df.setDetectRenames(true) df.setDetectRenames(true)
df.format(getDiffEntries(git, None, id).head) df.format(getDiffEntries(git, from, to).head)
new String(out.toByteArray, "UTF-8") new String(out.toByteArray, "UTF-8")
} }

View File

@@ -10,9 +10,15 @@
@import org.eclipse.jgit.diff.DiffEntry.ChangeType @import org.eclipse.jgit.diff.DiffEntry.ChangeType
@if(showIndex){ @if(showIndex){
<div class="pull-right" style="margin-bottom: 10px;"> <div class="pull-right" style="margin-bottom: 10px;">
@if(oldCommitId.isEmpty && newCommitId.isDefined) {
<a href="@helpers.url(repository)/patch/@newCommitId" class="btn btn-default">Patch</a>
}
@if(oldCommitId.isDefined && newCommitId.isDefined) {
<a href="@helpers.url(repository)/patch/@oldCommitId...@newCommitId" class="btn btn-default">Patch</a>
}
<div class="btn-group" data-toggle="buttons"> <div class="btn-group" data-toggle="buttons">
<input type="button" id="btn-unified" class="btn btn-default btn-small active" value="Unified"> <input type="button" id="btn-unified" class="btn btn-default active" value="Unified">
<input type="button" id="btn-split" class="btn btn-default btn-small" value="Split"> <input type="button" id="btn-split" class="btn btn-default" value="Split">
</div> </div>
</div> </div>
Showing <a href="javascript:void(0);" id="toggle-file-list">@diffs.size changed @helpers.plural(diffs.size, "file")</a> Showing <a href="javascript:void(0);" id="toggle-file-list">@diffs.size changed @helpers.plural(diffs.size, "file")</a>