grgit 很好,它还带来了 jgit + 提供对较低层的访问
所以,我们可以这样做
获取范围的 RevCommit 对象列表
import org.eclipse.jgit.api.Git
import org.eclipse.jgit.treewalk.TreeWalk
import org.eclipse.jgit.diff.DiffFormatter
import org.eclipse.jgit.treewalk.CanonicalTreeParser
import org.eclipse.jgit.diff.DiffEntry
import org.eclipse.jgit.treewalk.filter.PathFilter
def getCommits(def early, def later='HEAD') {
Git git = grgit.repository.jgit
def earliest = git.repository.resolve(early)
def latest = git.repository.resolve(later)
return git.log()
.addRange(earliest, latest)
.call()
}
每次提交获取差异信息
for (def commit in getCommits(earliestCommitId, latestCommitId)) {
for (DiffEntry diffEntry in diffCommitWithParent(commit, pathFilter)) {
println diffEntry // you'd want to do something more useful here
}
}
def diffCommitWithParent(def commit, def pathFilter) {
Git git = grgit.repository.jgit
def previousTree = commit.getParentCount() > 0 ? commit.getParent(0).getTree() : null
def currentTree = commit.getTree()
def reader = git.repository.newObjectReader()
def treeParserPrevious = new CanonicalTreeParser(null, reader, previousTree)
def treeParserCurrent = new CanonicalTreeParser(null, reader, currentTree)
return git.diff()
.setOldTree(treeParserPrevious)
.setNewTree(treeParserCurrent)
.setPathFilter(new PathFilter(pathFilter))
.call()
}