不足している 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);
}
}