mirror of
https://github.com/gitbucket/gitbucket.git
synced 2025-11-04 20:45:58 +01:00
(refs #13)Implementing file editing on the repository viewer
This commit is contained in:
@@ -12,7 +12,8 @@ import org.eclipse.jgit.lib._
|
||||
import org.apache.commons.io.FileUtils
|
||||
import org.eclipse.jgit.treewalk._
|
||||
import java.util.zip.{ZipEntry, ZipOutputStream}
|
||||
import scala.Some
|
||||
import jp.sf.amateras.scalatra.forms._
|
||||
import org.eclipse.jgit.dircache.DirCache
|
||||
|
||||
class RepositoryViewerController extends RepositoryViewerControllerBase
|
||||
with RepositoryService with AccountService with ActivityService with ReferrerAuthenticator with CollaboratorsAuthenticator
|
||||
@@ -23,6 +24,13 @@ class RepositoryViewerController extends RepositoryViewerControllerBase
|
||||
trait RepositoryViewerControllerBase extends ControllerBase {
|
||||
self: RepositoryService with AccountService with ActivityService with ReferrerAuthenticator with CollaboratorsAuthenticator =>
|
||||
|
||||
case class EditorForm(content: String, message: Option[String])
|
||||
|
||||
val editorForm = mapping(
|
||||
"content" -> trim(label("Content", text())),
|
||||
"message" -> trim(label("Messgae", optional(text())))
|
||||
)(EditorForm.apply)
|
||||
|
||||
/**
|
||||
* Returns converted HTML from Markdown for preview.
|
||||
*/
|
||||
@@ -74,9 +82,8 @@ trait RepositoryViewerControllerBase extends ControllerBase {
|
||||
/**
|
||||
* Displays the file content of the specified branch or commit.
|
||||
*/
|
||||
get("/:owner/:repository/edit/*")(referrersOnly { repository =>
|
||||
get("/:owner/:repository/edit/*")(collaboratorsOnly { repository =>
|
||||
val (id, path) = splitPath(repository, multiParams("splat").head)
|
||||
// val raw = params.get("raw").getOrElse("false").toBoolean
|
||||
|
||||
using(Git.open(getRepositoryDir(repository.owner, repository.name))){ git =>
|
||||
val revCommit = JGitUtil.getRevCommitFromId(git, git.getRepository.resolve(id))
|
||||
@@ -116,6 +123,24 @@ trait RepositoryViewerControllerBase extends ControllerBase {
|
||||
}
|
||||
})
|
||||
|
||||
post("/:owner/:repository/edit/*", editorForm)(collaboratorsOnly { (form, repository) =>
|
||||
// val (id, path) = splitPath(repository, multiParams("splat").head)
|
||||
// val loginAccount = context.loginAccount.get
|
||||
//
|
||||
// using(Git.open(getRepositoryDir(repository.owner, repository.name))){ git =>
|
||||
// val builder = DirCache.newInCore.builder()
|
||||
// val inserter = git.getRepository.newObjectInserter()
|
||||
// val headId = git.getRepository.resolve(Constants.HEAD + "^{commit}")
|
||||
//
|
||||
// builder.add(JGitUtil.createDirCacheEntry(path, FileMode.REGULAR_FILE,
|
||||
// inserter.insert(Constants.OBJ_BLOB, form.content.getBytes("UTF-8")))) // TODO charset auto detection
|
||||
// builder.finish()
|
||||
//
|
||||
// JGitUtil.createNewCommit(git, inserter, headId, builder.getDirCache.writeTree(inserter),
|
||||
// loginAccount.fullName, loginAccount.mailAddress, form.message.getOrElse(s"Update ${path.split("/").last}"))
|
||||
// }
|
||||
})
|
||||
|
||||
/**
|
||||
* Displays the file content of the specified branch or commit.
|
||||
*/
|
||||
@@ -163,7 +188,8 @@ trait RepositoryViewerControllerBase extends ControllerBase {
|
||||
JGitUtil.ContentInfo(viewer, None)
|
||||
}
|
||||
|
||||
repo.html.blob(id, repository, path.split("/").toList, content, new JGitUtil.CommitInfo(revCommit))
|
||||
repo.html.blob(id, repository, path.split("/").toList, content, new JGitUtil.CommitInfo(revCommit),
|
||||
hasWritePermission(repository.owner, repository.name, context.loginAccount))
|
||||
}
|
||||
} getOrElse NotFound
|
||||
}
|
||||
|
||||
@@ -2,7 +2,8 @@
|
||||
repository: service.RepositoryService.RepositoryInfo,
|
||||
pathList: List[String],
|
||||
content: util.JGitUtil.ContentInfo,
|
||||
latestCommit: util.JGitUtil.CommitInfo)(implicit context: app.Context)
|
||||
latestCommit: util.JGitUtil.CommitInfo,
|
||||
hasWritePermission: Boolean)(implicit context: app.Context)
|
||||
@import context._
|
||||
@import view.helpers._
|
||||
@html.main(s"${repository.owner}/${repository.name}", Some(repository)) {
|
||||
@@ -28,8 +29,15 @@
|
||||
<span class="muted">@datetime(latestCommit.time)</span>
|
||||
<a href="@url(repository)/commit/@latestCommit.id" class="commit-message">@link(latestCommit.summary, repository)</a>
|
||||
</div>
|
||||
@if(hasWritePermission){
|
||||
<div class="btn-group pull-right">
|
||||
<a class="btn btn-mini btn-danger" href="@url(repository)/delete/@encodeRefName(branch)/@pathList.mkString("/")">Delete</a>
|
||||
</div>
|
||||
}
|
||||
<div class="btn-group pull-right">
|
||||
<a class="btn btn-mini" href="@url(repository)/edit/@encodeRefName(branch)/@pathList.mkString("/")">Edit</a>
|
||||
@if(hasWritePermission){
|
||||
<a class="btn btn-mini" href="@url(repository)/edit/@encodeRefName(branch)/@pathList.mkString("/")">Edit</a>
|
||||
}
|
||||
<a class="btn btn-mini" href="?raw=true">Raw</a>
|
||||
<a class="btn btn-mini" href="@url(repository)/commits/@encodeRefName(branch)/@pathList.mkString("/")">History</a>
|
||||
</div>
|
||||
|
||||
@@ -59,7 +59,8 @@
|
||||
</div>
|
||||
<div style="text-align: right;">
|
||||
<a href="@url(repository)/blob/@encodeRefName(branch)/@pathList.mkString("/")" class="btn btn-danger">Cancel</a>
|
||||
<input type="submit" class="btn btn-success" value="Commit changes"/>
|
||||
<input type="submit" id="commit" class="btn btn-success" value="Commit changes"/>
|
||||
<input type="hidden" id="content" name="content" value=""/>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@@ -70,4 +71,8 @@
|
||||
var editor = ace.edit("editor");
|
||||
editor.setTheme("ace/theme/monokai");
|
||||
editor.getSession().setMode("ace/mode/@editorType(pathList.last)");
|
||||
|
||||
$('#commit').click(function(){
|
||||
$('#content').val(editor.getValue());
|
||||
});
|
||||
</script>
|
||||
|
||||
Reference in New Issue
Block a user