如何使用JGit获取存储库中的所有分支? 我们来看example repository。我们可以看到,它有5个分支 Here我找到了这个例子:
int c = 0;
List<Ref> call = new Git(repository).branchList().call();
for (Ref ref : call) {
System.out.println("Branch: " + ref + " " + ref.getName() + " "
+ ref.getObjectId().getName());
c++;
}
System.out.println("Number of branches: " + c);
但我得到的只是:
Branch: Ref[refs/heads/master=d766675da9e6bf72f09f320a92b48fa529ffefdc] refs/heads/master d766675da9e6bf72f09f320a92b48fa529ffefdc
Number of branches: 1
Branch: master
答案 0 :(得分:14)
如果您缺少远程分支,则必须将ListMode
的{{1}}设置为ListBranchCommand
或ALL
。默认的ListMode(REMOTE
)仅返回本地分支。
null
答案 1 :(得分:2)
我使用以下方法进行git分支,而不使用Jgit
克隆repo这是在pom.xml
中 <dependency>
<groupId>org.eclipse.jgit</groupId>
<artifactId>org.eclipse.jgit</artifactId>
<version>4.0.1.201506240215-r</version>
</dependency>
方法
public static List<String> fetchGitBranches(String gitUrl)
{
Collection<Ref> refs;
List<String> branches = new ArrayList<String>();
try {
refs = Git.lsRemoteRepository()
.setHeads(true)
.setRemote(gitUrl)
.call();
for (Ref ref : refs) {
branches.add(ref.getName().substring(ref.getName().lastIndexOf("/")+1, ref.getName().length()));
}
Collections.sort(branches);
} catch (InvalidRemoteException e) {
LOGGER.error(" InvalidRemoteException occured in fetchGitBranches",e);
e.printStackTrace();
} catch (TransportException e) {
LOGGER.error(" TransportException occurred in fetchGitBranches",e);
} catch (GitAPIException e) {
LOGGER.error(" GitAPIException occurr in fetchGitBranches",e);
}
return branches;
}