How to determine with JGit which branches have been merged to master?

匆匆过客 提交于 2019-12-18 08:59:05

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!