fix: application 列表接口查询 app lastCommit 效率优化测试
This commit is contained in:
@@ -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<String, String> appIconDataMapping = T.MapUtil.newHashMap();
|
||||
Map<String, String> appDirPathMapping = T.MapUtil.newHashMap(true);
|
||||
|
||||
List<CompletableFuture<Map>> 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<RevCommit> iterable = git.log()
|
||||
.add(branchRef)
|
||||
.addPath(appDirPath)
|
||||
.call();
|
||||
Iterator<RevCommit> 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<String, RevCommit> lastCommitMapping = this.getLastCommitIdDataByPath(repository, branch, appDirPathMapping);
|
||||
|
||||
for (Map<Object, Object> 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<Object, Object> 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<String, RevCommit> getLastCommitIdDataByPath(Repository repository, String branch, Map<String, String> pathMapping) {
|
||||
Map<String, RevCommit> result = new HashMap<>();
|
||||
try (Git git = new Git(repository);
|
||||
DiffFormatter diffFormatter = new DiffFormatter(DisabledOutputStream.INSTANCE);
|
||||
) {
|
||||
diffFormatter.setRepository(repository);
|
||||
|
||||
// 指定分支获取 log
|
||||
Iterable<RevCommit> 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<DiffEntry> diffs = diffFormatter.scan(parent.getTree(), commit.getTree());
|
||||
for (DiffEntry diff : diffs) {
|
||||
String newPath = diff.getNewPath();
|
||||
String oldPath = diff.getOldPath();
|
||||
|
||||
// 检查是否匹配目标路径
|
||||
for (Map.Entry<String, String> 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 一样
|
||||
|
||||
Reference in New Issue
Block a user