fix: diff commit 遇到 merge 时,parentId 取 parent[0] 当作 oldCommit
This commit is contained in:
@@ -323,8 +323,8 @@ public class GitServiceImpl implements IGitService {
|
||||
while (iterator.hasNext()) {
|
||||
RevCommit currentCommit = iterator.next();
|
||||
if (currentCommit.getId().getName().equals(commitId)) {
|
||||
if (iterator.hasNext()) {
|
||||
parentCommitId = iterator.next().getName();
|
||||
if (currentCommit.getParentCount() > 0) {
|
||||
parentCommitId = currentCommit.getParent(0).getName();
|
||||
}
|
||||
break;
|
||||
}
|
||||
@@ -370,23 +370,33 @@ public class GitServiceImpl implements IGitService {
|
||||
* 获取 commitIdA -> commitIdB 文件差异
|
||||
*
|
||||
* @param workspaceId
|
||||
* @param commitIdA
|
||||
* @param commitIdB
|
||||
* @param newCommitId
|
||||
* @param oldCommitId
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public List<Map<Object, Object>> getDiffFileListInCommits(String workspaceId, String commitIdA, String commitIdB) {
|
||||
log.info("[getDiffFileListInCommits] [begin] [workspaceId: {}] [commitIdA: {}] [commitIdB: {}]", workspaceId, commitIdA, commitIdB);
|
||||
public List<Map<Object, Object>> getDiffFileListInCommits(String workspaceId, String newCommitId, String oldCommitId) {
|
||||
log.info("[getDiffFileListInCommits] [begin] [workspaceId: {}] [newCommitId: {}] [oldCommitId: {}]", workspaceId, newCommitId, oldCommitId);
|
||||
try (Git git = this.getGitInstance(workspaceId);
|
||||
Repository repository = git.getRepository();
|
||||
RevWalk revWalk = new RevWalk(repository);
|
||||
DiffFormatter diffFormatter = new DiffFormatter(null)) {
|
||||
|
||||
CanonicalTreeParser oldTree = new CanonicalTreeParser();
|
||||
oldTree.reset(repository.newObjectReader(), revWalk.parseCommit(repository.resolve(commitIdB)).getTree());
|
||||
RevCommit oldCommit = revWalk.parseCommit(repository.resolve(oldCommitId));
|
||||
RevCommit newCommit = revWalk.parseCommit(repository.resolve(newCommitId));
|
||||
|
||||
if (newCommit.getParentCount() > 0) {
|
||||
final RevWalk rw = new RevWalk(repository);
|
||||
oldCommit = rw.parseCommit(newCommit.getParent(0).getId());
|
||||
rw.dispose();
|
||||
}
|
||||
|
||||
// oldTree
|
||||
CanonicalTreeParser oldTree = new CanonicalTreeParser();
|
||||
oldTree.reset(repository.newObjectReader(), oldCommit.getTree());
|
||||
// newTree
|
||||
CanonicalTreeParser newTree = new CanonicalTreeParser();
|
||||
newTree.reset(repository.newObjectReader(), revWalk.parseCommit(repository.resolve(commitIdA)).getTree());
|
||||
newTree.reset(repository.newObjectReader(), newCommit.getTree());
|
||||
|
||||
// diff
|
||||
List<Map<Object, Object>> files = T.ListUtil.list(true);
|
||||
@@ -462,7 +472,7 @@ public class GitServiceImpl implements IGitService {
|
||||
}
|
||||
return files;
|
||||
} catch (IOException e) {
|
||||
log.error(e, "[getDiffFileListInCommits] [error] [workspaceId: {}] [newCommitId: {}] [oldCommitId: {}]", workspaceId, commitIdA, commitIdB);
|
||||
log.error(e, "[getDiffFileListInCommits] [error] [workspaceId: {}] [newCommitId: {}] [oldCommitId: {}]", workspaceId, newCommitId, oldCommitId);
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user