2017-11-03 8 views
0

JGitを使用して特定のコミットに関連付けられているブランチの名前を取得する必要があります。私はJGitを使用して、リポジトリのコミットSHAの完全なリストを取得しました。今は、それが所属するブランチの名前を知る必要があります。JGitでコミットのブランチを見つけるには?

誰かが私にこれを達成する方法を教えてもらえると嬉しいです。

+1

ブランチのヒント(最新のコミット)のみがブランチに実際に関連付けられていることに言及してください。他のコミットについては、コミットのグラフと履歴を見るだけで推測できます。 – everton

答えて

1

:あなたがリモートブランチ(-r)またはリモートとローカルの支店(-a)の両方を一覧表示したい場合も、サンプル

setListMode 

public ListBranchCommand setListMode(ListBranchCommand.ListMode listMode) 

Parameters: 
listMode - optional: corresponds to the -r/-a options; by default, only local branches will be listed 

Returns: 
this instance 
、このAPIを必要とします枝に属していない。コミットは不変ですが、ブランチはその名前とそれらが指しているコミットを変更できます。

コミットを直接指すブランチまたはコミットの後継者があるブランチがある場合、コミットは到達可能です(ブランチまたはタグまたは他の参照)。

JGitの場合、NameRevCommandを使用して、コミットを直接指すブランチがある場合はそれを見つけることができます。例:

Map<ObjectId, String> map = git 
    .nameRev() 
    .addPrefix("refs/heads") 
    .add(ObjectId.fromString("<SHA-1>")) 
    .call(); 

上記のスニペットは、refs/heads名前空間で、指定されたコミットを参照するrefを探します。返されるマップは、キーが与えられたコミットIDであり、値がそれを指し示すブランチを表す多くても1つのエントリを含む。

0

document

クラスListBranchCommand

ListBranchCommand setContains(文字列containsCommitish)

setContains 

public ListBranchCommand setContains(String containsCommitish) 

If this is set, only the branches that contain the specified commit-ish as an ancestor are returned. 

Parameters: 
containsCommitish - a commit ID or ref name 

Returns: 
this instance 

Since: 
3.4 

このAPIは、あなたがかもしれgit branch --contains <commit-ish>

に対応している、言うようにGitのコミットDOEので

#list all the branches that "HEAD" belongs to. 
try { 
    Git git = Git.open(new File("D:/foo/.git")); 
    List<Ref> refs = git.branchList().setContains("HEAD").setListMode(ListBranchCommand.ListMode.ALL).call(); 
    System.out.println(refs); 
} catch (Exception e) { 
    System.out.println(e); 
} 
関連する問題