MergeUtil: Use InMemoryInserter for dry runs

As in Ifd9d768d, making flush a no-op is not sufficient to ensure
nothing gets written to the repo.

Change-Id: If08e8184b484a0e7ba41fd3e11130d6fb26ed6c6
This commit is contained in:
Dave Borowitz
2016-06-24 12:04:56 -04:00
parent 5244635047
commit 731585a27a
2 changed files with 11 additions and 31 deletions

View File

@@ -28,6 +28,7 @@ import com.google.gerrit.reviewdb.server.ReviewDb;
import com.google.gerrit.server.cache.CacheModule; import com.google.gerrit.server.cache.CacheModule;
import com.google.gerrit.server.config.GerritServerConfig; import com.google.gerrit.server.config.GerritServerConfig;
import com.google.gerrit.server.git.GitRepositoryManager; import com.google.gerrit.server.git.GitRepositoryManager;
import com.google.gerrit.server.git.InMemoryInserter;
import com.google.gerrit.server.git.MergeUtil; import com.google.gerrit.server.git.MergeUtil;
import com.google.gerrit.server.project.ProjectCache; import com.google.gerrit.server.project.ProjectCache;
import com.google.gerrit.server.project.ProjectState; import com.google.gerrit.server.project.ProjectState;
@@ -40,6 +41,7 @@ import com.google.inject.name.Named;
import org.eclipse.jgit.errors.LargeObjectException; import org.eclipse.jgit.errors.LargeObjectException;
import org.eclipse.jgit.lib.Config; import org.eclipse.jgit.lib.Config;
import org.eclipse.jgit.lib.ObjectId; import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.ObjectInserter;
import org.eclipse.jgit.lib.Repository; import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.merge.ThreeWayMerger; import org.eclipse.jgit.merge.ThreeWayMerger;
import org.eclipse.jgit.revwalk.RevCommit; import org.eclipse.jgit.revwalk.RevCommit;
@@ -221,10 +223,10 @@ public class ChangeKindCacheImpl implements ChangeKindCache {
// A trivial rebase can be detected by looking for the next commit // A trivial rebase can be detected by looking for the next commit
// having the same tree as would exist when the prior commit is // having the same tree as would exist when the prior commit is
// cherry-picked onto the next commit's new first parent. // cherry-picked onto the next commit's new first parent.
ThreeWayMerger merger = MergeUtil.newThreeWayMerger( try (ObjectInserter ins = new InMemoryInserter(repo)) {
repo, MergeUtil.createDryRunInserter(repo), key.strategyName); ThreeWayMerger merger =
MergeUtil.newThreeWayMerger(repo, ins, key.strategyName);
merger.setBase(prior.getParent(0)); merger.setBase(prior.getParent(0));
try {
if (merger.merge(next.getParent(0), prior) if (merger.merge(next.getParent(0), prior)
&& merger.getResultTreeId().equals(next.getTree())) { && merger.getResultTreeId().equals(next.getTree())) {
if (prior.getParentCount() == 1) { if (prior.getParentCount() == 1) {

View File

@@ -68,12 +68,10 @@ import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.revwalk.RevFlag; import org.eclipse.jgit.revwalk.RevFlag;
import org.eclipse.jgit.revwalk.RevSort; import org.eclipse.jgit.revwalk.RevSort;
import org.eclipse.jgit.revwalk.RevWalk; import org.eclipse.jgit.revwalk.RevWalk;
import org.eclipse.jgit.transport.PackParser;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
@@ -361,9 +359,9 @@ public class MergeUtil {
return false; return false;
} }
ThreeWayMerger m = newThreeWayMerger(repo, createDryRunInserter(repo)); try (ObjectInserter ins = new InMemoryInserter(repo)) {
try { return newThreeWayMerger(repo, ins)
return m.merge(new AnyObjectId[] {mergeTip, toMerge}); .merge(new AnyObjectId[] {mergeTip, toMerge});
} catch (LargeObjectException e) { } catch (LargeObjectException e) {
log.warn("Cannot merge due to LargeObjectException: " + toMerge.name()); log.warn("Cannot merge due to LargeObjectException: " + toMerge.name());
return false; return false;
@@ -409,8 +407,8 @@ public class MergeUtil {
// taking the delta relative to that one parent and redoing // taking the delta relative to that one parent and redoing
// that on the current merge tip. // that on the current merge tip.
// //
try { try (ObjectInserter ins = new InMemoryInserter(repo)) {
ThreeWayMerger m = newThreeWayMerger(repo, createDryRunInserter(repo)); ThreeWayMerger m = newThreeWayMerger(repo, ins);
m.setBase(toMerge.getParent(0)); m.setBase(toMerge.getParent(0));
return m.merge(mergeTip, toMerge); return m.merge(mergeTip, toMerge);
} catch (IOException e) { } catch (IOException e) {
@@ -437,26 +435,6 @@ public class MergeUtil {
} }
} }
public static ObjectInserter createDryRunInserter(Repository db) {
final ObjectInserter delegate = db.newObjectInserter();
return new ObjectInserter.Filter() {
@Override
protected ObjectInserter delegate() {
return delegate;
}
@Override
public PackParser newPackParser(InputStream in) throws IOException {
throw new UnsupportedOperationException();
}
@Override
public void flush() throws IOException {
// Do nothing.
}
};
}
public CodeReviewCommit mergeOneCommit(PersonIdent author, public CodeReviewCommit mergeOneCommit(PersonIdent author,
PersonIdent committer, Repository repo, CodeReviewRevWalk rw, PersonIdent committer, Repository repo, CodeReviewRevWalk rw,
ObjectInserter inserter, Branch.NameKey destBranch, ObjectInserter inserter, Branch.NameKey destBranch,