Skip to content

Commit c4ba8c7

Browse files
committed
initial commit
0 parents  commit c4ba8c7

File tree

6 files changed

+127
-0
lines changed

6 files changed

+127
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
*.iml
2+
.idea/
3+
target/

build.sbt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
organization := "gitbucket"
2+
3+
name := "pages-plugin"
4+
5+
version := "0.1"
6+
7+
scalaVersion := "2.11.7"
8+
9+
scalacOptions := Seq("-unchecked", "-deprecation", "-encoding", "utf8", "-feature")
10+
11+
resolvers += Resolver.jcenterRepo
12+
13+
resolvers += "amateras-repo" at "http://amateras.sourceforge.jp/mvn/"
14+
15+
libraryDependencies ++= Seq(
16+
"gitbucket" % "gitbucket-assembly" % "3.9.0" % "provided",
17+
"javax.servlet" % "javax.servlet-api" % "3.1.0" % "provided"
18+
)

project/build.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
sbt.version=0.13.9

project/plugins.sbt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
addSbtPlugin("org.scalariform" % "sbt-scalariform" % "1.6.0")

src/main/scala/Plugin.scala

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
2+
import gitbucket.core.controller.ControllerBase
3+
import gitbucket.core.util.Version
4+
import gitbucket.plugin.pages.PagesController
5+
6+
class Plugin extends gitbucket.core.plugin.Plugin {
7+
override val pluginId: String = "pages"
8+
override val pluginName: String = "Pages Plugin"
9+
override val versions: Seq[Version] = Seq(Version(3, 9))
10+
override val description: String = "Provides Pages feature on GitBucket"
11+
12+
override val controllers: Seq[(String, ControllerBase)] = Seq(
13+
"/*" -> new PagesController
14+
)
15+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package gitbucket.plugin.pages
2+
3+
import gitbucket.core.controller.ControllerBase
4+
import gitbucket.core.service.{ AccountService, RepositoryService }
5+
import gitbucket.core.util.ControlUtil._
6+
import gitbucket.core.util.{ Directory, JGitUtil, ReferrerAuthenticator }
7+
import org.eclipse.jgit.api.Git
8+
import org.eclipse.jgit.lib.ObjectId
9+
import org.eclipse.jgit.revwalk.RevCommit
10+
import org.eclipse.jgit.treewalk.TreeWalk
11+
12+
import scala.language.implicitConversions
13+
14+
class PagesController
15+
extends ControllerBase
16+
with AccountService
17+
with RepositoryService
18+
with ReferrerAuthenticator
19+
20+
trait PagesControllerBase {
21+
self: ControllerBase with AccountService with RepositoryService with ReferrerAuthenticator =>
22+
23+
val PAGES_BRANCH = "gh-pages"
24+
25+
get("/:owner/:repository/pages/*")(referrersOnly { repository =>
26+
val path = params("splat")
27+
using(Git.open(Directory.getRepositoryDir(repository.owner, repository.name))) { git =>
28+
val objectId = Option(git.getRepository.resolve(PAGES_BRANCH))
29+
.map(JGitUtil.getRevCommitFromId(git, _))
30+
.flatMap { revCommit =>
31+
getPathIndexObjectId(git, path, revCommit)
32+
}
33+
34+
objectId match {
35+
case Some((path0, objId)) =>
36+
// redirect [owner/repo/pages/path] -> [owner/repo/pages/path/]
37+
if (path0 != path && path0.startsWith(path) && !path.endsWith("/")) {
38+
redirect(s"/${repository.owner}/${repository.name}/pages/$path/")
39+
} else {
40+
JGitUtil.getObjectLoaderFromId(git, objId) { loader =>
41+
contentType = Option(servletContext.getMimeType(path0)).getOrElse("application/octet-stream")
42+
response.setContentLength(loader.getSize.toInt)
43+
loader.copyTo(response.getOutputStream)
44+
}
45+
()
46+
}
47+
case None =>
48+
NotFound()
49+
}
50+
}
51+
})
52+
53+
get("/:owner/:repository/pages")(referrersOnly { repository =>
54+
redirect(s"/${repository.owner}/${repository.name}/pages/")
55+
})
56+
57+
def getPathIndexObjectId(git: Git, path: String, revCommit: RevCommit) = {
58+
getPathObjectId0(git, path, revCommit)
59+
.orElse(getPathObjectId0(git, appendPath(path, "index.html"), revCommit))
60+
.orElse(getPathObjectId0(git, appendPath(path, "index.htm"), revCommit))
61+
}
62+
63+
def getPathObjectId0(git: Git, path: String, revCommit: RevCommit) = {
64+
getPathObjectId(git, path, revCommit).map(path -> _)
65+
}
66+
67+
def appendPath(base: String, suffix: String): String = {
68+
if (base == "") suffix
69+
else if (base.endsWith("/")) base + suffix
70+
else base + "/" + suffix
71+
}
72+
73+
// copy&paste from RepositoryViewerControllerBase
74+
private def getPathObjectId(git: Git, path: String, revCommit: RevCommit): Option[ObjectId] = {
75+
@scala.annotation.tailrec
76+
def _getPathObjectId(path: String, walk: TreeWalk): Option[ObjectId] = walk.next match {
77+
case true if walk.getPathString == path => Some(walk.getObjectId(0))
78+
case true => _getPathObjectId(path, walk)
79+
case false => None
80+
}
81+
82+
using(new TreeWalk(git.getRepository)) { treeWalk =>
83+
treeWalk.addTree(revCommit.getTree)
84+
treeWalk.setRecursive(true)
85+
_getPathObjectId(path, treeWalk)
86+
}
87+
}
88+
}
89+

0 commit comments

Comments
 (0)