diff --git a/src/main/scala/gitbucket/core/controller/RepositoryViewerController.scala b/src/main/scala/gitbucket/core/controller/RepositoryViewerController.scala index 77d9e700f..48cc2d3b1 100644 --- a/src/main/scala/gitbucket/core/controller/RepositoryViewerController.scala +++ b/src/main/scala/gitbucket/core/controller/RepositoryViewerController.scala @@ -841,7 +841,7 @@ trait RepositoryViewerControllerBase extends ControllerBase { defining(JGitUtil.getRevCommitFromId(git, objectId)) { revCommit => val lastModifiedCommit = if(path == ".") revCommit else JGitUtil.getLastModifiedCommit(git, revCommit, path) // get files - val files = JGitUtil.getFileList(git, revision, path) + val files = JGitUtil.getFileList(git, revision, path, context.settings.baseUrl) val parentPath = if (path == ".") Nil else path.split("/").toList // process README.md or README.markdown val readme = files.find { file => diff --git a/src/main/scala/gitbucket/core/util/JGitUtil.scala b/src/main/scala/gitbucket/core/util/JGitUtil.scala index 0ef399b19..c5fe1cd3d 100644 --- a/src/main/scala/gitbucket/core/util/JGitUtil.scala +++ b/src/main/scala/gitbucket/core/util/JGitUtil.scala @@ -151,9 +151,10 @@ object JGitUtil { * * @param name the module name * @param path the path in the repository - * @param url the repository url of this module + * @param repositoryUrl the repository url of this module + * @param viewerUrl the repository viewer url of this module */ - case class SubmoduleInfo(name: String, path: String, url: String) + case class SubmoduleInfo(name: String, path: String, repositoryUrl: String, viewerUrl: String) case class BranchMergeInfo(ahead: Int, behind: Int, isMerged: Boolean) @@ -252,9 +253,10 @@ object JGitUtil { * @param git the Git object * @param revision the branch name or commit id * @param path the directory path (optional) + * @param baseUrl the base url of GitBucket instance. This parameter is used to generate links of submodules (optional) * @return HTML of the file list */ - def getFileList(git: Git, revision: String, path: String = "."): List[FileInfo] = { + def getFileList(git: Git, revision: String, path: String = ".", baseUrl: Option[String] = None): List[FileInfo] = { using(new RevWalk(git.getRepository)){ revWalk => val objectId = git.getRepository.resolve(revision) if(objectId == null) return Nil @@ -340,7 +342,7 @@ object JGitUtil { useTreeWalk(revCommit){ treeWalk => while (treeWalk.next()) { val linkUrl = if (treeWalk.getFileMode(0) == FileMode.GITLINK) { - getSubmodules(git, revCommit.getTree).find(_.path == treeWalk.getPathString).map(_.url) + getSubmodules(git, revCommit.getTree, baseUrl).find(_.path == treeWalk.getPathString).map(_.viewerUrl) } else None fileList +:= (treeWalk.getObjectId(0), treeWalk.getFileMode(0), treeWalk.getNameString, treeWalk.getPathString, linkUrl) } @@ -730,7 +732,7 @@ object JGitUtil { /** * Read submodule information from .gitmodules */ - def getSubmodules(git: Git, tree: RevTree): List[SubmoduleInfo] = { + def getSubmodules(git: Git, tree: RevTree, baseUrl: Option[String]): List[SubmoduleInfo] = { val repository = git.getRepository getContentFromPath(git, tree, ".gitmodules", true).map { bytes => (try { @@ -738,7 +740,7 @@ object JGitUtil { config.getSubsections("submodule").asScala.map { module => val path = config.getString("submodule", module, "path") val url = config.getString("submodule", module, "url") - SubmoduleInfo(module, path, url) + SubmoduleInfo(module, path, url, StringUtil.getRepositoryViewerUrl(url, baseUrl)) } } catch { case e: ConfigInvalidException => { diff --git a/src/main/scala/gitbucket/core/util/StringUtil.scala b/src/main/scala/gitbucket/core/util/StringUtil.scala index 908fd2586..bf79ebf56 100644 --- a/src/main/scala/gitbucket/core/util/StringUtil.scala +++ b/src/main/scala/gitbucket/core/util/StringUtil.scala @@ -123,17 +123,22 @@ object StringUtil { "(?i)(? s"${removeUserName(base)}/$user/$repository" + case GitHubUrlPattern (_, user, repository) => s"https://github.com/$user/$repository" + case BitBucketUrlPattern(_, user, repository) => s"https://bitbucket.org/$user/$repository" + case GitLabUrlPattern (_, user, repository) => s"https://gitlab.com/$user/$repository" + case _ => gitRepositoryUrl + } + } -// /** -// * Encode search string for LIKE condition. -// * This method has been copied from Slick's SqlUtilsComponent. -// */ -// def likeEncode(s: String) = { -// val b = new StringBuilder -// for(c <- s) c match { -// case '%' | '_' | '^' => b append '^' append c -// case _ => b append c -// } -// b.toString -// } } diff --git a/src/test/scala/gitbucket/core/util/StringUtilSpec.scala b/src/test/scala/gitbucket/core/util/StringUtilSpec.scala index f5e850e70..ecb1e5635 100644 --- a/src/test/scala/gitbucket/core/util/StringUtilSpec.scala +++ b/src/test/scala/gitbucket/core/util/StringUtilSpec.scala @@ -63,4 +63,24 @@ class StringUtilSpec extends FunSpec { assert(StringUtil.extractCloseId("(refs #123)").toSeq == Nil) } } + + describe("getRepositoryViewerUrl") { + val baseUrl = Some("http://localhost:8080") + it("should convert GitBucket repository url"){ + assert(StringUtil.getRepositoryViewerUrl("http://localhost:8080/git/root/gitbucket.git", baseUrl) == "http://localhost:8080/root/gitbucket") + assert(StringUtil.getRepositoryViewerUrl("http://root@localhost:8080/git/root/gitbucket.git", baseUrl) == "http://localhost:8080/root/gitbucket") + } + it("should convert GitHub repository url"){ + assert(StringUtil.getRepositoryViewerUrl("https://github.com/root/gitbucket.git", baseUrl) == "https://github.com/root/gitbucket") + assert(StringUtil.getRepositoryViewerUrl("https://root@github.com/root/gitbucket.git", baseUrl) == "https://github.com/root/gitbucket") + } + it("should convert BitBucket repository url"){ + assert(StringUtil.getRepositoryViewerUrl("https://bitbucket.org/root/gitbucket.git", baseUrl) == "https://bitbucket.org/root/gitbucket") + assert(StringUtil.getRepositoryViewerUrl("https://root@bitbucket.org/root/gitbucket.git", baseUrl) == "https://bitbucket.org/root/gitbucket") + } + it("should convert GitLab repository url"){ + assert(StringUtil.getRepositoryViewerUrl("https://gitlab.com/root/gitbucket.git", baseUrl) == "https://gitlab.com/root/gitbucket") + assert(StringUtil.getRepositoryViewerUrl("https://root@gitlab.com/root/gitbucket.git", baseUrl) == "https://gitlab.com/root/gitbucket") + } + } }