@@ -13,8 +13,9 @@ import org.eclipse.jgit.api.Git
1313import org .eclipse .jgit .lib .ObjectId
1414import org .eclipse .jgit .revwalk .RevCommit
1515import org .scalatra .i18n .Messages
16- import scala .util .Using
1716
17+ import javax .servlet .http .HttpServletRequest
18+ import scala .util .Using
1819import scala .annotation .tailrec
1920import scala .language .implicitConversions
2021
@@ -30,26 +31,32 @@ trait PagesControllerBase extends ControllerBase {
3031 self : AccountService with RepositoryService with PagesService with ReferrerAuthenticator with OwnerAuthenticator =>
3132 import PagesControllerBase ._
3233
33- val optionsForm = mapping(" source " -> trim(label( " Pages Source " , text(required, pagesOption))))((source) =>
34- OptionsForm ( PageSourceType .valueOf(source ))
35- )
34+ val optionsForm : MappingValueType [ OptionsForm ] = mapping(
35+ " source " -> trim(label( " Pages Source " , text(required, pagesOption) ))
36+ )(source => OptionsForm ( PageSourceType .valueOf(source)))
3637
3738 val PAGES_BRANCHES = List (" gb-pages" , " gh-pages" )
3839
40+ def endsWithSlash (): Boolean = request.getServletPath.endsWith(" /" )
41+
3942 get(" /:owner/:repository/pages/*" )(referrersOnly { repository =>
4043 renderPage(repository, params(" splat" ))
4144 })
4245
4346 get(" /:owner/:repository/pages" )(referrersOnly { repository =>
44- renderPage(repository, " " )
47+ if (endsWithSlash()) {
48+ renderPage(repository, " " )
49+ } else {
50+ redirect(s " / ${repository.owner}/ ${repository.name}/pages/ " )
51+ }
4552 })
4653
4754 private def renderPage (repository : RepositoryInfo , path : String ) = {
4855 val defaultBranch = repository.repository.defaultBranch
4956 Using .resource(Git .open(Directory .getRepositoryDir(repository.owner, repository.name))) { git =>
5057 getPageSource(repository.owner, repository.name) match {
5158 case PageSourceType .GH_PAGES =>
52- renderFromBranch(repository, git, path, PAGES_BRANCHES .collectFirstOpt( resolveBranch(git, _)))
59+ renderFromBranch(repository, git, path, PAGES_BRANCHES .collectFirst( Function .unlift( resolveBranch(git, _) )))
5360 case PageSourceType .MASTER =>
5461 renderFromBranch(repository, git, path, resolveBranch(git, defaultBranch))
5562 case PageSourceType .MASTER_DOCS =>
@@ -100,14 +107,14 @@ trait PagesControllerBase extends ControllerBase {
100107 }
101108 }
102109
103- def resolveBranch (git : Git , name : String ) = Option (git.getRepository.resolve(name))
110+ def resolveBranch (git : Git , name : String ): Option [ ObjectId ] = Option (git.getRepository.resolve(name))
104111
105112 // redirect [owner/repo/pages/path] -> [owner/repo/pages/path/]
106113 def shouldRedirect (path : String , path0 : String ): Boolean =
107- ! isRoot(path) && path0 != path && path0.startsWith(path) && ! path.endsWith( " / " )
114+ ! isRoot(path) && path0 != path && path0.startsWith(path) && ! endsWithSlash( )
108115
109116 def getPageObjectId (git : Git , path : String , revCommit : RevCommit ): Option [(String , ObjectId )] = {
110- listProbablePages(path).collectFirstOpt( getPathObjectIdPair(git, _, revCommit))
117+ listProbablePages(path).collectFirst( Function .unlift( getPathObjectIdPair(git, _, revCommit) ))
111118 }
112119
113120 def listProbablePages (path : String ): List [String ] = {
@@ -135,20 +142,6 @@ trait PagesControllerBase extends ControllerBase {
135142object PagesControllerBase {
136143 case class OptionsForm (source : PageSourceType )
137144
138- implicit class listCollectFirst [A ](private val lst : List [A ]) extends AnyVal {
139- @ tailrec
140- final def collectFirstOpt [B ](f : A => Option [B ]): Option [B ] = {
141- lst match {
142- case head :: tail =>
143- f(head) match {
144- case Some (x) => Some (x)
145- case None => tail.collectFirstOpt(f)
146- }
147- case Nil => None
148- }
149- }
150- }
151-
152145 def pagesOption : Constraint = new Constraint () {
153146 override def validate (name : String , value : String , messages : Messages ): Option [String ] =
154147 PageSourceType .valueOpt(value) match {
0 commit comments