diff --git a/src/main/scala/gitbucket/core/controller/RepositorySettingsController.scala b/src/main/scala/gitbucket/core/controller/RepositorySettingsController.scala index be35a20..b0b30f5 100644 --- a/src/main/scala/gitbucket/core/controller/RepositorySettingsController.scala +++ b/src/main/scala/gitbucket/core/controller/RepositorySettingsController.scala @@ -238,7 +238,7 @@ val dummyWebHookInfo = WebHook(repository.owner, repository.name, url, ctype, token) val dummyPayload = { val ownerAccount = getAccountByUserName(repository.owner).get - val commits = if(repository.commitCount == 0) List.empty else git.log + val commits = if(JGitUtil.isEmpty(git)) List.empty else git.log .add(git.getRepository.resolve(repository.repository.defaultBranch)) .setMaxCount(4) .call.iterator.asScala.map(new CommitInfo(_)).toList diff --git a/src/main/scala/gitbucket/core/controller/RepositoryViewerController.scala b/src/main/scala/gitbucket/core/controller/RepositoryViewerController.scala index 701a66c..8bcd5ad 100644 --- a/src/main/scala/gitbucket/core/controller/RepositoryViewerController.scala +++ b/src/main/scala/gitbucket/core/controller/RepositoryViewerController.scala @@ -546,10 +546,10 @@ * @return HTML of the file list */ private def fileList(repository: RepositoryService.RepositoryInfo, revstr: String = "", path: String = ".") = { - if(repository.commitCount == 0){ - html.guide(repository, hasDeveloperRole(repository.owner, repository.name, context.loginAccount)) - } else { - using(Git.open(getRepositoryDir(repository.owner, repository.name))){ git => + using(Git.open(getRepositoryDir(repository.owner, repository.name))){ git => + if(JGitUtil.isEmpty(git)){ + html.guide(repository, hasDeveloperRole(repository.owner, repository.name, context.loginAccount)) + } else { // get specified commit JGitUtil.getDefaultBranch(git, repository, revstr).map { case (objectId, revision) => defining(JGitUtil.getRevCommitFromId(git, objectId)) { revCommit => @@ -569,9 +569,14 @@ html.files(revision, repository, if(path == ".") Nil else path.split("/").toList, // current path new JGitUtil.CommitInfo(lastModifiedCommit), // last modified commit - files, readme, hasDeveloperRole(repository.owner, repository.name, context.loginAccount), + JGitUtil.getCommitCount(repository.owner, repository.name, revision), + files, + readme, + hasDeveloperRole(repository.owner, repository.name, context.loginAccount), getPullRequestFromBranch(repository.owner, repository.name, revstr, repository.repository.defaultBranch), - flash.get("info"), flash.get("error")) + flash.get("info"), + flash.get("error") + ) } } getOrElse NotFound() } diff --git a/src/main/scala/gitbucket/core/service/RepositoryService.scala b/src/main/scala/gitbucket/core/service/RepositoryService.scala index 67ebf72..30b4d5c 100644 --- a/src/main/scala/gitbucket/core/service/RepositoryService.scala +++ b/src/main/scala/gitbucket/core/service/RepositoryService.scala @@ -421,26 +421,20 @@ object RepositoryService { case class RepositoryInfo(owner: String, name: String, repository: Repository, - issueCount: Int, pullCount: Int, commitCount: Int, forkedCount: Int, + issueCount: Int, pullCount: Int, forkedCount: Int, branchList: Seq[String], tags: Seq[JGitUtil.TagInfo], managers: Seq[String]) { /** * Creates instance with issue count and pull request count. */ def this(repo: JGitUtil.RepositoryInfo, model: Repository, issueCount: Int, pullCount: Int, forkedCount: Int, managers: Seq[String]) = - this( - repo.owner, repo.name, model, - issueCount, pullCount, repo.commitCount, forkedCount, - repo.branchList, repo.tags, managers) + this(repo.owner, repo.name, model, issueCount, pullCount, forkedCount, repo.branchList, repo.tags, managers) /** * Creates instance without issue count and pull request count. */ def this(repo: JGitUtil.RepositoryInfo, model: Repository, forkedCount: Int, managers: Seq[String]) = - this( - repo.owner, repo.name, model, - 0, 0, repo.commitCount, forkedCount, - repo.branchList, repo.tags, managers) + this(repo.owner, repo.name, model, 0, 0, forkedCount, repo.branchList, repo.tags, managers) def httpUrl(implicit context: Context): String = RepositoryService.httpUrl(owner, name) def sshUrl(implicit context: Context): Option[String] = RepositoryService.sshUrl(owner, name) @@ -454,7 +448,6 @@ (id, path.substring(id.length).stripPrefix("/")) } - } def httpUrl(owner: String, name: String)(implicit context: Context): String = s"${context.baseUrl}/git/${owner}/${name}.git" diff --git a/src/main/scala/gitbucket/core/util/JGitUtil.scala b/src/main/scala/gitbucket/core/util/JGitUtil.scala index 7d240d6..94063ac 100644 --- a/src/main/scala/gitbucket/core/util/JGitUtil.scala +++ b/src/main/scala/gitbucket/core/util/JGitUtil.scala @@ -32,14 +32,11 @@ * * @param owner the user name of the repository owner * @param name the repository name - * @param commitCount the commit count. If the repository has over 1000 commits then this property is 1001. * @param branchList the list of branch names * @param tags the list of tags */ - case class RepositoryInfo(owner: String, name: String, commitCount: Int, branchList: List[String], tags: List[TagInfo]){ - def this(owner: String, name: String) = { - this(owner, name, 0, Nil, Nil) - } + case class RepositoryInfo(owner: String, name: String, branchList: List[String], tags: List[TagInfo]){ + def this(owner: String, name: String) = this(owner, name, Nil, Nil) } /** @@ -169,6 +166,18 @@ revWalk.dispose revCommit } + + /** + * Returns the number of commits in the specified branch or commit. + * If the specified branch has over 10000 commits, this method returns 100001. + */ + def getCommitCount(owner: String, repository: String, branch: String): Int = { + using(Git.open(getRepositoryDir(owner, repository))){ git => + val commitId = git.getRepository.resolve(branch) + val commitCount = git.log.add(commitId).call.iterator.asScala.take(10001).size + commitCount + } + } /** * Returns the repository information. It contains branch names and tag names. @@ -176,13 +185,7 @@ def getRepositoryInfo(owner: String, repository: String): RepositoryInfo = { using(Git.open(getRepositoryDir(owner, repository))){ git => try { - // get commit count - val commitCount = git.log.all.call.iterator.asScala.map(_ => 1).take(10001).sum - - RepositoryInfo( - owner, repository, - // commit count - commitCount, + RepositoryInfo(owner, repository, // branches git.branchList.call.asScala.map { ref => ref.getName.stripPrefix("refs/heads/") @@ -195,9 +198,7 @@ ) } catch { // not initialized - case e: NoHeadException => RepositoryInfo( - owner, repository, 0, Nil, Nil) - + case e: NoHeadException => RepositoryInfo(owner, repository, Nil, Nil) } } } @@ -212,7 +213,7 @@ */ def getFileList(git: Git, revision: String, path: String = "."): List[FileInfo] = { using(new RevWalk(git.getRepository)){ revWalk => - val objectId = git.getRepository.resolve(revision) + val objectId = git.getRepository.resolve(revision) if(objectId == null) return Nil val revCommit = revWalk.parseCommit(objectId) diff --git a/src/main/twirl/gitbucket/core/menu.scala.html b/src/main/twirl/gitbucket/core/menu.scala.html index c4d3ebb..e75d7f8 100644 --- a/src/main/twirl/gitbucket/core/menu.scala.html +++ b/src/main/twirl/gitbucket/core/menu.scala.html @@ -23,7 +23,7 @@