diff --git a/src/main/scala/gitbucket/core/controller/ApiController.scala b/src/main/scala/gitbucket/core/controller/ApiController.scala index 959363f..4a9b218 100644 --- a/src/main/scala/gitbucket/core/controller/ApiController.scala +++ b/src/main/scala/gitbucket/core/controller/ApiController.scala @@ -105,22 +105,11 @@ }) }) - // copy code - private def splitPath(repository: RepositoryService.RepositoryInfo, path: String): (String, String) = { - val id = repository.branchList.collectFirst { - case branch if(path == branch || path.startsWith(branch + "/")) => branch - } orElse repository.tags.collectFirst { - case tag if(path == tag.name || path.startsWith(tag.name + "/")) => tag.name - } getOrElse path.split("/")(0) - - (id, path.substring(id.length).stripPrefix("/")) - } - /* * https://developer.github.com/v3/repos/contents/#get-contents */ get("/api/v3/repos/:owner/:repo/contents/*")(referrersOnly { repository => - val (id, path) = splitPath(repository, multiParams("splat").head) + val (id, path) = repository.splitPath(multiParams("splat").head) val refStr = params("ref") using(Git.open(getRepositoryDir(params("owner"), params("repo")))){ git => if (path.isEmpty) { diff --git a/src/main/scala/gitbucket/core/controller/RepositoryViewerController.scala b/src/main/scala/gitbucket/core/controller/RepositoryViewerController.scala index 2563558..455a5c1 100644 --- a/src/main/scala/gitbucket/core/controller/RepositoryViewerController.scala +++ b/src/main/scala/gitbucket/core/controller/RepositoryViewerController.scala @@ -125,7 +125,7 @@ * Displays the file list of the specified path and branch. */ get("/:owner/:repository/tree/*")(referrersOnly { repository => - val (id, path) = splitPath(repository, multiParams("splat").head) + val (id, path) = repository.splitPath(multiParams("splat").head) if(path.isEmpty){ fileList(repository, id) } else { @@ -137,7 +137,7 @@ * Displays the commit list of the specified resource. */ get("/:owner/:repository/commits/*")(referrersOnly { repository => - val (branchName, path) = splitPath(repository, multiParams("splat").head) + val (branchName, path) = repository.splitPath(multiParams("splat").head) val page = params.get("page").flatMap(_.toIntOpt).getOrElse(1) using(Git.open(getRepositoryDir(repository.owner, repository.name))){ git => @@ -153,7 +153,7 @@ }) get("/:owner/:repository/new/*")(collaboratorsOnly { repository => - val (branch, path) = splitPath(repository, multiParams("splat").head) + val (branch, path) = repository.splitPath(multiParams("splat").head) val protectedBranch = getProtectedBranchInfo(repository.owner, repository.name, branch).needStatusCheck(context.loginAccount.get.userName) html.editor(branch, repository, if(path.length == 0) Nil else path.split("/").toList, None, JGitUtil.ContentInfo("text", None, Some("UTF-8")), @@ -161,7 +161,7 @@ }) get("/:owner/:repository/edit/*")(collaboratorsOnly { repository => - val (branch, path) = splitPath(repository, multiParams("splat").head) + val (branch, path) = repository.splitPath(multiParams("splat").head) val protectedBranch = getProtectedBranchInfo(repository.owner, repository.name, branch).needStatusCheck(context.loginAccount.get.userName) using(Git.open(getRepositoryDir(repository.owner, repository.name))){ git => @@ -177,7 +177,7 @@ }) get("/:owner/:repository/remove/*")(collaboratorsOnly { repository => - val (branch, path) = splitPath(repository, multiParams("splat").head) + val (branch, path) = repository.splitPath(multiParams("splat").head) using(Git.open(getRepositoryDir(repository.owner, repository.name))){ git => val revCommit = JGitUtil.getRevCommitFromId(git, git.getRepository.resolve(branch)) @@ -235,7 +235,7 @@ }) get("/:owner/:repository/raw/*")(referrersOnly { repository => - val (id, path) = splitPath(repository, multiParams("splat").head) + val (id, path) = repository.splitPath(multiParams("splat").head) using(Git.open(getRepositoryDir(repository.owner, repository.name))){ git => val revCommit = JGitUtil.getRevCommitFromId(git, git.getRepository.resolve(id)) getPathObjectId(git, path, revCommit).flatMap { objectId => @@ -253,7 +253,7 @@ * Displays the file content of the specified branch or commit. */ val blobRoute = get("/:owner/:repository/blob/*")(referrersOnly { repository => - val (id, path) = splitPath(repository, multiParams("splat").head) + val (id, path) = repository.splitPath(multiParams("splat").head) val raw = params.get("raw").getOrElse("false").toBoolean using(Git.open(getRepositoryDir(repository.owner, repository.name))){ git => val revCommit = JGitUtil.getRevCommitFromId(git, git.getRepository.resolve(id)) @@ -285,7 +285,7 @@ * Blame data. */ ajaxGet("/:owner/:repository/get-blame/*")(referrersOnly { repository => - val (id, path) = splitPath(repository, multiParams("splat").head) + val (id, path) = repository.splitPath(multiParams("splat").head) contentType = formats("json") using(Git.open(getRepositoryDir(repository.owner, repository.name))){ git => val last = git.log.add(git.getRepository.resolve(id)).addPath(path).setMaxCount(1).call.iterator.next.name @@ -526,17 +526,6 @@ } }) - private def splitPath(repository: RepositoryService.RepositoryInfo, path: String): (String, String) = { - val id = repository.branchList.collectFirst { - case branch if(path == branch || path.startsWith(branch + "/")) => branch - } orElse repository.tags.collectFirst { - case tag if(path == tag.name || path.startsWith(tag.name + "/")) => tag.name - } getOrElse path.split("/")(0) - - (id, path.substring(id.length).stripPrefix("/")) - } - - private val readmeFiles = PluginRegistry().renderableExtensions.map { extension => s"readme.${extension}" } ++ Seq("readme.txt", "readme") diff --git a/src/main/scala/gitbucket/core/service/RepositoryService.scala b/src/main/scala/gitbucket/core/service/RepositoryService.scala index 7167b37..959e735 100644 --- a/src/main/scala/gitbucket/core/service/RepositoryService.scala +++ b/src/main/scala/gitbucket/core/service/RepositoryService.scala @@ -420,6 +420,17 @@ def httpUrl(implicit context: Context): String = RepositoryService.httpUrl(owner, name) def sshUrl(implicit context: Context): Option[String] = RepositoryService.sshUrl(owner, name) + + def splitPath(path: String): (String, String) = { + val id = branchList.collectFirst { + case branch if(path == branch || path.startsWith(branch + "/")) => branch + } orElse tags.collectFirst { + case tag if(path == tag.name || path.startsWith(tag.name + "/")) => tag.name + } getOrElse path.split("/")(0) + + (id, path.substring(id.length).stripPrefix("/")) + } + } def httpUrl(owner: String, name: String)(implicit context: Context): String = s"${context.baseUrl}/git/${owner}/${name}.git"