0

如何使用 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
    }
4

1 回答 1

0

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()
}
于 2020-06-21T17:15:42.630 回答