JGit - 获取影响文件/路径的所有提交(PlotCommitList)

Dor*_*lan 1 commit path filter jgit

我正在尝试获取包含我的存储库的特定目录或文件的所有提交。

我尝试了以下代码:

public PlotCommitList getPlotCommits(String path){
    System.out.println(path);
    PlotCommitList<PlotLane> plotCommitList = new PlotCommitList<PlotLane>();
    PlotWalk revWalk = new PlotWalk(repository);
    try {

        ObjectId rootId = repository.resolve("HEAD");
        if (rootId != null) {
            RevCommit root = revWalk.parseCommit(rootId);
            revWalk.markStart(root);
            revWalk.setTreeFilter(PathFilter.create(path));
            plotCommitList.source(revWalk);
            plotCommitList.fillTo(Integer.MAX_VALUE);
            return plotCommitList;
        }

    } catch (AmbiguousObjectException ex) {
        Logger.getLogger(GitRepository.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(GitRepository.class.getName()).log(Level.SEVERE, null, ex);
    }
    return plotCommitList;
}
Run Code Online (Sandbox Code Playgroud)

我不只得到影响该文件的提交。我得到了整个列表的一些“子列表”,但不仅仅是那些影响该文件的提交。

也许 TreeFilter 不起作用我怎么想?我应该使用其他方式来获得这些提交吗?我看到 log 命令有一个路径过滤器,但我还没有尝试过,因为它返回一个 RevCommit 列表,对于我的 PlotCommitList,我需要一个 revwalk 来用作源。而且我认为我不能将 RevCommit 转换为 PlotCommit。

一个人在这里遇到了同样的问题(文件A和文件B问题的第一个答案):链接 - 单击此处

rob*_*nst 5

您需要将PathFilterANY_DIFF过滤器结合使用:

revWalk.setTreeFilter(
    AndTreeFilter.create(PathFilter.create(path), TreeFilter.ANY_DIFF));
Run Code Online (Sandbox Code Playgroud)

仅使用 PathFilter 我认为会发生的情况是在指定树存在的地方选择所有提交(例如,从该文件的初始提交开始的所有提交)。

另请参阅setTreeFilterAPI 文档LogCommand如何做到这一点。