feat: commit diff 接口返回 commit详情信息

This commit is contained in:
shizhendong
2024-11-04 10:09:24 +08:00
parent c05f37af1a
commit f4a24d3215
4 changed files with 16 additions and 38 deletions

View File

@@ -79,20 +79,25 @@ public class GitController {
})
public R branchCommitDiff(@PathVariable("workspaceId") String workspaceId,
@PathVariable("branchName") String branchName,
@PathVariable("commitId") String commitId,
@PathVariable("commitId") String newCommitId,
@PathVariable(value = "oldCommitId", required = false) String oldCommitId) {
if (T.StrUtil.isEmpty(oldCommitId)) {
// oldCommitId 为空默认对比 parent commitId
oldCommitId = gitService.getParentCommitId(workspaceId, branchName, commitId);
oldCommitId = gitService.getParentCommitId(workspaceId, branchName, newCommitId);
}
List<Map<Object, Object>> diffList = gitService.getDiffFileListInCommits(workspaceId, commitId, oldCommitId);
Map<Object, Object> oldCommit = gitService.infoCommit(workspaceId, oldCommitId);
Map<Object, Object> newCommit = gitService.infoCommit(workspaceId, newCommitId);
List<Map<Object, Object>> diffList = gitService.getDiffFileListInCommits(workspaceId, newCommitId, oldCommitId);
Map<Object, Object> record = T.MapUtil.builder()
.put("oldBranch", branchName)
.put("newBranch", branchName)
.put("oldCommitId", oldCommitId)
.put("newCommitId", commitId)
.put("newCommitId", newCommitId)
.put("oldCommit", oldCommit)
.put("newCommit", newCommit)
.put("files", diffList)
.build();
return R.ok().putData("record", record);

View File

@@ -29,9 +29,7 @@ public interface IGitService {
String getLatestCommitId(String workspaceId, String branch);
List<Map<Object, Object>> getDiffFileListInCommits(String workspaceId, String commitId, String oldCommitId);
List<Map<Object, Object>> getDiffFileListInBranches(String workspaceId, String srcBranch, String tgtBranch);
List<Map<Object, Object>> getDiffFileListInCommits(String workspaceId, String newCommitId, String oldCommitId);
String mergeBranch(String workspaceId, String sourceBranch, String targetBranch, String message) throws RuntimeException;

View File

@@ -128,11 +128,16 @@ public class ApplicationMergeServiceImpl extends ServiceImpl<ApplicationMergeDao
}
});
Map<Object, Object> sourceCommit = gitService.infoCommit(workspaceId, commitA);
Map<Object, Object> targetCommit = gitService.infoCommit(workspaceId, commitB);
Map<Object, Object> m = T.MapUtil.builder()
.put("sourceBranch", sourceBranch)
.put("targetBranch", targetBranch)
.put("sourceCommitId", commitA)
.put("targetCommitId", commitB)
.put("sourceCommit", sourceCommit)
.put("targetCommit", targetCommit)
.put("files", diffFileListInCommits)
.build();
return m;

View File

@@ -426,37 +426,7 @@ public class GitServiceImpl implements IGitService {
}
return files;
} catch (IOException e) {
log.error(e, "[getDiffFileListInCommits] [error] [workspaceId: {}] [commitId: {}] [oldCommitId: {}]", workspaceId, commitIdA, commitIdB);
throw new RuntimeException(e);
}
}
/**
* 获取 branchA -> branchB 文件差异
*
* @param workspaceId
* @param branchA
* @param branchB
* @return
*/
@Override
public List<Map<Object, Object>> getDiffFileListInBranches(String workspaceId, String branchA, String branchB) {
try (Git git = this.getGitInstance(workspaceId);
Repository repository = git.getRepository();
RevWalk revWalk = new RevWalk(repository);) {
RevCommit commitA = revWalk.parseCommit(repository.resolve(branchA));
RevCommit commitB = revWalk.parseCommit(repository.resolve(branchB));
List<Map<Object, Object>> diffFileListInCommits = this.getDiffFileListInCommits(workspaceId, commitA.getName(), commitB.getName());
diffFileListInCommits.parallelStream()
.forEach(m -> {
T.MapUtil.renameKey(m, "oldContent", "sourceContent");
T.MapUtil.renameKey(m, "newContent", "targetContent");
});
return diffFileListInCommits;
} catch (IOException e) {
log.error(e, "[getDiffFileListInBranches] [error]");
log.error(e, "[getDiffFileListInCommits] [error] [workspaceId: {}] [newCommitId: {}] [oldCommitId: {}]", workspaceId, commitIdA, commitIdB);
throw new RuntimeException(e);
}
}