Create change ref from ChangeInserter

Reduce some duplicated code. However, there are still a few special
snowflakes, so leave a flag to turn this off:
 - ReceiveCommits manages its own BatchRefUpdate. (It also doesn't
   even use PatchSetInserter for the new patch set case.)
 - ReviewProjectAccess updates the ref using VersionedMetaData, which
   I'm not going to touch for now.

Change-Id: I6f4f9fb8a6364627a737698d1001ad29ce8a981a
This commit is contained in:
Dave Borowitz
2015-10-08 13:26:37 -04:00
parent fd22122b25
commit 9f22f40286
6 changed files with 37 additions and 63 deletions

View File

@@ -124,10 +124,10 @@ public class ReviewProjectAccess extends ProjectAccessHandler<Change.Id> {
RefNames.REFS_CONFIG), RefNames.REFS_CONFIG),
TimeUtil.nowTs()); TimeUtil.nowTs());
try (RevWalk rw = new RevWalk(md.getRepository())) { try (RevWalk rw = new RevWalk(md.getRepository())) {
ChangeInserter ins = changeInserterFactory.create( changeInserterFactory.create(md.getRepository(), rw, ctl, change, commit)
md.getRepository(), rw, ctl, change, commit) .setValidatePolicy(CommitValidators.Policy.NONE)
.setValidatePolicy(CommitValidators.Policy.NONE); .setUpdateRef(false) // Created by commitToNewRef.
ins.insert(); .insert();
} catch (InvalidChangeOperationException e) { } catch (InvalidChangeOperationException e) {
throw new IOException(e); throw new IOException(e);
} }

View File

