diff --git a/src/main/scala/app/AccountController.scala b/src/main/scala/app/AccountController.scala index d0a0a89..0a2b62a 100644 --- a/src/main/scala/app/AccountController.scala +++ b/src/main/scala/app/AccountController.scala @@ -385,20 +385,6 @@ 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 diff --git a/src/main/scala/app/PullRequestsController.scala b/src/main/scala/app/PullRequestsController.scala index 47c0614..384ad34 100644 --- a/src/main/scala/app/PullRequestsController.scala +++ b/src/main/scala/app/PullRequestsController.scala @@ -177,12 +177,6 @@ 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){ @@ -439,23 +433,18 @@ } /** - * 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) - }.head.id + 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 @@ (commits, diffs) } - } private def searchPullRequests(userName: Option[String], repository: RepositoryService.RepositoryInfo) = defining(repository.owner, repository.name){ case (owner, repoName) => diff --git a/src/main/scala/model/Activity.scala b/src/main/scala/model/Activity.scala index 9c2ae02..b9d5bb5 100644 --- a/src/main/scala/model/Activity.scala +++ b/src/main/scala/model/Activity.scala @@ -13,12 +13,6 @@ 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, diff --git a/src/main/scala/service/ActivityService.scala b/src/main/scala/service/ActivityService.scala index fce0b09..64fbb4e 100644 --- a/src/main/scala/service/ActivityService.scala +++ b/src/main/scala/service/ActivityService.scala @@ -153,19 +153,6 @@ 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 = + private def cut(value: String, length: Int): String = if(value.length > length) value.substring(0, length) + "..." else value } diff --git a/src/main/scala/service/RepositoryService.scala b/src/main/scala/service/RepositoryService.scala index 38e7606..774017e 100644 --- a/src/main/scala/service/RepositoryService.scala +++ b/src/main/scala/service/RepositoryService.scala @@ -52,7 +52,6 @@ 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 @@ 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 @@ 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 diff --git a/src/main/scala/servlet/GitRepositoryServlet.scala b/src/main/scala/servlet/GitRepositoryServlet.scala index d02cb2f..389c1af 100644 --- a/src/main/scala/servlet/GitRepositoryServlet.scala +++ b/src/main/scala/servlet/GitRepositoryServlet.scala @@ -81,7 +81,9 @@ 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,11 +92,25 @@ 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 { using(Git.open(Directory.getRepositoryDir(owner, repository))) { git => @@ -117,30 +133,15 @@ 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) { - 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 - } + val newCommits = commits.flatMap { commit => + if (!existIds.contains(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"){ command.getType match { diff --git a/src/main/scala/util/JGitUtil.scala b/src/main/scala/util/JGitUtil.scala index 5ab4bf7..4d89a3e 100644 --- a/src/main/scala/util/JGitUtil.scala +++ b/src/main/scala/util/JGitUtil.scala @@ -570,4 +570,16 @@ 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 + } + }