AbstractChangeNotes: Don't extend VersionedMetaData
Similar to ChangeUpdate in Ib163d209, most of the VersionedMetaData methods weren't actually being used, and using them accidentally would lead to unexpected results. Copy the few methods we do need into AbstractChangeNotes. This is not a complete rethinking of the AbstractChangeNotes API, just a minimal code change. But it does open up that possibility later if we want it. Change-Id: I1e56a80cc8daa90fa7ac0dc985d31f22123c0a55
This commit is contained in:
@@ -17,22 +17,23 @@ package com.google.gerrit.server.notedb;
|
|||||||
import com.google.gerrit.reviewdb.client.Change;
|
import com.google.gerrit.reviewdb.client.Change;
|
||||||
import com.google.gerrit.reviewdb.client.Project;
|
import com.google.gerrit.reviewdb.client.Project;
|
||||||
import com.google.gerrit.server.git.GitRepositoryManager;
|
import com.google.gerrit.server.git.GitRepositoryManager;
|
||||||
import com.google.gerrit.server.git.VersionedMetaData;
|
|
||||||
import com.google.gwtorm.server.OrmException;
|
import com.google.gwtorm.server.OrmException;
|
||||||
|
|
||||||
import org.eclipse.jgit.errors.ConfigInvalidException;
|
import org.eclipse.jgit.errors.ConfigInvalidException;
|
||||||
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.lib.Repository;
|
import org.eclipse.jgit.lib.Repository;
|
||||||
|
import org.eclipse.jgit.revwalk.RevWalk;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
/** View of contents at a single ref related to some change. **/
|
/** View of contents at a single ref related to some change. **/
|
||||||
public abstract class AbstractChangeNotes<T> extends VersionedMetaData {
|
public abstract class AbstractChangeNotes<T> {
|
||||||
protected final GitRepositoryManager repoManager;
|
protected final GitRepositoryManager repoManager;
|
||||||
protected final NotesMigration migration;
|
protected final NotesMigration migration;
|
||||||
private final Change.Id changeId;
|
private final Change.Id changeId;
|
||||||
|
|
||||||
|
private ObjectId revision;
|
||||||
private boolean loaded;
|
private boolean loaded;
|
||||||
|
|
||||||
AbstractChangeNotes(GitRepositoryManager repoManager,
|
AbstractChangeNotes(GitRepositoryManager repoManager,
|
||||||
@@ -46,6 +47,11 @@ public abstract class AbstractChangeNotes<T> extends VersionedMetaData {
|
|||||||
return changeId;
|
return changeId;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @return revision of the metadata that was loaded. */
|
||||||
|
public ObjectId getRevision() {
|
||||||
|
return revision;
|
||||||
|
}
|
||||||
|
|
||||||
public T load() throws OrmException {
|
public T load() throws OrmException {
|
||||||
if (loaded) {
|
if (loaded) {
|
||||||
return self();
|
return self();
|
||||||
@@ -54,8 +60,12 @@ public abstract class AbstractChangeNotes<T> extends VersionedMetaData {
|
|||||||
loadDefaults();
|
loadDefaults();
|
||||||
return self();
|
return self();
|
||||||
}
|
}
|
||||||
try (Repository repo = repoManager.openMetadataRepository(getProjectName())) {
|
try (Repository repo = repoManager.openMetadataRepository(getProjectName());
|
||||||
load(repo);
|
RevWalk walk = new RevWalk(repo)) {
|
||||||
|
Ref ref = repo.getRefDatabase().exactRef(getRefName());
|
||||||
|
ObjectId id = ref != null ? ref.getObjectId() : null;
|
||||||
|
revision = id != null ? walk.parseCommit(id).copy() : null;
|
||||||
|
onLoad(walk);
|
||||||
loaded = true;
|
loaded = true;
|
||||||
} catch (ConfigInvalidException | IOException e) {
|
} catch (ConfigInvalidException | IOException e) {
|
||||||
throw new OrmException(e);
|
throw new OrmException(e);
|
||||||
@@ -91,6 +101,13 @@ public abstract class AbstractChangeNotes<T> extends VersionedMetaData {
|
|||||||
*/
|
*/
|
||||||
public abstract Project.NameKey getProjectName();
|
public abstract Project.NameKey getProjectName();
|
||||||
|
|
||||||
|
/** @return name of the reference storing this configuration. */
|
||||||
|
protected abstract String getRefName();
|
||||||
|
|
||||||
|
/** Set up the metadata, parsing any state from the loaded revision. */
|
||||||
|
protected abstract void onLoad(RevWalk walk)
|
||||||
|
throws IOException, ConfigInvalidException;
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
protected final T self() {
|
protected final T self() {
|
||||||
return (T) this;
|
return (T) this;
|
||||||
|
@@ -66,7 +66,6 @@ import com.google.inject.Provider;
|
|||||||
import com.google.inject.Singleton;
|
import com.google.inject.Singleton;
|
||||||
|
|
||||||
import org.eclipse.jgit.errors.ConfigInvalidException;
|
import org.eclipse.jgit.errors.ConfigInvalidException;
|
||||||
import org.eclipse.jgit.lib.CommitBuilder;
|
|
||||||
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.lib.Repository;
|
import org.eclipse.jgit.lib.Repository;
|
||||||
@@ -543,15 +542,15 @@ public class ChangeNotes extends AbstractChangeNotes<ChangeNotes> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onLoad() throws IOException, ConfigInvalidException {
|
protected void onLoad(RevWalk walk)
|
||||||
|
throws IOException, ConfigInvalidException {
|
||||||
ObjectId rev = getRevision();
|
ObjectId rev = getRevision();
|
||||||
if (rev == null) {
|
if (rev == null) {
|
||||||
loadDefaults();
|
loadDefaults();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
try (RevWalk walk = new RevWalk(reader);
|
try (ChangeNotesParser parser = new ChangeNotesParser(
|
||||||
ChangeNotesParser parser = new ChangeNotesParser(
|
project, change.getId(), rev, walk, repoManager, noteUtil)) {
|
||||||
project, change.getId(), rev, walk, repoManager, noteUtil)) {
|
|
||||||
parser.parseAll();
|
parser.parseAll();
|
||||||
|
|
||||||
if (parser.status != null) {
|
if (parser.status != null) {
|
||||||
@@ -610,12 +609,6 @@ public class ChangeNotes extends AbstractChangeNotes<ChangeNotes> {
|
|||||||
hashtags = ImmutableSet.of();
|
hashtags = ImmutableSet.of();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
protected boolean onSave(CommitBuilder commit) {
|
|
||||||
throw new UnsupportedOperationException(
|
|
||||||
getClass().getSimpleName() + " is read-only");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Project.NameKey getProjectName() {
|
public Project.NameKey getProjectName() {
|
||||||
return project;
|
return project;
|
||||||
|
@@ -30,8 +30,8 @@ import com.google.inject.Inject;
|
|||||||
import com.google.inject.Singleton;
|
import com.google.inject.Singleton;
|
||||||
|
|
||||||
import org.eclipse.jgit.errors.ConfigInvalidException;
|
import org.eclipse.jgit.errors.ConfigInvalidException;
|
||||||
import org.eclipse.jgit.lib.CommitBuilder;
|
|
||||||
import org.eclipse.jgit.lib.ObjectId;
|
import org.eclipse.jgit.lib.ObjectId;
|
||||||
|
import org.eclipse.jgit.lib.ObjectReader;
|
||||||
import org.eclipse.jgit.notes.NoteMap;
|
import org.eclipse.jgit.notes.NoteMap;
|
||||||
import org.eclipse.jgit.revwalk.RevCommit;
|
import org.eclipse.jgit.revwalk.RevCommit;
|
||||||
import org.eclipse.jgit.revwalk.RevWalk;
|
import org.eclipse.jgit.revwalk.RevWalk;
|
||||||
@@ -112,33 +112,26 @@ public class DraftCommentNotes extends AbstractChangeNotes<DraftCommentNotes> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onLoad() throws IOException, ConfigInvalidException {
|
protected void onLoad(RevWalk walk)
|
||||||
|
throws IOException, ConfigInvalidException {
|
||||||
ObjectId rev = getRevision();
|
ObjectId rev = getRevision();
|
||||||
if (rev == null) {
|
if (rev == null) {
|
||||||
loadDefaults();
|
loadDefaults();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
try (RevWalk walk = new RevWalk(reader)) {
|
RevCommit tipCommit = walk.parseCommit(rev);
|
||||||
RevCommit tipCommit = walk.parseCommit(rev);
|
ObjectReader reader = walk.getObjectReader();
|
||||||
revisionNoteMap = RevisionNoteMap.parse(
|
revisionNoteMap = RevisionNoteMap.parse(
|
||||||
noteUtil, getChangeId(), reader, NoteMap.read(reader, tipCommit),
|
noteUtil, getChangeId(), reader, NoteMap.read(reader, tipCommit),
|
||||||
true);
|
true);
|
||||||
Multimap<RevId, PatchLineComment> cs = ArrayListMultimap.create();
|
Multimap<RevId, PatchLineComment> cs = ArrayListMultimap.create();
|
||||||
for (RevisionNote rn : revisionNoteMap.revisionNotes.values()) {
|
for (RevisionNote rn : revisionNoteMap.revisionNotes.values()) {
|
||||||
for (PatchLineComment c : rn.comments) {
|
for (PatchLineComment c : rn.comments) {
|
||||||
cs.put(c.getRevId(), c);
|
cs.put(c.getRevId(), c);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
comments = ImmutableListMultimap.copyOf(cs);
|
|
||||||
}
|
}
|
||||||
}
|
comments = ImmutableListMultimap.copyOf(cs);
|
||||||
|
|
||||||
@Override
|
|
||||||
protected boolean onSave(CommitBuilder commit) throws IOException,
|
|
||||||
ConfigInvalidException {
|
|
||||||
throw new UnsupportedOperationException(
|
|
||||||
getClass().getSimpleName() + " is read-only");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
Reference in New Issue
Block a user