From 0943d6e7fcbfbf10a55a67cd6cb84a8503d1addb Mon Sep 17 00:00:00 2001 From: Brad Larson Date: Thu, 27 Oct 2011 19:36:27 -0500 Subject: [PATCH] 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 --- Documentation/cmd-query.txt | 4 +++ Documentation/json.txt | 2 ++ .../gerrit/server/events/ChangeAttribute.java | 1 + .../gerrit/server/events/EventFactory.java | 4 +++ .../server/query/change/ChangeData.java | 31 +++++++++++++++++++ .../server/query/change/QueryProcessor.java | 15 ++++++++- .../google/gerrit/sshd/commands/Query.java | 5 +++ 7 files changed, 61 insertions(+), 1 deletion(-) diff --git a/Documentation/cmd-query.txt b/Documentation/cmd-query.txt index bc3950b2ca..d5da453047 100644 --- a/Documentation/cmd-query.txt +++ b/Documentation/cmd-query.txt @@ -14,6 +14,7 @@ SYNOPSIS [--patch-sets | --all-approvals] [--files] [--comments] + [--commit-message] [--] [limit:] @@ -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::: Maximum number of results to return. This is actually a query operator, and not a command line option. If more diff --git a/Documentation/json.txt b/Documentation/json.txt index 99b158daae..b1dbc32cc9 100644 --- a/Documentation/json.txt +++ b/Documentation/json.txt @@ -28,6 +28,8 @@ owner:: Owner in <> 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. diff --git a/gerrit-server/src/main/java/com/google/gerrit/server/events/ChangeAttribute.java b/gerrit-server/src/main/java/com/google/gerrit/server/events/ChangeAttribute.java index e9a977e3c4..90506415c7 100644 --- a/gerrit-server/src/main/java/com/google/gerrit/server/events/ChangeAttribute.java +++ b/gerrit-server/src/main/java/com/google/gerrit/server/events/ChangeAttribute.java @@ -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; diff --git a/gerrit-server/src/main/java/com/google/gerrit/server/events/EventFactory.java b/gerrit-server/src/main/java/com/google/gerrit/server/events/EventFactory.java index 58105333f8..cf035c8423 100644 --- a/gerrit-server/src/main/java/com/google/gerrit/server/events/EventFactory.java +++ b/gerrit-server/src/main/java/com/google/gerrit/server/events/EventFactory.java @@ -119,6 +119,10 @@ public class EventFactory { } } + public void addCommitMessage(ChangeAttribute a, String commitMessage) { + a.commitMessage = commitMessage; + } + public void addPatchSets(ChangeAttribute a, Collection ps) { addPatchSets(a, ps, null, false, null); } diff --git a/gerrit-server/src/main/java/com/google/gerrit/server/query/change/ChangeData.java b/gerrit-server/src/main/java/com/google/gerrit/server/query/change/ChangeData.java index 16b43adb11..2f648a928a 100644 --- a/gerrit-server/src/main/java/com/google/gerrit/server/query/change/ChangeData.java +++ b/gerrit-server/src/main/java/com/google/gerrit/server/query/change/ChangeData.java @@ -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 patches; private Collection approvals; private Map> approvalsMap; @@ -164,6 +173,28 @@ public class ChangeData { return r; } + public String commitMessage(GitRepositoryManager repoManager, + Provider 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 patches(Provider db) throws OrmException { if (patches == null) { diff --git a/gerrit-server/src/main/java/com/google/gerrit/server/query/change/QueryProcessor.java b/gerrit-server/src/main/java/com/google/gerrit/server/query/change/QueryProcessor.java index 2143b813fc..2ee9b3fc03 100644 --- a/gerrit-server/src/main/java/com/google/gerrit/server/query/change/QueryProcessor.java +++ b/gerrit-server/src/main/java/com/google/gerrit/server/query/change/QueryProcessor.java @@ -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 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 db) { + ChangeQueryRewriter queryRewriter, Provider 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), diff --git a/gerrit-sshd/src/main/java/com/google/gerrit/sshd/commands/Query.java b/gerrit-sshd/src/main/java/com/google/gerrit/sshd/commands/Query.java index ff8d6bd38f..ed5e01c596 100644 --- a/gerrit-sshd/src/main/java/com/google/gerrit/sshd/commands/Query.java +++ b/gerrit-sshd/src/main/java/com/google/gerrit/sshd/commands/Query.java @@ -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 query;