diff --git a/src/main/scala/gitbucket/core/controller/ControllerBase.scala b/src/main/scala/gitbucket/core/controller/ControllerBase.scala index 5d449ba..a94c90a 100644 --- a/src/main/scala/gitbucket/core/controller/ControllerBase.scala +++ b/src/main/scala/gitbucket/core/controller/ControllerBase.scala @@ -180,11 +180,14 @@ * Context object for the current request. */ case class Context(settings: SystemSettingsService.SystemSettings, loginAccount: Option[Account], request: HttpServletRequest){ - val path = settings.baseUrl.getOrElse(request.getContextPath) val currentPath = request.getRequestURI.substring(request.getContextPath.length) val baseUrl = settings.baseUrl(request) val host = new java.net.URL(baseUrl).getHost + val repoBase = RepoBase( + baseUrl, + if (settings.ssh) Some(SshAddress(host, settings.sshPortOrDefault)) else None + ) val platform = request.getHeader("User-Agent") match { case null => null case agent if agent.contains("Mac") => "mac" diff --git a/src/main/scala/gitbucket/core/service/RepositoryService.scala b/src/main/scala/gitbucket/core/service/RepositoryService.scala index 8c4e49e..1d68345 100644 --- a/src/main/scala/gitbucket/core/service/RepositoryService.scala +++ b/src/main/scala/gitbucket/core/service/RepositoryService.scala @@ -2,7 +2,7 @@ import gitbucket.core.model.{Collaborator, Repository, Account} import gitbucket.core.model.Profile._ -import gitbucket.core.util.JGitUtil +import gitbucket.core.util.{JGitUtil, RepoBase} import profile.simple._ trait RepositoryService { self: AccountService => @@ -392,23 +392,11 @@ object RepositoryService { - object RepositoryInfo { - def httpUrl(baseUrl:String, owner:String, name:String):String = s"${baseUrl}/git/${owner}/${name}.git" - def sshUrl(baseUrl:String, owner:String, name:String)(port: Int, userName: String):String = { - val host = """^https?://(.+?)(:\d+)?/""".r.findFirstMatchIn(baseUrl).get.group(1) - s"ssh://${userName}@${host}:${port}/${owner}/${name}.git" - } - } case class RepositoryInfo(owner: String, name: String, repository: Repository, - httpUrl: String, sshUrl:(Int, String)=>String, issueCount: Int, pullCount: Int, commitCount: Int, forkedCount: Int, - branchList: Seq[String], tags: Seq[JGitUtil.TagInfo], managers: Seq[String]){ + branchList: Seq[String], tags: Seq[JGitUtil.TagInfo], managers: Seq[String]) { - def sshOpenRepoUrl(platform: String, port: Int, userName: String) = openRepoUrl(platform, sshUrl(port, userName)) - - def httpOpenRepoUrl(platform: String) = openRepoUrl(platform, httpUrl) - - def openRepoUrl(platform: String, openUrl: String) = s"github-${platform}://openRepo/${openUrl}" + def urls(repoBase:RepoBase):RepositoryUrls = new RepositoryUrls(repoBase, owner, name) /** * Creates instance with issue count and pull request count. @@ -416,8 +404,6 @@ def this(repo: JGitUtil.RepositoryInfo, model: Repository, baseUrl:String, issueCount: Int, pullCount: Int, forkedCount: Int, managers: Seq[String]) = this( repo.owner, repo.name, model, - RepositoryInfo.httpUrl(baseUrl, repo.owner, repo.name), - RepositoryInfo.sshUrl(baseUrl, repo.owner, repo.name), issueCount, pullCount, repo.commitCount, forkedCount, repo.branchList, repo.tags, managers) @@ -427,11 +413,27 @@ def this(repo: JGitUtil.RepositoryInfo, model: Repository, baseUrl:String, forkedCount: Int, managers: Seq[String]) = this( repo.owner, repo.name, model, - RepositoryInfo.httpUrl(baseUrl, repo.owner, repo.name), - RepositoryInfo.sshUrl(baseUrl, repo.owner, repo.name), 0, 0, repo.commitCount, forkedCount, repo.branchList, repo.tags, managers) } + final class RepositoryUrls(repoBase:RepoBase, owner:String, name:String) { + def httpUrl:String = + s"${repoBase.baseUrl}/git/${owner}/${name}.git" + + // BETTER make this return an Option and use it in the gui + def sshUrl(userName: String):String = + repoBase.sshAddress.fold("")(adr => s"ssh://${userName}@${adr.host}:${adr.port}/${owner}/${name}.git") + + def sshOpenRepoUrl(platform: String, userName: String) = + openRepoUrl(platform, sshUrl(userName)) + + def httpOpenRepoUrl(platform: String) = + openRepoUrl(platform, httpUrl) + + private def openRepoUrl(platform: String, openUrl: String) = + s"github-${platform}://openRepo/${openUrl}" + } + case class RepositoryTreeNode(owner: String, name: String, children: List[RepositoryTreeNode]) } diff --git a/src/main/scala/gitbucket/core/service/WikiService.scala b/src/main/scala/gitbucket/core/service/WikiService.scala index 3d2f230..ed898ba 100644 --- a/src/main/scala/gitbucket/core/service/WikiService.scala +++ b/src/main/scala/gitbucket/core/service/WikiService.scala @@ -38,10 +38,11 @@ */ case class WikiPageHistoryInfo(name: String, committer: String, message: String, date: Date) - def httpUrl(repository: RepositoryInfo) = repository.httpUrl.replaceFirst("\\.git\\Z", ".wiki.git") + def httpUrl(repoBase:RepoBase, repository: RepositoryInfo) = + repository.urls(repoBase).httpUrl.replaceFirst("\\.git\\Z", ".wiki.git") - def sshUrl(repository: RepositoryInfo, settings: SystemSettingsService.SystemSettings, userName: String) = - repository.sshUrl(settings.sshPortOrDefault, userName).replaceFirst("\\.git\\Z", ".wiki.git") + def sshUrl(repoBase:RepoBase, repository: RepositoryInfo, userName: String) = + repository.urls(repoBase).sshUrl(userName).replaceFirst("\\.git\\Z", ".wiki.git") } trait WikiService { diff --git a/src/main/scala/gitbucket/core/util/RepoBase.scala b/src/main/scala/gitbucket/core/util/RepoBase.scala new file mode 100644 index 0000000..5a577b8 --- /dev/null +++ b/src/main/scala/gitbucket/core/util/RepoBase.scala @@ -0,0 +1,3 @@ +package gitbucket.core.util + +case class RepoBase(baseUrl:String, sshAddress:Option[SshAddress]) diff --git a/src/main/scala/gitbucket/core/util/SshAddress.scala b/src/main/scala/gitbucket/core/util/SshAddress.scala new file mode 100644 index 0000000..aafbcba --- /dev/null +++ b/src/main/scala/gitbucket/core/util/SshAddress.scala @@ -0,0 +1,3 @@ +package gitbucket.core.util + +case class SshAddress(host:String, port:Int) diff --git a/src/main/scala/gitbucket/core/view/Markdown.scala b/src/main/scala/gitbucket/core/view/Markdown.scala index 2d57d1c..b31eef8 100644 --- a/src/main/scala/gitbucket/core/view/Markdown.scala +++ b/src/main/scala/gitbucket/core/view/Markdown.scala @@ -60,6 +60,8 @@ pages: List[String]) (implicit val context: Context) extends Renderer(options) with LinkConverter with RequestCache { + private val repositoryUrls = repository.urls(context.repoBase) + override def heading(text: String, level: Int, raw: String): String = { val id = generateAnchorName(text) val out = new StringBuilder() @@ -135,7 +137,7 @@ (link, link) } - val url = repository.httpUrl.replaceFirst("/git/", "/").stripSuffix(".git") + "/wiki/" + StringUtil.urlEncode(page) + val url = repositoryUrls.httpUrl.replaceFirst("/git/", "/").stripSuffix(".git") + "/wiki/" + StringUtil.urlEncode(page) if(pages.contains(page)){ "" + escape(label) + "" } else { @@ -157,14 +159,14 @@ } else if(context.currentPath.contains("/tree/")){ val paths = context.currentPath.split("/") val branch = if(paths.length > 3) paths.drop(4).mkString("/") else repository.repository.defaultBranch - repository.httpUrl.replaceFirst("/git/", "/").stripSuffix(".git") + "/blob/" + branch + "/" + url + (if(isImage) "?raw=true" else "") + repositoryUrls.httpUrl.replaceFirst("/git/", "/").stripSuffix(".git") + "/blob/" + branch + "/" + url + (if(isImage) "?raw=true" else "") } else { val paths = context.currentPath.split("/") val branch = if(paths.length > 3) paths.last else repository.repository.defaultBranch - repository.httpUrl.replaceFirst("/git/", "/").stripSuffix(".git") + "/blob/" + branch + "/" + url + (if(isImage) "?raw=true" else "") + repositoryUrls.httpUrl.replaceFirst("/git/", "/").stripSuffix(".git") + "/blob/" + branch + "/" + url + (if(isImage) "?raw=true" else "") } } else { - repository.httpUrl.replaceFirst("/git/", "/").stripSuffix(".git") + "/wiki/_blob/" + url + repositoryUrls.httpUrl.replaceFirst("/git/", "/").stripSuffix(".git") + "/wiki/_blob/" + url } } diff --git a/src/main/twirl/gitbucket/core/main.scala.html b/src/main/twirl/gitbucket/core/main.scala.html index b1d597a..2b179b4 100644 --- a/src/main/twirl/gitbucket/core/main.scala.html +++ b/src/main/twirl/gitbucket/core/main.scala.html @@ -37,7 +37,7 @@ @repository.map { repository => @if(!repository.repository.isPrivate){ - + } } diff --git a/src/main/twirl/gitbucket/core/menu.scala.html b/src/main/twirl/gitbucket/core/menu.scala.html index 56e889b..828769e 100644 --- a/src/main/twirl/gitbucket/core/menu.scala.html +++ b/src/main/twirl/gitbucket/core/menu.scala.html @@ -80,8 +80,8 @@
Step 1: From your project repository, check out a new branch and test the changes.
@defining(s"git checkout -b ${pullreq.requestUserName}-${pullreq.requestBranch} ${pullreq.branch}\n" + - s"git pull ${forkedRepository.httpUrl} ${pullreq.requestBranch}"){ command => + s"git pull ${forkedRepository.urls(repoBase).httpUrl} ${pullreq.requestBranch}"){ command => @helper.html.copy("merge-command-copy-1", command){@Html(command)} @@ -174,24 +174,24 @@ @if(settings.ssh && loginAccount.isDefined){ $('#repository-url-http').click(function(){ // Update URL box - $('#repository-url').val('@forkedRepository.httpUrl'); + $('#repository-url').val('@forkedRepository.urls(repoBase).httpUrl'); $('#repository-url-copy').attr('data-clipboard-text', $('#repository-url').val()); // Update command guidance $('#merge-command').text($('#merge-command').text().replace( - '@forkedRepository.sshUrl(settings.sshPortOrDefault, loginAccount.get.userName)', - '@forkedRepository.httpUrl' + '@forkedRepository.urls(repoBase).sshUrl(loginAccount.get.userName)', + '@forkedRepository.urls(repoBase).httpUrl' )); $('#merge-command-copy-1').attr('data-clipboard-text', $('#merge-command').text()); }); $('#repository-url-ssh').click(function(){ // Update URL box - $('#repository-url').val('@forkedRepository.sshUrl(settings.sshPortOrDefault, loginAccount.get.userName)'); + $('#repository-url').val('@forkedRepository.urls(repoBase).sshUrl(loginAccount.get.userName)'); $('#repository-url-copy').attr('data-clipboard-text', $('#repository-url').val()); // Update command guidance $('#merge-command').text($('#merge-command').text().replace( - '@forkedRepository.httpUrl', - '@forkedRepository.sshUrl(settings.sshPortOrDefault, loginAccount.get.userName)' + '@forkedRepository.urls(repoBase).httpUrl', + '@forkedRepository.urls(repoBase).sshUrl(loginAccount.get.userName)' )); $('#merge-command-copy-1').attr('data-clipboard-text', $('#merge-command').text()); }); diff --git a/src/main/twirl/gitbucket/core/repo/guide.scala.html b/src/main/twirl/gitbucket/core/repo/guide.scala.html index a33545f..e39d37b 100644 --- a/src/main/twirl/gitbucket/core/repo/guide.scala.html +++ b/src/main/twirl/gitbucket/core/repo/guide.scala.html @@ -10,10 +10,10 @@ } else {