问题
How do I use JGit to determine which branches have been merged to master?
I want to do the equivalent of the normal git command:
git branch -a --merged
Is this possible in JGit?
回答1:
RevWalk::isMergedInto()
can be used to determine if a commit was merged into another. That is, isMergedInto
returns true if the commit given in the first argument is an ancestor of the second given commit.
try( RevWalk revWalk = new RevWalk( repository ) ) {
RevCommit masterHead = revWalk.parseCommit( repository.resolve( "refs/heads/master" );
RevCommit otherHead = revWalk.parseCommit( repository.resolve( "refs/heads/other-branch" );
if( revWalk.isMergedInto( otherHead, masterHead ) ) {
...
}
}
To get a list of all branches the ListBranchesCommand
is used:
List<Ref> branches = Git.wrap( repository ).listBranches().setListMode( ListMode.ALL ).call();
来源:https://stackoverflow.com/questions/26644919/how-to-determine-with-jgit-which-branches-have-been-merged-to-master