mirror of
https://github.com/gitbucket/gitbucket.git
synced 2025-11-05 13:05:50 +01:00
(refs #343)Retrieve all commit id from Git repository in pre commit hook instead of COMMIT_LOG table
This commit is contained in:
@@ -385,20 +385,6 @@ trait AccountControllerBase extends AccountManagementControllerBase {
|
||||
getWikiRepositoryDir(repository.owner, repository.name),
|
||||
getWikiRepositoryDir(loginUserName, repository.name))
|
||||
|
||||
// insert commit id
|
||||
using(Git.open(getRepositoryDir(loginUserName, repository.name))){ git =>
|
||||
JGitUtil.getRepositoryInfo(loginUserName, repository.name, baseUrl).branchList.foreach { branch =>
|
||||
JGitUtil.getCommitLog(git, branch) match {
|
||||
case Right((commits, _)) => commits.foreach { commit =>
|
||||
if(!existsCommitId(loginUserName, repository.name, commit.id)){
|
||||
insertCommitId(loginUserName, repository.name, commit.id)
|
||||
}
|
||||
}
|
||||
case Left(_) => ???
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Record activity
|
||||
recordForkActivity(repository.owner, repository.name, loginUserName)
|
||||
// redirect to the repository
|
||||
|
||||
@@ -177,12 +177,6 @@ trait PullRequestsControllerBase extends ControllerBase {
|
||||
val (commits, _) = getRequestCompareInfo(owner, name, pullreq.commitIdFrom,
|
||||
pullreq.requestUserName, pullreq.requestRepositoryName, pullreq.commitIdTo)
|
||||
|
||||
commits.flatten.foreach { commit =>
|
||||
if(!existsCommitId(owner, name, commit.id)){
|
||||
insertCommitId(owner, name, commit.id)
|
||||
}
|
||||
}
|
||||
|
||||
// close issue by content of pull request
|
||||
val defaultBranch = getRepository(owner, name, baseUrl).get.repository.defaultBranch
|
||||
if(pullreq.branch == defaultBranch){
|
||||
@@ -438,24 +432,19 @@ trait PullRequestsControllerBase extends ControllerBase {
|
||||
(defaultOwner, value)
|
||||
}
|
||||
|
||||
/**
|
||||
* Extracts all repository names from [[service.RepositoryService.RepositoryTreeNode]] as flat list.
|
||||
*/
|
||||
private def getRepositoryNames(node: RepositoryTreeNode): List[String] =
|
||||
node.owner :: node.children.map { child => getRepositoryNames(child) }.flatten
|
||||
|
||||
/**
|
||||
* Returns the identifier of the root commit (or latest merge commit) of the specified branch.
|
||||
*/
|
||||
private def getForkedCommitId(oldGit: Git, newGit: Git, userName: String, repositoryName: String, branch: String,
|
||||
requestUserName: String, requestRepositoryName: String, requestBranch: String): String =
|
||||
JGitUtil.getCommitLogs(newGit, requestBranch, true){ commit =>
|
||||
existsCommitId(userName, repositoryName, commit.getName) && JGitUtil.getBranchesOfCommit(oldGit, commit.getName).contains(branch)
|
||||
defining(JGitUtil.getAllCommitIds(oldGit)){ existIds =>
|
||||
JGitUtil.getCommitLogs(newGit, requestBranch, true) { commit =>
|
||||
existIds.contains(commit.name) && JGitUtil.getBranchesOfCommit(oldGit, commit.getName).contains(branch)
|
||||
}.head.id
|
||||
}
|
||||
|
||||
private def getRequestCompareInfo(userName: String, repositoryName: String, branch: String,
|
||||
requestUserName: String, requestRepositoryName: String, requestCommitId: String): (Seq[Seq[CommitInfo]], Seq[DiffInfo]) = {
|
||||
|
||||
requestUserName: String, requestRepositoryName: String, requestCommitId: String): (Seq[Seq[CommitInfo]], Seq[DiffInfo]) =
|
||||
using(
|
||||
Git.open(getRepositoryDir(userName, repositoryName)),
|
||||
Git.open(getRepositoryDir(requestUserName, requestRepositoryName))
|
||||
@@ -473,7 +462,6 @@ trait PullRequestsControllerBase extends ControllerBase {
|
||||
|
||||
(commits, diffs)
|
||||
}
|
||||
}
|
||||
|
||||
private def searchPullRequests(userName: Option[String], repository: RepositoryService.RepositoryInfo) =
|
||||
defining(repository.owner, repository.name){ case (owner, repoName) =>
|
||||
|
||||
@@ -13,12 +13,6 @@ object Activities extends Table[Activity]("ACTIVITY") with BasicTemplate {
|
||||
def autoInc = userName ~ repositoryName ~ activityUserName ~ activityType ~ message ~ additionalInfo.? ~ activityDate returning activityId
|
||||
}
|
||||
|
||||
object CommitLog extends Table[(String, String, String)]("COMMIT_LOG") with BasicTemplate {
|
||||
def commitId = column[String]("COMMIT_ID")
|
||||
def * = userName ~ repositoryName ~ commitId
|
||||
def byPrimaryKey(userName: String, repositoryName: String, commitId: String) = byRepository(userName, repositoryName) && (this.commitId is commitId.bind)
|
||||
}
|
||||
|
||||
case class Activity(
|
||||
activityId: Int,
|
||||
userName: String,
|
||||
|
||||
@@ -153,19 +153,6 @@ trait ActivityService {
|
||||
Some(message),
|
||||
currentDate)
|
||||
|
||||
def insertCommitId(userName: String, repositoryName: String, commitId: String) = {
|
||||
CommitLog insert (userName, repositoryName, commitId)
|
||||
}
|
||||
|
||||
def insertAllCommitIds(userName: String, repositoryName: String, commitIds: List[String]) =
|
||||
CommitLog insertAll (commitIds.map(commitId => (userName, repositoryName, commitId)): _*)
|
||||
|
||||
def getAllCommitIds(userName: String, repositoryName: String): List[String] =
|
||||
Query(CommitLog).filter(_.byRepository(userName, repositoryName)).map(_.commitId).list
|
||||
|
||||
def existsCommitId(userName: String, repositoryName: String, commitId: String): Boolean =
|
||||
Query(CommitLog).filter(_.byPrimaryKey(userName, repositoryName, commitId)).firstOption.isDefined
|
||||
|
||||
private def cut(value: String, length: Int): String =
|
||||
if(value.length > length) value.substring(0, length) + "..." else value
|
||||
}
|
||||
|
||||
@@ -52,7 +52,6 @@ trait RepositoryService { self: AccountService =>
|
||||
val issueComments = Query(IssueComments).filter(_.byRepository(oldUserName, oldRepositoryName)).list
|
||||
val issueLabels = Query(IssueLabels ).filter(_.byRepository(oldUserName, oldRepositoryName)).list
|
||||
val collaborators = Query(Collaborators).filter(_.byRepository(oldUserName, oldRepositoryName)).list
|
||||
val commitLog = Query(CommitLog ).filter(_.byRepository(oldUserName, oldRepositoryName)).list
|
||||
val activities = Query(Activities ).filter(_.byRepository(oldUserName, oldRepositoryName)).list
|
||||
|
||||
Repositories.filter { t =>
|
||||
@@ -78,7 +77,6 @@ trait RepositoryService { self: AccountService =>
|
||||
Labels .insertAll(labels .map(_.copy(userName = newUserName, repositoryName = newRepositoryName)) :_*)
|
||||
IssueLabels .insertAll(issueLabels .map(_.copy(userName = newUserName, repositoryName = newRepositoryName)) :_*)
|
||||
Collaborators .insertAll(collaborators .map(_.copy(userName = newUserName, repositoryName = newRepositoryName)) :_*)
|
||||
CommitLog .insertAll(commitLog .map(_.copy(_1 = newUserName, _2 = newRepositoryName)) :_*)
|
||||
Activities .insertAll(activities .map(_.copy(userName = newUserName, repositoryName = newRepositoryName)) :_*)
|
||||
|
||||
// Update activity messages
|
||||
@@ -102,7 +100,6 @@ trait RepositoryService { self: AccountService =>
|
||||
|
||||
def deleteRepository(userName: String, repositoryName: String): Unit = {
|
||||
Activities .filter(_.byRepository(userName, repositoryName)).delete
|
||||
CommitLog .filter(_.byRepository(userName, repositoryName)).delete
|
||||
Collaborators .filter(_.byRepository(userName, repositoryName)).delete
|
||||
IssueLabels .filter(_.byRepository(userName, repositoryName)).delete
|
||||
Labels .filter(_.byRepository(userName, repositoryName)).delete
|
||||
|
||||
@@ -81,7 +81,9 @@ class GitBucketReceivePackFactory extends ReceivePackFactory[HttpServletRequest]
|
||||
logger.debug("repository:" + owner + "/" + repository)
|
||||
|
||||
if(!repository.endsWith(".wiki")){
|
||||
receivePack.setPostReceiveHook(new CommitLogHook(owner, repository, pusher, baseUrl(request)))
|
||||
val hook = new CommitLogHook(owner, repository, pusher, baseUrl(request))
|
||||
receivePack.setPreReceiveHook(hook)
|
||||
receivePack.setPostReceiveHook(hook)
|
||||
}
|
||||
receivePack
|
||||
}
|
||||
@@ -90,10 +92,24 @@ class GitBucketReceivePackFactory extends ReceivePackFactory[HttpServletRequest]
|
||||
|
||||
import scala.collection.JavaConverters._
|
||||
|
||||
class CommitLogHook(owner: String, repository: String, pusher: String, baseUrl: String) extends PostReceiveHook
|
||||
class CommitLogHook(owner: String, repository: String, pusher: String, baseUrl: String) extends PostReceiveHook with PreReceiveHook
|
||||
with RepositoryService with AccountService with IssuesService with ActivityService with PullRequestService with WebHookService {
|
||||
|
||||
private val logger = LoggerFactory.getLogger(classOf[CommitLogHook])
|
||||
private var existIds: Seq[String] = Nil
|
||||
|
||||
def onPreReceive(receivePack: ReceivePack, commands: java.util.Collection[ReceiveCommand]): Unit = {
|
||||
try {
|
||||
using(Git.open(Directory.getRepositoryDir(owner, repository))) { git =>
|
||||
existIds = JGitUtil.getAllCommitIds(git)
|
||||
}
|
||||
} catch {
|
||||
case ex: Exception => {
|
||||
logger.error(ex.toString, ex)
|
||||
throw ex
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
def onPostReceive(receivePack: ReceivePack, commands: java.util.Collection[ReceiveCommand]): Unit = {
|
||||
try {
|
||||
@@ -117,29 +133,14 @@ class CommitLogHook(owner: String, repository: String, pusher: String, baseUrl:
|
||||
countIssue(IssueSearchCondition(state = "closed"), Map.empty, false, owner -> repository)
|
||||
|
||||
// Extract new commit and apply issue comment
|
||||
val newCommits = if(commits.size > 1000){
|
||||
val existIds = getAllCommitIds(owner, repository)
|
||||
commits.flatMap { commit =>
|
||||
if(!existIds.contains(commit.id)){
|
||||
if(issueCount > 0) {
|
||||
val newCommits = commits.flatMap { commit =>
|
||||
if (!existIds.contains(commit.id)) {
|
||||
if (issueCount > 0) {
|
||||
createIssueComment(commit)
|
||||
}
|
||||
Some(commit)
|
||||
} else None
|
||||
}
|
||||
} else {
|
||||
commits.flatMap { commit =>
|
||||
if(!existsCommitId(owner, repository, commit.id)){
|
||||
if(issueCount > 0) {
|
||||
createIssueComment(commit)
|
||||
}
|
||||
Some(commit)
|
||||
} else None
|
||||
}
|
||||
}
|
||||
|
||||
// batch insert all new commit id
|
||||
insertAllCommitIds(owner, repository, newCommits.map(_.id))
|
||||
|
||||
// record activity
|
||||
if(refName(1) == "heads"){
|
||||
|
||||
@@ -570,4 +570,16 @@ object JGitUtil {
|
||||
case e: MissingObjectException => None
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns all commit id in the specified repository.
|
||||
*/
|
||||
def getAllCommitIds(git: Git): Seq[String] = {
|
||||
val existIds = new scala.collection.mutable.ListBuffer[String]()
|
||||
val i = git.log.all.call.iterator
|
||||
while(i.hasNext){
|
||||
existIds += i.next.name
|
||||
}
|
||||
existIds.toSeq
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user