2017-08-09 51 views
0

我需要從上一次Jenkins Build中提取的提交列表,並獲取自該日期以來的合併提交列表。 我已經編碼到目前爲止獲取合併提交列表。只需要一個解決方案來提取指定日期之間的這些提交。 參考代碼:Jgit-如何使用Jgit獲取兩個日期之間的合併提交列表?

public static void main(String[] args)throws IOException , GitAPIException{ 
    ArrayList<String> CommitIds=new ArrayList<String>(); 
    FileRepositoryBuilder repositoryBuilder = new FileRepositoryBuilder(); 
    repositoryBuilder.setMustExist(true); 
    repositoryBuilder.setGitDir(new File("/path/to/repo")); 
    Repository repo = repositoryBuilder.build(); 
    Git git = Git.open(new File("/path/to/repo")); 
    RevWalk walk = new RevWalk(repo); 
    git.checkout().setName("branch").call(); 
    String branchName=repo.getBranch(); 
    System.out.println(branchName); 
    Iterable<RevCommit> commits = git.log().all().call(); 
    RevCommit masterHead = walk.parseCommit(repo.resolve("refs/heads/master")); 
    for (RevCommit commit : commits) { 
     boolean foundInThisBranch = false; 

     RevCommit otherHead = walk.parseCommit(repo.resolve(
       commit.getName())); 
     for (Map.Entry<String, Ref> e : repo.getAllRefs().entrySet()) { 
      if (e.getKey().startsWith(Constants.R_HEADS)) { 
       if (walk.isMergedInto(otherHead, walk.parseCommit(
         e.getValue().getObjectId()))) { 
        String foundInBranch = e.getValue().getName(); 
        if (branchName.equals(foundInBranch)) { 
         foundInThisBranch = true; 
         break; 
        } 
       } 
      } 
     } 
     if (foundInThisBranch) 
     { 
      CommitIds.add(commit.getName()); 


    } 

     } 

    System.out.println(CommitIds); 
} 

回答

0

您可以使用LogCommand#setRevFilter(RevFilter)CommitTimeRevFilter#between(Date,Date),E。 g .:

ObjectId masterId = git.getRepository().exactRef("refs/heads/master").getObjectId(); 
Date since = new SimpleDateFormat("yyyy-MM-dd").parse("2017-08-01"); 
Date until = new SimpleDateFormat("yyyy-MM-dd").parse("2017-08-10"); 
RevFilter between = CommitTimeRevFilter.between(since, until); 
for (RevCommit commit : git.log().add(masterId).setRevFilter(between).call()) { 
    System.out.println( "* " 
         + commit.getId().getName() 
         + " " 
         + commit.getShortMessage()); 
}