8

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?

Community
  • 1
  • 1
Braden
  • 1,548
  • 2
  • 12
  • 20

3 Answers3

6

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.
}
Braden
  • 1,548
  • 2
  • 12
  • 20
  • +1 no the traversing a single branch. My answer was more about finding the branch given a commit. – VonC May 04 '12 at 05:52
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).

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Yeah, it looks like there is no support for going from commit -> branch, but do you know if traversing only a single branch's commits is possible? – Braden May 03 '12 at 22:26
  • +1 because your answer helped me get in the right direction. Interesting to see exactly what they implemented. – Braden May 04 '12 at 17:52
1

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);
        }
    }
YunFeng
  • 65
  • 1
  • 8