RepoContext: Add convenience method for addRefUpdate

Most callers are immediately constructing a ReceiveCommand, so this
variant reduces boilerplate. A few callers want to maintain a reference
to the ReceiveCommand, so keep the other variant around.

Change-Id: Ia6cb0f7fc63eba380a225c671def41db3caffc44
This commit is contained in:
Dave Borowitz
2017-04-12 09:32:55 -04:00
parent 76513b6723
commit c9377e78ee
14 changed files with 37 additions and 37 deletions

View File

@@ -67,7 +67,6 @@ import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.PersonIdent; import org.eclipse.jgit.lib.PersonIdent;
import org.eclipse.jgit.lib.RefUpdate; import org.eclipse.jgit.lib.RefUpdate;
import org.eclipse.jgit.revwalk.RevCommit; import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.transport.ReceiveCommand;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
@@ -919,7 +918,7 @@ public class ConsistencyCheckerIT extends AbstractDaemonTest {
new BatchUpdateOp() { new BatchUpdateOp() {
@Override @Override
public void updateRepo(RepoContext ctx) throws IOException { public void updateRepo(RepoContext ctx) throws IOException {
ctx.addRefUpdate(new ReceiveCommand(oldId, newId, dest)); ctx.addRefUpdate(oldId, newId, dest);
} }
@Override @Override

View File

@@ -40,7 +40,6 @@ import java.util.Optional;
import org.eclipse.jgit.lib.ObjectId; import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.Ref; import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.lib.Repository; import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.transport.ReceiveCommand;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
@@ -71,7 +70,7 @@ public class NoteDbOnlyIT extends AbstractDaemonTest {
public void updateRepo(RepoContext ctx) throws IOException { public void updateRepo(RepoContext ctx) throws IOException {
ObjectId oldId = ctx.getRepoView().getRef(backup).orElse(ObjectId.zeroId()); ObjectId oldId = ctx.getRepoView().getRef(backup).orElse(ObjectId.zeroId());
newId = ctx.getRepoView().getRef(master).get(); newId = ctx.getRepoView().getRef(master).get();
ctx.addRefUpdate(new ReceiveCommand(oldId, newId, backup)); ctx.addRefUpdate(oldId, newId, backup);
} }
@Override @Override

View File

@@ -341,7 +341,7 @@ public class ChangeInserter implements InsertChangeOp {
return; return;
} }
if (updateRefCommand == null) { if (updateRefCommand == null) {
ctx.addRefUpdate(new ReceiveCommand(ObjectId.zeroId(), commitId, psId.toRefName())); ctx.addRefUpdate(ObjectId.zeroId(), commitId, psId.toRefName());
} else { } else {
ctx.addRefUpdate(updateRefCommand); ctx.addRefUpdate(updateRefCommand);
} }

View File

@@ -76,7 +76,6 @@ import org.eclipse.jgit.lib.RefUpdate;
import org.eclipse.jgit.lib.Repository; import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevCommit; import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.revwalk.RevWalk; import org.eclipse.jgit.revwalk.RevWalk;
import org.eclipse.jgit.transport.ReceiveCommand;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@@ -504,8 +503,7 @@ public class ConsistencyChecker {
new BatchUpdateOp() { new BatchUpdateOp() {
@Override @Override
public void updateRepo(RepoContext ctx) throws IOException { public void updateRepo(RepoContext ctx) throws IOException {
ctx.addRefUpdate( ctx.addRefUpdate(commit, ObjectId.zeroId(), psIdToDelete.toRefName());
new ReceiveCommand(commit, ObjectId.zeroId(), psIdToDelete.toRefName()));
} }
}); });
if (!reuseOldPsId) { if (!reuseOldPsId) {

View File

@@ -43,7 +43,6 @@ import java.util.Optional;
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.revwalk.RevWalk; import org.eclipse.jgit.revwalk.RevWalk;
import org.eclipse.jgit.transport.ReceiveCommand;
class DeleteChangeOp implements BatchUpdateOp { class DeleteChangeOp implements BatchUpdateOp {
static boolean allowDrafts(Config cfg) { static boolean allowDrafts(Config cfg) {
@@ -173,7 +172,7 @@ class DeleteChangeOp implements BatchUpdateOp {
String prefix = new PatchSet.Id(id, 1).toRefName(); String prefix = new PatchSet.Id(id, 1).toRefName();
prefix = prefix.substring(0, prefix.length() - 1); prefix = prefix.substring(0, prefix.length() - 1);
for (Map.Entry<String, ObjectId> e : ctx.getRepoView().getRefs(prefix).entrySet()) { for (Map.Entry<String, ObjectId> e : ctx.getRepoView().getRefs(prefix).entrySet()) {
ctx.addRefUpdate(new ReceiveCommand(e.getValue(), ObjectId.zeroId(), prefix + e.getKey())); ctx.addRefUpdate(e.getValue(), ObjectId.zeroId(), prefix + e.getKey());
} }
} }
} }

View File

@@ -47,7 +47,6 @@ import java.io.IOException;
import java.util.Collection; import java.util.Collection;
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.transport.ReceiveCommand;
@Singleton @Singleton
public class DeleteDraftPatchSet public class DeleteDraftPatchSet
@@ -133,10 +132,9 @@ public class DeleteDraftPatchSet
return; return;
} }
ctx.addRefUpdate( ctx.addRefUpdate(
new ReceiveCommand(
ObjectId.fromString(patchSet.getRevision().get()), ObjectId.fromString(patchSet.getRevision().get()),
ObjectId.zeroId(), ObjectId.zeroId(),
patchSet.getRefName())); patchSet.getRefName());
} }
private void deleteDraftPatchSet(PatchSet patchSet, ChangeContext ctx) throws OrmException { private void deleteDraftPatchSet(PatchSet patchSet, ChangeContext ctx) throws OrmException {

View File

@@ -207,9 +207,7 @@ public class PatchSetInserter implements BatchUpdateOp {
public void updateRepo(RepoContext ctx) public void updateRepo(RepoContext ctx)
throws AuthException, ResourceConflictException, IOException, OrmException { throws AuthException, ResourceConflictException, IOException, OrmException {
validate(ctx); validate(ctx);
ctx.addRefUpdate( ctx.addRefUpdate(ObjectId.zeroId(), commitId, getPatchSetId().toRefName());
new ReceiveCommand(
ObjectId.zeroId(), commitId, getPatchSetId().toRefName(), ReceiveCommand.Type.CREATE));
} }
@Override @Override

View File

@@ -59,7 +59,6 @@ import org.eclipse.jgit.lib.RefUpdate;
import org.eclipse.jgit.lib.Repository; import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevCommit; import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.revwalk.RevWalk; import org.eclipse.jgit.revwalk.RevWalk;
import org.eclipse.jgit.transport.ReceiveCommand;
/** /**
* Utility functions to manipulate change edits. * Utility functions to manipulate change edits.
@@ -223,9 +222,7 @@ public class ChangeEditUtil {
new BatchUpdateOp() { new BatchUpdateOp() {
@Override @Override
public void updateRepo(RepoContext ctx) throws Exception { public void updateRepo(RepoContext ctx) throws Exception {
ctx.addRefUpdate( ctx.addRefUpdate(edit.getEditCommit().copy(), ObjectId.zeroId(), edit.getRefName());
new ReceiveCommand(
edit.getEditCommit().copy(), ObjectId.zeroId(), edit.getRefName()));
} }
}); });
bu.execute(); bu.execute();

View File

@@ -71,7 +71,6 @@ import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.revwalk.RevCommit; import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.revwalk.RevWalk; import org.eclipse.jgit.revwalk.RevWalk;
import org.eclipse.jgit.transport.PushCertificate; import org.eclipse.jgit.transport.PushCertificate;
import org.eclipse.jgit.transport.ReceiveCommand;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@@ -211,7 +210,7 @@ public class ReplaceOp implements BatchUpdateOp {
} }
if (updateRef) { if (updateRef) {
ctx.addRefUpdate(new ReceiveCommand(ObjectId.zeroId(), commitId, patchSetId.toRefName())); ctx.addRefUpdate(ObjectId.zeroId(), commitId, patchSetId.toRefName());
} }
} }

View File

@@ -63,7 +63,6 @@ import org.eclipse.jgit.lib.PersonIdent;
import org.eclipse.jgit.lib.Ref; import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.revwalk.RevCommit; import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.revwalk.RevWalk; import org.eclipse.jgit.revwalk.RevWalk;
import org.eclipse.jgit.transport.ReceiveCommand;
import org.eclipse.jgit.transport.RefSpec; import org.eclipse.jgit.transport.RefSpec;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@@ -82,7 +81,7 @@ public class SubmoduleOp {
public void updateRepo(RepoContext ctx) throws Exception { public void updateRepo(RepoContext ctx) throws Exception {
CodeReviewCommit c = composeGitlinksCommit(branch); CodeReviewCommit c = composeGitlinksCommit(branch);
if (c != null) { if (c != null) {
ctx.addRefUpdate(new ReceiveCommand(c.getParent(0), c, branch.get())); ctx.addRefUpdate(c.getParent(0), c, branch.get());
addBranchTip(branch, c); addBranchTip(branch, c);
} }
} }

View File

@@ -37,7 +37,6 @@ import java.util.List;
import org.eclipse.jgit.lib.ObjectId; import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.PersonIdent; import org.eclipse.jgit.lib.PersonIdent;
import org.eclipse.jgit.revwalk.RevCommit; import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.transport.ReceiveCommand;
public class CherryPick extends SubmitStrategy { public class CherryPick extends SubmitStrategy {
@@ -135,7 +134,7 @@ public class CherryPick extends SubmitStrategy {
args.mergeTip.moveTipTo(newCommit, newCommit); args.mergeTip.moveTipTo(newCommit, newCommit);
args.commitStatus.put(newCommit); args.commitStatus.put(newCommit);
ctx.addRefUpdate(new ReceiveCommand(ObjectId.zeroId(), newCommit, psId.toRefName())); ctx.addRefUpdate(ObjectId.zeroId(), newCommit, psId.toRefName());
patchSetInfo = args.patchSetInfoFactory.get(ctx.getRevWalk(), newCommit, psId); patchSetInfo = args.patchSetInfoFactory.get(ctx.getRevWalk(), newCommit, psId);
} }

View File

@@ -43,7 +43,6 @@ import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.PersonIdent; import org.eclipse.jgit.lib.PersonIdent;
import org.eclipse.jgit.lib.Repository; import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevCommit; import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.transport.ReceiveCommand;
/** This strategy covers RebaseAlways and RebaseIfNecessary ones. */ /** This strategy covers RebaseAlways and RebaseIfNecessary ones. */
public class RebaseSubmitStrategy extends SubmitStrategy { public class RebaseSubmitStrategy extends SubmitStrategy {
@@ -156,8 +155,7 @@ public class RebaseSubmitStrategy extends SubmitStrategy {
toMerge.setStatusCode(SKIPPED_IDENTICAL_TREE); toMerge.setStatusCode(SKIPPED_IDENTICAL_TREE);
return; return;
} }
ctx.addRefUpdate( ctx.addRefUpdate(ObjectId.zeroId(), newCommit, newPatchSetId.toRefName());
new ReceiveCommand(ObjectId.zeroId(), newCommit, newPatchSetId.toRefName()));
} else { } else {
// Stale read of patch set is ok; see comments in RebaseChangeOp. // Stale read of patch set is ok; see comments in RebaseChangeOp.
PatchSet origPs = PatchSet origPs =

View File

@@ -14,10 +14,12 @@
package com.google.gerrit.server.update; package com.google.gerrit.server.update;
import java.io.IOException; import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.ObjectInserter; import org.eclipse.jgit.lib.ObjectInserter;
import org.eclipse.jgit.transport.ReceiveCommand; import org.eclipse.jgit.transport.ReceiveCommand;
import java.io.IOException;
/** Context for performing the {@link BatchUpdateOp#updateRepo} phase. */ /** Context for performing the {@link BatchUpdateOp#updateRepo} phase. */
public interface RepoContext extends Context { public interface RepoContext extends Context {
/** /**
@@ -31,10 +33,28 @@ public interface RepoContext extends Context {
/** /**
* Add a command to the pending list of commands. * Add a command to the pending list of commands.
* *
* <p>This method is the only way of updating refs in the repository from a {@link BatchUpdateOp}. * <p>Adding commands to the {@code RepoContext} is the only way of updating refs in the
* repository from a {@link BatchUpdateOp}.
* *
* @param cmd ref update command. * @param cmd ref update command.
* @throws IOException if an error occurred opening the repo. * @throws IOException if an error occurred opening the repo.
*/ */
void addRefUpdate(ReceiveCommand cmd) throws IOException; void addRefUpdate(ReceiveCommand cmd) throws IOException;
/**
* Add a command to the pending list of commands.
*
* <p>Adding commands to the {@code RepoContext} is the only way of updating refs in the
* repository from a {@link BatchUpdateOp}.
*
* @param oldId the old object ID; must not be null. Use {@link ObjectId#zeroId()} for ref
* creation.
* @param newId the new object ID; must not be null. Use {@link ObjectId#zeroId()} for ref
* deletion.
* @param refName the ref name.
* @throws IOException if an error occurred opening the repo.
*/
default void addRefUpdate(ObjectId oldId, ObjectId newId, String refName) throws IOException {
addRefUpdate(new ReceiveCommand(oldId, newId, refName));
}
} }

View File

@@ -39,7 +39,6 @@ import com.google.inject.util.Providers;
import org.eclipse.jgit.internal.storage.dfs.InMemoryRepository; import org.eclipse.jgit.internal.storage.dfs.InMemoryRepository;
import org.eclipse.jgit.junit.TestRepository; import org.eclipse.jgit.junit.TestRepository;
import org.eclipse.jgit.revwalk.RevCommit; import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.transport.ReceiveCommand;
import org.junit.After; import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
@@ -122,9 +121,7 @@ public class BatchUpdateTest {
new RepoOnlyOp() { new RepoOnlyOp() {
@Override @Override
public void updateRepo(RepoContext ctx) throws Exception { public void updateRepo(RepoContext ctx) throws Exception {
ctx.addRefUpdate( ctx.addRefUpdate(masterCommit.getId(), branchCommit.getId(), "refs/heads/master");
new ReceiveCommand(
masterCommit.getId(), branchCommit.getId(), "refs/heads/master"));
} }
}); });
bu.execute(); bu.execute();