缺少的JGit文档似乎没有说明在使用RevWalk时如何使用/检测分支。
这个问题说的几乎是一样的。
所以我的问题是:如何从 RevCommit 获取分支名称/ID? 或者如何事先指定要遍历的分支?
通过循环分支找到了更好的方法。
我通过呼叫在树枝上循环
for (Ref branch : git.branchList().call()){
git.checkout().setName(branch.getName()).call();
// Then just revwalk as normal.
}
查看 JGit 的当前实现(请参阅其 git 存储库和 RevCommit 类),我没有找到与"Git:查找提交来自哪个分支"中列出的等效内容。
即:
git branch --contains <commit>
仅实现了git branch
的某些选项(如ListBranchCommand.java
)。
可以使用以下代码通过提交获取"from"分支:
/**
* find out which branch that specified commit come from.
*
* @param commit
* @return branch name.
* @throws GitException
*/
public String getFromBranch(RevCommit commit) throws GitException{
try {
Collection<ReflogEntry> entries = git.reflog().call();
for (ReflogEntry entry:entries){
if (!entry.getOldId().getName().equals(commit.getName())){
continue;
}
CheckoutEntry checkOutEntry = entry.parseCheckout();
if (checkOutEntry != null){
return checkOutEntry.getFromBranch();
}
}
return null;
} catch (Exception e) {
throw new GitException("fail to get ref log.", e);
}
}