Skip to content

Commit c8ef41a

Browse files
committed
support fallback branches: gb-pages then gh-pages; ref #5
1 parent 929cb49 commit c8ef41a

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

src/main/scala/gitbucket/plugin/pages/pages.scala

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import org.eclipse.jgit.lib.ObjectId
99
import org.eclipse.jgit.revwalk.RevCommit
1010
import org.eclipse.jgit.treewalk.TreeWalk
1111

12+
import scala.annotation.tailrec
1213
import scala.language.implicitConversions
1314

1415
class PagesController
@@ -20,12 +21,12 @@ class PagesController
2021
trait PagesControllerBase extends ControllerBase {
2122
self: AccountService with RepositoryService with ReferrerAuthenticator =>
2223

23-
val PAGES_BRANCH = "gh-pages"
24+
val PAGES_BRANCHES = List("gb-pages", "gh-pages")
2425

2526
get("/:owner/:repository/pages/*")(referrersOnly { repository =>
2627
val path = params("splat")
2728
using(Git.open(Directory.getRepositoryDir(repository.owner, repository.name))) { git =>
28-
val objectId = Option(git.getRepository.resolve(PAGES_BRANCH))
29+
val objectId = resolveBranch(git, PAGES_BRANCHES)
2930
.map(JGitUtil.getRevCommitFromId(git, _))
3031
.flatMap { revCommit =>
3132
getPathIndexObjectId(git, path, revCommit)
@@ -54,6 +55,17 @@ trait PagesControllerBase extends ControllerBase {
5455
redirect(s"/${repository.owner}/${repository.name}/pages/")
5556
})
5657

58+
@tailrec
59+
final def resolveBranch(git: Git, names: List[String]): Option[ObjectId] = {
60+
names match {
61+
case name :: rest =>
62+
val objectId = Option(git.getRepository.resolve(name))
63+
if (objectId.isEmpty) resolveBranch(git, rest)
64+
else objectId
65+
case Nil => None
66+
}
67+
}
68+
5769
def shouldRedirect(path: String, path0: String) =
5870
!isRoot(path) && path0 != path && path0.startsWith(path) && !path.endsWith("/")
5971

0 commit comments

Comments
 (0)