Output full commit message in query results

If the user adds a flag to the query parameters, include the full
commit message in the result data.  This is helpful when verifying
commit messages and with similar tasks.

Change-Id: Iff93bfebfa0c5e2209be29fe60ae12809a76479a
This commit is contained in:
Brad Larson
2011-10-27 19:36:27 -05:00
parent 9f9829e7ec
commit 0943d6e7fc
7 changed files with 61 additions and 1 deletions

View File

@@ -14,6 +14,7 @@ SYNOPSIS
[--patch-sets | --all-approvals]
[--files]
[--comments]
[--commit-message]
[--]
<query>
[limit:<n>]
@@ -69,6 +70,9 @@ OPTIONS
--patch-sets flag then all in-line comments are included for
each patch set.
--commit-message::
Include the full commit message in the change description.
limit:<n>::
Maximum number of results to return. This is actually a
query operator, and not a command line option. If more

View File

@@ -28,6 +28,8 @@ owner:: Owner in <<account,account attribute>>
url:: Canonical URL to reach this change
commitMessage:: The full commit message for the change.
lastUpdated:: Time in seconds since the UNIX epoch when this change
was last updated.

View File

@@ -27,6 +27,7 @@ public class ChangeAttribute {
public String subject;
public AccountAttribute owner;
public String url;
public String commitMessage;
public Long createdOn;
public Long lastUpdated;

View File

@@ -119,6 +119,10 @@ public class EventFactory {
}
}
public void addCommitMessage(ChangeAttribute a, String commitMessage) {
a.commitMessage = commitMessage;
}
public void addPatchSets(ChangeAttribute a, Collection<PatchSet> ps) {
addPatchSets(a, ps, null, false, null);
}

View File

@@ -20,15 +20,23 @@ import com.google.gerrit.reviewdb.Patch;
import com.google.gerrit.reviewdb.PatchLineComment;
import com.google.gerrit.reviewdb.PatchSet;
import com.google.gerrit.reviewdb.PatchSetApproval;
import com.google.gerrit.reviewdb.Project;
import com.google.gerrit.reviewdb.ReviewDb;
import com.google.gerrit.reviewdb.TrackingId;
import com.google.gerrit.server.CurrentUser;
import com.google.gerrit.server.git.GitRepositoryManager;
import com.google.gerrit.server.patch.PatchList;
import com.google.gerrit.server.patch.PatchListCache;
import com.google.gerrit.server.patch.PatchListEntry;
import com.google.gwtorm.client.OrmException;
import com.google.inject.Provider;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.revwalk.RevWalk;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
@@ -40,6 +48,7 @@ import java.util.Map;
public class ChangeData {
private final Change.Id legacyId;
private Change change;
private String commitMessage;
private Collection<PatchSet> patches;
private Collection<PatchSetApproval> approvals;
private Map<PatchSet.Id,Collection<PatchSetApproval>> approvalsMap;
@@ -164,6 +173,28 @@ public class ChangeData {
return r;
}
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();
}
}
return commitMessage;
}
public Collection<PatchSet> patches(Provider<ReviewDb> db)
throws OrmException {
if (patches == null) {

View File

@@ -23,6 +23,7 @@ import com.google.gerrit.server.events.ChangeAttribute;
import com.google.gerrit.server.events.EventFactory;
import com.google.gerrit.server.events.PatchSetAttribute;
import com.google.gerrit.server.events.QueryStats;
import com.google.gerrit.server.git.GitRepositoryManager;
import com.google.gerrit.server.query.Predicate;
import com.google.gerrit.server.query.QueryParseException;
import com.google.gson.Gson;
@@ -66,6 +67,7 @@ public class QueryProcessor {
private final ChangeQueryBuilder queryBuilder;
private final ChangeQueryRewriter queryRewriter;
private final Provider<ReviewDb> db;
private final GitRepositoryManager repoManager;
private final int maxLimit;
private OutputFormat outputFormat = OutputFormat.TEXT;
@@ -74,6 +76,7 @@ public class QueryProcessor {
private boolean includeApprovals;
private boolean includeComments;
private boolean includeFiles;
private boolean includeCommitMessage;
private OutputStream outputStream = DisabledOutputStream.INSTANCE;
private PrintWriter out;
@@ -81,11 +84,13 @@ public class QueryProcessor {
@Inject
QueryProcessor(EventFactory eventFactory,
ChangeQueryBuilder.Factory queryBuilder, CurrentUser currentUser,
ChangeQueryRewriter queryRewriter, Provider<ReviewDb> db) {
ChangeQueryRewriter queryRewriter, Provider<ReviewDb> db,
GitRepositoryManager repoManager) {
this.eventFactory = eventFactory;
this.queryBuilder = queryBuilder.create(currentUser);
this.queryRewriter = queryRewriter;
this.db = db;
this.repoManager = repoManager;
this.maxLimit = currentUser.getCapabilities()
.getRange(GlobalCapability.QUERY_LIMIT)
.getMax();
@@ -111,6 +116,10 @@ public class QueryProcessor {
includeFiles = on;
}
public void setIncludeCommitMessage(boolean on) {
includeCommitMessage = on;
}
public void setOutput(OutputStream out, OutputFormat fmt) {
this.outputStream = out;
this.outputFormat = fmt;
@@ -177,6 +186,10 @@ public class QueryProcessor {
eventFactory.extend(c, d.getChange());
eventFactory.addTrackingIds(c, d.trackingIds(db));
if (includeCommitMessage) {
eventFactory.addCommitMessage(c, d.commitMessage(repoManager, db));
}
if (includePatchSets) {
if (includeFiles) {
eventFactory.addPatchSets(c, d.patches(db),

View File

@@ -61,6 +61,11 @@ class Query extends BaseCommand {
processor.setIncludeFiles(on);
}
@Option(name = "--commit-message", usage = "Include the full commit message for a change")
void setCommitMessage(boolean on) {
processor.setIncludeCommitMessage(on);
}
@Argument(index = 0, required = true, multiValued = true, metaVar = "QUERY", usage = "Query to execute")
private List<String> query;