diff --git a/CHANGELOG.md b/CHANGELOG.md index 0c0229deed..3aca698218 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ 1. 【server】构建触发器新增延迟执行参数(感谢@Steve.Liu) 2. 【server】数据库字段类型超大的 varchar 改为 CLOB(感谢@Alex) +3. 【server】获取仓库分支方式修改(避免大仓库执行时间太长) ------ diff --git a/modules/server/src/main/java/io/jpom/util/GitUtil.java b/modules/server/src/main/java/io/jpom/util/GitUtil.java index 3766f42f98..b031b5c305 100644 --- a/modules/server/src/main/java/io/jpom/util/GitUtil.java +++ b/modules/server/src/main/java/io/jpom/util/GitUtil.java @@ -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; @@ -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工具 @@ -124,17 +124,39 @@ private static Git initGit(String url, String branchName, File file, Credentials * @throws IOException IO */ private static List branchList(String url, File file, CredentialsProvider credentialsProvider, PrintWriter printWriter) throws GitAPIException, IOException { - try (Git git = initGit(url, null, file, credentialsProvider, printWriter)) { - // - List list = git.branchList().setListMode(ListBranchCommand.ListMode.REMOTE).call(); - List 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 call = lsRemoteCommand + .setHeads(true) + .setTags(true) + .call(); + if (CollUtil.isEmpty(call)) { + return null; + } + Map> 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 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; @@ -148,7 +170,7 @@ public static List getBranchList(String url, String userName, String use File gitFile = FileUtil.file(file, "gitTemp", tempId); try { List list = branchList(url, gitFile, new UsernamePasswordCredentialsProvider(userName, userPwd), printWriter); - if (list.isEmpty()) { + if (CollUtil.isEmpty(list)) { throw new JpomRuntimeException("该仓库还没有任何分支"); } return list;