Merge "Extract tr: values from commit instead of database"

This commit is contained in:
Shawn Pearce
2013-10-08 18:26:09 +00:00
committed by Gerrit Code Review
6 changed files with 84 additions and 24 deletions

View File

@@ -14,7 +14,13 @@
package com.google.gerrit.server.config;
import com.google.common.collect.Sets;
import org.eclipse.jgit.revwalk.FooterLine;
import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;
public class TrackingFooters {
protected List<TrackingFooter> trackingFooters;
@@ -26,4 +32,24 @@ public class TrackingFooters {
public List<TrackingFooter> getTrackingFooters() {
return trackingFooters;
}
public Set<String> extract(List<FooterLine> lines) {
Set<String> r = Sets.newHashSet();
for (FooterLine footer : lines) {
for (TrackingFooter config : trackingFooters) {
if (footer.matches(config.footerKey())) {
Matcher m = config.match().matcher(footer.getValue());
while (m.find()) {
String id = m.groupCount() > 0
? m.group(1)
: m.group();
if (!id.isEmpty()) {
r.add(id);
}
}
}
}
}
return r;
}
}

View File

@@ -22,7 +22,6 @@ import com.google.gerrit.reviewdb.client.ChangeMessage;
import com.google.gerrit.reviewdb.client.PatchLineComment;
import com.google.gerrit.reviewdb.client.PatchSet;
import com.google.gerrit.reviewdb.client.PatchSetApproval;
import com.google.gerrit.reviewdb.client.TrackingId;
import com.google.gerrit.server.ChangeUtil;
import com.google.gerrit.server.query.change.ChangeData;
import com.google.gerrit.server.query.change.ChangeQueryBuilder;
@@ -220,11 +219,12 @@ public class ChangeField {
@Override
public Iterable<String> get(ChangeData input, FillArgs args)
throws OrmException {
Set<String> r = Sets.newHashSet();
for (TrackingId id : input.trackingIds(args.db)) {
r.add(id.getTrackingId());
try {
return args.trackingFooters.extract(
input.commitFooters(args.repoManager, args.db));
} catch (IOException e) {
throw new OrmException(e);
}
return r;
}
};

View File

@@ -15,6 +15,7 @@
package com.google.gerrit.server.index;
import com.google.gerrit.reviewdb.server.ReviewDb;
import com.google.gerrit.server.config.TrackingFooters;
import com.google.gerrit.server.git.GitRepositoryManager;
import com.google.gerrit.server.patch.PatchListCache;
import com.google.gwtorm.server.OrmException;
@@ -59,14 +60,17 @@ public abstract class FieldDef<I, T> {
public static class FillArgs {
final Provider<ReviewDb> db;
final GitRepositoryManager repoManager;
final TrackingFooters trackingFooters;
final PatchListCache patchListCache;
@Inject
FillArgs(Provider<ReviewDb> db,
GitRepositoryManager repoManager,
TrackingFooters trackingFooters,
PatchListCache patchListCache) {
this.db = db;
this.repoManager = repoManager;
this.trackingFooters = trackingFooters;
this.patchListCache = patchListCache;
}
}

View File

@@ -30,7 +30,6 @@ import com.google.gerrit.reviewdb.client.Patch;
import com.google.gerrit.reviewdb.client.PatchLineComment;
import com.google.gerrit.reviewdb.client.PatchSet;
import com.google.gerrit.reviewdb.client.PatchSetApproval;
import com.google.gerrit.reviewdb.client.Project;
import com.google.gerrit.reviewdb.client.TrackingId;
import com.google.gerrit.reviewdb.server.ReviewDb;
import com.google.gerrit.server.CurrentUser;
@@ -44,8 +43,12 @@ import com.google.gwtorm.server.OrmException;
import com.google.gwtorm.server.ResultSet;
import com.google.inject.Provider;
import org.eclipse.jgit.errors.IncorrectObjectTypeException;
import org.eclipse.jgit.errors.MissingObjectException;
import org.eclipse.jgit.errors.RepositoryNotFoundException;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.FooterLine;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.revwalk.RevWalk;
@@ -136,6 +139,7 @@ public class ChangeData {
private ChangeDataSource returnedBySource;
private Change change;
private String commitMessage;
private List<FooterLine> commitFooters;
private PatchSet currentPatchSet;
private Set<PatchSet.Id> limitedIds;
private Collection<PatchSet> patches;
@@ -317,25 +321,39 @@ public class ChangeData {
public String commitMessage(GitRepositoryManager repoManager,
Provider<ReviewDb> db) throws IOException, OrmException {
if (commitMessage == null) {
PatchSet.Id psId = change(db).currentPatchSetId();
String sha1 = db.get().patchSets().get(psId).getRevision().get();
Project.NameKey name = change.getProject();
Repository repo = repoManager.openRepository(name);
try {
RevWalk walk = new RevWalk(repo);
try {
RevCommit c = walk.parseCommit(ObjectId.fromString(sha1));
commitMessage = c.getFullMessage();
} finally {
walk.release();
}
} finally {
repo.close();
}
loadCommitData(repoManager, db);
}
return commitMessage;
}
public List<FooterLine> commitFooters(GitRepositoryManager repoManager,
Provider<ReviewDb> db) throws IOException, OrmException {
if (commitFooters == null) {
loadCommitData(repoManager, db);
}
return commitFooters;
}
private void loadCommitData(GitRepositoryManager repoManager,
Provider<ReviewDb> db) throws OrmException, RepositoryNotFoundException,
IOException, MissingObjectException, IncorrectObjectTypeException {
PatchSet.Id psId = change(db).currentPatchSetId();
String sha1 = db.get().patchSets().get(psId).getRevision().get();
Repository repo = repoManager.openRepository(change.getProject());
try {
RevWalk walk = new RevWalk(repo);
try {
RevCommit c = walk.parseCommit(ObjectId.fromString(sha1));
commitMessage = c.getFullMessage();
commitFooters = c.getFooterLines();
} finally {
walk.release();
}
} finally {
repo.close();
}
}
/**
* @param db review database.
* @return patches for the change. If {@link #limitToPatchSets(Collection)}