(refs #13)Implementing file editing on the repository viewer

This commit is contained in:
takezoe
2014-04-24 07:42:00 +09:00
parent dd688f48b7
commit 3721b328a6
3 changed files with 46 additions and 7 deletions

View File

@@ -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
}