Merge changes Ibc646fc9,I206f84f3

* changes:
  Use repo from BatchUpdate.Context directly more often
  BatchUpdate: Make repo and RevWalk available to all phases
This commit is contained in:
Dave Borowitz 2015-12-22 16:16:23 +00:00 committed by Gerrit Code Review
commit 141c01a4e9
6 changed files with 208 additions and 38 deletions

View File

@ -84,7 +84,6 @@ public class ChangeInserter extends BatchUpdate.InsertChangeOp {
private final RefControl refControl;
private final IdentifiedUser user;
private final Change change;
private final PatchSet patchSet;
private final RevCommit commit;
@ -101,6 +100,7 @@ public class ChangeInserter extends BatchUpdate.InsertChangeOp {
private boolean updateRef;
// Fields set during the insertion process.
private Change change;
private ChangeMessage changeMessage;
private PatchSetInfo patchSetInfo;
@ -230,9 +230,6 @@ public class ChangeInserter extends BatchUpdate.InsertChangeOp {
public void updateRepo(RepoContext ctx)
throws ResourceConflictException, IOException {
validate(ctx);
patchSetInfo = patchSetInfoFactory.get(
ctx.getRevWalk(), commit, patchSet.getId());
change.setCurrentPatchSet(patchSetInfo);
if (!updateRef) {
return;
}
@ -242,9 +239,14 @@ public class ChangeInserter extends BatchUpdate.InsertChangeOp {
@Override
public void updateChange(ChangeContext ctx) throws OrmException, IOException {
change = ctx.getChange(); // Use defensive copy created by ChangeControl.
ReviewDb db = ctx.getDb();
ChangeControl ctl = ctx.getChangeControl();
ChangeUpdate update = ctx.getChangeUpdate();
patchSetInfo = patchSetInfoFactory.get(
ctx.getRevWalk(), commit, patchSet.getId());
ctx.getChange().setCurrentPatchSet(patchSetInfo);
if (patchSet.getGroups() == null) {
patchSet.setGroups(GroupCollector.getDefaultGroups(patchSet));
}

View File

@ -200,14 +200,13 @@ public class PatchSetInserter extends BatchUpdate.Op {
throws ResourceConflictException, IOException {
init();
validate(ctx);
patchSetInfo = patchSetInfoFactory.get(ctx.getRevWalk(), commit, psId);
ctx.addRefUpdate(new ReceiveCommand(ObjectId.zeroId(),
commit, getPatchSetId().toRefName(), ReceiveCommand.Type.CREATE));
}
@Override
public void updateChange(ChangeContext ctx) throws OrmException,
InvalidChangeOperationException {
InvalidChangeOperationException, IOException {
ChangeControl ctl = ctx.getChangeControl();
change = ctx.getChange();
@ -242,6 +241,7 @@ public class PatchSetInserter extends BatchUpdate.Op {
changeMessage.setMessage(message);
}
patchSetInfo = patchSetInfoFactory.get(ctx.getRevWalk(), commit, psId);
// TODO(dborowitz): Throw ResourceConflictException instead of using
// AtomicUpdate.
change = db.changes().atomicUpdate(id, new AtomicUpdate<Change>() {

View File

@ -42,7 +42,6 @@ import com.google.gerrit.server.change.PublishDraftPatchSet.Input;
import com.google.gerrit.server.git.BatchUpdate;
import com.google.gerrit.server.git.BatchUpdate.ChangeContext;
import com.google.gerrit.server.git.BatchUpdate.Context;
import com.google.gerrit.server.git.BatchUpdate.RepoContext;
import com.google.gerrit.server.git.UpdateException;
import com.google.gerrit.server.mail.CreateChangeSender;
import com.google.gerrit.server.mail.MailUtil.MailRecipients;
@ -155,7 +154,6 @@ public class PublishDraftPatchSet implements RestModifyView<RevisionResource, In
private PatchSet patchSet;
private Change change;
private boolean wasDraftChange;
private RevCommit commit;
private PatchSetInfo patchSetInfo;
private MailRecipients recipients;
@ -164,29 +162,18 @@ public class PublishDraftPatchSet implements RestModifyView<RevisionResource, In
this.patchSet = patchSet;
}
@Override
public void updateRepo(RepoContext ctx)
throws RestApiException, OrmException, IOException {
PatchSet ps = patchSet;
if (ps == null) {
// Don't save in patchSet, since we're not in a transaction. Here we
// just need the revision, which is immutable.
ps = ctx.getDb().patchSets().get(psId);
if (ps == null) {
throw new ResourceNotFoundException(psId.toString());
}
}
commit = ctx.getRevWalk().parseCommit(
ObjectId.fromString(ps.getRevision().get()));
patchSetInfo = patchSetInfoFactory.get(ctx.getRevWalk(), commit, psId);
}
@Override
public void updateChange(ChangeContext ctx)
throws RestApiException, OrmException {
throws RestApiException, OrmException, IOException {
if (!ctx.getChangeControl().canPublish(ctx.getDb())) {
throw new AuthException("Cannot publish this draft patch set");
}
if (patchSet == null) {
patchSet = ctx.getDb().patchSets().get(psId);
if (patchSet == null) {
throw new ResourceNotFoundException(psId.toString());
}
}
saveChange(ctx);
savePatchSet(ctx);
addReviewers(ctx);
@ -204,7 +191,6 @@ public class PublishDraftPatchSet implements RestModifyView<RevisionResource, In
private void savePatchSet(ChangeContext ctx)
throws RestApiException, OrmException {
patchSet = ctx.getDb().patchSets().get(psId);
if (!patchSet.isDraft()) {
throw new ResourceConflictException("Patch set is not a draft");
}
@ -217,10 +203,15 @@ public class PublishDraftPatchSet implements RestModifyView<RevisionResource, In
ctx.getDb().patchSets().update(Collections.singleton(patchSet));
}
private void addReviewers(ChangeContext ctx) throws OrmException {
private void addReviewers(ChangeContext ctx)
throws OrmException, IOException {
LabelTypes labelTypes = ctx.getChangeControl().getLabelTypes();
Collection<Account.Id> oldReviewers = approvalsUtil.getReviewers(
ctx.getDb(), ctx.getChangeNotes()).values();
RevCommit commit = ctx.getRevWalk().parseCommit(
ObjectId.fromString(patchSet.getRevision().get()));
patchSetInfo = patchSetInfoFactory.get(ctx.getRevWalk(), commit, psId);
List<FooterLine> footerLines = commit.getFooterLines();
recipients =
getRecipientsFromFooters(accountResolver, patchSet, footerLines);

View File

@ -144,7 +144,7 @@ public class RebaseChangeOp extends BatchUpdate.Op {
@Override
public void updateChange(ChangeContext ctx)
throws OrmException, InvalidChangeOperationException {
throws OrmException, InvalidChangeOperationException, IOException {
patchSetInserter.updateChange(ctx);
rebasedPatchSet = patchSetInserter.getPatchSet();
}

View File

@ -102,6 +102,19 @@ public class BatchUpdate implements AutoCloseable {
}
public class Context {
private Repository repoWrapper;
public Repository getRepository() throws IOException {
if (repoWrapper == null) {
repoWrapper = new ReadOnlyRepository(BatchUpdate.this.getRepository());
}
return repoWrapper;
}
public RevWalk getRevWalk() throws IOException {
return BatchUpdate.this.getRevWalk();
}
public Project.NameKey getProject() {
return project;
}
@ -124,19 +137,13 @@ public class BatchUpdate implements AutoCloseable {
}
public class RepoContext extends Context {
@Override
public Repository getRepository() throws IOException {
initRepository();
return repo;
}
public RevWalk getRevWalk() throws IOException {
initRepository();
return revWalk;
return BatchUpdate.this.getRepository();
}
public ObjectInserter getInserter() throws IOException {
initRepository();
return inserter;
return BatchUpdate.this.getObjectInserter();
}
public BatchRefUpdate getBatchRefUpdate() throws IOException {

View File

@ -0,0 +1,170 @@
// Copyright (C) 2015 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.gerrit.server.git;
import static com.google.common.base.Preconditions.checkNotNull;
import org.eclipse.jgit.lib.BaseRepositoryBuilder;
import org.eclipse.jgit.lib.ObjectDatabase;
import org.eclipse.jgit.lib.ObjectInserter;
import org.eclipse.jgit.lib.ObjectReader;
import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.lib.RefDatabase;
import org.eclipse.jgit.lib.RefRename;
import org.eclipse.jgit.lib.RefUpdate;
import org.eclipse.jgit.lib.ReflogReader;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.lib.StoredConfig;
import java.io.IOException;
import java.util.List;
import java.util.Map;
class ReadOnlyRepository extends Repository {
private static final String MSG =
"Cannot modify a " + ReadOnlyRepository.class.getSimpleName();
private static BaseRepositoryBuilder<?, ?> builder(Repository r) {
checkNotNull(r);
return new BaseRepositoryBuilder<>()
.setFS(r.getFS())
.setGitDir(r.getDirectory())
.setWorkTree(r.getWorkTree())
.setIndexFile(r.getIndexFile());
}
private final Repository delegate;
private final RefDb refdb;
private final ObjDb objdb;
ReadOnlyRepository(Repository delegate) {
super(builder(delegate));
this.delegate = delegate;
this.refdb = new RefDb(delegate.getRefDatabase());
this.objdb = new ObjDb(delegate.getObjectDatabase());
}
@Override
public void create(boolean bare) throws IOException {
throw new UnsupportedOperationException(MSG);
}
@Override
public ObjectDatabase getObjectDatabase() {
return objdb;
}
@Override
public RefDatabase getRefDatabase() {
return refdb;
}
@Override
public StoredConfig getConfig() {
return delegate.getConfig();
}
@Override
public void scanForRepoChanges() throws IOException {
delegate.scanForRepoChanges();
}
@Override
public void notifyIndexChanged() {
delegate.notifyIndexChanged();
}
@Override
public ReflogReader getReflogReader(String refName) throws IOException {
return delegate.getReflogReader(refName);
}
private static class RefDb extends RefDatabase {
private final RefDatabase delegate;
private RefDb(RefDatabase delegate) {
this.delegate = checkNotNull(delegate);
}
@Override
public void create() throws IOException {
throw new UnsupportedOperationException(MSG);
}
@Override
public void close() {
delegate.close();
}
@Override
public boolean isNameConflicting(String name) throws IOException {
return delegate.isNameConflicting(name);
}
@Override
public RefUpdate newUpdate(String name, boolean detach) throws IOException {
throw new UnsupportedOperationException(MSG);
}
@Override
public RefRename newRename(String fromName, String toName)
throws IOException {
throw new UnsupportedOperationException(MSG);
}
@Override
public Ref getRef(String name) throws IOException {
return delegate.getRef(name);
}
@Override
public Map<String, Ref> getRefs(String prefix) throws IOException {
return delegate.getRefs(prefix);
}
@Override
public List<Ref> getAdditionalRefs() throws IOException {
return delegate.getAdditionalRefs();
}
@Override
public Ref peel(Ref ref) throws IOException {
return delegate.peel(ref);
}
}
private static class ObjDb extends ObjectDatabase {
private final ObjectDatabase delegate;
private ObjDb(ObjectDatabase delegate) {
this.delegate = checkNotNull(delegate);
}
@Override
public ObjectInserter newInserter() {
throw new UnsupportedOperationException(MSG);
}
@Override
public ObjectReader newReader() {
return delegate.newReader();
}
@Override
public void close() {
delegate.close();
}
}
}