diff --git a/src/main/scala/gitbucket/core/controller/RepositoryViewerController.scala b/src/main/scala/gitbucket/core/controller/RepositoryViewerController.scala index 77d9e70..48cc2d3 100644 --- a/src/main/scala/gitbucket/core/controller/RepositoryViewerController.scala +++ b/src/main/scala/gitbucket/core/controller/RepositoryViewerController.scala @@ -841,7 +841,7 @@ 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 0ef399b..c5fe1cd 100644 --- a/src/main/scala/gitbucket/core/util/JGitUtil.scala +++ b/src/main/scala/gitbucket/core/util/JGitUtil.scala @@ -151,9 +151,10 @@ * * @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 @@ * @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 @@ 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 @@ /** * 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 @@ 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 908fd25..bf79ebf 100644 --- a/src/main/scala/gitbucket/core/util/StringUtil.scala +++ b/src/main/scala/gitbucket/core/util/StringUtil.scala @@ -123,17 +123,22 @@ "(?i)(? b append '^' append c -// case _ => b append c -// } -// b.toString -// } + def getRepositoryViewerUrl(gitRepositoryUrl: String, baseUrl: Option[String]): String = { + def removeUserName(baseUrl: String): String = baseUrl.replaceFirst("(https?://).+@", "$1") + + gitRepositoryUrl match { + case GitBucketUrlPattern(base, user, repository) if baseUrl.map(removeUserName(base).startsWith).getOrElse(false) + => 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 + } + } + } diff --git a/src/test/scala/gitbucket/core/util/StringUtilSpec.scala b/src/test/scala/gitbucket/core/util/StringUtilSpec.scala index f5e850e..ecb1e56 100644 --- a/src/test/scala/gitbucket/core/util/StringUtilSpec.scala +++ b/src/test/scala/gitbucket/core/util/StringUtilSpec.scala @@ -63,4 +63,24 @@ 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") + } + } }