Question

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?

Was it helpful?

Solution

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.
}

OTHER TIPS

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).

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);
        }
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top