mirror of
https://github.com/gitbucket/gitbucket.git
synced 2025-11-03 12:05:59 +01:00
Improve git push performance (#3284)
This commit is contained in:
@@ -245,7 +245,7 @@ class CommitLogHook(owner: String, repository: String, pusher: String, baseUrl:
|
|||||||
with RequestCache {
|
with RequestCache {
|
||||||
|
|
||||||
private val logger = LoggerFactory.getLogger(classOf[CommitLogHook])
|
private val logger = LoggerFactory.getLogger(classOf[CommitLogHook])
|
||||||
private var existIds: Seq[String] = Nil
|
private var newCommitIds: Seq[String] = Nil
|
||||||
|
|
||||||
def onPreReceive(receivePack: ReceivePack, commands: java.util.Collection[ReceiveCommand]): Unit = {
|
def onPreReceive(receivePack: ReceivePack, commands: java.util.Collection[ReceiveCommand]): Unit = {
|
||||||
Database() withTransaction { implicit session =>
|
Database() withTransaction { implicit session =>
|
||||||
@@ -260,13 +260,43 @@ class CommitLogHook(owner: String, repository: String, pusher: String, baseUrl:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
Using.resource(Git.open(Directory.getRepositoryDir(owner, repository))) { git =>
|
Using.resource(Git.open(Directory.getRepositoryDir(owner, repository))) { git =>
|
||||||
existIds = JGitUtil.getAllCommitIds(git)
|
commands.asScala.foreach { command =>
|
||||||
|
val commits = getCommits(git, command)
|
||||||
|
if (commits.size < 100) {
|
||||||
|
newCommitIds = commits
|
||||||
|
.takeWhile { commit =>
|
||||||
|
!existCommit(git, commit)
|
||||||
|
}
|
||||||
|
.map(_.id)
|
||||||
|
} else {
|
||||||
|
val allCommits = JGitUtil.getAllCommitIds(git)
|
||||||
|
newCommitIds = commits.collect {
|
||||||
|
case commit if !allCommits.contains(commit.id) =>
|
||||||
|
commit.id
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} catch {
|
} catch {
|
||||||
case ex: Exception => {
|
case ex: Exception =>
|
||||||
logger.error(ex.toString, ex)
|
logger.error(ex.toString, ex)
|
||||||
throw ex
|
throw ex
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private def existCommit(git: Git, commit: CommitInfo): Boolean = {
|
||||||
|
JGitUtil.getBranchesOfCommit(git, commit.id).nonEmpty
|
||||||
|
}
|
||||||
|
|
||||||
|
private def getCommits(git: Git, command: ReceiveCommand): Seq[CommitInfo] = {
|
||||||
|
val refName = command.getRefName.split("/")
|
||||||
|
if (refName(1) == "tags") {
|
||||||
|
Nil
|
||||||
|
} else {
|
||||||
|
command.getType match {
|
||||||
|
case ReceiveCommand.Type.DELETE => Nil
|
||||||
|
case _ => JGitUtil.getCommitLog(git, command.getOldId.name, command.getNewId.name)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -279,20 +309,12 @@ class CommitLogHook(owner: String, repository: String, pusher: String, baseUrl:
|
|||||||
Using.resource(Git.open(Directory.getRepositoryDir(owner, repository))) { git =>
|
Using.resource(Git.open(Directory.getRepositoryDir(owner, repository))) { git =>
|
||||||
JGitUtil.removeCache(git)
|
JGitUtil.removeCache(git)
|
||||||
|
|
||||||
val pushedIds = scala.collection.mutable.Set[String]()
|
|
||||||
commands.asScala.foreach { command =>
|
commands.asScala.foreach { command =>
|
||||||
logger.debug(s"commandType: ${command.getType}, refName: ${command.getRefName}")
|
logger.debug(s"commandType: ${command.getType}, refName: ${command.getRefName}")
|
||||||
implicit val apiContext: Context = api.JsonFormat.Context(baseUrl, sshUrl)
|
implicit val apiContext: Context = api.JsonFormat.Context(baseUrl, sshUrl)
|
||||||
val refName = command.getRefName.split("/")
|
val refName = command.getRefName.split("/")
|
||||||
val branchName = refName.drop(2).mkString("/")
|
val branchName = refName.drop(2).mkString("/")
|
||||||
val commits = if (refName(1) == "tags") {
|
val commits = getCommits(git, command)
|
||||||
Nil
|
|
||||||
} else {
|
|
||||||
command.getType match {
|
|
||||||
case ReceiveCommand.Type.DELETE => Nil
|
|
||||||
case _ => JGitUtil.getCommitLog(git, command.getOldId.name, command.getNewId.name)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
val repositoryInfo = getRepository(owner, repository).get
|
val repositoryInfo = getRepository(owner, repository).get
|
||||||
|
|
||||||
@@ -312,8 +334,9 @@ class CommitLogHook(owner: String, repository: String, pusher: String, baseUrl:
|
|||||||
|
|
||||||
// Extract new commit and apply issue comment
|
// Extract new commit and apply issue comment
|
||||||
val defaultBranch = repositoryInfo.repository.defaultBranch
|
val defaultBranch = repositoryInfo.repository.defaultBranch
|
||||||
val newCommits = commits.flatMap { commit =>
|
val pushedIds = scala.collection.mutable.Set[String]()
|
||||||
if (!existIds.contains(commit.id) && !pushedIds.contains(commit.id)) {
|
val newCommits = commits.collect {
|
||||||
|
case commit if newCommitIds.contains(commit.id) && !pushedIds.contains(commit.id) =>
|
||||||
if (issueCount > 0) {
|
if (issueCount > 0) {
|
||||||
pushedIds.add(commit.id)
|
pushedIds.add(commit.id)
|
||||||
createIssueComment(owner, repository, commit)
|
createIssueComment(owner, repository, commit)
|
||||||
@@ -333,9 +356,8 @@ class CommitLogHook(owner: String, repository: String, pusher: String, baseUrl:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Some(commit)
|
commit
|
||||||
} else None
|
}.toList
|
||||||
}
|
|
||||||
|
|
||||||
// set PR as merged
|
// set PR as merged
|
||||||
val pulls = getPullRequestsByBranch(owner, repository, branchName, Some(false))
|
val pulls = getPullRequestsByBranch(owner, repository, branchName, Some(false))
|
||||||
@@ -431,10 +453,9 @@ class CommitLogHook(owner: String, repository: String, pusher: String, baseUrl:
|
|||||||
// update repository last modified time.
|
// update repository last modified time.
|
||||||
updateLastActivityDate(owner, repository)
|
updateLastActivityDate(owner, repository)
|
||||||
} catch {
|
} catch {
|
||||||
case ex: Exception => {
|
case ex: Exception =>
|
||||||
logger.error(ex.toString, ex)
|
logger.error(ex.toString, ex)
|
||||||
throw ex
|
throw ex
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -506,10 +527,9 @@ class WikiCommitHook(owner: String, repository: String, pusher: String, baseUrl:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch {
|
} catch {
|
||||||
case ex: Exception => {
|
case ex: Exception =>
|
||||||
logger.error(ex.toString, ex)
|
logger.error(ex.toString, ex)
|
||||||
throw ex
|
throw ex
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user