mirror of
https://github.com/gitbucket/gitbucket.git
synced 2025-11-08 06:25:51 +01:00
Improve the commit hook to add commit messages to the issue comment.
This commit is contained in:
@@ -9,7 +9,7 @@ import org.slf4j.LoggerFactory
|
|||||||
import javax.servlet.ServletConfig
|
import javax.servlet.ServletConfig
|
||||||
import javax.servlet.ServletContext
|
import javax.servlet.ServletContext
|
||||||
import javax.servlet.http.HttpServletRequest
|
import javax.servlet.http.HttpServletRequest
|
||||||
import util.Directory
|
import util.{JGitUtil, Directory}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Provides Git repository via HTTP.
|
* Provides Git repository via HTTP.
|
||||||
@@ -43,36 +43,41 @@ class GitRepositoryServlet extends GitServlet {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class GitBucketRecievePackFactory extends ReceivePackFactory[HttpServletRequest] {
|
class GitBucketRecievePackFactory extends ReceivePackFactory[HttpServletRequest] {
|
||||||
|
|
||||||
|
private val logger = LoggerFactory.getLogger(classOf[GitBucketRecievePackFactory])
|
||||||
|
|
||||||
override def create(req: HttpServletRequest, db: Repository): ReceivePack = {
|
override def create(req: HttpServletRequest, db: Repository): ReceivePack = {
|
||||||
val receivePack = new ReceivePack(db)
|
val receivePack = new ReceivePack(db)
|
||||||
|
|
||||||
println("----")
|
|
||||||
println("contextPath: " + req.getContextPath)
|
|
||||||
println("requestURI: " + req.getRequestURI)
|
|
||||||
println("remoteUser:" + req.getRemoteUser)
|
|
||||||
|
|
||||||
val userName = req.getSession.getAttribute("USER_INFO")
|
val userName = req.getSession.getAttribute("USER_INFO")
|
||||||
println("userName: " + userName)
|
|
||||||
|
|
||||||
println("----")
|
logger.debug("requestURI: " + req.getRequestURI)
|
||||||
|
logger.debug("userName:" + userName)
|
||||||
|
|
||||||
receivePack.setPostReceiveHook(new CommitLogHook())
|
val pathList = req.getRequestURI.split("/")
|
||||||
|
val owner = pathList(2)
|
||||||
|
val repository = pathList(3).replaceFirst("\\.git$", "")
|
||||||
|
|
||||||
|
logger.debug("repository:" + owner + "/" + repository)
|
||||||
|
|
||||||
|
receivePack.setPostReceiveHook(new CommitLogHook(owner, repository))
|
||||||
receivePack
|
receivePack
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
import scala.collection.JavaConverters._
|
import scala.collection.JavaConverters._
|
||||||
|
|
||||||
class CommitLogHook extends PostReceiveHook {
|
class CommitLogHook(owner: String, repository: String) extends PostReceiveHook {
|
||||||
|
|
||||||
|
private val logger = LoggerFactory.getLogger(classOf[CommitLogHook])
|
||||||
|
|
||||||
def onPostReceive(receivePack: ReceivePack, commands: java.util.Collection[ReceiveCommand]): Unit = {
|
def onPostReceive(receivePack: ReceivePack, commands: java.util.Collection[ReceiveCommand]): Unit = {
|
||||||
println("**** hook ****")
|
JGitUtil.withGit(Directory.getRepositoryDir(owner, repository)) { git =>
|
||||||
commands.asScala.foreach { command =>
|
commands.asScala.foreach { command =>
|
||||||
println(command.getRefName)
|
JGitUtil.getCommitLog(git, command.getOldId.name, command.getNewId.name).foreach { commit =>
|
||||||
println(command.getMessage)
|
// TODO extract issue id and add comment to issue
|
||||||
println(command.getRef().getName())
|
logger.debug(commit.id + ":" + commit.message)
|
||||||
println("--")
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -154,6 +154,39 @@ object JGitUtil {
|
|||||||
commits
|
commits
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the commit list between two revisions.
|
||||||
|
*
|
||||||
|
* @param git the Git object
|
||||||
|
* @param from the from revision
|
||||||
|
* @param to the to revision
|
||||||
|
* @return the commit list
|
||||||
|
*/
|
||||||
|
def getCommitLog(git: Git, from: String, to: String): List[CommitInfo] = {
|
||||||
|
@scala.annotation.tailrec
|
||||||
|
def getCommitLog(i: java.util.Iterator[RevCommit], logs: List[CommitInfo]): List[CommitInfo] =
|
||||||
|
i.hasNext match {
|
||||||
|
case true => {
|
||||||
|
val revCommit = i.next
|
||||||
|
if(revCommit.name == from){
|
||||||
|
logs
|
||||||
|
} else {
|
||||||
|
getCommitLog(i, logs :+ new CommitInfo(revCommit))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
case false => logs
|
||||||
|
}
|
||||||
|
|
||||||
|
val revWalk = new RevWalk(git.getRepository)
|
||||||
|
revWalk.markStart(revWalk.parseCommit(git.getRepository.resolve(to)))
|
||||||
|
|
||||||
|
val commits = getCommitLog(revWalk.iterator, Nil)
|
||||||
|
revWalk.release
|
||||||
|
|
||||||
|
commits.reverse
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the latest RevCommit of the specified path.
|
* Returns the latest RevCommit of the specified path.
|
||||||
*
|
*
|
||||||
|
|||||||
Reference in New Issue
Block a user