gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 06:33:47 26 4
gpt4 key购买 nike

我需要从上次 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com