java - Jgit-如何使用 Jgit 获取两个日期之间的 merge 提交列表?

标签 java eclipse git jenkins

我需要从上次 Jenkins 构建中提取的日期的提交列表,并获取自该日期以来的 merge 提交列表。 到目前为止我已经编码以获得 merge 提交列表。只需要一个解决方案来提取指定日期之间的这些提交。 引用代码:

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

最佳答案

您可以使用LogCommand#setRevFilter(RevFilter)CommitTimeRevFilter#between(Date,Date) ,e。例如:

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

关于java - Jgit-如何使用 Jgit 获取两个日期之间的 merge 提交列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45586672/

相关文章:

java - Sonar :sonar work?如何

java - GWT:将客户端和服务器端分成不同的 WAR

c - 如何在 Eclipse 中关闭多行注释中的 '*'?

android - 共享首选项 xml 文件

git - 将问题从 Bitbucket 转移到 Github

node.js - 在 Git post-receive hook 中运行无限长的进程

java - 从两个不同的数据源中选择排序的数据

eclipse - 如何更改 JBoss 错误消息的语言?

git - Lint :eslint when commit the code into local repositary 上的错误

java - 安卓工作室 : Why is 'languageLevel' different from 'JDK Location' value on new AS installation?