Check for unrelated histories

This commit is contained in:
Rene Pfeuffer
2019-09-23 12:16:07 +02:00
parent eb1812dea6
commit e533820ae6
4 changed files with 32 additions and 4 deletions

View File

@@ -70,6 +70,7 @@ import java.util.Optional;
import java.util.concurrent.TimeUnit;
import static java.util.Optional.of;
import static java.util.Optional.ofNullable;
//~--- JDK imports ------------------------------------------------------------
@@ -722,12 +723,13 @@ public final class GitUtil
/**
* Computes the first common ancestor of two revisions, aka merge base.
*/
public static ObjectId computeCommonAncestor(org.eclipse.jgit.lib.Repository repository, ObjectId revision1, ObjectId revision2) throws IOException {
public static Optional<ObjectId> computeCommonAncestor(org.eclipse.jgit.lib.Repository repository, ObjectId revision1, ObjectId revision2) throws IOException {
try (RevWalk mergeBaseWalk = new RevWalk(repository)) {
mergeBaseWalk.setRevFilter(RevFilter.MERGE_BASE);
mergeBaseWalk.markStart(mergeBaseWalk.lookupCommit(revision1));
mergeBaseWalk.markStart(mergeBaseWalk.parseCommit(revision2));
return mergeBaseWalk.next().getId();
RevCommit commonAncestor = mergeBaseWalk.next();
return ofNullable(commonAncestor).map(RevCommit::getId);
}
}

View File

@@ -10,11 +10,15 @@ import org.eclipse.jgit.revwalk.RevWalk;
import org.eclipse.jgit.treewalk.EmptyTreeIterator;
import org.eclipse.jgit.treewalk.TreeWalk;
import org.eclipse.jgit.treewalk.filter.PathFilter;
import sonia.scm.BadRequestException;
import sonia.scm.repository.GitUtil;
import sonia.scm.util.Util;
import java.io.IOException;
import java.util.List;
import java.util.Optional;
import static java.util.Collections.emptyList;
final class Differ implements AutoCloseable {
@@ -55,7 +59,9 @@ final class Differ implements AutoCloseable {
if (!Strings.isNullOrEmpty(request.getAncestorChangeset()))
{
ObjectId otherRevision = repository.resolve(request.getAncestorChangeset());
ObjectId ancestorId = computeCommonAncestor(repository, revision, otherRevision);
ObjectId ancestorId =
computeCommonAncestor(repository, revision, otherRevision)
.orElseThrow(NoCommonHistoryException::new);
RevTree tree = walk.parseCommit(ancestorId).getTree();
treeWalk.addTree(tree);
}
@@ -82,7 +88,7 @@ final class Differ implements AutoCloseable {
return new Differ(commit, walk, treeWalk);
}
private static ObjectId computeCommonAncestor(org.eclipse.jgit.lib.Repository repository, ObjectId revision1, ObjectId revision2) throws IOException {
private static Optional<ObjectId> computeCommonAncestor(org.eclipse.jgit.lib.Repository repository, ObjectId revision1, ObjectId revision2) throws IOException {
return GitUtil.computeCommonAncestor(repository, revision1, revision2);
}
@@ -115,4 +121,16 @@ final class Differ implements AutoCloseable {
return entries;
}
}
private static class NoCommonHistoryException extends BadRequestException {
private NoCommonHistoryException() {
super(emptyList(), "no common history");
}
@Override
public String getCode() {
return "4iRct4avG1";
}
}
}