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

时间:2017-11-21 15:37:38

标签: java git version-control 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}

1 个答案:

答案 0 :(得分:1)

我认为,var str = "A\nB\n\n\nC\n\n\n\n\nD"设置非常接近。缺少的是将排序顺序设置为首先显示较新的提交,并设置从开始步行的位置开始的“点”。在您的情况下,起点将是有问题的分支。

"A\nB\n\nC\n\n\n\nD"