2012-10-02 34 views
1

Hy我正在尝试获取包含特定目录或存储库文件的所有提交。JGit - 获取影响文件/路径的所有提交(PlotCommitList)

我试过如下因素代码:

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; 
} 

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

也许TreeFilter不工作我的想法?我应该用其他方式来获得这些提交? 我看到日志命令有一个路径过滤器,但我没有尝试过,因为它返回一个RevCommit列表和我的PlotCommitList我需要一个revwalk作为源使用。而且我认为我无法将RevCommit投射到PlotCommit。

一个男人有同样的问题在这里(用的fileA和FILEB问题第1个答案):Link - Click Here

回答

3

您需要的PathFilterANY_DIFF过滤器组合:

revWalk.setTreeFilter(
    AndTreeFilter.create(PathFilter.create(path), TreeFilter.ANY_DIFF)); 

由于只有PathFilter我想什么恰巧所有提交都是在指定树存在的地方选择的(例如,从该文件的初始提交开始的所有提交)。

另请参阅API docs of setTreeFilterLogCommand如何操作。

+0

谢谢,我会给它一个去,并告诉你它是否工作。 –

+0

它就像一个魅力,我是如此愚蠢!这非常简单,并且有文件记录。谢谢你的提示。 –