InlineEdit: Extend change edit container with commit and patch set
Change-Id: Ic75adf1f6d98e052f1f06ea28644e17f081cb8f8
This commit is contained in:
@@ -17,11 +17,13 @@ package com.google.gerrit.server.edit;
|
||||
import static com.google.common.base.Preconditions.checkNotNull;
|
||||
|
||||
import com.google.gerrit.reviewdb.client.Change;
|
||||
import com.google.gerrit.reviewdb.client.PatchSet;
|
||||
import com.google.gerrit.reviewdb.client.RevId;
|
||||
import com.google.gerrit.server.IdentifiedUser;
|
||||
|
||||
import org.eclipse.jgit.lib.ObjectId;
|
||||
import org.eclipse.jgit.lib.Ref;
|
||||
import org.eclipse.jgit.revwalk.RevCommit;
|
||||
|
||||
/**
|
||||
* A single user's edit for a change.
|
||||
@@ -34,14 +36,21 @@ public class ChangeEdit {
|
||||
private final IdentifiedUser user;
|
||||
private final Change change;
|
||||
private final Ref ref;
|
||||
private final RevCommit editCommit;
|
||||
private final PatchSet basePatchSet;
|
||||
|
||||
public ChangeEdit(IdentifiedUser user, Change change, Ref ref) {
|
||||
public ChangeEdit(IdentifiedUser user, Change change, Ref ref,
|
||||
RevCommit editCommit, PatchSet basePatchSet) {
|
||||
checkNotNull(user);
|
||||
checkNotNull(change);
|
||||
checkNotNull(ref);
|
||||
checkNotNull(editCommit);
|
||||
checkNotNull(basePatchSet);
|
||||
this.user = user;
|
||||
this.change = change;
|
||||
this.ref = ref;
|
||||
this.editCommit = editCommit;
|
||||
this.basePatchSet = basePatchSet;
|
||||
}
|
||||
|
||||
public Change getChange() {
|
||||
@@ -63,4 +72,12 @@ public class ChangeEdit {
|
||||
public String getRefName() {
|
||||
return ChangeEditUtil.editRefName(user.getAccountId(), change.getId());
|
||||
}
|
||||
|
||||
public RevCommit getEditCommit() {
|
||||
return editCommit;
|
||||
}
|
||||
|
||||
public PatchSet getBasePatchSet() {
|
||||
return basePatchSet;
|
||||
}
|
||||
}
|
||||
|
@@ -72,17 +72,14 @@ public class ChangeEditModifier {
|
||||
private final TimeZone tz;
|
||||
private final GitRepositoryManager gitManager;
|
||||
private final Provider<CurrentUser> currentUser;
|
||||
private final ChangeEditUtil editUtil;
|
||||
|
||||
@Inject
|
||||
ChangeEditModifier(@GerritPersonIdent PersonIdent gerritIdent,
|
||||
GitRepositoryManager gitManager,
|
||||
Provider<ReviewDb> dbProvider,
|
||||
Provider<CurrentUser> currentUser,
|
||||
ChangeEditUtil editUtil) {
|
||||
Provider<CurrentUser> currentUser) {
|
||||
this.gitManager = gitManager;
|
||||
this.currentUser = currentUser;
|
||||
this.editUtil = editUtil;
|
||||
this.tz = gerritIdent.getTimeZone();
|
||||
}
|
||||
|
||||
@@ -193,7 +190,7 @@ public class ChangeEditModifier {
|
||||
try {
|
||||
String refName = edit.getRefName();
|
||||
RevCommit prevEdit = rw.parseCommit(edit.getRef().getObjectId());
|
||||
PatchSet basePs = editUtil.getBasePatchSet(edit, prevEdit);
|
||||
PatchSet basePs = edit.getBasePatchSet();
|
||||
|
||||
RevCommit base = rw.parseCommit(ObjectId.fromString(
|
||||
basePs.getRevision().get()));
|
||||
|
@@ -86,19 +86,26 @@ public class ChangeEditUtil {
|
||||
* @throws IOException
|
||||
*/
|
||||
public Optional<ChangeEdit> byChange(Change change)
|
||||
throws AuthException, IOException {
|
||||
throws AuthException, IOException, InvalidChangeOperationException {
|
||||
if (!user.get().isIdentifiedUser()) {
|
||||
throw new AuthException("Authentication required");
|
||||
}
|
||||
Repository repo = gitManager.openRepository(change.getProject());
|
||||
try {
|
||||
IdentifiedUser identifiedUser = (IdentifiedUser) user.get();
|
||||
IdentifiedUser me = (IdentifiedUser) user.get();
|
||||
Ref ref = repo.getRefDatabase().getRef(editRefName(
|
||||
identifiedUser.getAccountId(), change.getId()));
|
||||
me.getAccountId(), change.getId()));
|
||||
if (ref == null) {
|
||||
return Optional.absent();
|
||||
}
|
||||
return Optional.of(new ChangeEdit(identifiedUser, change, ref));
|
||||
RevWalk rw = new RevWalk(repo);
|
||||
try {
|
||||
RevCommit commit = rw.parseCommit(ref.getObjectId());
|
||||
PatchSet basePs = getBasePatchSet(change, commit);
|
||||
return Optional.of(new ChangeEdit(me, change, ref, commit, basePs));
|
||||
} finally {
|
||||
rw.release();
|
||||
}
|
||||
} finally {
|
||||
repo.close();
|
||||
}
|
||||
@@ -125,19 +132,16 @@ public class ChangeEditUtil {
|
||||
RevWalk rw = new RevWalk(repo);
|
||||
ObjectInserter inserter = repo.newObjectInserter();
|
||||
try {
|
||||
RevCommit editCommit = rw.parseCommit(edit.getRef().getObjectId());
|
||||
if (editCommit == null) {
|
||||
throw new NoSuchChangeException(change.getId());
|
||||
}
|
||||
|
||||
PatchSet basePatchSet = getBasePatchSet(edit, editCommit);
|
||||
PatchSet basePatchSet = edit.getBasePatchSet();
|
||||
if (!basePatchSet.getId().equals(change.currentPatchSetId())) {
|
||||
throw new ResourceConflictException(
|
||||
"only edit for current patch set can be published");
|
||||
}
|
||||
|
||||
insertPatchSet(edit, change, repo, rw, basePatchSet,
|
||||
squashEdit(repo, rw, inserter, editCommit, basePatchSet));
|
||||
squashEdit(repo, rw, inserter, edit.getEditCommit(),
|
||||
basePatchSet));
|
||||
} finally {
|
||||
inserter.release();
|
||||
rw.release();
|
||||
@@ -167,41 +171,7 @@ public class ChangeEditUtil {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve base patch set the edit was created on.
|
||||
*
|
||||
* @param edit change edit to retrieve base patch set for
|
||||
* @return parent patch set of the edit
|
||||
* @throws IOException
|
||||
* @throws InvalidChangeOperationException
|
||||
*/
|
||||
public PatchSet getBasePatchSet(ChangeEdit edit) throws IOException,
|
||||
InvalidChangeOperationException {
|
||||
Change change = edit.getChange();
|
||||
Repository repo = gitManager.openRepository(change.getProject());
|
||||
try {
|
||||
RevWalk rw = new RevWalk(repo);
|
||||
try {
|
||||
return getBasePatchSet(edit, rw.parseCommit(
|
||||
edit.getRef().getObjectId()));
|
||||
} finally {
|
||||
rw.release();
|
||||
}
|
||||
} finally {
|
||||
repo.close();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve base patch set the edit was created on.
|
||||
*
|
||||
* @param edit change edit to retrieve base patch set for
|
||||
* @param commit change edit commit
|
||||
* @return parent patch set of the edit
|
||||
* @throws IOException
|
||||
* @throws InvalidChangeOperationException
|
||||
*/
|
||||
public PatchSet getBasePatchSet(ChangeEdit edit, RevCommit commit)
|
||||
private PatchSet getBasePatchSet(Change change, RevCommit commit)
|
||||
throws IOException, InvalidChangeOperationException {
|
||||
if (commit.getParentCount() != 1) {
|
||||
throw new InvalidChangeOperationException(
|
||||
@@ -223,11 +193,11 @@ public class ChangeEditUtil {
|
||||
rev.abbreviate(8).name()));
|
||||
}
|
||||
PatchSet parentPatchSet = Iterables.getOnlyElement(r);
|
||||
if (!edit.getChange().getId().equals(
|
||||
if (!change.getId().equals(
|
||||
parentPatchSet.getId().getParentKey())) {
|
||||
throw new InvalidChangeOperationException(String.format(
|
||||
"different change edit ID %d and its parent patch set %d",
|
||||
edit.getChange().getId().get(),
|
||||
change.getId().get(),
|
||||
parentPatchSet.getId().getParentKey().get()));
|
||||
}
|
||||
return parentPatchSet;
|
||||
|
Reference in New Issue
Block a user