Search changes by commit messages

Search for changes that matches an arbitrary string in body commit
messages.

Change-Id: I4ba7f8eb6c47b8d7cd1523bed8d7272cb26583a2
This commit is contained in:
monica.dionisio
2010-08-25 14:46:13 -03:00
committed by Ulrik Sjölin
parent 5cd6d0a09b
commit 70a7ca0726
4 changed files with 141 additions and 2 deletions

View File

@@ -140,6 +140,11 @@ Matches changes where the approval score 'VALUE' has been set during
a review. See <<labels,labels>> below for more detail on the format
of the argument.
[[message]]
message:'MESSAGE'::
+
Changes that matches 'MESSAGE' arbitrary string in body commit messages.
[[file]]
file:\^'REGEX'::
+

View File

@@ -27,6 +27,7 @@ import com.google.gerrit.server.account.AccountResolver;
import com.google.gerrit.server.account.GroupCache;
import com.google.gerrit.server.config.AuthConfig;
import com.google.gerrit.server.config.WildProjectName;
import com.google.gerrit.server.git.GitRepositoryManager;
import com.google.gerrit.server.patch.PatchListCache;
import com.google.gerrit.server.project.ChangeControl;
import com.google.gerrit.server.query.IntPredicate;
@@ -75,6 +76,7 @@ public class ChangeQueryBuilder extends QueryBuilder<ChangeData> {
public static final String FIELD_HAS = "has";
public static final String FIELD_LABEL = "label";
public static final String FIELD_LIMIT = "limit";
public static final String FIELD_MESSAGE = "message";
public static final String FIELD_OWNER = "owner";
public static final String FIELD_PROJECT = "project";
public static final String FIELD_REF = "ref";
@@ -102,6 +104,7 @@ public class ChangeQueryBuilder extends QueryBuilder<ChangeData> {
final ApprovalTypes approvalTypes;
final Project.NameKey wildProjectName;
final PatchListCache patchListCache;
final GitRepositoryManager repoManager;
@Inject
Arguments(Provider<ReviewDb> dbProvider,
@@ -112,7 +115,8 @@ public class ChangeQueryBuilder extends QueryBuilder<ChangeData> {
AccountResolver accountResolver, GroupCache groupCache,
AuthConfig authConfig, ApprovalTypes approvalTypes,
@WildProjectName Project.NameKey wildProjectName,
PatchListCache patchListCache) {
PatchListCache patchListCache,
GitRepositoryManager repoManager) {
this.dbProvider = dbProvider;
this.rewriter = rewriter;
this.userFactory = userFactory;
@@ -124,6 +128,7 @@ public class ChangeQueryBuilder extends QueryBuilder<ChangeData> {
this.approvalTypes = approvalTypes;
this.wildProjectName = wildProjectName;
this.patchListCache = patchListCache;
this.repoManager = repoManager;
}
}
@@ -283,6 +288,11 @@ public class ChangeQueryBuilder extends QueryBuilder<ChangeData> {
args.userFactory, args.dbProvider, args.approvalTypes, name);
}
@Operator
public Predicate<ChangeData> message(String text) {
return new MessagePredicate(args.dbProvider, args.repoManager, text);
}
@Operator
public Predicate<ChangeData> starredby(String who)
throws QueryParseException, OrmException {

View File

@@ -38,7 +38,7 @@ public class ChangeQueryRewriter extends QueryRewriter<ChangeData> {
new ChangeQueryBuilder.Arguments( //
new InvalidProvider<ReviewDb>(), //
new InvalidProvider<ChangeQueryRewriter>(), //
null, null, null, null, null, null, null, null, null), null));
null, null, null, null, null, null, null, null, null, null), null));
private final Provider<ReviewDb> dbProvider;

View File

@@ -0,0 +1,124 @@
// Copyright (C) 2010 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.gerrit.server.query.change;
import com.google.gerrit.reviewdb.Change;
import com.google.gerrit.reviewdb.PatchSet;
import com.google.gerrit.reviewdb.Project;
import com.google.gerrit.reviewdb.RevId;
import com.google.gerrit.reviewdb.ReviewDb;
import com.google.gerrit.server.git.GitRepositoryManager;
import com.google.gerrit.server.query.OperatorPredicate;
import com.google.gwtorm.client.OrmException;
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.AnyObjectId;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevWalk;
import org.eclipse.jgit.revwalk.filter.MessageRevFilter;
import org.eclipse.jgit.revwalk.filter.RevFilter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
/**
* Predicate to match changes that contains specified text in commit messages
* body.
*/
public class MessagePredicate extends OperatorPredicate<ChangeData> {
private static final Logger log =
LoggerFactory.getLogger(MessagePredicate.class);
private final Provider<ReviewDb> db;
private final GitRepositoryManager repoManager;
private final RevFilter rFilter;
public MessagePredicate(Provider<ReviewDb> db,
GitRepositoryManager repoManager, String text) {
super(ChangeQueryBuilder.FIELD_MESSAGE, text);
this.db = db;
this.repoManager = repoManager;
this.rFilter = MessageRevFilter.create(text);
}
@Override
public boolean match(ChangeData object) throws OrmException {
final PatchSet patchSet = object.currentPatchSet(db);
if (patchSet == null) {
return false;
}
final RevId revision = patchSet.getRevision();
if (revision == null) {
return false;
}
final AnyObjectId objectId = ObjectId.fromString(revision.get());
if (objectId == null) {
return false;
}
final Change change = object.change(db);
if (change == null) {
return false;
}
final Project.NameKey projectName = change.getProject();
if (projectName == null) {
return false;
}
try {
final Repository repo = repoManager.openRepository(projectName.get());
try {
final RevWalk rw = new RevWalk(repo);
try {
return rFilter.include(rw, rw.parseCommit(objectId));
} finally {
rw.release();
}
} finally {
repo.close();
}
} catch (RepositoryNotFoundException e) {
log.error("Repository \"" + projectName.get() + "\" unknown.", e);
} catch (MissingObjectException e) {
log.error(projectName.get() + "\" commit does not exist.", e);
} catch (IncorrectObjectTypeException e) {
log.error(projectName.get() + "\" revision is not a commit.", e);
} catch (IOException e) {
log.error("Could not search for commit message in \"" + projectName.get()
+ "\" repository.", e);
}
return false;
}
@Override
public int getCost() {
return 1;
}
}