Git diff request correctly throws NotFoundException if target revision is non-existent (#2141)

Sometimes it happens that a git diff command request is performed with a non-existent target branch. This is usually fine but the underlying system might have already garbage-collected the revisions associated with that branch. In this case, the revision for that deleted branch might turn up null which currently causes a 500 error. We catch this specific corner-case and throw the correct NotFoundException instead.
This commit is contained in:
Konstantin Schaper
2022-10-24 19:49:28 +02:00
committed by GitHub
parent 8a0686459d
commit 96ce4cb8e6
2 changed files with 10 additions and 4 deletions

View File

@@ -37,14 +37,15 @@ import org.eclipse.jgit.treewalk.AbstractTreeIterator;
import org.eclipse.jgit.treewalk.EmptyTreeIterator;
import org.eclipse.jgit.treewalk.TreeWalk;
import org.eclipse.jgit.treewalk.filter.PathFilter;
import sonia.scm.ContextEntry;
import sonia.scm.NotFoundException;
import sonia.scm.repository.GitUtil;
import sonia.scm.util.Util;
import java.io.IOException;
import java.util.List;
import static sonia.scm.ContextEntry.ContextBuilder.entity;
import static sonia.scm.NotFoundException.notFound;
final class Differ implements AutoCloseable {
private final RevWalk walk;
@@ -70,13 +71,13 @@ final class Differ implements AutoCloseable {
ObjectId revision = repository.resolve(request.getRevision());
if (revision == null) {
throw NotFoundException.notFound(ContextEntry.ContextBuilder.entity("revision not found", request.getRevision()));
throw notFound(entity("Revision", request.getRevision()));
}
RevCommit commit;
try {
commit = walk.parseCommit(revision);
} catch (MissingObjectException ex) {
throw NotFoundException.notFound(ContextEntry.ContextBuilder.entity("revision not found", request.getRevision()));
throw notFound(entity("Revision", request.getRevision()));
}
walk.markStart(commit);
@@ -92,6 +93,9 @@ final class Differ implements AutoCloseable {
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);
RevTree tree = walk.parseCommit(ancestorId).getTree();
treeWalk.addTree(tree);