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

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