gpt4 book ai didi

jgit - 使用 JGit 确定前后提交的次数

转载 作者:行者123 更新时间:2023-12-01 10:47:57 27 4
gpt4 key购买 nike

在 GitHub 项目中,当我们转到任何分支页面时,我们可以看到描述分支 w.r.t 的提交前/后数量的图表。掌握。

我们如何使用 JGit 确定数字后面的那些人?

我用过 BranchTrackingStatus类(class),但我得到 BranchTrackingStatus对象始终为空。

这是我使用的代码

private static Lis<Integer> getCounts(Repository repository, String branchName) throws IOException{
BranchTrackingStatus trackingStatus = BranchTrackingStatus.of(repository, branchName);
List<Integer> counts = new ArrayList<Integer>();
if (trackingStatus != null){
counts.add(trackingStatus.getAheadCount());
counts.add(trackingStatus.getBehindCount());
} else {
counts.add(0);
counts.add(0);
}
return counts;
}

public void show(String repoName, String baseBranchName) throws IOException, GitAPIException{

Repository repository = repoManager.openRepository(new Project.NameKey(repoName));

List<Ref> call = new Git(repository).branchList().call();
for (Ref ref : call) {
List<Integer> counts = getCounts(repository, ref.getName());
System.out.println("Commits ahead : "+counts.get(0));
System.out.println("Commits behind : "+counts.get(1));
}
}

最佳答案

BranchTrackingStatus.of()假设 branchName表示本地分支,通过其全名(例如 refs/heads/master )或短名称(例如 master )。它返回 null如果给定 branchName找不到或跟踪分支未配置或不存在。

要比较两个任意分支,您可以采用 BranchTrackingStatus代码如下:

void calculateDivergence(Ref local, Ref tracking) throws IOException {
try (RevWalk walk = new RevWalk(repository)) {
RevCommit localCommit = walk.parseCommit(local.getObjectId());
RevCommit trackingCommit = walk.parseCommit(tracking.getObjectId());
walk.setRevFilter(RevFilter.MERGE_BASE);
walk.markStart(localCommit);
walk.markStart(trackingCommit);
RevCommit mergeBase = walk.next();
walk.reset();
walk.setRevFilter(RevFilter.ALL);
aheadCount = RevWalkUtils.count(walk, localCommit, mergeBase);
behindCount = RevWalkUtils.count(walk, trackingCommit, mergeBase);
}
}

关于jgit - 使用 JGit 确定前后提交的次数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23605838/

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