如何使用 JGit 获取特定路径的最新修订 ID,不包括给定分支中的合并?

How to get the latest revision id for a specific path excluding merges in a given branch using JGit?

我正在努力将复杂的 Mercurial 查询转换为 Git。我发现 JGit 可以用来实现同样的效果,而无需在代码中手工编写查询。如果文件在分支中被修改并排除合并,目标是根据路径过滤器获取最新的修订 ID。这是我目前所拥有的:

public static void main(String[] args) throws IOException {
try (Repository repository = CookbookHelper.openJGitCookbookRepository()) {
    Ref head = repository.getRef("HEAD");
    //Use pathFilter to filter this just for maps directory
    PathFilter pathFilter = PathFilter.create("directory/path/*")

    RevWalk walk = new RevWalk(repository)

    walk.setRevFilter(RevFilter.NO_MERGES)
    walk.setTreeFilter(AndTreeFilter.create(PathFilterGroup.create(pathFilter))

    RevCommit commit = walk.parseCommit(${REVISION});
    RevTree tree = commit.getTree();
    // now use a TreeWalk to iterate over all files in the Tree recursively and you can set Filters to narrow down the results if needed
    try (TreeWalk treeWalk = new TreeWalk(repository)) {
         treeWalk.addTree(tree);
         while (treeWalk.next()) {
           // Some custom logic here
            }
        }
    }
}
}

拿到 TreeWalk 后我有点受阻。感谢您的帮助!

编辑: 这是我正在转换的 Mercurial 查询:

hg log -r max((merge() and branch(${REVISION}) and ancestors(${REVISION}) " \
            "and descendants(not branch(${REVISION}) and file('directory/path/*') " \
            "and not ancestors(min(branch(${REVISION}))))) or max( file('directory/path/*') " \
            "and branch(${REVISION}) and ancestors(${REVISION})) " \
            "or min(branch(${REVISION})) and public() or p1(min(branch(${REVISION})))) --template {node}

我认为您的 RevWalk 设置非常接近。缺少的是将排序顺序设置为首先显示较新的提交,并设置 'point' 从哪里开始遍历。在您的情况下,起点将是有问题的分支。

RevWalk walk = new RevWalk( repository )
walk.setRevFilter( RevFilter.NO_MERGES )
walk.setTreeFilter( AndTreeFilter.create( PathFilterGroup.create( pathFilter ) );

walk.sort( RevSort.COMMIT_TIME_DESC, true );
walk.markStart( walk.parseCommit( repository.resolve( "refs/heads/branch-name" ) );
if( walk.next() != null ) {
  // now the RevWalk points to the newest commit in which the 
  // file was modified in the branch, excluding merges.
}

walk.close();