Skip to content

Commit

Permalink
获取仓库分支方式修改
Browse files Browse the repository at this point in the history
  • Loading branch information
bwcx-jzy committed Sep 14, 2021
1 parent 2ec403d commit 6c69f57
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 16 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

1. 【server】构建触发器新增延迟执行参数(感谢@Steve.Liu)
2. 【server】数据库字段类型超大的 varchar 改为 CLOB(感谢@Alex
3. 【server】获取仓库分支方式修改(避免大仓库执行时间太长)

------

Expand Down
54 changes: 38 additions & 16 deletions modules/server/src/main/java/io/jpom/util/GitUtil.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.jpom.util;

import cn.hutool.core.collection.CollStreamUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
Expand All @@ -21,10 +23,8 @@
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.*;
import java.util.stream.Collectors;

/**
* git工具
Expand Down Expand Up @@ -124,17 +124,39 @@ private static Git initGit(String url, String branchName, File file, Credentials
* @throws IOException IO
*/
private static List<String> branchList(String url, File file, CredentialsProvider credentialsProvider, PrintWriter printWriter) throws GitAPIException, IOException {
try (Git git = initGit(url, null, file, credentialsProvider, printWriter)) {
//
List<Ref> list = git.branchList().setListMode(ListBranchCommand.ListMode.REMOTE).call();
List<String> all = new ArrayList<>(list.size());
list.forEach(ref -> {
String name = ref.getName();
if (name.startsWith(Constants.R_REMOTES + Constants.DEFAULT_REMOTE_NAME)) {
all.add(name.substring((Constants.R_REMOTES + Constants.DEFAULT_REMOTE_NAME).length() + 1));
}
});
return all;
try {
LsRemoteCommand lsRemoteCommand = Git.lsRemoteRepository()
.setRemote(url).setCredentialsProvider(credentialsProvider);
//
Collection<Ref> call = lsRemoteCommand
.setHeads(true)
.setTags(true)
.call();
if (CollUtil.isEmpty(call)) {
return null;
}
Map<String, List<Ref>> refMap = CollStreamUtil.groupByKey(call, ref -> {
String name = ref.getName();
if (name.startsWith(Constants.R_TAGS)) {
return Constants.R_TAGS;
} else if (name.startsWith(Constants.R_HEADS)) {
return Constants.R_HEADS;
}
return null;
});

// branch list
List<Ref> branchListRef = refMap.get(Constants.R_HEADS);
if (branchListRef == null) {
return null;
}
return branchListRef.stream().map(ref -> {
String name = ref.getName();
if (name.startsWith(Constants.R_HEADS)) {
return name.substring((Constants.R_HEADS).length());
}
return null;
}).filter(Objects::nonNull).collect(Collectors.toList());
} catch (TransportException t) {
checkTransportException(t);
throw t;
Expand All @@ -148,7 +170,7 @@ public static List<String> getBranchList(String url, String userName, String use
File gitFile = FileUtil.file(file, "gitTemp", tempId);
try {
List<String> list = branchList(url, gitFile, new UsernamePasswordCredentialsProvider(userName, userPwd), printWriter);
if (list.isEmpty()) {
if (CollUtil.isEmpty(list)) {
throw new JpomRuntimeException("该仓库还没有任何分支");
}
return list;
Expand Down

0 comments on commit 6c69f57

Please sign in to comment.