Skip to content

Commit 2f89c59

Browse files
committed
make branch lookup look nicer
1 parent 9c4b311 commit 2f89c59

File tree

1 file changed

+21
-13
lines changed

1 file changed

+21
-13
lines changed

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

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,15 @@ class PagesController
2727

2828
trait PagesControllerBase extends ControllerBase {
2929
self: AccountService with RepositoryService with PagesService with ReferrerAuthenticator with OwnerAuthenticator =>
30+
import PagesControllerBase._
3031

3132
case class OptionsForm(source: PageSourceType)
3233

3334
val optionsForm = mapping(
3435
"source" -> trim(label("Pages Source", text(required, pagesOption)))
3536
)(
36-
(source) => OptionsForm(PageSourceType.valueOf(source))
37-
)
37+
(source) => OptionsForm(PageSourceType.valueOf(source))
38+
)
3839

3940
val PAGES_BRANCHES = List("gb-pages", "gh-pages")
4041

@@ -67,7 +68,7 @@ trait PagesControllerBase extends ControllerBase {
6768
}
6869
source match {
6970
case PageSourceType.GH_PAGES =>
70-
val objectId = resolveBranch(git, PAGES_BRANCHES)
71+
val objectId = PAGES_BRANCHES.collectFirst(resolveBranch(git, _))
7172
.map(JGitUtil.getRevCommitFromId(git, _))
7273
.flatMap { revCommit =>
7374
getPathIndexObjectId(git, path, revCommit)
@@ -111,16 +112,7 @@ trait PagesControllerBase extends ControllerBase {
111112
redirect(s"/${repository.owner}/${repository.name}/settings/pages")
112113
})
113114

114-
@tailrec
115-
final def resolveBranch(git: Git, names: List[String]): Option[ObjectId] = {
116-
names match {
117-
case name :: rest =>
118-
val objectId = Option(git.getRepository.resolve(name))
119-
if (objectId.isEmpty) resolveBranch(git, rest)
120-
else objectId
121-
case Nil => None
122-
}
123-
}
115+
def resolveBranch(git: Git, name: String) = Option(git.getRepository.resolve(name))
124116

125117
def shouldRedirect(path: String, path0: String) =
126118
!isRoot(path) && path0 != path && path0.startsWith(path) && !path.endsWith("/")
@@ -152,3 +144,19 @@ trait PagesControllerBase extends ControllerBase {
152144
}
153145
}
154146

147+
148+
object PagesControllerBase {
149+
implicit class listCollectFirst[A](private val lst: List[A]) extends AnyVal {
150+
@tailrec
151+
final def collectFirst[B](f: A => Option[B]): Option[B] = {
152+
lst match {
153+
case head :: tail =>
154+
f(head) match {
155+
case Some(x) => Some(x)
156+
case None => tail.collectFirst(f)
157+
}
158+
case Nil => None
159+
}
160+
}
161+
}
162+
}

0 commit comments

Comments
 (0)