问题
The lacking JGit docs dont seem to say anything about how to use/detect branches while using a RevWalk.
This question says pretty much the same thing.
So my question is: How do I get the branch name/id from a RevCommit? Or how do I specify which branch to traverse before hand?
回答1:
Found out a better way to do it by looping branches.
I looped over the branches by calling
for (Ref branch : git.branchList().call()){
git.checkout().setName(branch.getName()).call();
// Then just revwalk as normal.
}
回答2:
Looking at the current implementation of JGit (see its git repo and its RevCommit class), I didn't find the equivalent of what is listed in "Git: Finding what branch a commit came from".
Ie:
git branch --contains <commit>
Only some of the options of git branch
are implemented (like in ListBranchCommand.java).
回答3:
could use below code to get "from" branch by commit:
/**
* 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);
}
}
来源:https://stackoverflow.com/questions/10435377/jgit-how-to-get-branch-when-traversing-repos