2017-11-03 15 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(); 

. 반환 된 맵은 키가 주어진 커밋 ID이고 값이 키를 가리키는 분기를 나타내는 최대 하나의 항목을 포함합니다.

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>

당신은 수도에 대응

힘내에서
#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); 
}