Show the number of commits of selected branch

This commit is contained in:
Naoki Takezoe
2016-12-17 20:22:30 +09:00
parent 87fb136b85
commit 4f4afc5686
6 changed files with 35 additions and 35 deletions

View File

@@ -238,7 +238,7 @@ trait RepositorySettingsControllerBase extends ControllerBase {
val dummyWebHookInfo = WebHook(repository.owner, repository.name, url, ctype, token) val dummyWebHookInfo = WebHook(repository.owner, repository.name, url, ctype, token)
val dummyPayload = { val dummyPayload = {
val ownerAccount = getAccountByUserName(repository.owner).get 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)) .add(git.getRepository.resolve(repository.repository.defaultBranch))
.setMaxCount(4) .setMaxCount(4)
.call.iterator.asScala.map(new CommitInfo(_)).toList .call.iterator.asScala.map(new CommitInfo(_)).toList

View File

@@ -546,10 +546,10 @@ trait RepositoryViewerControllerBase extends ControllerBase {
* @return HTML of the file list * @return HTML of the file list
*/ */
private def fileList(repository: RepositoryService.RepositoryInfo, revstr: String = "", path: String = ".") = { private def fileList(repository: RepositoryService.RepositoryInfo, revstr: String = "", path: String = ".") = {
if(repository.commitCount == 0){ using(Git.open(getRepositoryDir(repository.owner, repository.name))){ git =>
html.guide(repository, hasDeveloperRole(repository.owner, repository.name, context.loginAccount)) if(JGitUtil.isEmpty(git)){
} else { html.guide(repository, hasDeveloperRole(repository.owner, repository.name, context.loginAccount))
using(Git.open(getRepositoryDir(repository.owner, repository.name))){ git => } else {
// get specified commit // get specified commit
JGitUtil.getDefaultBranch(git, repository, revstr).map { case (objectId, revision) => JGitUtil.getDefaultBranch(git, repository, revstr).map { case (objectId, revision) =>
defining(JGitUtil.getRevCommitFromId(git, objectId)) { revCommit => defining(JGitUtil.getRevCommitFromId(git, objectId)) { revCommit =>
@@ -569,9 +569,14 @@ trait RepositoryViewerControllerBase extends ControllerBase {
html.files(revision, repository, html.files(revision, repository,
if(path == ".") Nil else path.split("/").toList, // current path if(path == ".") Nil else path.split("/").toList, // current path
new JGitUtil.CommitInfo(lastModifiedCommit), // last modified commit 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), getPullRequestFromBranch(repository.owner, repository.name, revstr, repository.repository.defaultBranch),
flash.get("info"), flash.get("error")) flash.get("info"),
flash.get("error")
)
} }
} getOrElse NotFound() } getOrElse NotFound()
} }

View File

@@ -421,26 +421,20 @@ trait RepositoryService { self: AccountService =>
object RepositoryService { object RepositoryService {
case class RepositoryInfo(owner: String, name: String, repository: Repository, 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]) { branchList: Seq[String], tags: Seq[JGitUtil.TagInfo], managers: Seq[String]) {
/** /**
* Creates instance with issue count and pull request count. * 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]) = def this(repo: JGitUtil.RepositoryInfo, model: Repository, issueCount: Int, pullCount: Int, forkedCount: Int, managers: Seq[String]) =
this( this(repo.owner, repo.name, model, issueCount, pullCount, forkedCount, repo.branchList, repo.tags, managers)
repo.owner, repo.name, model,
issueCount, pullCount, repo.commitCount, forkedCount,
repo.branchList, repo.tags, managers)
/** /**
* Creates instance without issue count and pull request count. * Creates instance without issue count and pull request count.
*/ */
def this(repo: JGitUtil.RepositoryInfo, model: Repository, forkedCount: Int, managers: Seq[String]) = def this(repo: JGitUtil.RepositoryInfo, model: Repository, forkedCount: Int, managers: Seq[String]) =
this( this(repo.owner, repo.name, model, 0, 0, forkedCount, repo.branchList, repo.tags, managers)
repo.owner, repo.name, model,
0, 0, repo.commitCount, forkedCount,
repo.branchList, repo.tags, managers)
def httpUrl(implicit context: Context): String = RepositoryService.httpUrl(owner, name) def httpUrl(implicit context: Context): String = RepositoryService.httpUrl(owner, name)
def sshUrl(implicit context: Context): Option[String] = RepositoryService.sshUrl(owner, name) def sshUrl(implicit context: Context): Option[String] = RepositoryService.sshUrl(owner, name)
@@ -454,7 +448,6 @@ object RepositoryService {
(id, path.substring(id.length).stripPrefix("/")) (id, path.substring(id.length).stripPrefix("/"))
} }
} }
def httpUrl(owner: String, name: String)(implicit context: Context): String = s"${context.baseUrl}/git/${owner}/${name}.git" def httpUrl(owner: String, name: String)(implicit context: Context): String = s"${context.baseUrl}/git/${owner}/${name}.git"

View File

@@ -32,14 +32,11 @@ object JGitUtil {
* *
* @param owner the user name of the repository owner * @param owner the user name of the repository owner
* @param name the repository name * @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 branchList the list of branch names
* @param tags the list of tags * @param tags the list of tags
*/ */
case class RepositoryInfo(owner: String, name: String, commitCount: Int, branchList: List[String], tags: List[TagInfo]){ case class RepositoryInfo(owner: String, name: String, branchList: List[String], tags: List[TagInfo]){
def this(owner: String, name: String) = { def this(owner: String, name: String) = this(owner, name, Nil, Nil)
this(owner, name, 0, Nil, Nil)
}
} }
/** /**
@@ -170,19 +167,25 @@ object JGitUtil {
revCommit 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. * Returns the repository information. It contains branch names and tag names.
*/ */
def getRepositoryInfo(owner: String, repository: String): RepositoryInfo = { def getRepositoryInfo(owner: String, repository: String): RepositoryInfo = {
using(Git.open(getRepositoryDir(owner, repository))){ git => using(Git.open(getRepositoryDir(owner, repository))){ git =>
try { try {
// get commit count RepositoryInfo(owner, repository,
val commitCount = git.log.all.call.iterator.asScala.map(_ => 1).take(10001).sum
RepositoryInfo(
owner, repository,
// commit count
commitCount,
// branches // branches
git.branchList.call.asScala.map { ref => git.branchList.call.asScala.map { ref =>
ref.getName.stripPrefix("refs/heads/") ref.getName.stripPrefix("refs/heads/")
@@ -195,9 +198,7 @@ object JGitUtil {
) )
} catch { } catch {
// not initialized // not initialized
case e: NoHeadException => RepositoryInfo( case e: NoHeadException => RepositoryInfo(owner, repository, Nil, Nil)
owner, repository, 0, Nil, Nil)
} }
} }
} }
@@ -212,7 +213,7 @@ object JGitUtil {
*/ */
def getFileList(git: Git, revision: String, path: String = "."): List[FileInfo] = { def getFileList(git: Git, revision: String, path: String = "."): List[FileInfo] = {
using(new RevWalk(git.getRepository)){ revWalk => using(new RevWalk(git.getRepository)){ revWalk =>
val objectId = git.getRepository.resolve(revision) val objectId = git.getRepository.resolve(revision)
if(objectId == null) return Nil if(objectId == null) return Nil
val revCommit = revWalk.parseCommit(objectId) val revCommit = revWalk.parseCommit(objectId)

View File

@@ -23,7 +23,7 @@
<div class="sidebar"> <div class="sidebar">
<ul class="sidebar-menu"> <ul class="sidebar-menu">
@menuitem("", "files", "Files", "code") @menuitem("", "files", "Files", "code")
@if(repository.commitCount != 0) { @if(repository.branchList.nonEmpty) {
@menuitem("/branches", "branches", "Branches", "git-branch", repository.branchList.length) @menuitem("/branches", "branches", "Branches", "git-branch", repository.branchList.length)
@menuitem("/tags", "tags", "Tags", "tag", repository.tags.length) @menuitem("/tags", "tags", "Tags", "tag", repository.tags.length)
} }

View File

@@ -2,6 +2,7 @@
repository: gitbucket.core.service.RepositoryService.RepositoryInfo, repository: gitbucket.core.service.RepositoryService.RepositoryInfo,
pathList: List[String], pathList: List[String],
latestCommit: gitbucket.core.util.JGitUtil.CommitInfo, latestCommit: gitbucket.core.util.JGitUtil.CommitInfo,
commitCount: Int,
files: List[gitbucket.core.util.JGitUtil.FileInfo], files: List[gitbucket.core.util.JGitUtil.FileInfo],
readme: Option[(List[String], String)], readme: Option[(List[String], String)],
hasWritePermission: Boolean, hasWritePermission: Boolean,
@@ -25,7 +26,7 @@
<div class="pull-right"> <div class="pull-right">
<div class="btn-group"> <div class="btn-group">
<a href="@helpers.url(repository)/find/@helpers.encodeRefName(branch)" class="btn btn-sm btn-default" data-hotkey="t"><i class="octicon octicon-search"></i></a> <a href="@helpers.url(repository)/find/@helpers.encodeRefName(branch)" class="btn btn-sm btn-default" data-hotkey="t"><i class="octicon octicon-search"></i></a>
<a href="@helpers.url(repository)/commits/@helpers.encodeRefName(branch)/@pathList.mkString("/")" class="btn btn-sm btn-default"><i class="octicon octicon-history"></i> @if(repository.commitCount > 10000){10000+} else {@repository.commitCount} @helpers.plural(repository.commitCount, "commit")</a> <a href="@helpers.url(repository)/commits/@helpers.encodeRefName(branch)/@pathList.mkString("/")" class="btn btn-sm btn-default"><i class="octicon octicon-history"></i> @if(commitCount > 10000){10000+} else {@commitCount} @helpers.plural(commitCount, "commit")</a>
</div> </div>
</div> </div>
@if(pathList.isEmpty){ @if(pathList.isEmpty){