Show correct image diff if compare across multiple commits

Pass the ancestor commit as the old revision if it is set in the GitDiffResult. This fixes the image diff renderer if the pull requests consists of multiple commits to show the full diff instead of the changes between the latest two commits.
This commit is contained in:
Eduard Heimbuch
2023-11-01 14:05:54 +01:00
parent f847e324b1
commit 69c165749a
3 changed files with 23 additions and 7 deletions

View File

@@ -52,17 +52,19 @@ final class Differ implements AutoCloseable {
private final TreeWalk treeWalk;
private final RevCommit commit;
private final PathFilter pathFilter;
private final ObjectId commonAncestor;
private Differ(RevCommit commit, RevWalk walk, TreeWalk treeWalk, PathFilter pathFilter) {
private Differ(RevCommit commit, RevWalk walk, TreeWalk treeWalk, PathFilter pathFilter, ObjectId commonAncestor) {
this.commit = commit;
this.walk = walk;
this.treeWalk = treeWalk;
this.pathFilter = pathFilter;
this.commonAncestor = commonAncestor;
}
static Diff diff(Repository repository, DiffCommandRequest request) throws IOException {
try (Differ differ = create(repository, request)) {
return differ.diff(repository);
return differ.diff(repository, differ.commonAncestor);
}
}
@@ -91,12 +93,13 @@ final class Differ implements AutoCloseable {
pathFilter = PathFilter.create(request.getPath());
}
ObjectId ancestorId = null;
if (!Strings.isNullOrEmpty(request.getAncestorChangeset())) {
ObjectId otherRevision = repository.resolve(request.getAncestorChangeset());
if (otherRevision == null) {
throw notFound(entity("Revision", request.getAncestorChangeset()));
}
ObjectId ancestorId = GitUtil.computeCommonAncestor(repository, revision, otherRevision);
ancestorId = GitUtil.computeCommonAncestor(repository, revision, otherRevision);
RevTree tree = walk.parseCommit(ancestorId).getTree();
treeWalk.addTree(tree);
} else if (commit.getParentCount() > 0) {
@@ -113,12 +116,12 @@ final class Differ implements AutoCloseable {
treeWalk.addTree(commit.getTree());
return new Differ(commit, walk, treeWalk, pathFilter);
return new Differ(commit, walk, treeWalk, pathFilter, ancestorId);
}
private Diff diff(Repository repository) throws IOException {
private Diff diff(Repository repository, ObjectId ancestorChangeset) throws IOException {
List<DiffEntry> entries = scanWithRename(repository, pathFilter, treeWalk);
return new Diff(commit, entries);
return new Diff(commit, entries, ancestorChangeset);
}
static List<DiffEntry> scanWithRename(Repository repository, PathFilter pathFilter, TreeWalk treeWalk) throws IOException {
@@ -146,10 +149,12 @@ final class Differ implements AutoCloseable {
private final RevCommit commit;
private final List<DiffEntry> entries;
private final ObjectId commonAncestor;
private Diff(RevCommit commit, List<DiffEntry> entries) {
private Diff(RevCommit commit, List<DiffEntry> entries, ObjectId commonAncestor) {
this.commit = commit;
this.entries = entries;
this.commonAncestor = commonAncestor;
}
public RevCommit getCommit() {
@@ -159,6 +164,10 @@ final class Differ implements AutoCloseable {
public List<DiffEntry> getEntries() {
return entries;
}
public ObjectId getCommonAncestor() {
return commonAncestor;
}
}
}

View File

@@ -27,6 +27,7 @@ package sonia.scm.repository.spi;
import org.eclipse.jgit.diff.DiffEntry;
import org.eclipse.jgit.diff.DiffFormatter;
import org.eclipse.jgit.errors.AmbiguousObjectException;
import org.eclipse.jgit.lib.ObjectId;
import sonia.scm.NotUniqueRevisionException;
import sonia.scm.repository.GitUtil;
import sonia.scm.repository.InternalRepositoryException;
@@ -88,6 +89,10 @@ public class GitDiffResultCommand extends AbstractGitCommand implements DiffResu
@Override
public String getOldRevision() {
ObjectId commonAncestor = diff.getCommonAncestor();
if (commonAncestor != null) {
return commonAncestor.name();
}
return diff.getCommit().getParentCount() > 0 ? GitUtil.getId(diff.getCommit().getParent(0).getId()) : null;
}