grgit log 得到等同于 git log --name-status --reverse --pretty=format:'%H'

grgit log to get equivalent of git log --name-status --reverse --pretty=format:'%H'

如何使用 grgit 获取更改后的提交路径?

在下面的代码中,我得到了一个 grgit Commit 对象,它有关于提交的元数据,但没有修改文件的列表。我想得到相当于 git log --name-status --reverse --pretty=format:'%H'

关于将提交 sha 转换为包含路径和修改类型详细信息的 grgit 或 jgit 对象的任何想法?

    def commits =  grgit.log {
        range('sha', 'HEAD')
    }
    commits.each { def change ->
        def description = grgit.describe { commit = change }
        println description
        println change
    }

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()
}