@@ -276,26 +276,13 @@ public class ChangeUtil {
ChangeInserter ins = changeInserterFactory.create( ChangeInserter ins = changeInserterFactory.create(
git, revWalk, refControl.getProjectControl(), change, revertCommit) git, revWalk, refControl.getProjectControl(), change, revertCommit)
.setValidatePolicy(CommitValidators.Policy.GERRIT); .setValidatePolicy(CommitValidators.Policy.GERRIT);
ins.validate();
PatchSet ps = ins.getPatchSet();
RefUpdate ru = git.updateRef(ps.getRefName());
ru.setExpectedOldObjectId(ObjectId.zeroId());
ru.setNewObjectId(revertCommit);
ru.disableRefLog();
if (ru.update(revWalk) != RefUpdate.Result.NEW) {
throw new IOException(String.format(
"Failed to create ref %s in %s: %s", ps.getRefName(),
change.getDest().getParentKey().get(), ru.getResult()));
}
StringBuilder msgBuf = new StringBuilder(); StringBuilder msgBuf = new StringBuilder();
msgBuf.append("Patch Set ").append(patchSetId.get()).append(": Reverted"); msgBuf.append("Patch Set ").append(patchSetId.get()).append(": Reverted");
msgBuf.append("\n\n"); msgBuf.append("\n\n");
msgBuf.append("This patchset was reverted in change: ") msgBuf.append("This patchset was reverted in change: ")
.append(change.getKey().get()); .append(change.getKey().get());
ins.setMessage(msgBuf.toString())
ins.setMessage(msgBuf.toString()).insert(); .insert();
try { try {
RevertedSender cm = revertedSenderFactory.create(change.getId()); RevertedSender cm = revertedSenderFactory.create(change.getId());

View File

@@ -59,6 +59,7 @@ import com.google.inject.Provider;
import com.google.inject.assistedinject.Assisted; import com.google.inject.assistedinject.Assisted;
import org.eclipse.jgit.lib.ObjectId; import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.RefUpdate;
import org.eclipse.jgit.lib.Repository; import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.notes.NoteMap; import org.eclipse.jgit.notes.NoteMap;
import org.eclipse.jgit.revwalk.RevCommit; import org.eclipse.jgit.revwalk.RevCommit;
@@ -114,10 +115,10 @@ public class ChangeInserter {
private RequestScopePropagator requestScopePropagator; private RequestScopePropagator requestScopePropagator;
private boolean runHooks; private boolean runHooks;
private boolean sendMail; private boolean sendMail;
private boolean updateRef;
// Fields set during the insertion process. // Fields set during the insertion process.
private ChangeMessage changeMessage; private ChangeMessage changeMessage;
private boolean validated; // TODO(dborowitz): Remove; see validate().
@Inject @Inject
ChangeInserter(Provider<ReviewDb> dbProvider, ChangeInserter(Provider<ReviewDb> dbProvider,
@@ -162,6 +163,7 @@ public class ChangeInserter {
this.hashtags = Collections.emptySet(); this.hashtags = Collections.emptySet();
this.runHooks = true; this.runHooks = true;
this.sendMail = true; this.sendMail = true;
this.updateRef = true;
user = checkUser(projectControl); user = checkUser(projectControl);
patchSet = patchSet =
@@ -243,6 +245,11 @@ public class ChangeInserter {
return this; return this;
} }
public ChangeInserter setUpdateRef(boolean updateRef) {
this.updateRef = updateRef;
return this;
}
public PatchSetInfo getPatchSetInfo() { public PatchSetInfo getPatchSetInfo() {
return patchSetInfo; return patchSetInfo;
} }
@@ -260,6 +267,8 @@ public class ChangeInserter {
throws OrmException, IOException, InvalidChangeOperationException { throws OrmException, IOException, InvalidChangeOperationException {
validate(); validate();
updateRef();
ReviewDb db = dbProvider.get(); ReviewDb db = dbProvider.get();
ProjectControl projectControl = refControl.getProjectControl(); ProjectControl projectControl = refControl.getProjectControl();
ChangeControl ctl = projectControl.controlFor(change); ChangeControl ctl = projectControl.controlFor(change);
@@ -350,11 +359,23 @@ public class ChangeInserter {
return change; return change;
} }
// TODO(dborowitz): This is only public because callers expect validation to private void updateRef() throws IOException {
// happen before updating any refs, and they are still updating refs manually. if (!updateRef) {
// Make private once we have migrated ref updates into this class. return;
public void validate() throws IOException, InvalidChangeOperationException { }
if (validated || validatePolicy == CommitValidators.Policy.NONE) { RefUpdate ru = git.updateRef(patchSet.getRefName());
ru.setExpectedOldObjectId(ObjectId.zeroId());
ru.setNewObjectId(commit);
ru.disableRefLog();
if (ru.update(revWalk) != RefUpdate.Result.NEW) {
throw new IOException(String.format(
"Failed to create ref %s in %s: %s", ru.getRef().getName(),
change.getDest().getParentKey().get(), ru.getResult()));
}
}
private void validate() throws IOException, InvalidChangeOperationException {
if (validatePolicy == CommitValidators.Policy.NONE) {
return; return;
} }
CommitValidators cv = CommitValidators cv =
@@ -386,6 +407,5 @@ public class ChangeInserter {
} catch (CommitValidationException e) { } catch (CommitValidationException e) {
throw new InvalidChangeOperationException(e.getMessage()); throw new InvalidChangeOperationException(e.getMessage());
} }
validated = true;
} }
} }

View File

@@ -60,7 +60,6 @@ import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.ObjectInserter; import org.eclipse.jgit.lib.ObjectInserter;
import org.eclipse.jgit.lib.PersonIdent; import org.eclipse.jgit.lib.PersonIdent;
import org.eclipse.jgit.lib.Ref; import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.lib.RefUpdate;
import org.eclipse.jgit.lib.Repository; import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevWalk; import org.eclipse.jgit.revwalk.RevWalk;
import org.eclipse.jgit.util.ChangeIdUtil; import org.eclipse.jgit.util.ChangeIdUtil;
@@ -241,24 +240,9 @@ public class CherryPickChange {
git, revWalk, refControl.getProjectControl(), change, git, revWalk, refControl.getProjectControl(), change,
cherryPickCommit) cherryPickCommit)
.setValidatePolicy(CommitValidators.Policy.GERRIT); .setValidatePolicy(CommitValidators.Policy.GERRIT);
ins.setMessage( return ins.setMessage(
messageForDestinationChange(ins.getPatchSet().getId(), sourceBranch)); messageForDestinationChange(ins.getPatchSet().getId(), sourceBranch))
ins.validate(); .insert();
PatchSet newPatchSet = ins.getPatchSet();
final RefUpdate ru = git.updateRef(newPatchSet.getRefName());
ru.setExpectedOldObjectId(ObjectId.zeroId());
ru.setNewObjectId(cherryPickCommit);
ru.disableRefLog();
if (ru.update(revWalk) != RefUpdate.Result.NEW) {
throw new IOException(String.format(
"Failed to create ref %s in %s: %s", newPatchSet.getRefName(),
change.getDest().getParentKey().get(), ru.getResult()));
}
ins.insert();
return change;
} }
private void addMessageToSourceChange(Change change, PatchSet.Id patchSetId, private void addMessageToSourceChange(Change change, PatchSet.Id patchSetId,

View File

@@ -58,7 +58,6 @@ import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.ObjectInserter; import org.eclipse.jgit.lib.ObjectInserter;
import org.eclipse.jgit.lib.PersonIdent; import org.eclipse.jgit.lib.PersonIdent;
import org.eclipse.jgit.lib.Ref; import org.eclipse.jgit.lib.Ref;
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;
@@ -201,10 +200,6 @@ public class CreateChange implements
.setValidatePolicy(CommitValidators.Policy.GERRIT); .setValidatePolicy(CommitValidators.Policy.GERRIT);
ins.setMessage(String.format("Uploaded patch set %s.", ins.setMessage(String.format("Uploaded patch set %s.",
ins.getPatchSet().getPatchSetId())); ins.getPatchSet().getPatchSetId()));
ins.validate();
updateRef(git, rw, c, change, ins.getPatchSet());
String topic = input.topic; String topic = input.topic;
if (topic != null) { if (topic != null) {
topic = Strings.emptyToNull(topic.trim()); topic = Strings.emptyToNull(topic.trim());
@@ -219,19 +214,6 @@ public class CreateChange implements
} }
} }
private static void updateRef(Repository git, RevWalk rw, RevCommit c,
Change change, PatchSet newPatchSet) throws IOException {
RefUpdate ru = git.updateRef(newPatchSet.getRefName());
ru.setExpectedOldObjectId(ObjectId.zeroId());
ru.setNewObjectId(c);
ru.disableRefLog();
if (ru.update(rw) != RefUpdate.Result.NEW) {
throw new IOException(String.format(
"Failed to create ref %s in %s: %s", newPatchSet.getRefName(),
change.getDest().getParentKey().get(), ru.getResult()));
}
}
private static Change.Key getChangeId(ObjectId id, RevCommit emptyCommit) { private static Change.Key getChangeId(ObjectId id, RevCommit emptyCommit) {
List<String> idList = emptyCommit.getFooterLines( List<String> idList = emptyCommit.getFooterLines(
FooterConstants.CHANGE_ID); FooterConstants.CHANGE_ID);

View File

@@ -1766,6 +1766,7 @@ public class ReceiveCommits {
.setMessage("Uploaded patch set " + ps.getPatchSetId() + ".") .setMessage("Uploaded patch set " + ps.getPatchSetId() + ".")
.setRequestScopePropagator(requestScopePropagator) .setRequestScopePropagator(requestScopePropagator)
.setSendMail(true) .setSendMail(true)
.setUpdateRef(false)
.insert(); .insert();
created = true; created = true;