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