From 2d3040feec5baf2dad2141b839784ad55df291b9 Mon Sep 17 00:00:00 2001 From: shizhendong Date: Thu, 7 Nov 2024 14:46:12 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20application=20=E5=88=97=E8=A1=A8?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E6=9F=A5=E8=AF=A2=20app=20lastCommit=20?= =?UTF-8?q?=E6=95=88=E7=8E=87=E4=BC=98=E5=8C=96=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app/service/impl/GitServiceImpl.java | 98 ++++++++++++------- 1 file changed, 64 insertions(+), 34 deletions(-) diff --git a/src/main/java/net/geedge/asw/module/app/service/impl/GitServiceImpl.java b/src/main/java/net/geedge/asw/module/app/service/impl/GitServiceImpl.java index b6c6dd5..bf0e5b7 100644 --- a/src/main/java/net/geedge/asw/module/app/service/impl/GitServiceImpl.java +++ b/src/main/java/net/geedge/asw/module/app/service/impl/GitServiceImpl.java @@ -36,6 +36,7 @@ import org.eclipse.jgit.transport.RemoteRefUpdate; import org.eclipse.jgit.treewalk.CanonicalTreeParser; import org.eclipse.jgit.treewalk.TreeWalk; import org.eclipse.jgit.treewalk.filter.PathFilter; +import org.eclipse.jgit.util.io.DisabledOutputStream; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Value; @@ -48,7 +49,6 @@ import java.nio.charset.StandardCharsets; import java.nio.file.Path; import java.text.MessageFormat; import java.util.*; -import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutorService; import java.util.concurrent.TimeUnit; import java.util.stream.Collectors; @@ -521,8 +521,8 @@ public class GitServiceImpl implements IGitService { treeWalk.setRecursive(true); Map appIconDataMapping = T.MapUtil.newHashMap(); + Map appDirPathMapping = T.MapUtil.newHashMap(true); - List> futureList = T.ListUtil.list(false); while (treeWalk.next()) { String filePath = treeWalk.getPathString(); String fileName = treeWalk.getNameString(); @@ -545,25 +545,7 @@ public class GitServiceImpl implements IGitService { String appId = T.MapUtil.getStr(metaJsonMap, "id", ""); String appDirPath = treeWalk.getPathString().replaceAll(fileName, ""); - futureList.add( - CompletableFuture.supplyAsync(() -> { - try { - Iterable iterable = git.log() - .add(branchRef) - .addPath(appDirPath) - .call(); - Iterator iterator = iterable.iterator(); - RevCommit commit = iterator.hasNext() ? iterator.next() : null; - return T.MapUtil.builder() - .put("id", appId) - .put("commit", this.buildAswCommitInfo(commit)) - .build(); - } catch (Exception e) { - log.warn(e); - } - return T.MapUtil.newHashMap(); - }, this.virtualThreadExecutor) - ); + appDirPathMapping.put(appId, appDirPath); resultList.add(m); } else if (T.StrUtil.equals("icon.png", fileName)) { @@ -576,6 +558,8 @@ public class GitServiceImpl implements IGitService { } } + Map lastCommitMapping = this.getLastCommitIdDataByPath(repository, branch, appDirPathMapping); + for (Map map : resultList) { String applicationName = T.MapUtil.getStr(map, "name"); if (T.StrUtil.isNotEmpty(applicationName)) { @@ -584,20 +568,10 @@ public class GitServiceImpl implements IGitService { map.put("icon", "data:image/png;base64," + iconBase64Str); } } - } - try { - CompletableFuture.allOf(futureList.toArray(new CompletableFuture[0])).get(); - futureList.forEach(f -> { - Map map = f.getNow(null); - if (T.MapUtil.isNotEmpty(map)) { - String id = T.MapUtil.getStr(map, "id"); - Map item = resultList.stream().filter(m -> T.MapUtil.getStr(m, "id").equals(id)).findFirst().get(); - item.put("commit", T.MapUtil.get(map, "commit", Map.class)); - } - }); - } catch (Exception e) { - log.warn(e); + String appId = T.MapUtil.getStr(map, "id"); + RevCommit lastCommit = T.MapUtil.get(lastCommitMapping, appId, RevCommit.class); + map.put("commit", this.buildAswCommitInfo(lastCommit)); } } } catch (IOException e) { @@ -1149,6 +1123,62 @@ public class GitServiceImpl implements IGitService { return null; } + /** + * 通过 path 获取 lastCommit + * + * @param repository + * @param branch + * @param pathMapping + * @return + */ + public Map getLastCommitIdDataByPath(Repository repository, String branch, Map pathMapping) { + Map result = new HashMap<>(); + try (Git git = new Git(repository); + DiffFormatter diffFormatter = new DiffFormatter(DisabledOutputStream.INSTANCE); + ) { + diffFormatter.setRepository(repository); + + // 指定分支获取 log + Iterable commits = git.log() + .add(git.getRepository().resolve(branch)) + .call(); + for (RevCommit commit : commits) { + // 获取上次提交,用以对比差异 + RevCommit parent = commit.getParentCount() > 0 ? commit.getParent(0) : null; + if (parent == null) { + continue; + } + + // 计算当前提交与上次提交之间的差异 + List diffs = diffFormatter.scan(parent.getTree(), commit.getTree()); + for (DiffEntry diff : diffs) { + String newPath = diff.getNewPath(); + String oldPath = diff.getOldPath(); + + // 检查是否匹配目标路径 + for (Map.Entry entry : pathMapping.entrySet()) { + String key = entry.getKey(); + String path = entry.getValue(); + if (T.BooleanUtil.and( + (newPath.startsWith(path) || oldPath.startsWith(path)), + !result.containsKey(key) + )) { + result.put(key, commit); + } + } + } + + // 如果所有路径都找到对应的最后提交,提前结束循环 + if (result.size() == pathMapping.size()) { + break; + } + } + } catch (IOException | GitAPIException e) { + log.error(e, "[getLastCommitIdDataByPath] [workspace: {}] [branch: {}]", repository.getDirectory().getPath(), branch); + } + return result; + } + /** * 解决冲突提交到 srcBranch * 提交指定 srcBranch,tgtBranch 最新 commitId,像 merge commit 一样