diff --git a/src/main/scala/app/RepositoryViewerController.scala b/src/main/scala/app/RepositoryViewerController.scala index 375fd84..08bc547 100644 --- a/src/main/scala/app/RepositoryViewerController.scala +++ b/src/main/scala/app/RepositoryViewerController.scala @@ -12,7 +12,8 @@ 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 @@ 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 @@ /** * 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 @@ } }) + 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 @@ 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 } diff --git a/src/main/twirl/repo/blob.scala.html b/src/main/twirl/repo/blob.scala.html index 42ab0cc..f7849fa 100644 --- a/src/main/twirl/repo/blob.scala.html +++ b/src/main/twirl/repo/blob.scala.html @@ -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 @@ @datetime(latestCommit.time) + @if(hasWritePermission){ +