本文共 1443 字,大约阅读时间需要 4 分钟。
使用jGit 通过gitUrl 获取Git的所有分支
问题引入
在企业中,在针对代码规范规约的时候,就需要保证你的jar包的代码是可追溯的。尤其在大的企业里,对代码质量的检查也是很多的,比如通过sonar进行代码的管理,通过自己编写maven 插件来自定义规范并通过Jenkins 去自动化持续发布和部署。那么在进行提交你所发布的jar包时就需要根据你的git地址去拉取你的分支,然后你要选择你的分支,那如何根据git地址获取所有分支?
jGit 的使用
新建maven工程,添加依赖
org.eclipse.jgit
org.eclipse.jgit
5.5.1.201910021850-r
org.apache.commons
commons-lang3
3.10
新建测试类public class GitTest {
public static void main(String[] args) throws Exception {
GitTest test = new GitTest();
test.getRemoteBranchs("https://gitee.com/njitzyd/JavaDemoCollection.git","你的仓库用户名","你的仓库密码");
}
public void getRemoteBranchs(String url, String username, String password){
try {
Collection refList;
if (StringUtils.isNotEmpty(username) && StringUtils.isNotEmpty(password)) {
UsernamePasswordCredentialsProvider pro = new UsernamePasswordCredentialsProvider(username, password);
refList = Git.lsRemoteRepository().setRemote(url).setCredentialsProvider(pro).call();
} else {
refList = Git.lsRemoteRepository().setRemote(url).call();
}
List branchnameList = new ArrayList<>(4);
for (Ref ref : refList) {
String refName = ref.getName();
if (refName.startsWith("refs/heads/")) { //需要进行筛选
String branchName = refName.replace("refs/heads/", "");
branchnameList.add(branchName);
}
}
System.out.println("共用分支" + branchnameList.size() + "个");
branchnameList.forEach(System.out::println);
} catch (Exception e) {
System.out.println("error");
}
}
}
运行查看结果共用分支2个
develop
master
可以看到代码的所有分支已经拿到,这样就实现了功能!!!
转载地址:https://blog.csdn.net/weixin_32048757/article/details/112957729 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!